Skip to content

Commit

Permalink
Cairo integration tests (#676)
Browse files Browse the repository at this point in the history
* Modify the testing pipeline, add cairo1 runner binary

* Add cairo1 programs from lambdaclass

* Add binaries of cairo1-runner, cairo-compile, sierra-compile-json, add fetching corelib when nonexistent

* Code refactors, fixes of integration tests

* Integration tests minor fixes

* Add the integrations tests environment generation

* Fixes in integration tests, refactoring of makefile

* Remove cairo0 rust binary

* Add unmarshaling option for gas_builtin

* Include gas builtin in umarshaling and marshaling of cairo file

* Change the naming convention of integration tests

* Remove accidentaly added script

* Rename cairozero_test.go, add tracker for failing integration tests

* Remove the file with list of failing integration tests

* modify writing to file functionality
  • Loading branch information
MaksymMalicki authored Dec 2, 2024
1 parent 212c55b commit f506eb5
Show file tree
Hide file tree
Showing 159 changed files with 1,563 additions and 152 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ cov.out
cpu.out
*.test
integration_tests/BenchMarks.txt
rust_vm_bin/

# Local Env Specific
.python-version
.DS_Store
.idea/
vendor/
vendor/
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ integration:
@echo "Running integration tests..."
@$(MAKE) build
@if [ $$? -eq 0 ]; then \
if [ ! -d ./rust_vm_bin ]; then \
mkdir -p ./rust_vm_bin; \
fi; \
if [ ! -d ./rust_vm_bin/cairo ]; then \
mkdir -p ./rust_vm_bin/cairo; \
fi; \
if [ ! -f ./rust_vm_bin/cairo/cairo-compile ] || [ ! -f ./rust_vm_bin/cairo/sierra-compile-json ] || [ ! -d ./rust_vm_bin/corelib ]; then \
cd ./rust_vm_bin/cairo; \
git clone --single-branch --branch feat/main-casm-json --depth=1 https://github.com/zmalatrax/cairo.git; \
mv cairo/corelib ../../rust_vm_bin/; \
cd cairo/crates/bin && \
cargo build --release --bin cairo-compile --bin sierra-compile-json && \
cd ../../../; \
mv cairo/target/release/cairo-compile cairo/target/release/sierra-compile-json ../cairo/ && \
rm -rf ./cairo; \
fi; \
if [ ! -f ./rust_vm_bin/cairo/cairo1-run ] || [ ! -f ./rust_vm_bin/cairo-vm-cli ]; then \
git clone https://github.com/lambdaclass/cairo-vm.git && \
cd cairo-vm/; \
cargo build --release --bin cairo-vm-cli --bin cairo1-run; \
cd ..; \
mv cairo-vm/target/release/cairo1-run ../cairo/ && \
mv cairo-vm/target/release/cairo-vm-cli ../../rust_vm_bin/ && \
rm -rf cairo-vm; \
cd ../../; \
fi; \
go test ./integration_tests/... -v; \
else \
echo "Integration tests were not run"; \
Expand Down
9 changes: 9 additions & 0 deletions integration_tests/cairo_1_programs/array_append.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use array::ArrayTrait;

fn main() -> Array<u32> {
let mut numbers = ArrayTrait::new();
numbers.append(4_u32);
numbers.append(2_u32);
let _x = numbers.pop_front();
numbers
}
10 changes: 10 additions & 0 deletions integration_tests/cairo_1_programs/array_get__small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use array::ArrayTrait;

fn main() -> u32 {
let mut numbers = ArrayTrait::new();
numbers.append(4_u32);
numbers.append(3_u32);
numbers.append(2_u32);
numbers.append(1_u32);
*numbers.at(1)
}
9 changes: 9 additions & 0 deletions integration_tests/cairo_1_programs/array_integer_tuple.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use core::array::ArrayTrait;


fn main() -> (Array<u32>, u32) {
let mut numbers = ArrayTrait::new();
numbers.append(1);

(numbers, 1)
}
11 changes: 11 additions & 0 deletions integration_tests/cairo_1_programs/bitwise__recursive.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() -> u128 {
let a = 1234_u128;
let b = 5678_u128;

let c0 = a & b;
let c1 = a ^ b;
let c2 = a | b;

let c3 = c0 + c1 + c2;
c3
}
5 changes: 5 additions & 0 deletions integration_tests/cairo_1_programs/bytes31_ret.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() -> bytes31 {
let a: u128 = 123;
let b: bytes31 = a.into();
b
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use core::nullable::{nullable_from_box, match_nullable, FromNullableResult};


#[derive(Drop, Copy)]
struct FP16x16 {
mag: u32,
sign: bool
}

fn main() -> Felt252Dict<Nullable<FP16x16>> {
// Create the dictionary
let mut d: Felt252Dict<Nullable<FP16x16>> = Default::default();

let box_a = BoxTrait::new(identity(FP16x16 { mag: 1, sign: false }));
let box_b = BoxTrait::new(identity(FP16x16 { mag: 1, sign: true }));
let box_c = BoxTrait::new(identity(FP16x16 { mag: 1, sign: true }));

// Insert it as a `Span`
d.insert(0, nullable_from_box(box_c));
d.insert(1, nullable_from_box(box_a));
d.insert(2, nullable_from_box(box_b));

d
}

// TODO: remove this temporary fix once fixed in cairo
#[inline(never)]
fn identity<T>(t: T) -> T { t }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use core::nullable::{nullable_from_box, match_nullable, FromNullableResult};

fn main() -> Felt252Dict<Nullable<Span<felt252>>> {
// Create the dictionary
let mut d: Felt252Dict<Nullable<Span<felt252>>> = Default::default();

// Create the array to insert
let a = array![8, 9, 10, 11];
let b = array![1, 2, 3];

// Insert it as a `Span`
d.insert(66675, nullable_from_box(BoxTrait::new(a.span())));
d.insert(66676, nullable_from_box(BoxTrait::new(b.span())));
d
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
struct NullableVec<T> {
items: Felt252Dict<Nullable<Box<T>>>,
len: usize,
}

fn main() -> NullableVec<u32> {
let mut d: Felt252Dict<Nullable<Box<u32>>> = Default::default();

// Populate the dictionary
d.insert(0, nullable_from_box(BoxTrait::new(BoxTrait::new(identity(10)))));
d.insert(1, nullable_from_box(BoxTrait::new(BoxTrait::new(identity(20)))));
d.insert(2, nullable_from_box(BoxTrait::new(BoxTrait::new(identity(30)))));

// Return NullableVec
NullableVec {
items: d,
len: 3,
}
}

// TODO: remove this temporary fix once fixed in cairo
#[inline(never)]
fn identity<T>(t: T) -> T { t }
28 changes: 28 additions & 0 deletions integration_tests/cairo_1_programs/dict_with_struct.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use core::nullable::{nullable_from_box, match_nullable, FromNullableResult};


#[derive(Drop, Copy)]
struct FP16x16 {
mag: u32,
sign: bool
}

fn main() -> SquashedFelt252Dict<Nullable<FP16x16>> {
// Create the dictionary
let mut d: Felt252Dict<Nullable<FP16x16>> = Default::default();

let box_a = BoxTrait::new(identity(FP16x16 { mag: 1, sign: false }));
let box_b = BoxTrait::new(identity(FP16x16 { mag: 1, sign: true }));
let box_c = BoxTrait::new(identity(FP16x16 { mag: 1, sign: true }));

// Insert it as a `Span`
d.insert(0, nullable_from_box(box_c));
d.insert(1, nullable_from_box(box_a));
d.insert(2, nullable_from_box(box_b));

d.squash()
}

// TODO: remove this temporary fix once fixed in cairo
#[inline(never)]
fn identity<T>(t: T) -> T { t }
15 changes: 15 additions & 0 deletions integration_tests/cairo_1_programs/dictionaries.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use dict::Felt252DictTrait;

fn main() -> felt252 {
let mut dict_u8 = felt252_dict_new::<u8>();
let mut dict_felt = felt252_dict_new::<felt252>();
let _dict_felt2 = felt252_dict_new::<felt252>();

dict_u8.insert(10, 110);
dict_u8.insert(10, 110);

let _val10 = dict_u8[10]; // 110
let _val11 = dict_felt[11]; // 0
dict_felt.insert(11, 1024);
dict_felt[11] // 1024
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

fn main() -> felt252 {
let message_hash: felt252 = 0x503f4bea29baee10b22a7f10bdc82dda071c977c1f25b8f3973d34e6b03b2c;
let signature_r: felt252 = 0xbe96d72eb4f94078192c2e84d5230cde2a70f4b45c8797e2c907acff5060bb;
let signature_s: felt252 = 0x677ae6bba6daf00d2631fab14c8acf24be6579f9d9e98f67aa7f2770e57a1f5;
core::ecdsa::recover_public_key(:message_hash, :signature_r, :signature_s, y_parity: false).unwrap()
}
58 changes: 58 additions & 0 deletions integration_tests/cairo_1_programs/enum_flow.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
enum MyEnumShort {
a: felt252,
b: felt252
}
enum MyEnumLong {
a: felt252,
b: felt252,
c: felt252
}
enum MyEnumGeneric<S, T> {
a: T,
b: S,
c: T
}

impl MyEnumGenericDrop of Drop<MyEnumGeneric<(), felt252>>;

fn main() -> felt252 {
let es0 = MyEnumShort::a(10);
match_short(es0);
let es1 = MyEnumShort::b(11);
match_short(es1);
let el0 = MyEnumLong::a(20);
match_long(el0);
let el1 = MyEnumLong::b(21);
match_long(el1);
let el2 = MyEnumLong::c(22);
match_long(el2);
let _eg1: MyEnumGeneric<(), felt252> = MyEnumGeneric::<(), felt252>::a(30);
let _eg2: MyEnumGeneric<(), felt252> = MyEnumGeneric::<(), felt252>::b(());
let _eg3: MyEnumGeneric<(), felt252> = MyEnumGeneric::<(), felt252>::c(32);
300
}

fn match_short(e: MyEnumShort) -> felt252 {
match e {
MyEnumShort::a(x) => {
x
},
MyEnumShort::b(x) => {
x
},
}
}

fn match_long(e: MyEnumLong) -> felt252 {
match e {
MyEnumLong::a(x) => {
x
},
MyEnumLong::b(x) => {
x
},
MyEnumLong::c(x) => {
x
},
}
}
18 changes: 18 additions & 0 deletions integration_tests/cairo_1_programs/enum_match__small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
enum MyEnum {
A: felt252,
B: (felt252, felt252),
}

fn get_value(e: MyEnum) -> felt252 {
match e {
MyEnum::A(a) => a,
MyEnum::B((x,y)) => x - y,
}
}

fn main() -> (felt252, felt252) {
(
get_value(MyEnum::A(10)),
get_value(MyEnum::B((20, 30))),
)
}
14 changes: 14 additions & 0 deletions integration_tests/cairo_1_programs/factorial__small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use core::felt252;

fn main() -> felt252 {
let n = 10;
let result = factorial(n);
result
}

fn factorial(n: felt252) -> felt252 {
match n {
0 => 1,
_ => n * factorial(n - 1),
}
}
15 changes: 15 additions & 0 deletions integration_tests/cairo_1_programs/felt_dict__small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use core::nullable::{nullable_from_box, match_nullable, FromNullableResult};

fn main() -> SquashedFelt252Dict<Nullable<Span<felt252>>> {
// Create the dictionary
let mut d: Felt252Dict<Nullable<Span<felt252>>> = Default::default();

// Create the array to insert
let a = array![8, 9, 10, 11];
let b = array![1, 2, 3];

// Insert it as a `Span`
d.insert(66675, nullable_from_box(BoxTrait::new(a.span())));
d.insert(66676, nullable_from_box(BoxTrait::new(b.span())));
d.squash()
}
18 changes: 18 additions & 0 deletions integration_tests/cairo_1_programs/felt_dict_squash__small.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use core::nullable::{nullable_from_box, match_nullable, FromNullableResult};
use core::dict::Felt252DictEntry;

fn main() -> SquashedFelt252Dict<Nullable<Span<felt252>>> {
// Create the dictionary
let mut d: Felt252Dict<Nullable<Span<felt252>>> = Default::default();

// Create the array to insert
let a = array![8, 9, 10, 11];
let b = array![1, 2, 3];
let c = array![4, 5, 6];

// Insert it as a `Span`
d.insert(66675, nullable_from_box(BoxTrait::new(a.span())));
d.insert(66676, nullable_from_box(BoxTrait::new(b.span())));
d.insert(66675, nullable_from_box(BoxTrait::new(c.span())));
d.squash()
}
6 changes: 6 additions & 0 deletions integration_tests/cairo_1_programs/felt_span.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use core::nullable::{nullable_from_box, match_nullable, FromNullableResult};

fn main() -> Nullable<Span<felt252>> {
let a = array![8, 9, 10, 11];
nullable_from_box(BoxTrait::new(a.span()))
}
14 changes: 14 additions & 0 deletions integration_tests/cairo_1_programs/fibonacci.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use core::felt252;

fn main() -> felt252 {
let n = 10;
let result = fib(1, 1, n);
result
}

fn fib(a: felt252, b: felt252, n: felt252) -> felt252 {
match n {
0 => a,
_ => fib(b, a + b, n - 1),
}
}
8 changes: 8 additions & 0 deletions integration_tests/cairo_1_programs/hello.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum MyEnum {
A: (),
B: felt252,
}

fn main() -> MyEnum {
MyEnum::B(1234)
}
3 changes: 3 additions & 0 deletions integration_tests/cairo_1_programs/null_ret.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() -> Nullable<u32> {
null()
}
Loading

0 comments on commit f506eb5

Please sign in to comment.