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

Cairo integration tests #676

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
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.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.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
}
7 changes: 7 additions & 0 deletions integration_tests/cairo_1_programs/ecdsa_recover.cairo
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.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.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.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.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()
}
23 changes: 23 additions & 0 deletions integration_tests/cairo_1_programs/nullable_box_vec.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
struct NullableVec<T> {
items: SquashedFelt252Dict<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.squash(),
len: 3,
}
}

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

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

// Create the array to insert
let a = array![8, 9, 10];

// Insert it as a `Span`
d.insert(0, nullable_from_box(BoxTrait::new(a.span())));
}
3 changes: 3 additions & 0 deletions integration_tests/cairo_1_programs/ops.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() -> u32 {
2_u32 + 4_u32
}
5 changes: 5 additions & 0 deletions integration_tests/cairo_1_programs/pedersen_example.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use core::pedersen::pedersen;

fn main() -> felt252 {
pedersen(1, 0)
}
Loading
Loading