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

Replace cairo programs in libfuncs/ #1038

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
05bd348
cairo -> sierra in libfuncs/array.rs
FrancoGiachetta Jan 9, 2025
f40a381
remove unwanted files
FrancoGiachetta Jan 9, 2025
0856b02
cairo -> sierra in libfuncs/bool.rs
FrancoGiachetta Jan 9, 2025
1d4385f
format
FrancoGiachetta Jan 9, 2025
7653d98
clippy
FrancoGiachetta Jan 9, 2025
fd12e2f
comment original cairo program
FrancoGiachetta Jan 10, 2025
fad6435
cairo -> sierra in libfuncs/bounded_int.rs
FrancoGiachetta Jan 10, 2025
66ad30b
cairo -> sierra in libfuncs/box.rs
FrancoGiachetta Jan 10, 2025
ac1ce07
cairo -> sierra in libfuncs/bytes32.rs and format
FrancoGiachetta Jan 10, 2025
1f4c715
cairo -> sierra in libfuncs/cast.rs
FrancoGiachetta Jan 10, 2025
90f51eb
cairo -> sierra in libfuncs/circuit.rs
FrancoGiachetta Jan 10, 2025
03929c2
cairo -> sierra in libfuncs/const.rs
FrancoGiachetta Jan 10, 2025
9dc5fb6
cairo -> sierra in libfuncs/ec.rs
FrancoGiachetta Jan 10, 2025
8129681
cairo -> sierra in libfuncs/enum.rs
FrancoGiachetta Jan 10, 2025
80a4ffb
cairo -> sierra in libfuncs/felt252.rs
FrancoGiachetta Jan 10, 2025
739942c
cairo -> sierra in libfuncs/felt252_dict.rs
FrancoGiachetta Jan 10, 2025
4cf0cdc
cairo -> sierra in libfuncs/felt252_dict_entry.rs
FrancoGiachetta Jan 10, 2025
6703d25
Merge branch 'main' into sierra-tests-libfuncs
FrancoGiachetta Jan 10, 2025
ee23ae5
cairo -> sierra in libfuncs/gas.rs
FrancoGiachetta Jan 10, 2025
ecff09b
cairo -> sierra in libfuncs/int_range.rs
FrancoGiachetta Jan 10, 2025
602d79e
cairo -> sierra in libfuncs/nullable.rs
FrancoGiachetta Jan 10, 2025
f0b7477
cairo -> sierra in libfuncs/pedersen.rs
FrancoGiachetta Jan 10, 2025
72f2ac0
cairo -> sierra in libfuncs/poseidon.rs
FrancoGiachetta Jan 10, 2025
6e6413b
cairo -> sierra in libfuncs/starknet.rs
FrancoGiachetta Jan 10, 2025
ac5c2cd
cairo -> sierra in libfuncs/uint256.rs
FrancoGiachetta Jan 10, 2025
f54d2e5
cairo -> sierra in libfuncs/uint512.rs
FrancoGiachetta Jan 10, 2025
c17c32f
clippy
FrancoGiachetta Jan 10, 2025
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
8,435 changes: 7,951 additions & 484 deletions src/libfuncs/array.rs

Large diffs are not rendered by default.

