diff --git a/CHANGELOG.md b/CHANGELOG.md index 0231a38a4e..92cc6b271b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,16 +1,31 @@ ## Cairo-VM Changelog #### Upcoming Changes +* fix: [#1862](https://github.com/lambdaclass/cairo-vm/pull/1862): + * Use MaybeRelocatable for relocation table + +* fix: [#1873](https://github.com/lambdaclass/cairo-vm/pull/1873) + * Fix broken num-prime `is_prime` call +* fix: [#1868](https://github.com/lambdaclass/cairo-vm/pull/1855): + * Adds logic to include the 3 new builtins in `builtin_segments` when serializing the output cairo pie's metadata. + +* fix: [#1855](https://github.com/lambdaclass/cairo-vm/pull/1855): + * Adds logic to skip pedersen additional data comparison when checking pie compatibility. + +* serde: add `size` field to `Identifier` [#1861]https://github.com/lambdaclass/cairo-vm/pull/1861 #### [2.0.0-rc0] - 2024-10-22 -* fix: [#1862](https://github.com/lambdaclass/cairo-vm/pull/1862): - * Use MaybeRelocatable for relocation table +* fix: [#1864](https://github.com/lambdaclass/cairo-vm/pull/1864): + * Runner: include data from constants segment to the bytecode when assembling program * chore: bump `cairo-lang-` dependencies to 2.9.0-dev.0 [#1858](https://github.com/lambdaclass/cairo-vm/pull/1858/files) * chore: update Rust required version to 1.81.0 [#1857](https://github.com/lambdaclass/cairo-vm/pull/1857) +* fix: [#1851](https://github.com/lambdaclass/cairo-vm/pull/1851): + * Fix unsorted signature and mod builtin outputs in air_private_input. + * feat(BREAKING): [#1824](https://github.com/lambdaclass/cairo-vm/pull/1824)[#1838](https://github.com/lambdaclass/cairo-vm/pull/1838): * Add support for dynamic layout * CLI change(BREAKING): The flag `cairo_layout_params_file` must be specified when using dynamic layout. diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index f22a6edd9a..8a293c680e 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -204,10 +204,14 @@ pub fn cairo_run_program( cairo_run_config.copy_to_output(), ); - let data: Vec = instructions - .flat_map(|inst| inst.assemble().encode()) - .map(|x| Felt252::from(&x)) - .map(MaybeRelocatable::from) + // The bytecode includes all program instructions plus entry/footer, + // plus data from the constants segments. + let data: Vec = casm_program + .assemble_ex(&entry_code.instructions, &libfunc_footer) + .bytecode + .into_iter() + .map(Into::::into) + .map(Into::::into) .collect(); let program = if cairo_run_config.proof_mode { diff --git a/vm/src/math_utils/is_prime.rs b/vm/src/math_utils/is_prime.rs index 0e02ef1705..a39a9978a1 100644 --- a/vm/src/math_utils/is_prime.rs +++ b/vm/src/math_utils/is_prime.rs @@ -9,7 +9,7 @@ mod with_std { use num_bigint::BigUint; pub fn is_prime(n: &BigUint) -> bool { - num_prime::nt_funcs::is_prime(n, None).probably() + num_prime::nt_funcs::is_prime::(n, None).probably() } } diff --git a/vm/src/serde/deserialize_program.rs b/vm/src/serde/deserialize_program.rs index 3fbd848932..052d9ae09c 100644 --- a/vm/src/serde/deserialize_program.rs +++ b/vm/src/serde/deserialize_program.rs @@ -102,6 +102,7 @@ pub struct Identifier { pub full_name: Option, pub members: Option>, pub cairo_type: Option, + pub size: Option, } #[cfg_attr(feature = "test_utils", derive(Arbitrary))] @@ -566,7 +567,7 @@ mod tests { "attributes": [], "debug_info": { "instruction_locations": {} - }, + }, "builtins": [], "data": [ "0x480680017fff8000", @@ -1012,6 +1013,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); identifiers.insert( @@ -1025,6 +1027,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); identifiers.insert( @@ -1036,6 +1039,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); identifiers.insert( @@ -1049,6 +1053,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); identifiers.insert( @@ -1060,6 +1065,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); identifiers.insert( @@ -1071,6 +1077,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); identifiers.insert( @@ -1082,6 +1089,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -1097,7 +1105,7 @@ mod tests { "attributes": [], "debug_info": { "instruction_locations": {} - }, + }, "builtins": [], "data": [ ], @@ -1178,10 +1186,10 @@ mod tests { "start_pc": 402, "value": "SafeUint256: subtraction overflow" } - ], + ], "debug_info": { "instruction_locations": {} - }, + }, "builtins": [], "data": [ ], @@ -1235,7 +1243,7 @@ mod tests { let valid_json = r#" { "prime": "0x800000000000011000000000000000000000000000000000000000000000001", - "attributes": [], + "attributes": [], "debug_info": { "file_contents": {}, "instruction_locations": { @@ -1286,7 +1294,7 @@ mod tests { } } } - }, + }, "builtins": [], "data": [ ], @@ -1344,7 +1352,7 @@ mod tests { let valid_json = r#" { "prime": "0x800000000000011000000000000000000000000000000000000000000000001", - "attributes": [], + "attributes": [], "debug_info": { "file_contents": {}, "instruction_locations": { @@ -1391,7 +1399,7 @@ mod tests { } } } - }, + }, "builtins": [], "data": [ ], diff --git a/vm/src/serde/serialize_program.rs b/vm/src/serde/serialize_program.rs index f8bd0d076a..1e9f83e8d1 100644 --- a/vm/src/serde/serialize_program.rs +++ b/vm/src/serde/serialize_program.rs @@ -127,6 +127,7 @@ pub(crate) struct IdentifierSerializer { pub full_name: Option, pub members: Option>, pub cairo_type: Option, + pub size: Option, } impl From for Identifier { @@ -138,6 +139,7 @@ impl From for Identifier { full_name: identifier_serialer.full_name, members: identifier_serialer.members, cairo_type: identifier_serialer.cairo_type, + size: identifier_serialer.size, } } } @@ -151,6 +153,7 @@ impl From for IdentifierSerializer { full_name: identifier_serialer.full_name, members: identifier_serialer.members, cairo_type: identifier_serialer.cairo_type, + size: identifier_serialer.size, } } } diff --git a/vm/src/types/program.rs b/vm/src/types/program.rs index 0fc8c6dc83..477f996577 100644 --- a/vm/src/types/program.rs +++ b/vm/src/types/program.rs @@ -720,6 +720,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -732,6 +733,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -773,6 +775,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -785,6 +788,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -934,6 +938,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -946,6 +951,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -1059,6 +1065,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -1071,6 +1078,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -1123,6 +1131,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -1135,6 +1144,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -1182,6 +1192,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); identifiers.insert( @@ -1193,6 +1204,7 @@ mod tests { full_name: Some("__main__.main.Args".to_string()), members: Some(HashMap::new()), cairo_type: None, + size: Some(0), }, ); identifiers.insert( @@ -1204,6 +1216,7 @@ mod tests { full_name: Some("__main__.main.ImplicitArgs".to_string()), members: Some(HashMap::new()), cairo_type: None, + size: Some(0), }, ); identifiers.insert( @@ -1215,6 +1228,7 @@ mod tests { full_name: Some("__main__.main.Return".to_string()), members: Some(HashMap::new()), cairo_type: None, + size: Some(0), }, ); identifiers.insert( @@ -1226,6 +1240,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); @@ -1281,6 +1296,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); identifiers.insert( @@ -1292,6 +1308,7 @@ mod tests { full_name: Some("__main__.main.Args".to_string()), members: Some(HashMap::new()), cairo_type: None, + size: Some(0), }, ); identifiers.insert( @@ -1303,6 +1320,7 @@ mod tests { full_name: Some("__main__.main.ImplicitArgs".to_string()), members: Some(HashMap::new()), cairo_type: None, + size: Some(0), }, ); identifiers.insert( @@ -1314,6 +1332,7 @@ mod tests { full_name: Some("__main__.main.Return".to_string()), members: Some(HashMap::new()), cairo_type: None, + size: Some(0), }, ); identifiers.insert( @@ -1325,6 +1344,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ); diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index 3339c0b562..085e3549b6 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -263,6 +263,8 @@ impl ModBuiltinRunner { }); } + instances.sort_by_key(|input| input.index); + vec![PrivateInput::Mod(ModInput { instances, zero_value_address: relocation_table diff --git a/vm/src/vm/runners/builtin_runner/signature.rs b/vm/src/vm/runners/builtin_runner/signature.rs index 6e94206ef3..2b48bd8460 100644 --- a/vm/src/vm/runners/builtin_runner/signature.rs +++ b/vm/src/vm/runners/builtin_runner/signature.rs @@ -210,7 +210,17 @@ impl SignatureBuiltinRunner { pub fn air_private_input(&self, memory: &Memory) -> Vec { let mut private_inputs = vec![]; - for (addr, signature) in self.signatures.borrow().iter() { + + // Collect and sort the signatures by their index before the loop + let binding = self.signatures.borrow(); + let mut sorted_signatures: Vec<_> = binding.iter().collect(); + sorted_signatures.sort_by_key(|(addr, _)| { + addr.offset + .checked_div(CELLS_PER_SIGNATURE as usize) + .unwrap_or_default() + }); + + for (addr, signature) in sorted_signatures { if let (Ok(pubkey), Some(msg)) = ( memory.get_integer(*addr), (*addr + 1_usize) @@ -554,4 +564,67 @@ mod tests { assert_eq!(signature_a.s, signature_b.s); } } + #[test] + fn get_air_private_input() { + let mut builtin = SignatureBuiltinRunner::new(Some(512), true); + + builtin.base = 0; + + let signature1_r = Felt252::from(1234); + let signature1_s = Felt252::from(5678); + let signature2_r = Felt252::from(8765); + let signature2_s = Felt252::from(4321); + + let sig1_addr = Relocatable::from((builtin.base as isize, 0)); + let sig2_addr = Relocatable::from((builtin.base as isize, CELLS_PER_SIGNATURE as usize)); + + builtin + .add_signature(sig1_addr, &(signature1_r, signature1_s)) + .unwrap(); + builtin + .add_signature(sig2_addr, &(signature2_r, signature2_s)) + .unwrap(); + + let pubkey1 = Felt252::from(1111); + let msg1 = Felt252::from(2222); + let pubkey2 = Felt252::from(3333); + let msg2 = Felt252::from(4444); + + let segments = segments![ + ((0, 0), 1111), + ((0, 1), 2222), + ((0, 2), 3333), + ((0, 3), 4444) + ]; + let w1 = + Felt252::from(&div_mod(&BigInt::one(), &signature1_s.to_bigint(), &EC_ORDER).unwrap()); + + let w2 = + Felt252::from(&div_mod(&BigInt::one(), &signature2_s.to_bigint(), &EC_ORDER).unwrap()); + + let expected_private_inputs = vec![ + PrivateInput::Signature(PrivateInputSignature { + index: 0, + pubkey: pubkey1, + msg: msg1, + signature_input: SignatureInput { + r: signature1_r, + w: w1, + }, + }), + PrivateInput::Signature(PrivateInputSignature { + index: 1, + pubkey: pubkey2, + msg: msg2, + signature_input: SignatureInput { + r: signature2_r, + w: w2, + }, + }), + ]; + + let private_inputs = builtin.air_private_input(&segments.memory); + + assert_eq!(private_inputs, expected_private_inputs); + } } diff --git a/vm/src/vm/runners/cairo_pie.rs b/vm/src/vm/runners/cairo_pie.rs index d86b1840c8..4383671971 100644 --- a/vm/src/vm/runners/cairo_pie.rs +++ b/vm/src/vm/runners/cairo_pie.rs @@ -289,6 +289,10 @@ impl CairoPie { return Err(CairoPieValidationError::DiffAdditionalData); } for (name, data) in self.additional_data.0.iter() { + // As documented above, we skip the pedersen field when comparing. + if *name == BuiltinName::pedersen { + continue; + } if !pie.additional_data.0.get(name).is_some_and(|d| d == data) { return Err(CairoPieValidationError::DiffAdditionalDataForBuiltin(*name)); } @@ -759,6 +763,9 @@ pub(super) mod serde_impl { BuiltinName::ec_op, BuiltinName::keccak, BuiltinName::poseidon, + BuiltinName::range_check96, + BuiltinName::add_mod, + BuiltinName::mul_mod, ]; for name in BUILTIN_ORDERED_LIST { diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 239515458d..65209c35de 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -4489,6 +4489,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, )] .into_iter() @@ -4517,6 +4518,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ), ( @@ -4528,6 +4530,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, ), ] @@ -4557,6 +4560,7 @@ mod tests { full_name: None, members: None, cairo_type: None, + size: None, }, )] .into_iter()