-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize
Array<Felt252>
return value into output segment (#1764)
* Fix bug * Refactor * Clippy * Update changelog * Typo * Simplify logic * Add fn to check that the return type is Array<Felt252>> * Add check to cairo_run_program * First draft * Progress & conc * Leave a gap for final builtin pointers if copy_to_output_builtin is true * Reorder code so we do not lose variables in the middle * progress * Success * 🧹 * Fix bug * Handle panic flag * Update fetch return values * Add doc * refactor & serialize_output changes * Move check up * Fix output builtin final ptr * Add TODO * Update tests * Remove uneeded TODO * Find the lost segment arena pointer * Add serialized versions of programs used for cairo 1 testing * Fix some tests * Update tests * Fix some test programs * Impl custom Serde for tensor_new test * Make test pass * Doc * Custom serialization for felt_dict tests * More custom serde * Custom serde * Clippy * Update doc * Remove debug print * Add CHANGELOG entry * Update CHANGELOG.md * Fix typos
- Loading branch information
Showing
42 changed files
with
1,065 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
cairo_programs/cairo-1-programs/serialized_output/array_append.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use array::ArrayTrait; | ||
|
||
fn main() -> Array<felt252> { | ||
let mut numbers = ArrayTrait::new(); | ||
numbers.append(4_u32); | ||
numbers.append(2_u32); | ||
let _x = numbers.pop_front(); | ||
let mut output: Array<felt252> = ArrayTrait::new(); | ||
numbers.serialize(ref output); | ||
output | ||
} |
13 changes: 13 additions & 0 deletions
13
cairo_programs/cairo-1-programs/serialized_output/array_get.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use array::ArrayTrait; | ||
|
||
fn main() -> Array<felt252> { | ||
let mut numbers = ArrayTrait::new(); | ||
numbers.append(4_u32); | ||
numbers.append(3_u32); | ||
numbers.append(2_u32); | ||
numbers.append(1_u32); | ||
let res = *numbers.at(1); | ||
let mut output: Array<felt252> = ArrayTrait::new(); | ||
res.serialize(ref output); | ||
output | ||
} |
12 changes: 12 additions & 0 deletions
12
cairo_programs/cairo-1-programs/serialized_output/array_integer_tuple.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use core::array::ArrayTrait; | ||
|
||
|
||
fn main() -> Array<felt252> { | ||
let mut numbers = ArrayTrait::new(); | ||
numbers.append(1); | ||
|
||
let res = (numbers, 1); | ||
let mut output: Array<felt252> = ArrayTrait::new(); | ||
res.serialize(ref output); | ||
output | ||
} |
13 changes: 13 additions & 0 deletions
13
cairo_programs/cairo-1-programs/serialized_output/bitwise.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
fn main() -> Array<felt252> { | ||
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; | ||
let mut output: Array<felt252> = ArrayTrait::new(); | ||
c3.serialize(ref output); | ||
output | ||
} |
7 changes: 7 additions & 0 deletions
7
cairo_programs/cairo-1-programs/serialized_output/bytes31_ret.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
fn main() -> Array<felt252> { | ||
let a: u128 = 123; | ||
let b: bytes31 = a.into(); | ||
let mut output: Array<felt252> = ArrayTrait::new(); | ||
b.serialize(ref output); | ||
output | ||
} |
45 changes: 45 additions & 0 deletions
45
cairo_programs/cairo-1-programs/serialized_output/dict_with_struct.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use core::nullable::{nullable_from_box, match_nullable, FromNullableResult}; | ||
|
||
|
||
#[derive(Drop, Copy, Serde)] | ||
struct FP16x16 { | ||
mag: u32, | ||
sign: bool | ||
} | ||
|
||
fn main() -> Array<felt252> { | ||
// 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)); | ||
|
||
// We can't implement Serde for a Felt252Dict due to mutability requirements | ||
// So we will serialize the dict explicitely | ||
let mut output: Array<felt252> = ArrayTrait::new(); | ||
// Serialize entry 0 | ||
0.serialize(ref output); | ||
let array_0 = d.get(0).deref(); | ||
array_0.serialize(ref output); | ||
// Serialize entry 1 | ||
1.serialize(ref output); | ||
let array_1 = d.get(1).deref(); | ||
array_1.serialize(ref output); | ||
// Serialize entry 2 | ||
2.serialize(ref output); | ||
let array_2 = d.get(2).deref(); | ||
array_2.serialize(ref output); | ||
// Squash after serializing | ||
d.squash(); | ||
output | ||
} | ||
|
||
// TODO: remove this temporary fix once fixed in cairo | ||
#[inline(never)] | ||
fn identity<T>(t: T) -> T { t } |
19 changes: 19 additions & 0 deletions
19
cairo_programs/cairo-1-programs/serialized_output/dictionaries.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use dict::Felt252DictTrait; | ||
|
||
fn main() -> Array<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); | ||
let res = dict_felt[11]; // 1024 | ||
|
||
let mut output: Array<felt252> = ArrayTrait::new(); | ||
res.serialize(ref output); | ||
output | ||
} |
10 changes: 10 additions & 0 deletions
10
cairo_programs/cairo-1-programs/serialized_output/ecdsa_recover.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
fn main() -> Array<felt252> { | ||
let message_hash: felt252 = 0x503f4bea29baee10b22a7f10bdc82dda071c977c1f25b8f3973d34e6b03b2c; | ||
let signature_r: felt252 = 0xbe96d72eb4f94078192c2e84d5230cde2a70f4b45c8797e2c907acff5060bb; | ||
let signature_s: felt252 = 0x677ae6bba6daf00d2631fab14c8acf24be6579f9d9e98f67aa7f2770e57a1f5; | ||
let res = core::ecdsa::recover_public_key(:message_hash, :signature_r, :signature_s, y_parity: false).unwrap(); | ||
let mut output: Array<felt252> = ArrayTrait::new(); | ||
res.serialize(ref output); | ||
output | ||
} |
Oops, something went wrong.