189 changes: 125 additions & 64 deletions src/libfuncs/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,65 +209,99 @@ pub fn build_bool_to_felt252<'ctx, 'this>(

#[cfg(test)]
mod test {
use cairo_lang_sierra::ProgramParser;

use crate::{
utils::test::{jit_enum, jit_struct, load_cairo, run_program},
utils::test::{jit_enum, jit_struct, run_sierra_program},
values::Value,
};

#[test]
fn run_not() {
let program = load_cairo!(
use array::ArrayTrait;

fn run_test(a: bool) -> bool {
!a
}
);

let result = run_program(&program, "run_test", &[jit_enum!(0, jit_struct!())]).return_value;
// use array::ArrayTrait;
//
// fn run_test(a: bool) -> bool {
// !a
// }
let program = ProgramParser::new().parse(r#"
type [0] = Struct<ut@Tuple> [storable: true, drop: true, dup: true, zero_sized: true];
type [1] = Enum<ut@core::bool, [0], [0]> [storable: true, drop: true, dup: true, zero_sized: false];

libfunc [0] = bool_not_impl;
libfunc [2] = store_temp<[1]>;

[0]([0]) -> ([1]); // 0
[2]([1]) -> ([1]); // 1
return([1]); // 2

[0]@0([0]: [1]) -> ([1]);
"#).map_err(|e| e.to_string()).unwrap();

let result = run_sierra_program(&program, &[jit_enum!(0, jit_struct!())]).return_value;
assert_eq!(result, jit_enum!(1, jit_struct!()));

let result = run_program(&program, "run_test", &[jit_enum!(1, jit_struct!())]).return_value;
let result = run_sierra_program(&program, &[jit_enum!(1, jit_struct!())]).return_value;
assert_eq!(result, jit_enum!(0, jit_struct!()));
}

#[test]
fn run_and() {
let program = load_cairo!(
use array::ArrayTrait;

fn run_test(a: bool, b: bool) -> bool {
a && b
}
);

let result = run_program(
// use array::ArrayTrait;

// fn run_test(a: bool, b: bool) -> bool {
// a && b
// }
let program = ProgramParser::new().parse(r#"
type [0] = Struct<ut@Tuple> [storable: true, drop: true, dup: true, zero_sized: true];
type [1] = Enum<ut@core::bool, [0], [0]> [storable: true, drop: true, dup: true, zero_sized: false];

libfunc [2] = enum_match<[1]>;
libfunc [3] = branch_align;
libfunc [4] = drop<[0]>;
libfunc [5] = drop<[1]>;
libfunc [1] = struct_construct<[0]>;
libfunc [0] = enum_init<[1], 0>;
libfunc [6] = store_temp<[1]>;

[2]([0]) { fallthrough([2]) 8([3]) }; // 0
[3]() -> (); // 1
[4]([2]) -> (); // 2
[5]([1]) -> (); // 3
[1]() -> ([4]); // 4
[0]([4]) -> ([5]); // 5
[6]([5]) -> ([5]); // 6
return([5]); // 7
[3]() -> (); // 8
[4]([3]) -> (); // 9
[6]([1]) -> ([1]); // 10
return([1]); // 11

[0]@0([0]: [1], [1]: [1]) -> ([1]);
"#).map_err(|e| e.to_string()).unwrap();

let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(1, jit_struct!()), jit_enum!(1, jit_struct!())],
)
.return_value;
assert_eq!(result, jit_enum!(1, jit_struct!()));

let result = run_program(
let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(1, jit_struct!()), jit_enum!(0, jit_struct!())],
)
.return_value;
assert_eq!(result, jit_enum!(0, jit_struct!()));

let result = run_program(
let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(0, jit_struct!()), jit_enum!(1, jit_struct!())],
)
.return_value;
assert_eq!(result, jit_enum!(0, jit_struct!()));

let result = run_program(
let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(0, jit_struct!()), jit_enum!(0, jit_struct!())],
)
.return_value;
Expand All @@ -276,41 +310,43 @@ mod test {

#[test]
fn run_xor() {
let program = load_cairo!(
use array::ArrayTrait;
let program = ProgramParser::new().parse(r#"
type [0] = Struct<ut@Tuple> [storable: true, drop: true, dup: true, zero_sized: true];
type [1] = Enum<ut@core::bool, [0], [0]> [storable: true, drop: true, dup: true, zero_sized: false];

libfunc [0] = bool_xor_impl;
libfunc [2] = store_temp<[1]>;

fn run_test(a: bool, b: bool) -> bool {
a ^ b
}
);
[0]([0], [1]) -> ([2]); // 0
[2]([2]) -> ([2]); // 1
return([2]); // 2

let result = run_program(
[0]@0([0]: [1], [1]: [1]) -> ([1]);
"#).map_err(|e| e.to_string()).unwrap();

let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(1, jit_struct!()), jit_enum!(1, jit_struct!())],
)
.return_value;
assert_eq!(result, jit_enum!(0, jit_struct!()));

let result = run_program(
let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(1, jit_struct!()), jit_enum!(0, jit_struct!())],
)
.return_value;
assert_eq!(result, jit_enum!(1, jit_struct!()));

let result = run_program(
let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(0, jit_struct!()), jit_enum!(1, jit_struct!())],
)
.return_value;
assert_eq!(result, jit_enum!(1, jit_struct!()));

let result = run_program(
let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(0, jit_struct!()), jit_enum!(0, jit_struct!())],
)
.return_value;
Expand All @@ -319,41 +355,57 @@ mod test {

#[test]
fn run_or() {
let program = load_cairo!(
use array::ArrayTrait;

fn run_test(a: bool, b: bool) -> bool {
a || b
}
);

let result = run_program(
let program = ProgramParser::new().parse(r#"
type [0] = Struct<ut@Tuple> [storable: true, drop: true, dup: true, zero_sized: true];
type [1] = Enum<ut@core::bool, [0], [0]> [storable: true, drop: true, dup: true, zero_sized: false];

libfunc [2] = enum_match<[1]>;
libfunc [3] = branch_align;
libfunc [4] = drop<[0]>;
libfunc [6] = store_temp<[1]>;
libfunc [5] = drop<[1]>;
libfunc [1] = struct_construct<[0]>;
libfunc [0] = enum_init<[1], 1>;

[2]([0]) { fallthrough([2]) 5([3]) }; // 0
[3]() -> (); // 1
[4]([2]) -> (); // 2
[6]([1]) -> ([1]); // 3
return([1]); // 4
[3]() -> (); // 5
[4]([3]) -> (); // 6
[5]([1]) -> (); // 7
[1]() -> ([4]); // 8
[0]([4]) -> ([5]); // 9
[6]([5]) -> ([5]); // 10
return([5]); // 11

[0]@0([0]: [1], [1]: [1]) -> ([1]);
"#).map_err(|e| e.to_string()).unwrap();

let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(1, jit_struct!()), jit_enum!(1, jit_struct!())],
)
.return_value;
assert_eq!(result, jit_enum!(1, jit_struct!()));

let result = run_program(
let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(1, jit_struct!()), jit_enum!(0, jit_struct!())],
)
.return_value;
assert_eq!(result, jit_enum!(1, jit_struct!()));

let result = run_program(
let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(0, jit_struct!()), jit_enum!(1, jit_struct!())],
)
.return_value;
assert_eq!(result, jit_enum!(1, jit_struct!()));

