Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fibonacci test #25

Draft
wants to merge 86 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
3cbb748
Add basic structs and runner initialization
fmoletta Jul 17, 2023
c32247d
Start implementing tests
fmoletta Jul 17, 2023
93ff635
Add initializers + move vm into runner + minor fixes
fmoletta Jul 17, 2023
871638a
Minor fixes
fmoletta Jul 17, 2023
768049f
Improve runner test
fmoletta Jul 17, 2023
683eca8
Fix memory new size
fmoletta Jul 18, 2023
6982d8b
Add memory tests
fmoletta Jul 18, 2023
330c509
Merge branch 'main' of github.com:lambdaclass/cairo-vm_in_C into cair…
fmoletta Jul 18, 2023
74d742c
Implement memory_get + run new formatter
fmoletta Jul 18, 2023
2c5b7d6
Implement memory_insert
fmoletta Jul 18, 2023
d62a338
fixes'
fmoletta Jul 18, 2023
1ae32e5
Add file
fmoletta Jul 19, 2023
44f91eb
Finish load_data tests
fmoletta Jul 19, 2023
9b83d6d
Fix bug
fmoletta Jul 19, 2023
0bdf1e1
Fix bug
fmoletta Jul 19, 2023
d193b3f
Add boolean is_felt to maybe_relocatable type
fmoletta Jul 20, 2023
1f43986
Fixes
fmoletta Jul 20, 2023
49805ee
Add fix + test
fmoletta Jul 20, 2023
4353f64
Add more tests
fmoletta Jul 20, 2023
1b22061
Add test
fmoletta Jul 20, 2023
2f11a7e
Handle memory gaps in memory functions
fmoletta Jul 21, 2023
65cb901
Add more tests
fmoletta Jul 21, 2023
8093541
Fix bug
fmoletta Jul 21, 2023
d026137
Add tests
fmoletta Jul 21, 2023
3ab79b3
Merge branch 'main' of github.com:lambdaclass/cairo-vm_in_C into cair…
fmoletta Jul 21, 2023
d1b7492
Use felt_t in maybe_relocatable + adapt tests
fmoletta Jul 24, 2023
27ad5c5
Rename function & change parameter type
fmoletta Jul 24, 2023
c3da197
Remove unused import
fmoletta Jul 24, 2023
f147768
Remove unused import
fmoletta Jul 24, 2023
1726141
Add memory_free function
fmoletta Jul 24, 2023
e778bb9
Free memory in memory_tests
fmoletta Jul 24, 2023
31ffde5
fmt
fmoletta Jul 24, 2023
8d16b3f
Use clear in stead of free
fmoletta Jul 24, 2023
2dc7b1e
Free runner
fmoletta Jul 24, 2023
b9cbaf5
Add function comments
fmoletta Jul 24, 2023
d3c7165
Add missing execution segment data loading
fmoletta Jul 24, 2023
073b9c2
Add error handling to runner init & memory load_data
fmoletta Jul 24, 2023
5ea00bc
Revert "Add error handling to runner init & memory load_data"
fmoletta Jul 24, 2023
b366829
add error handling to memory_load_data
fmoletta Jul 24, 2023
59aa6cd
Add err case result
fmoletta Jul 24, 2023
1641508
Add warning, move comment to header
fmoletta Jul 24, 2023
4a0fce9
Move comments to header
fmoletta Jul 24, 2023
364bb88
Free program
fmoletta Jul 24, 2023
fd379ad
Free data used in load_data tests
fmoletta Jul 24, 2023
34d73ec
Swap clear for free
fmoletta Jul 24, 2023
e9ebde7
Copy name collision quickfix from parser pr
fmoletta Jul 24, 2023
91fc3af
Free felts stored in memory
fmoletta Jul 24, 2023
276c7ed
Fix bug
fmoletta Jul 24, 2023
87908c5
Revert "Copy name collision quickfix from parser pr"
fmoletta Jul 24, 2023
5ce16e1
Revert "Free felts stored in memory"
fmoletta Jul 24, 2023
27d58db
Revert "Revert "Copy name collision quickfix from parser pr""
fmoletta Jul 24, 2023
5d1cb15
Add free in memory_add_segment
fmoletta Jul 24, 2023
ecc539f
Add clear
fmoletta Jul 24, 2023
5003cf4
Revert "Add clear"
fmoletta Jul 24, 2023
156b6bc
Test
fmoletta Jul 24, 2023
0e06021
fmt
fmoletta Jul 24, 2023
39fa31e
Revert change
fmoletta Jul 24, 2023
fb62c88
Test
fmoletta Jul 24, 2023
9339225
Revert "Test"
fmoletta Jul 24, 2023
ad17642
Test
fmoletta Jul 24, 2023
d51f564
Test
fmoletta Jul 24, 2023
1215e12
Revert
fmoletta Jul 24, 2023
e95a506
Test
fmoletta Jul 24, 2023
2868221
Test
fmoletta Jul 24, 2023
04f4bb3
Revert
fmoletta Jul 24, 2023
b6eae99
Merge branch 'main' into cairo-runner-and-initialization
jrchatruc Jul 24, 2023
46080d8
Test
jrchatruc Jul 25, 2023
7a3eab0
Merge branch 'main' into cairo-runner-and-initialization
jrchatruc Jul 25, 2023
7a9c388
format
jrchatruc Jul 25, 2023
e3d3268
Merge branch 'cairo-runner-and-initialization' into fibonacci-test
toni-calvin Jul 25, 2023
24e66a2
Initial test
toni-calvin Jul 25, 2023
52ffec1
Preparing test for runner initialization
toni-calvin Jul 25, 2023
90f7961
Clang format
toni-calvin Jul 25, 2023
818c7a8
Passing parsing tests with new structure
toni-calvin Jul 25, 2023
cb7d74c
Merge branch 'cairo-runner-and-initialization' into fibonacci-test
toni-calvin Jul 25, 2023
c88d330
Merge branch 'main' into fibonacci-test
toni-calvin Jul 26, 2023
bfe81b0
Resolve merge
toni-calvin Jul 26, 2023
19d42b4
Passing tests with new Program structure
toni-calvin Jul 26, 2023
cf2a55c
Clang formatting
toni-calvin Jul 26, 2023
44568c9
Fixing emoty StringArray initialization
toni-calvin Jul 26, 2023
2987c17
Free program->data (Clist version)
toni-calvin Jul 26, 2023
861b559
Free program->data (Clist version)
toni-calvin Jul 26, 2023
0501ff3
Free program->data (Clist version)
toni-calvin Jul 26, 2023
44766c5
WIP: Fixing memory allocation
toni-calvin Jul 26, 2023
cfc8fe7
WIP: Fixing memory allocation
toni-calvin Jul 26, 2023
c8d8da7
WIP: Fixing memory allocation
toni-calvin Jul 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lambdaworks/lib/lambdaworks.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ void mul(felt_t a, felt_t b, felt_t result);

/* Writes the result variable with a / b. */
void lw_div(felt_t a, felt_t b, felt_t result);

#ifdef __cplusplus
}
#endif
3 changes: 1 addition & 2 deletions src/cairo_runner.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "cairo_runner.h"
#include "program.h"
#include "relocatable.h"
#include "vm.h"

cairo_runner runner_new(struct program program) {
cairo_runner runner_new(Program program) {
virtual_machine vm = vm_new();
cairo_runner runner = {program, vm, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
return runner;
Expand Down
6 changes: 3 additions & 3 deletions src/cairo_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#define RUNNER_H

#include "clist.h"
#include "program.h"
#include "parser.h"
#include "relocatable.h"
#include "vm.h"

typedef struct cairo_runner {
struct program program;
Program program;
virtual_machine vm;
relocatable program_base;
relocatable execution_base;
Expand All @@ -18,7 +18,7 @@ typedef struct cairo_runner {
} cairo_runner;

// Creates a new, empty cairo_runner
cairo_runner runner_new(struct program program);
cairo_runner runner_new(Program program);

// Frees resources used by the cairo_runner struct
void runner_free(cairo_runner *runner);
Expand Down
39 changes: 23 additions & 16 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ void parse_attributes(simdjson::dom::array attributes_array, Program *program) {
void parse_data(simdjson::dom::array data_array, Program *program) {

size_t elements = data_array.size();
program->data = (felt_t *)malloc(elements * sizeof(felt_t));
if (program->data == NULL) {
printf("Data allocation failed\n");
return;
}
program->data = CList_init(sizeof(maybe_relocatable));
// Loop through the elements in the data array
for (size_t i = 0; i < elements; ++i) {
// Get the element at the current index
Expand All @@ -65,11 +61,8 @@ void parse_data(simdjson::dom::array data_array, Program *program) {
// Convert the unsigned integer to the felt_t array
felt_t felt;
from(felt, num);

// Copy the elements from felt to the program's data array
for (int j = 0; j < 4; ++j) {
program->data[i][j] = felt[j];
}
maybe_relocatable relocatable_elem = maybe_relocatable_from_felt_limbs(felt);
program->data->add(program->data, &relocatable_elem);
}
}

Expand Down Expand Up @@ -99,21 +92,33 @@ void parse_data(simdjson::dom::array data_array, Program *program) {
// }
// }

void free_program(Program *program) {
void program_free(Program *program) {
if (program->attributes.length > 0) {
for (size_t i = 0; i < program->attributes.length; ++i) {
free(program->attributes.data[i]);
}
}
free(program->attributes.data);
free(program->compiler_version);
free(program->data);
program->data->free(program->data);
// free(program->debug_info.fileContent.start);
free(program);
}

Program get_empty_program(void) {
StringArray empty_array;
empty_array.length = 0;
empty_array.data = NULL;
Program *program_ptr;
program_ptr = (Program *)malloc(sizeof(Program));
program_ptr->attributes = empty_array;
program_ptr->compiler_version = NULL;
program_ptr->data = CList_init(sizeof(maybe_relocatable));
program_ptr->main = 0;
return Program(*program_ptr);
}

// Function to parse the JSON file and populate the Program struct
Program *parse_json_filename(const char *filename) {
Program parse_json_filename(const char *filename) {
// Add using namespace inside the parse_json function
using namespace simdjson;

Expand All @@ -136,7 +141,6 @@ Program *parse_json_filename(const char *filename) {
// Parse attributes array
dom::array attributes_array = root["attributes"].get_array();
parse_attributes(attributes_array, program);

// Parse compiler version
const char *compiler_version = root["compiler_version"].get_c_str().value();
program->compiler_version = (char *)malloc(strlen(compiler_version) * sizeof(char) + 1);
Expand All @@ -154,5 +158,8 @@ Program *parse_json_filename(const char *filename) {
// dom::element debug_info = root["debug_info"];
// parse_debug_info(debug_info, program);

return program;
// Parse main field (hardcoded right now)
program->main = 0;

return Program(*program);
}
24 changes: 14 additions & 10 deletions src/parser.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include "../lambdaworks/lib/lambdaworks.h"
#include "clist.h"
#include "relocatable.h"
#include "stddef.h"
#include "stdint.h"

Expand Down Expand Up @@ -56,19 +58,21 @@ typedef struct {

typedef struct {
StringArray attributes;
StringArray builtins;
// StringArray builtins;
char *compiler_version;
felt_t *data;
DebugInfo debug_info;
StringArray hints;
Identifiers *identifiers;
StringArray main_scope;
char *prime;
CList *data;
// DebugInfo debug_info;
// StringArray hints;
// Identifiers *identifiers;
// StringArray main_scope;
// char *prime;
int main;
} Program;
void free_program(Program *program);

void program_free(Program *program);
uint64_t hex_string_to_uint64(const char *hex);
Program *parse_json_filename(const char *filename);
Program *parse_json_data(const char *parse_json_data);
Program get_empty_program(void);
Program parse_json_filename(const char *filename);
#ifdef __cplusplus
} // extern "C"
#endif
2 changes: 1 addition & 1 deletion src/program.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "program.h"

void program_free(struct program *program) { program->data->free(program->data); }
void program_free_aux(struct program *program) { program->data->free(program->data); }
2 changes: 1 addition & 1 deletion src/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ struct program {
};

// Frees resources used by program struct
void program_free(struct program *program);
void program_free_aux(struct program *program);

#endif
9 changes: 8 additions & 1 deletion src/relocatable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "../lambdaworks/lib/lambdaworks.h"
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct relocatable {
unsigned int segment_index;
unsigned int offset;
Expand All @@ -19,7 +23,10 @@ typedef struct maybe_relocatable {
} maybe_relocatable;

bool maybe_relocatable_equal(maybe_relocatable a, maybe_relocatable b);

maybe_relocatable maybe_relocatable_from_felt_limbs(limb_t *felt);

#ifdef __cplusplus
} // extern "C"
#endif

#endif
13 changes: 13 additions & 0 deletions test/fibonacci_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "fibonacci_test.h"
#include "stdio.h"

// This test does the following things
// Parses fibonacci json into custom program struct
// Initializes a runner with data field in the struct
// Runs cairo run with initialized runner
void fib_test(void) {
Program program = parse_json_filename("cairo_programs/fibonacci.json");
cairo_runner runner = runner_new(program);
relocatable relocatable = runner_initialize(&runner);
printf("%u", relocatable.offset);
}
8 changes: 8 additions & 0 deletions test/fibonacci_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef FIB_TEST_H
#define FIB_TEST_H
#include "cairo_runner.h"
#include "parser.h"

void fib_test(void);

#endif
28 changes: 14 additions & 14 deletions test/parser_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@
#include <stdlib.h>
#include <string.h>

void parsing_attributes_test(Program *program) {
void parsing_attributes_test(Program program) {
printf("Test: parsing__empty_attributes_array\n");

char *expected[] = {"attr1", "attr2"};
size_t num_values = sizeof(expected) / sizeof(expected[0]);
assert(num_values == program->attributes.length);
assert(num_values == program.attributes.length);
for (size_t i = 0; i < num_values; ++i) {
assert(strcmp(expected[i], program->attributes.data[i]) == 0);
assert(strcmp(expected[i], program.attributes.data[i]) == 0);
}
printf("OK! \n");
}

void parsing_empty_attributes_test(Program *program) {
void parsing_empty_attributes_test(Program program) {
printf("Test: parsing__empty_attributes_array\n");
size_t num_values = 0;
assert(num_values == program->attributes.length);
assert(program->attributes.data == NULL);
assert(num_values == program.attributes.length);
assert(program.attributes.data == NULL);
printf("OK! \n");
}

void parsing_compiler_version(Program *program) {
void parsing_compiler_version(Program program) {
printf("Test: parsing_compiler_version\n");
char *expected = "0.10.3";
assert(strcmp(expected, program->compiler_version) == 0);
assert(strcmp(expected, program.compiler_version) == 0);
printf("OK! \n");
}

void parsing_data_test(Program *program) {
void parsing_data_test(Program program) {
printf("Test: parsing_data_array\n");

char *hex_values[] = {
Expand Down Expand Up @@ -75,23 +75,23 @@ void parsing_data_test(Program *program) {
uint64_t num = hex_string_to_uint64(hex_values[i]);
felt_t expected;
from(expected, num);
for (size_t j = 0; j < 4; ++j) {
assert(expected[j] == program->data[i][j]);
}
maybe_relocatable expected_elem = maybe_relocatable_from_felt_limbs(expected);
maybe_relocatable *program_value = program.data->at(program.data, i);
assert(maybe_relocatable_equal(expected_elem, *program_value));
}
printf("OK! \n");
}

void parsing_tests(void) {

const char *filename = "cairo_programs/fibonacci.json";
Program *program = parse_json_filename(filename);
Program program = parse_json_filename(filename);
printf("---------------------------------\n");
parsing_empty_attributes_test(program);
printf("---------------------------------\n");
parsing_compiler_version(program);
printf("---------------------------------\n");
parsing_data_test(program);
printf("---------------------------------\n");
free_program(program);
program_free(&program);
}
14 changes: 6 additions & 8 deletions test/runner_tests.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#include "runner_tests.h"
#include "clist.h"
#include "memory.h"
#include "program.h"
#include "run_context.h"
#include "utils.h"
#include "vm.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

void initialize_runner_no_builtins_no_proof_mode_non_empty_program(void) {
struct CList *program_data = CList_init(sizeof(maybe_relocatable));
Program program = get_empty_program();
felt_t felt_one;
one(felt_one);
maybe_relocatable elem = maybe_relocatable_from_felt_limbs(felt_one);
program_data->add(program_data, &elem);
struct program program = {0, program_data};
program.data->add(program.data, &elem);
cairo_runner runner = runner_new(program);
runner_initialize(&runner);

Expand Down Expand Up @@ -57,9 +56,8 @@ void initialize_runner_no_builtins_no_proof_mode_non_empty_program(void) {
}

void initialize_runner_no_builtins_no_proof_mode_empty_program(void) {
struct CList *program_data = CList_init(sizeof(maybe_relocatable));
struct program program = {0, program_data};
cairo_runner runner = runner_new(program);
Program empty_program = get_empty_program();
cairo_runner runner = runner_new(empty_program);
runner_initialize(&runner);

// Check runner data
Expand Down Expand Up @@ -104,5 +102,5 @@ void runner_tests(void) {
initialize_runner_no_builtins_no_proof_mode_empty_program();
printf("--------------------------------- \n");
printf("Test: initialize_runner_no_builtins_no_proof_mode_non_empty_program \n");
initialize_runner_no_builtins_no_proof_mode_empty_program();
initialize_runner_no_builtins_no_proof_mode_non_empty_program();
}
4 changes: 3 additions & 1 deletion test/test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "decoder_test.h"
#include "felt_test.h"
#include "fibonacci_test.h"
#include "memory_tests.h"
#include "parser_test.h"
#include "runner_tests.h"
Expand All @@ -11,7 +12,8 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char **argv)
felt_tests();
decoding_tests();
memory_tests();
runner_tests();
parsing_tests();
runner_tests();
// fib_test();
return 0;
}
Loading