let result = run_program(
let result = run_sierra_program(
&program,
"run_test",
&[jit_enum!(0, jit_struct!()), jit_enum!(0, jit_struct!())],
)
.return_value;
Expand All @@ -362,16 +414,25 @@ mod test {

#[test]
fn bool_to_felt252() {
let program = load_cairo!(
fn run_test(a: bool) -> felt252 {
bool_to_felt252(a)
}
);
let program = ProgramParser::new().parse(r#"
type [0] = Struct<ut@Tuple> [storable: true, drop: true, dup: true, zero_sized: true];
type [1] = Enum<ut@core::bool, [0], [0]> [storable: true, drop: true, dup: true, zero_sized: false];
type [2] = felt252 [storable: true, drop: true, dup: true, zero_sized: false];

libfunc [0] = bool_to_felt252;
libfunc [2] = store_temp<[2]>;

[0]([0]) -> ([1]); // 0
[2]([1]) -> ([1]); // 1
return([1]); // 2

[0]@0([0]: [1]) -> ([2]);
"#).map_err(|e| e.to_string()).unwrap();

let result = run_program(&program, "run_test", &[jit_enum!(1, jit_struct!())]).return_value;
let result = run_sierra_program(&program, &[jit_enum!(1, jit_struct!())]).return_value;
assert_eq!(result, Value::Felt252(1.into()));

let result = run_program(&program, "run_test", &[jit_enum!(0, jit_struct!())]).return_value;
let result = run_sierra_program(&program, &[jit_enum!(0, jit_struct!())]).return_value;
assert_eq!(result, Value::Felt252(0.into()));
}
}
Loading
Loading