From 844d01cc0f10910122d251860ba2f88b14211df3 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 13:23:35 -0300 Subject: [PATCH 01/35] Change instance def ratios to Option --- .../bitwise_instance_def.rs | 12 +++++----- .../builtins_instance_def.rs | 22 +++++++++---------- .../ec_op_instance_def.rs | 12 +++++----- .../ecdsa_instance_def.rs | 12 +++++----- .../keccak_instance_def.rs | 19 +++++++++------- .../pedersen_instance_def.rs | 12 +++++----- .../range_check_instance_def.rs | 12 +++++----- src/utils.rs | 2 +- 8 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/types/instance_definitions/bitwise_instance_def.rs b/src/types/instance_definitions/bitwise_instance_def.rs index 0f1a6c5b04..6f1036de68 100644 --- a/src/types/instance_definitions/bitwise_instance_def.rs +++ b/src/types/instance_definitions/bitwise_instance_def.rs @@ -3,19 +3,19 @@ pub(crate) const INPUT_CELLS_PER_BITWISE: u32 = 2; #[derive(Clone, Debug, PartialEq)] pub(crate) struct BitwiseInstanceDef { - pub(crate) ratio: u32, + pub(crate) ratio: Option, pub(crate) total_n_bits: u32, } impl BitwiseInstanceDef { pub(crate) fn default() -> Self { BitwiseInstanceDef { - ratio: 256, + ratio: Some(256), total_n_bits: 251, } } - pub(crate) fn new(ratio: u32) -> Self { + pub(crate) fn new(ratio: Option) -> Self { BitwiseInstanceDef { ratio, total_n_bits: 251, @@ -50,16 +50,16 @@ mod tests { #[test] fn test_new() { let builtin_instance = BitwiseInstanceDef { - ratio: 8, + ratio: Some(8), total_n_bits: 251, }; - assert_eq!(BitwiseInstanceDef::new(8), builtin_instance); + assert_eq!(BitwiseInstanceDef::new(Some(8)), builtin_instance); } #[test] fn test_default() { let builtin_instance = BitwiseInstanceDef { - ratio: 256, + ratio: Some(256), total_n_bits: 251, }; assert_eq!(BitwiseInstanceDef::default(), builtin_instance); diff --git a/src/types/instance_definitions/builtins_instance_def.rs b/src/types/instance_definitions/builtins_instance_def.rs index c29ee400b2..7b3c926fe7 100644 --- a/src/types/instance_definitions/builtins_instance_def.rs +++ b/src/types/instance_definitions/builtins_instance_def.rs @@ -55,11 +55,11 @@ impl BuiltinsInstanceDef { pub(crate) fn perpetual_with_bitwise() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(32, 1)), - range_check: Some(RangeCheckInstanceDef::new(16, 8)), - ecdsa: Some(EcdsaInstanceDef::new(2048)), - bitwise: Some(BitwiseInstanceDef::new(64)), - ec_op: Some(EcOpInstanceDef::new(1024)), + pedersen: Some(PedersenInstanceDef::new(Some(32), 1)), + range_check: Some(RangeCheckInstanceDef::new(Some(16), 8)), + ecdsa: Some(EcdsaInstanceDef::new(Some(2048))), + bitwise: Some(BitwiseInstanceDef::new(Some(64))), + ec_op: Some(EcOpInstanceDef::new(Some(1024))), keccak: None, } } @@ -67,10 +67,10 @@ impl BuiltinsInstanceDef { pub(crate) fn bitwise() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(256, 1)), + pedersen: Some(PedersenInstanceDef::new(Some(256), 1)), range_check: Some(RangeCheckInstanceDef::default()), - ecdsa: Some(EcdsaInstanceDef::new(1024)), - bitwise: Some(BitwiseInstanceDef::new(8)), + ecdsa: Some(EcdsaInstanceDef::new(Some(1024))), + bitwise: Some(BitwiseInstanceDef::new(Some(8))), ec_op: None, keccak: None, } @@ -79,12 +79,12 @@ impl BuiltinsInstanceDef { pub(crate) fn recursive() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(256, 1)), + pedersen: Some(PedersenInstanceDef::new(Some(256), 1)), range_check: Some(RangeCheckInstanceDef::default()), ecdsa: None, - bitwise: Some(BitwiseInstanceDef::new(16)), + bitwise: Some(BitwiseInstanceDef::new(Some(16))), ec_op: None, - keccak: Some(KeccakInstanceDef::new(2048, vec![200; 8])), + keccak: Some(KeccakInstanceDef::new(Some(2048), vec![200; 8])), } } diff --git a/src/types/instance_definitions/ec_op_instance_def.rs b/src/types/instance_definitions/ec_op_instance_def.rs index 03083c6866..5f5a55d30a 100644 --- a/src/types/instance_definitions/ec_op_instance_def.rs +++ b/src/types/instance_definitions/ec_op_instance_def.rs @@ -3,7 +3,7 @@ pub(crate) const INPUT_CELLS_PER_EC_OP: u32 = 5; #[derive(Clone, Debug, PartialEq)] pub(crate) struct EcOpInstanceDef { - pub(crate) ratio: u32, + pub(crate) ratio: Option, pub(crate) scalar_height: u32, pub(crate) _scalar_bits: u32, } @@ -11,13 +11,13 @@ pub(crate) struct EcOpInstanceDef { impl EcOpInstanceDef { pub(crate) fn default() -> Self { EcOpInstanceDef { - ratio: 256, + ratio: Some(256), scalar_height: 256, _scalar_bits: 252, } } - pub(crate) fn new(ratio: u32) -> Self { + pub(crate) fn new(ratio: Option) -> Self { EcOpInstanceDef { ratio, scalar_height: 256, @@ -53,17 +53,17 @@ mod tests { #[test] fn test_new() { let builtin_instance = EcOpInstanceDef { - ratio: 8, + ratio: Some(8), scalar_height: 256, _scalar_bits: 252, }; - assert_eq!(EcOpInstanceDef::new(8), builtin_instance); + assert_eq!(EcOpInstanceDef::new(Some(8)), builtin_instance); } #[test] fn test_default() { let builtin_instance = EcOpInstanceDef { - ratio: 256, + ratio: Some(256), scalar_height: 256, _scalar_bits: 252, }; diff --git a/src/types/instance_definitions/ecdsa_instance_def.rs b/src/types/instance_definitions/ecdsa_instance_def.rs index fdd48c1d3f..2c22cbbdfd 100644 --- a/src/types/instance_definitions/ecdsa_instance_def.rs +++ b/src/types/instance_definitions/ecdsa_instance_def.rs @@ -3,7 +3,7 @@ pub(crate) const _INPUTCELLS_PER_SIGNATURE: u32 = 2; #[derive(Debug, PartialEq)] pub(crate) struct EcdsaInstanceDef { - pub(crate) ratio: u32, + pub(crate) ratio: Option, pub(crate) _repetitions: u32, pub(crate) _height: u32, pub(crate) _n_hash_bits: u32, @@ -12,14 +12,14 @@ pub(crate) struct EcdsaInstanceDef { impl EcdsaInstanceDef { pub(crate) fn default() -> Self { EcdsaInstanceDef { - ratio: 512, + ratio: Some(512), _repetitions: 1, _height: 256, _n_hash_bits: 251, } } - pub(crate) fn new(ratio: u32) -> Self { + pub(crate) fn new(ratio: Option) -> Self { EcdsaInstanceDef { ratio, _repetitions: 1, @@ -56,18 +56,18 @@ mod tests { #[test] fn test_new() { let builtin_instance = EcdsaInstanceDef { - ratio: 8, + ratio: Some(8), _repetitions: 1, _height: 256, _n_hash_bits: 251, }; - assert_eq!(EcdsaInstanceDef::new(8), builtin_instance); + assert_eq!(EcdsaInstanceDef::new(Some(8)), builtin_instance); } #[test] fn test_default() { let builtin_instance = EcdsaInstanceDef { - ratio: 512, + ratio: Some(512), _repetitions: 1, _height: 256, _n_hash_bits: 251, diff --git a/src/types/instance_definitions/keccak_instance_def.rs b/src/types/instance_definitions/keccak_instance_def.rs index d1bf77195e..394f61ccbe 100644 --- a/src/types/instance_definitions/keccak_instance_def.rs +++ b/src/types/instance_definitions/keccak_instance_def.rs @@ -1,6 +1,6 @@ #[derive(Clone, Debug, PartialEq)] pub(crate) struct KeccakInstanceDef { - pub(crate) _ratio: u32, + pub(crate) ratio: Option, pub(crate) _state_rep: Vec, pub(crate) _instance_per_component: u32, } @@ -8,8 +8,8 @@ pub(crate) struct KeccakInstanceDef { impl Default for KeccakInstanceDef { fn default() -> Self { Self { - // _ratio should be equal to 2 ** 11 -> 2048 - _ratio: 2048, + // ratio should be equal to 2 ** 11 -> 2048 + ratio: Some(2048), _state_rep: vec![200; 8], _instance_per_component: 16, } @@ -17,9 +17,9 @@ impl Default for KeccakInstanceDef { } impl KeccakInstanceDef { - pub(crate) fn new(_ratio: u32, _state_rep: Vec) -> Self { + pub(crate) fn new(ratio: Option, _state_rep: Vec) -> Self { Self { - _ratio, + ratio, _state_rep, ..Default::default() } @@ -53,17 +53,20 @@ mod tests { #[test] fn test_new() { let builtin_instance = KeccakInstanceDef { - _ratio: 2048, + ratio: Some(2048), _state_rep: vec![200; 8], _instance_per_component: 16, }; - assert_eq!(KeccakInstanceDef::new(2048, vec![200; 8]), builtin_instance); + assert_eq!( + KeccakInstanceDef::new(Some(2048), vec![200; 8]), + builtin_instance + ); } #[test] fn test_default() { let builtin_instance = KeccakInstanceDef { - _ratio: 2048, + ratio: Some(2048), _state_rep: vec![200; 8], _instance_per_component: 16, }; diff --git a/src/types/instance_definitions/pedersen_instance_def.rs b/src/types/instance_definitions/pedersen_instance_def.rs index bd225d059c..afee43b9ce 100644 --- a/src/types/instance_definitions/pedersen_instance_def.rs +++ b/src/types/instance_definitions/pedersen_instance_def.rs @@ -5,7 +5,7 @@ pub(crate) const INPUT_CELLS_PER_HASH: u32 = 2; #[derive(Debug, PartialEq)] pub(crate) struct PedersenInstanceDef { - pub(crate) ratio: u32, + pub(crate) ratio: Option, pub(crate) _repetitions: u32, pub(crate) _element_height: u32, pub(crate) _element_bits: u32, @@ -16,7 +16,7 @@ pub(crate) struct PedersenInstanceDef { impl PedersenInstanceDef { pub(crate) fn default() -> Self { PedersenInstanceDef { - ratio: 8, + ratio: Some(8), _repetitions: 4, _element_height: 256, _element_bits: 252, @@ -25,7 +25,7 @@ impl PedersenInstanceDef { } } - pub(crate) fn new(ratio: u32, _repetitions: u32) -> Self { + pub(crate) fn new(ratio: Option, _repetitions: u32) -> Self { PedersenInstanceDef { ratio, _repetitions, @@ -64,20 +64,20 @@ mod tests { #[test] fn test_new() { let builtin_instance = PedersenInstanceDef { - ratio: 10, + ratio: Some(10), _repetitions: 2, _element_height: 256, _element_bits: 252, _n_inputs: 2, _hash_limit: BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), }; - assert_eq!(PedersenInstanceDef::new(10, 2), builtin_instance); + assert_eq!(PedersenInstanceDef::new(Some(10), 2), builtin_instance); } #[test] fn test_default() { let builtin_instance = PedersenInstanceDef { - ratio: 8, + ratio: Some(8), _repetitions: 4, _element_height: 256, _element_bits: 252, diff --git a/src/types/instance_definitions/range_check_instance_def.rs b/src/types/instance_definitions/range_check_instance_def.rs index 24057a7888..f844fd1e0b 100644 --- a/src/types/instance_definitions/range_check_instance_def.rs +++ b/src/types/instance_definitions/range_check_instance_def.rs @@ -2,19 +2,19 @@ pub(crate) const CELLS_PER_RANGE_CHECK: u32 = 1; #[derive(Debug, PartialEq)] pub(crate) struct RangeCheckInstanceDef { - pub(crate) ratio: u32, + pub(crate) ratio: Option, pub(crate) n_parts: u32, } impl RangeCheckInstanceDef { pub(crate) fn default() -> Self { RangeCheckInstanceDef { - ratio: 8, + ratio: Some(8), n_parts: 8, } } - pub(crate) fn new(ratio: u32, n_parts: u32) -> Self { + pub(crate) fn new(ratio: Option, n_parts: u32) -> Self { RangeCheckInstanceDef { ratio, n_parts } } @@ -46,16 +46,16 @@ mod tests { #[test] fn test_new() { let builtin_instance = RangeCheckInstanceDef { - ratio: 10, + ratio: Some(10), n_parts: 10, }; - assert_eq!(RangeCheckInstanceDef::new(10, 10), builtin_instance); + assert_eq!(RangeCheckInstanceDef::new(Some(10), 10), builtin_instance); } #[test] fn test_default() { let builtin_instance = RangeCheckInstanceDef { - ratio: 8, + ratio: Some(8), n_parts: 8, }; assert_eq!(RangeCheckInstanceDef::default(), builtin_instance); diff --git a/src/utils.rs b/src/utils.rs index 8e9db41827..34be8b8262 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -217,7 +217,7 @@ pub mod test_utils { let mut vm = VirtualMachine::new(false); vm.builtin_runners = vec![( "range_check", - RangeCheckBuiltinRunner::new(8, 8, true).into(), + RangeCheckBuiltinRunner::new(Some(8), 8, true).into(), )]; vm }}; From fe0655976df325c1ae03ad07262eb93ab47da308 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 13:25:24 -0300 Subject: [PATCH 02/35] Move builtin runner ratios to option --- src/vm/runners/builtin_runner/bitwise.rs | 18 ++--- src/vm/runners/builtin_runner/ec_op.rs | 19 +++--- src/vm/runners/builtin_runner/hash.rs | 42 ++++++------ src/vm/runners/builtin_runner/keccak.rs | 6 +- src/vm/runners/builtin_runner/mod.rs | 70 +++++++++++--------- src/vm/runners/builtin_runner/range_check.rs | 54 +++++++-------- src/vm/runners/builtin_runner/signature.rs | 38 +---------- src/vm/runners/cairo_runner.rs | 4 +- src/vm/vm_memory/memory.rs | 8 +-- 9 files changed, 117 insertions(+), 142 deletions(-) diff --git a/src/vm/runners/builtin_runner/bitwise.rs b/src/vm/runners/builtin_runner/bitwise.rs index d4f5bc33ff..29d1bff2b5 100644 --- a/src/vm/runners/builtin_runner/bitwise.rs +++ b/src/vm/runners/builtin_runner/bitwise.rs @@ -21,7 +21,7 @@ use super::BITWISE_BUILTIN_NAME; #[derive(Debug, Clone)] pub struct BitwiseBuiltinRunner { - ratio: u32, + ratio: Option, pub base: usize, pub(crate) cells_per_instance: u32, pub(crate) n_input_cells: u32, @@ -61,7 +61,7 @@ impl BitwiseBuiltinRunner { self.base } - pub fn ratio(&self) -> u32 { + pub fn ratio(&self) -> Option { self.ratio } @@ -241,7 +241,7 @@ mod tests { #[test] fn get_used_instances() { - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true); + let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); let mut vm = vm!(); @@ -259,7 +259,7 @@ mod tests { #[test] fn final_stack() { - let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true); + let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); let mut vm = vm!(); @@ -282,7 +282,7 @@ mod tests { #[test] fn final_stack_error_stop_pointer() { - let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true); + let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); let mut vm = vm!(); @@ -309,7 +309,7 @@ mod tests { #[test] fn final_stack_error_when_notincluded() { - let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), false); + let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), false); let mut vm = vm!(); @@ -332,7 +332,7 @@ mod tests { #[test] fn final_stack_error_non_relocatable() { - let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true); + let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); let mut vm = vm!(); @@ -356,7 +356,7 @@ mod tests { #[test] fn get_used_cells_and_allocated_size_test() { let builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true).into(); + BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true).into(); let mut vm = vm!(); @@ -401,7 +401,7 @@ mod tests { #[test] fn get_allocated_memory_units() { - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true); + let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); let mut vm = vm!(); diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index 152df6dd8c..35860b5f16 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -19,7 +19,7 @@ use super::EC_OP_BUILTIN_NAME; #[derive(Debug, Clone)] pub struct EcOpBuiltinRunner { - ratio: u32, + ratio: Option, pub base: usize, pub(crate) cells_per_instance: u32, pub(crate) n_input_cells: u32, @@ -127,7 +127,7 @@ impl EcOpBuiltinRunner { self.base } - pub fn ratio(&self) -> u32 { + pub fn ratio(&self) -> Option { self.ratio } @@ -344,7 +344,7 @@ mod tests { #[test] fn get_used_instances() { - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(10), true); + let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![1]); @@ -354,7 +354,7 @@ mod tests { #[test] fn final_stack() { - let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(10), true); + let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true); let mut vm = vm!(); @@ -377,7 +377,7 @@ mod tests { #[test] fn final_stack_error_stop_pointer() { - let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(10), true); + let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true); let mut vm = vm!(); @@ -404,7 +404,7 @@ mod tests { #[test] fn final_stack_error_when_notincluded() { - let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(10), false); + let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), false); let mut vm = vm!(); @@ -427,7 +427,7 @@ mod tests { #[test] fn final_stack_error_non_relocatable() { - let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(10), true); + let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true); let mut vm = vm!(); @@ -450,7 +450,8 @@ mod tests { #[test] fn get_used_cells_and_allocated_size_test() { - let builtin: BuiltinRunner = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(10), true).into(); + let builtin: BuiltinRunner = + EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true).into(); let mut vm = vm!(); @@ -494,7 +495,7 @@ mod tests { #[test] fn get_allocated_memory_units() { - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(10), true); + let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true); let mut vm = vm!(); diff --git a/src/vm/runners/builtin_runner/hash.rs b/src/vm/runners/builtin_runner/hash.rs index 599f21363c..11888b64cb 100644 --- a/src/vm/runners/builtin_runner/hash.rs +++ b/src/vm/runners/builtin_runner/hash.rs @@ -19,7 +19,7 @@ use super::EC_OP_BUILTIN_NAME; #[derive(Debug, Clone)] pub struct HashBuiltinRunner { pub base: usize, - ratio: u32, + ratio: Option, pub(crate) cells_per_instance: u32, pub(crate) n_input_cells: u32, pub(crate) stop_ptr: Option, @@ -31,7 +31,7 @@ pub struct HashBuiltinRunner { } impl HashBuiltinRunner { - pub fn new(ratio: u32, included: bool) -> Self { + pub fn new(ratio: Option, included: bool) -> Self { HashBuiltinRunner { base: 0, ratio, @@ -60,7 +60,7 @@ impl HashBuiltinRunner { self.base } - pub fn ratio(&self) -> u32 { + pub fn ratio(&self) -> Option { self.ratio } @@ -228,7 +228,7 @@ mod tests { #[test] fn get_used_instances() { - let builtin = HashBuiltinRunner::new(10, true); + let builtin = HashBuiltinRunner::new(Some(10), true); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![1]); @@ -238,7 +238,7 @@ mod tests { #[test] fn final_stack() { - let mut builtin = HashBuiltinRunner::new(10, true); + let mut builtin = HashBuiltinRunner::new(Some(10), true); let mut vm = vm!(); @@ -261,7 +261,7 @@ mod tests { #[test] fn final_stack_error_stop_pointer() { - let mut builtin = HashBuiltinRunner::new(10, true); + let mut builtin = HashBuiltinRunner::new(Some(10), true); let mut vm = vm!(); @@ -288,7 +288,7 @@ mod tests { #[test] fn final_stack_error_when_not_included() { - let mut builtin = HashBuiltinRunner::new(10, false); + let mut builtin = HashBuiltinRunner::new(Some(10), false); let mut vm = vm!(); @@ -311,7 +311,7 @@ mod tests { #[test] fn final_stack_error_non_relocatable() { - let mut builtin = HashBuiltinRunner::new(10, true); + let mut builtin = HashBuiltinRunner::new(Some(10), true); let mut vm = vm!(); @@ -334,7 +334,7 @@ mod tests { #[test] fn get_used_cells_and_allocated_size_test() { - let builtin: BuiltinRunner = HashBuiltinRunner::new(10, true).into(); + let builtin: BuiltinRunner = HashBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -379,7 +379,7 @@ mod tests { #[test] fn get_allocated_memory_units() { - let builtin = HashBuiltinRunner::new(10, true); + let builtin = HashBuiltinRunner::new(Some(10), true); let mut vm = vm!(); @@ -423,7 +423,7 @@ mod tests { #[test] fn deduce_memory_cell_pedersen_for_preset_memory_valid() { let memory = memory![((0, 3), 32), ((0, 4), 72), ((0, 5), 0)]; - let builtin = HashBuiltinRunner::new(8, true); + let builtin = HashBuiltinRunner::new(Some(8), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 5)), &memory); assert_eq!( @@ -441,7 +441,7 @@ mod tests { #[test] fn deduce_memory_cell_pedersen_for_preset_memory_incorrect_offset() { let memory = memory![((0, 4), 32), ((0, 5), 72), ((0, 6), 0)]; - let builtin = HashBuiltinRunner::new(8, true); + let builtin = HashBuiltinRunner::new(Some(8), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 6)), &memory); assert_eq!(result, Ok(None)); } @@ -449,7 +449,7 @@ mod tests { #[test] fn deduce_memory_cell_pedersen_for_preset_memory_no_values_to_hash() { let memory = memory![((0, 4), 72), ((0, 5), 0)]; - let builtin = HashBuiltinRunner::new(8, true); + let builtin = HashBuiltinRunner::new(Some(8), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 5)), &memory); assert_eq!(result, Ok(None)); } @@ -457,7 +457,7 @@ mod tests { #[test] fn deduce_memory_cell_pedersen_for_preset_memory_already_computed() { let memory = memory![((0, 3), 32), ((0, 4), 72), ((0, 5), 0)]; - let mut builtin = HashBuiltinRunner::new(8, true); + let mut builtin = HashBuiltinRunner::new(Some(8), true); builtin.verified_addresses = RefCell::new(vec![Relocatable::from((0, 5))]); let result = builtin.deduce_memory_cell(Relocatable::from((0, 5)), &memory); assert_eq!(result, Ok(None)); @@ -465,14 +465,14 @@ mod tests { #[test] fn get_memory_segment_addresses() { - let builtin = HashBuiltinRunner::new(256, true); + let builtin = HashBuiltinRunner::new(Some(256), true); assert_eq!(builtin.get_memory_segment_addresses(), (0, None),); } #[test] fn get_memory_accesses_missing_segment_used_sizes() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(256, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(256), true)); let vm = vm!(); assert_eq!( @@ -483,7 +483,7 @@ mod tests { #[test] fn get_memory_accesses_empty() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(256, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![0]); @@ -492,7 +492,7 @@ mod tests { #[test] fn get_memory_accesses() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(256, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); @@ -509,7 +509,7 @@ mod tests { #[test] fn get_used_cells_missing_segment_used_sizes() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(256, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(256), true)); let vm = vm!(); assert_eq!( @@ -520,7 +520,7 @@ mod tests { #[test] fn get_used_cells_empty() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(256, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![0]); @@ -529,7 +529,7 @@ mod tests { #[test] fn get_used_cells() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(256, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index 39ee2ee19a..84214a6631 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -17,7 +17,7 @@ const KECCAK_ARRAY_LEN: usize = 25; #[derive(Debug, Clone)] pub struct KeccakBuiltinRunner { - ratio: u32, + ratio: Option, pub base: usize, pub(crate) cells_per_instance: u32, pub(crate) n_input_cells: u32, @@ -32,7 +32,7 @@ impl KeccakBuiltinRunner { pub(crate) fn new(instance_def: &KeccakInstanceDef, included: bool) -> Self { KeccakBuiltinRunner { base: 0, - ratio: instance_def._ratio, + ratio: instance_def.ratio, n_input_cells: instance_def._state_rep.len() as u32, cells_per_instance: instance_def.cells_per_builtin(), stop_ptr: None, @@ -59,7 +59,7 @@ impl KeccakBuiltinRunner { self.base } - pub fn ratio(&self) -> u32 { + pub fn ratio(&self) -> Option { self.ratio } diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 78e1b95677..41fd0e59e8 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -132,13 +132,13 @@ impl BuiltinRunner { pub fn ratio(&self) -> Option { match self { - BuiltinRunner::Bitwise(bitwise) => Some(bitwise.ratio()), - BuiltinRunner::EcOp(ec) => Some(ec.ratio()), - BuiltinRunner::Hash(hash) => Some(hash.ratio()), + BuiltinRunner::Bitwise(bitwise) => bitwise.ratio(), + BuiltinRunner::EcOp(ec) => ec.ratio(), + BuiltinRunner::Hash(hash) => hash.ratio(), BuiltinRunner::Output(_) => None, - BuiltinRunner::RangeCheck(range_check) => Some(range_check.ratio()), - BuiltinRunner::Keccak(keccak) => Some(keccak.ratio()), - BuiltinRunner::Signature(ref signature) => Some(signature.ratio()), + BuiltinRunner::RangeCheck(range_check) => range_check.ratio(), + BuiltinRunner::Keccak(keccak) => keccak.ratio(), + BuiltinRunner::Signature(ref signature) => signature.ratio(), } } @@ -493,21 +493,21 @@ mod tests { #[test] fn get_n_input_cells_bitwise() { - let bitwise = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true); + let bitwise = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); let builtin: BuiltinRunner = bitwise.clone().into(); assert_eq!(bitwise.n_input_cells, builtin.n_input_cells()) } #[test] fn get_n_input_cells_hash() { - let hash = HashBuiltinRunner::new(10, true); + let hash = HashBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = hash.clone().into(); assert_eq!(hash.n_input_cells, builtin.n_input_cells()) } #[test] fn get_n_input_cells_range_check() { - let range_check = RangeCheckBuiltinRunner::new(10, 10, true); + let range_check = RangeCheckBuiltinRunner::new(Some(10), 10, true); let builtin: BuiltinRunner = range_check.clone().into(); assert_eq!(range_check.n_input_cells, builtin.n_input_cells()) } @@ -535,21 +535,21 @@ mod tests { #[test] fn get_cells_per_instance_bitwise() { - let bitwise = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true); + let bitwise = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); let builtin: BuiltinRunner = bitwise.clone().into(); assert_eq!(bitwise.cells_per_instance, builtin.cells_per_instance()) } #[test] fn get_cells_per_instance_hash() { - let hash = HashBuiltinRunner::new(10, true); + let hash = HashBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = hash.clone().into(); assert_eq!(hash.cells_per_instance, builtin.cells_per_instance()) } #[test] fn get_cells_per_instance_range_check() { - let range_check = RangeCheckBuiltinRunner::new(10, 10, true); + let range_check = RangeCheckBuiltinRunner::new(Some(10), 10, true); let builtin: BuiltinRunner = range_check.clone().into(); assert_eq!(range_check.cells_per_instance, builtin.cells_per_instance()) } @@ -584,21 +584,21 @@ mod tests { #[test] fn get_name_bitwise() { - let bitwise = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(10), true); + let bitwise = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); let builtin: BuiltinRunner = bitwise.into(); assert_eq!(BITWISE_BUILTIN_NAME, builtin.name()) } #[test] fn get_name_hash() { - let hash = HashBuiltinRunner::new(10, true); + let hash = HashBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = hash.into(); assert_eq!(HASH_BUILTIN_NAME, builtin.name()) } #[test] fn get_name_range_check() { - let range_check = RangeCheckBuiltinRunner::new(10, 10, true); + let range_check = RangeCheckBuiltinRunner::new(Some(10), 10, true); let builtin: BuiltinRunner = range_check.into(); assert_eq!(RANGE_CHECK_BUILTIN_NAME, builtin.name()) } @@ -627,7 +627,7 @@ mod tests { #[test] fn get_allocated_memory_units_bitwise_with_items() { let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::new(10), + &BitwiseInstanceDef::new(Some(10)), true, )); @@ -672,7 +672,10 @@ mod tests { #[test] fn get_allocated_memory_units_ec_op_with_items() { - let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::new(10), true)); + let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new( + &EcOpInstanceDef::new(Some(10)), + true, + )); let mut vm = vm!(); @@ -715,7 +718,7 @@ mod tests { #[test] fn get_allocated_memory_units_hash_with_items() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(10, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(10), true)); let mut vm = vm!(); @@ -758,7 +761,7 @@ mod tests { #[test] fn get_allocated_memory_units_range_check_with_items() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(10, 12, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(10), 12, true)); let mut vm = vm!(); @@ -822,7 +825,7 @@ mod tests { #[test] fn get_allocated_memory_units_range_check() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(8, 8, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, true)); let vm = vm!(); assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); } @@ -864,7 +867,7 @@ mod tests { #[test] fn get_range_check_usage_range_check() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(8, 8, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, true)); let memory = memory![((0, 0), 1), ((0, 1), 2), ((0, 2), 3), ((0, 3), 4)]; assert_eq!(builtin.get_range_check_usage(&memory), Some((1, 4))); } @@ -930,7 +933,10 @@ mod tests { #[test] fn get_used_diluted_check_units_ec_op() { - let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::new(10), true)); + let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new( + &EcOpInstanceDef::new(Some(10)), + true, + )); assert_eq!(builtin.get_used_diluted_check_units(270, 7), 0); } @@ -942,7 +948,7 @@ mod tests { #[test] fn get_used_diluted_check_units_range_check() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(8, 8, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, true)); assert_eq!(builtin.get_used_diluted_check_units(270, 7), 0); } @@ -965,7 +971,7 @@ mod tests { let output_builtin: BuiltinRunner = OutputBuiltinRunner::new(true).into(); assert_eq!(output_builtin.get_memory_segment_addresses(), (0, None),); let range_check_builtin: BuiltinRunner = - BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(8, 8, true)); + BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, true)); assert_eq!( range_check_builtin.get_memory_segment_addresses(), (0, None), @@ -1093,7 +1099,7 @@ mod tests { #[test] fn run_security_checks_range_check_missing_memory_cells_with_offsets() { - let range_check_builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let range_check_builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let builtin: BuiltinRunner = range_check_builtin.into(); let mut vm = vm!(); @@ -1117,7 +1123,7 @@ mod tests { #[test] fn run_security_checks_range_check_missing_memory_cells() { let builtin: BuiltinRunner = - BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(8, 8, true)); + BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, true)); let mut vm = vm!(); vm.segments.memory = memory![((0, 1), 1)]; @@ -1132,7 +1138,7 @@ mod tests { #[test] fn run_security_checks_range_check_empty() { - let range_check_builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let range_check_builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let builtin: BuiltinRunner = range_check_builtin.into(); @@ -1319,7 +1325,7 @@ mod tests { /// method when the builtin is a RangeCheckBuiltinRunner. #[test] fn get_used_perm_range_check_units_range_check() { - let builtin_runner: BuiltinRunner = RangeCheckBuiltinRunner::new(8, 8, true).into(); + let builtin_runner: BuiltinRunner = RangeCheckBuiltinRunner::new(Some(8), 8, true).into(); let mut vm = vm!(); vm.current_step = 8; @@ -1340,7 +1346,7 @@ mod tests { let output_builtin: BuiltinRunner = OutputBuiltinRunner::new(true).into(); assert_eq!(output_builtin.ratio(), None,); let range_check_builtin: BuiltinRunner = - BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(8, 8, true)); + BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, true)); assert_eq!(range_check_builtin.ratio(), (Some(8)),); let keccak_builtin: BuiltinRunner = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true).into(); @@ -1390,7 +1396,7 @@ mod tests { vm.segments.segment_used_sizes = Some(vec![4]); let range_check_builtin: BuiltinRunner = - BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(8, 8, true)); + BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, true)); assert_eq!(range_check_builtin.get_used_instances(&vm.segments), Ok(4)); } @@ -1404,7 +1410,7 @@ mod tests { BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), false)), BuiltinRunner::Hash(HashBuiltinRunner::new(1, false)), BuiltinRunner::Output(OutputBuiltinRunner::new(false)), - BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(8, 8, false)), + BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, false)), BuiltinRunner::Keccak(KeccakBuiltinRunner::new( &KeccakInstanceDef::default(), false, @@ -1431,7 +1437,7 @@ mod tests { BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), false)), BuiltinRunner::Hash(HashBuiltinRunner::new(1, false)), BuiltinRunner::Output(OutputBuiltinRunner::new(false)), - BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(8, 8, false)), + BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, false)), BuiltinRunner::Keccak(KeccakBuiltinRunner::new( &KeccakInstanceDef::default(), false, diff --git a/src/vm/runners/builtin_runner/range_check.rs b/src/vm/runners/builtin_runner/range_check.rs index edb5e34f46..89835d9ae3 100644 --- a/src/vm/runners/builtin_runner/range_check.rs +++ b/src/vm/runners/builtin_runner/range_check.rs @@ -28,7 +28,7 @@ use super::RANGE_CHECK_BUILTIN_NAME; #[derive(Debug, Clone)] pub struct RangeCheckBuiltinRunner { - ratio: u32, + ratio: Option, base: usize, pub(crate) stop_ptr: Option, pub(crate) cells_per_instance: u32, @@ -41,7 +41,7 @@ pub struct RangeCheckBuiltinRunner { } impl RangeCheckBuiltinRunner { - pub fn new(ratio: u32, n_parts: u32, included: bool) -> RangeCheckBuiltinRunner { + pub fn new(ratio: Option, n_parts: u32, included: bool) -> RangeCheckBuiltinRunner { let inner_rc_bound = 1_usize << 16; let bound = Felt::one().shl(16 * n_parts); @@ -81,7 +81,7 @@ impl RangeCheckBuiltinRunner { self.base } - pub fn ratio(&self) -> u32 { + pub fn ratio(&self) -> Option { self.ratio } @@ -262,7 +262,7 @@ mod tests { #[test] fn get_used_instances() { - let builtin = RangeCheckBuiltinRunner::new(10, 12, true); + let builtin = RangeCheckBuiltinRunner::new(Some(10), 12, true); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![1]); @@ -272,7 +272,7 @@ mod tests { #[test] fn final_stack() { - let mut builtin = RangeCheckBuiltinRunner::new(10, 12, true); + let mut builtin = RangeCheckBuiltinRunner::new(Some(10), 12, true); let mut vm = vm!(); @@ -295,7 +295,7 @@ mod tests { #[test] fn final_stack_error_stop_pointer() { - let mut builtin = RangeCheckBuiltinRunner::new(10, 12, true); + let mut builtin = RangeCheckBuiltinRunner::new(Some(10), 12, true); let mut vm = vm!(); @@ -322,7 +322,7 @@ mod tests { #[test] fn final_stack_error_when_notincluded() { - let mut builtin = RangeCheckBuiltinRunner::new(10, 12, false); + let mut builtin = RangeCheckBuiltinRunner::new(Some(10), 12, false); let mut vm = vm!(); @@ -345,7 +345,7 @@ mod tests { #[test] fn final_stack_error_non_relocatable() { - let mut builtin = RangeCheckBuiltinRunner::new(10, 12, true); + let mut builtin = RangeCheckBuiltinRunner::new(Some(10), 12, true); let mut vm = vm!(); @@ -368,7 +368,7 @@ mod tests { #[test] fn get_used_cells_and_allocated_size_test() { - let builtin: BuiltinRunner = RangeCheckBuiltinRunner::new(10, 12, true).into(); + let builtin: BuiltinRunner = RangeCheckBuiltinRunner::new(Some(10), 12, true).into(); let mut vm = vm!(); @@ -413,7 +413,7 @@ mod tests { #[test] fn get_allocated_memory_units() { - let builtin = RangeCheckBuiltinRunner::new(10, 12, true); + let builtin = RangeCheckBuiltinRunner::new(Some(10), 12, true); let mut vm = vm!(); @@ -456,7 +456,7 @@ mod tests { #[test] fn initialize_segments_for_range_check() { - let mut builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let mut builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let mut segments = MemorySegmentManager::new(); builtin.initialize_segments(&mut segments); assert_eq!(builtin.base, 0); @@ -464,7 +464,7 @@ mod tests { #[test] fn get_initial_stack_for_range_check_with_base() { - let mut builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let mut builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); builtin.base = 1; let initial_stack = builtin.initial_stack(); assert_eq!( @@ -476,14 +476,14 @@ mod tests { #[test] fn get_memory_segment_addresses() { - let builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); assert_eq!(builtin.get_memory_segment_addresses(), (0, None),); } #[test] fn get_memory_accesses_missing_segment_used_sizes() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(256, 8, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(256), 8, true)); let vm = vm!(); assert_eq!( @@ -494,7 +494,7 @@ mod tests { #[test] fn get_memory_accesses_empty() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(256, 8, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(256), 8, true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![0]); @@ -503,7 +503,7 @@ mod tests { #[test] fn get_memory_accesses() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(256, 8, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(256), 8, true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); @@ -520,19 +520,19 @@ mod tests { #[test] fn test_base() { - let builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); assert_eq!(builtin.base(), 0); } #[test] fn test_ratio() { - let builtin = RangeCheckBuiltinRunner::new(8, 8, true); - assert_eq!(builtin.ratio(), 8); + let builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); + assert_eq!(builtin.ratio(), Some(8)); } #[test] fn get_used_cells_missing_segment_used_sizes() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(256, 8, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(256), 8, true)); let vm = vm!(); assert_eq!( @@ -543,7 +543,7 @@ mod tests { #[test] fn get_used_cells_empty() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(256, 8, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(256), 8, true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![0]); @@ -552,7 +552,7 @@ mod tests { #[test] fn get_used_cells() { - let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(256, 8, true)); + let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(256), 8, true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); @@ -561,14 +561,14 @@ mod tests { #[test] fn get_range_check_usage_succesful_a() { - let builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let memory = memory![((0, 0), 1), ((0, 1), 2), ((0, 2), 3), ((0, 3), 4)]; assert_eq!(builtin.get_range_check_usage(&memory), Some((1, 4))); } #[test] fn get_range_check_usage_succesful_b() { - let builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let memory = memory![ ((0, 0), 1465218365), ((0, 1), 2134570341), @@ -580,7 +580,7 @@ mod tests { #[test] fn get_range_check_usage_succesful_c() { - let builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let memory = memory![ ((0, 0), 634834751465218365_i64), ((0, 1), 42876922134570341_i64), @@ -594,7 +594,7 @@ mod tests { #[test] fn get_range_check_empty_memory() { - let builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let memory = Memory::new(); assert_eq!(builtin.get_range_check_usage(&memory), None); } @@ -602,7 +602,7 @@ mod tests { /// Test that the method get_used_perm_range_check_units works as intended. #[test] fn get_used_perm_range_check_units() { - let builtin_runner = RangeCheckBuiltinRunner::new(8, 8, true); + let builtin_runner = RangeCheckBuiltinRunner::new(Some(8), 8, true); let mut vm = vm!(); vm.current_step = 8; diff --git a/src/vm/runners/builtin_runner/signature.rs b/src/vm/runners/builtin_runner/signature.rs index 1095de4a93..b73e7e65c5 100644 --- a/src/vm/runners/builtin_runner/signature.rs +++ b/src/vm/runners/builtin_runner/signature.rs @@ -26,7 +26,7 @@ use super::SIGNATURE_BUILTIN_NAME; #[derive(Debug, Clone)] pub struct SignatureBuiltinRunner { pub(crate) included: bool, - ratio: u32, + ratio: Option, base: usize, pub(crate) cells_per_instance: u32, pub(crate) n_input_cells: u32, @@ -270,23 +270,9 @@ mod tests { }, }; - #[test] - fn get_used_cells_and_allocated_size_min_step_not_reached() { - let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); - let mut vm = vm!(); - vm.current_step = 100; - vm.segments.segment_used_sizes = Some(vec![1]); - assert_eq!( - builtin.get_used_cells_and_allocated_size(&vm), - Err(MemoryError::InsufficientAllocatedCells( - InsufficientAllocatedCellsError::MinStepNotReached(512, SIGNATURE_BUILTIN_NAME) - )) - ); - } - #[test] fn get_used_cells_and_allocated_size_valid() { - let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(10), true); + let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true); let mut vm = vm!(); vm.current_step = 110; vm.segments.segment_used_sizes = Some(vec![1]); @@ -505,7 +491,7 @@ mod tests { #[test] fn test_ratio() { let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); - assert_eq!(builtin.ratio(), 512); + assert_eq!(builtin.ratio(), Some(512)); } #[test] @@ -540,24 +526,6 @@ mod tests { ) } - #[test] - fn get_used_cells_and_allocated_size_safe_div_fail() { - let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); - let mut vm = vm!(); - vm.segments.segment_used_sizes = Some(vec![600]); - vm.current_step = 551; - assert_eq!( - builtin.get_used_cells_and_allocated_size(&vm), - Err(MemoryError::InsufficientAllocatedCells( - InsufficientAllocatedCellsError::CurrentStepNotDivisibleByBuiltinRatio( - SIGNATURE_BUILTIN_NAME, - 551, - builtin.ratio as usize - ) - )) - ) - } - #[test] fn get_used_cells_and_allocated_size_insufficient_allocated() { let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index 8f210c33dd..f49f28a23e 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -3743,7 +3743,7 @@ mod tests { let mut vm = vm!(); vm.builtin_runners = vec![( RANGE_CHECK_BUILTIN_NAME, - RangeCheckBuiltinRunner::new(8, 8, true).into(), + RangeCheckBuiltinRunner::new(Some(8), 8, true).into(), )]; vm.segments.memory.data = vec![vec![Some(MemoryCell::new(mayberelocatable!( 0x80FF_8000_0530u64 @@ -3809,7 +3809,7 @@ mod tests { let mut vm = vm!(); vm.builtin_runners = vec![( RANGE_CHECK_BUILTIN_NAME, - RangeCheckBuiltinRunner::new(8, 8, true).into(), + RangeCheckBuiltinRunner::new(Some(8), 8, true).into(), )]; vm.segments.memory.data = vec![vec![Some(MemoryCell::new(mayberelocatable!( 0x80FF_8000_0530u64 diff --git a/src/vm/vm_memory/memory.rs b/src/vm/vm_memory/memory.rs index 6f6afe4892..8f35da534e 100644 --- a/src/vm/vm_memory/memory.rs +++ b/src/vm/vm_memory/memory.rs @@ -630,7 +630,7 @@ mod memory_tests { #[test] fn validate_existing_memory_for_range_check_within_bounds() { - let mut builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let mut builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let mut segments = MemorySegmentManager::new(); builtin.initialize_segments(&mut segments); builtin.add_validation_rule(&mut segments.memory); @@ -654,7 +654,7 @@ mod memory_tests { #[test] fn validate_existing_memory_for_range_check_outside_bounds() { - let mut builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let mut builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let mut segments = MemorySegmentManager::new(); segments.add(); builtin.initialize_segments(&mut segments); @@ -741,7 +741,7 @@ mod memory_tests { #[test] fn validate_existing_memory_for_range_check_relocatable_value() { - let mut builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let mut builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let mut segments = MemorySegmentManager::new(); builtin.initialize_segments(&mut segments); segments.memory = memory![((0, 0), (0, 4))]; @@ -755,7 +755,7 @@ mod memory_tests { #[test] fn validate_existing_memory_for_range_check_out_of_bounds_diff_segment() { - let mut builtin = RangeCheckBuiltinRunner::new(8, 8, true); + let mut builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let mut segments = MemorySegmentManager::new(); segments.memory = Memory::new(); segments.add(); From 64ce3c65d6f05c1f654a2c25bfab7801c1a674d8 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 13:25:53 -0300 Subject: [PATCH 03/35] Modify methods to accomodate dynamic ratios --- src/vm/errors/memory_errors.rs | 4 -- src/vm/runners/builtin_runner/bitwise.rs | 54 +++++++++---------- src/vm/runners/builtin_runner/ec_op.rs | 51 +++++++----------- src/vm/runners/builtin_runner/hash.rs | 55 +++++++++----------- src/vm/runners/builtin_runner/keccak.rs | 54 +++++++++---------- src/vm/runners/builtin_runner/range_check.rs | 53 +++++++++---------- src/vm/runners/builtin_runner/signature.rs | 55 +++++++++----------- 7 files changed, 149 insertions(+), 177 deletions(-) diff --git a/src/vm/errors/memory_errors.rs b/src/vm/errors/memory_errors.rs index afe99d6867..67aefbc073 100644 --- a/src/vm/errors/memory_errors.rs +++ b/src/vm/errors/memory_errors.rs @@ -89,10 +89,6 @@ pub enum MemoryError { #[derive(Debug, PartialEq, Eq, Error)] pub enum InsufficientAllocatedCellsError { - #[error("Number of steps must be at least {0} for the {1} builtin.")] - MinStepNotReached(usize, &'static str), - #[error("Failed to get allocated size for builtin {0}, current vm step {1} is not divisible by builtin ratio {2}")] - CurrentStepNotDivisibleByBuiltinRatio(&'static str, usize, usize), #[error("The {0} builtin used {1} cells but the capacity is {2}.")] BuiltinCells(&'static str, usize, usize), #[error("There are only {0} cells to fill the range checks holes, but potentially {1} are required.")] diff --git a/src/vm/runners/builtin_runner/bitwise.rs b/src/vm/runners/builtin_runner/bitwise.rs index 29d1bff2b5..1b1c43e5e6 100644 --- a/src/vm/runners/builtin_runner/bitwise.rs +++ b/src/vm/runners/builtin_runner/bitwise.rs @@ -111,9 +111,23 @@ impl BitwiseBuiltinRunner { } pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - let value = safe_div_usize(vm.current_step, self.ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) + match self.ratio { + None => { + // Dynamic layout has the exact number of instances it needs (up to a power of 2). + let instances: usize = + self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; + let components = ((instances / self.instances_per_component as usize) as usize) + .next_power_of_two(); + Ok(self.cells_per_instance as usize + * self.instances_per_component as usize + * components) + } + Some(ratio) => { + let value = safe_div_usize(vm.current_step, ratio as usize) + .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; + Ok(self.cells_per_instance as usize * value) + } + } } pub fn get_memory_segment_addresses(&self) -> (usize, Option) { @@ -130,33 +144,17 @@ impl BitwiseBuiltinRunner { &self, vm: &VirtualMachine, ) -> Result<(usize, usize), MemoryError> { - let ratio = self.ratio as usize; - let min_step = ratio * self.instances_per_component as usize; - if vm.current_step < min_step { - Err( - InsufficientAllocatedCellsError::MinStepNotReached(min_step, BITWISE_BUILTIN_NAME) - .into(), + let used = self.get_used_cells(&vm.segments)?; + let size = self.get_allocated_memory_units(vm)?; + if used > size { + return Err(InsufficientAllocatedCellsError::BuiltinCells( + BITWISE_BUILTIN_NAME, + used, + size, ) - } else { - let used = self.get_used_cells(&vm.segments)?; - let size = self.cells_per_instance as usize - * safe_div_usize(vm.current_step, ratio).map_err(|_| { - InsufficientAllocatedCellsError::CurrentStepNotDivisibleByBuiltinRatio( - BITWISE_BUILTIN_NAME, - vm.current_step, - ratio, - ) - })?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - BITWISE_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) + .into()); } + Ok((used, size)) } pub fn get_used_diluted_check_units(&self, diluted_spacing: u32, diluted_n_bits: u32) -> usize { diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index 35860b5f16..f4ede9c8f7 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -5,7 +5,7 @@ use crate::types::instance_definitions::ec_op_instance_def::{ EcOpInstanceDef, CELLS_PER_EC_OP, INPUT_CELLS_PER_EC_OP, }; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; -use crate::vm::errors::memory_errors::{InsufficientAllocatedCellsError, MemoryError}; +use crate::vm::errors::memory_errors::MemoryError; use crate::vm::errors::runner_errors::RunnerError; use crate::vm::vm_core::VirtualMachine; use crate::vm::vm_memory::memory::Memory; @@ -213,9 +213,23 @@ impl EcOpBuiltinRunner { } pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - let value = safe_div_usize(vm.current_step, self.ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) + match self.ratio { + None => { + // Dynamic layout has the exact number of instances it needs (up to a power of 2). + let instances: usize = + self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; + let components = ((instances / self.instances_per_component as usize) as usize) + .next_power_of_two(); + Ok(self.cells_per_instance as usize + * self.instances_per_component as usize + * components) + } + Some(ratio) => { + let value = safe_div_usize(vm.current_step, ratio as usize) + .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; + Ok(self.cells_per_instance as usize * value) + } + } } pub fn get_memory_segment_addresses(&self) -> (usize, Option) { @@ -232,33 +246,8 @@ impl EcOpBuiltinRunner { &self, vm: &VirtualMachine, ) -> Result<(usize, usize), MemoryError> { - let ratio = self.ratio as usize; - let min_step = ratio * self.instances_per_component as usize; - if vm.current_step < min_step { - Err( - InsufficientAllocatedCellsError::MinStepNotReached(min_step, EC_OP_BUILTIN_NAME) - .into(), - ) - } else { - let used = self.get_used_cells(&vm.segments)?; - let size = self.cells_per_instance as usize - * safe_div_usize(vm.current_step, ratio).map_err(|_| { - InsufficientAllocatedCellsError::CurrentStepNotDivisibleByBuiltinRatio( - EC_OP_BUILTIN_NAME, - vm.current_step, - ratio, - ) - })?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - EC_OP_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) - } + let size = self.get_used_cells(&vm.segments)?; + Ok((size, size)) } pub fn get_used_instances( diff --git a/src/vm/runners/builtin_runner/hash.rs b/src/vm/runners/builtin_runner/hash.rs index 11888b64cb..7cb8c59ac2 100644 --- a/src/vm/runners/builtin_runner/hash.rs +++ b/src/vm/runners/builtin_runner/hash.rs @@ -115,9 +115,23 @@ impl HashBuiltinRunner { } pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - let value = safe_div_usize(vm.current_step, self.ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) + match self.ratio { + None => { + // Dynamic layout has the exact number of instances it needs (up to a power of 2). + let instances: usize = + self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; + let components = ((instances / self.instances_per_component as usize) as usize) + .next_power_of_two(); + Ok(self.cells_per_instance as usize + * self.instances_per_component as usize + * components) + } + Some(ratio) => { + let value = safe_div_usize(vm.current_step, ratio as usize) + .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; + Ok(self.cells_per_instance as usize * value) + } + } } pub fn get_memory_segment_addresses(&self) -> (usize, Option) { @@ -134,35 +148,18 @@ impl HashBuiltinRunner { &self, vm: &VirtualMachine, ) -> Result<(usize, usize), MemoryError> { - let ratio = self.ratio as usize; - let min_step = ratio * self.instances_per_component as usize; - if vm.current_step < min_step { - Err( - InsufficientAllocatedCellsError::MinStepNotReached(min_step, EC_OP_BUILTIN_NAME) - .into(), + let used = self.get_used_cells(&vm.segments)?; + let size = self.get_allocated_memory_units(vm)?; + if used > size { + return Err(InsufficientAllocatedCellsError::BuiltinCells( + EC_OP_BUILTIN_NAME, + used, + size, ) - } else { - let used = self.get_used_cells(&vm.segments)?; - let size = self.cells_per_instance as usize - * safe_div_usize(vm.current_step, ratio).map_err(|_| { - InsufficientAllocatedCellsError::CurrentStepNotDivisibleByBuiltinRatio( - EC_OP_BUILTIN_NAME, - vm.current_step, - ratio, - ) - })?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - EC_OP_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) + .into()); } + Ok((used, size)) } - pub fn get_used_instances( &self, segments: &MemorySegmentManager, diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index 84214a6631..ad8152e572 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -120,9 +120,23 @@ impl KeccakBuiltinRunner { } pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - let value = safe_div_usize(vm.current_step, self.ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) + match self.ratio { + None => { + // Dynamic layout has the exact number of instances it needs (up to a power of 2). + let instances: usize = + self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; + let components = ((instances / self.instances_per_component as usize) as usize) + .next_power_of_two(); + Ok(self.cells_per_instance as usize + * self.instances_per_component as usize + * components) + } + Some(ratio) => { + let value = safe_div_usize(vm.current_step, ratio as usize) + .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; + Ok(self.cells_per_instance as usize * value) + } + } } pub fn get_memory_segment_addresses(&self) -> (usize, Option) { @@ -139,33 +153,17 @@ impl KeccakBuiltinRunner { &self, vm: &VirtualMachine, ) -> Result<(usize, usize), MemoryError> { - let ratio = self.ratio as usize; - let min_step = ratio * self.instances_per_component as usize; - if vm.current_step < min_step { - Err( - InsufficientAllocatedCellsError::MinStepNotReached(min_step, KECCAK_BUILTIN_NAME) - .into(), + let used = self.get_used_cells(&vm.segments)?; + let size = self.get_allocated_memory_units(vm)?; + if used > size { + return Err(InsufficientAllocatedCellsError::BuiltinCells( + KECCAK_BUILTIN_NAME, + used, + size, ) - } else { - let used = self.get_used_cells(&vm.segments)?; - let size = self.cells_per_instance as usize - * safe_div_usize(vm.current_step, ratio).map_err(|_| { - InsufficientAllocatedCellsError::CurrentStepNotDivisibleByBuiltinRatio( - KECCAK_BUILTIN_NAME, - vm.current_step, - ratio, - ) - })?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - KECCAK_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) + .into()); } + Ok((used, size)) } pub fn get_used_instances( diff --git a/src/vm/runners/builtin_runner/range_check.rs b/src/vm/runners/builtin_runner/range_check.rs index 89835d9ae3..edb88feebe 100644 --- a/src/vm/runners/builtin_runner/range_check.rs +++ b/src/vm/runners/builtin_runner/range_check.rs @@ -113,9 +113,23 @@ impl RangeCheckBuiltinRunner { } pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - let value = safe_div_usize(vm.current_step, self.ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) + match self.ratio { + None => { + // Dynamic layout has the exact number of instances it needs (up to a power of 2). + let instances: usize = + self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; + let components = ((instances / self.instances_per_component as usize) as usize) + .next_power_of_two(); + Ok(self.cells_per_instance as usize + * self.instances_per_component as usize + * components) + } + Some(ratio) => { + let value = safe_div_usize(vm.current_step, ratio as usize) + .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; + Ok(self.cells_per_instance as usize * value) + } + } } pub fn get_memory_segment_addresses(&self) -> (usize, Option) { @@ -132,34 +146,17 @@ impl RangeCheckBuiltinRunner { &self, vm: &VirtualMachine, ) -> Result<(usize, usize), MemoryError> { - let ratio = self.ratio as usize; - let min_step = ratio * self.instances_per_component as usize; - if vm.current_step < min_step { - Err(InsufficientAllocatedCellsError::MinStepNotReached( - min_step, + let used = self.get_used_cells(&vm.segments)?; + let size = self.get_allocated_memory_units(vm)?; + if used > size { + return Err(InsufficientAllocatedCellsError::BuiltinCells( RANGE_CHECK_BUILTIN_NAME, + used, + size, ) - .into()) - } else { - let used = self.get_used_cells(&vm.segments)?; - let size = self.cells_per_instance as usize - * safe_div_usize(vm.current_step, ratio).map_err(|_| { - InsufficientAllocatedCellsError::CurrentStepNotDivisibleByBuiltinRatio( - RANGE_CHECK_BUILTIN_NAME, - vm.current_step, - ratio, - ) - })?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - RANGE_CHECK_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) + .into()); } + Ok((used, size)) } pub fn get_range_check_usage(&self, memory: &Memory) -> Option<(usize, usize)> { diff --git a/src/vm/runners/builtin_runner/signature.rs b/src/vm/runners/builtin_runner/signature.rs index b73e7e65c5..2789f53bb9 100644 --- a/src/vm/runners/builtin_runner/signature.rs +++ b/src/vm/runners/builtin_runner/signature.rs @@ -154,14 +154,28 @@ impl SignatureBuiltinRunner { Ok(None) } - pub fn ratio(&self) -> u32 { + pub fn ratio(&self) -> Option { self.ratio } pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - let value = safe_div_usize(vm.current_step, self.ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) + match self.ratio { + None => { + // Dynamic layout has the exact number of instances it needs (up to a power of 2). + let instances: usize = + self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; + let components = ((instances / self.instances_per_component as usize) as usize) + .next_power_of_two(); + Ok(self.cells_per_instance as usize + * self.instances_per_component as usize + * components) + } + Some(ratio) => { + let value = safe_div_usize(vm.current_step, ratio as usize) + .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; + Ok(self.cells_per_instance as usize * value) + } + } } pub fn get_memory_segment_addresses(&self) -> (usize, Option) { @@ -178,34 +192,17 @@ impl SignatureBuiltinRunner { &self, vm: &VirtualMachine, ) -> Result<(usize, usize), MemoryError> { - let ratio = self.ratio as usize; - let min_step = ratio * self.instances_per_component as usize; - if vm.current_step < min_step { - Err(InsufficientAllocatedCellsError::MinStepNotReached( - min_step, + let used = self.get_used_cells(&vm.segments)?; + let size = self.get_allocated_memory_units(vm)?; + if used > size { + return Err(InsufficientAllocatedCellsError::BuiltinCells( SIGNATURE_BUILTIN_NAME, + used, + size, ) - .into()) - } else { - let used = self.get_used_cells(&vm.segments)?; - let size = self.cells_per_instance as usize - * safe_div_usize(vm.current_step, ratio).map_err(|_| { - InsufficientAllocatedCellsError::CurrentStepNotDivisibleByBuiltinRatio( - SIGNATURE_BUILTIN_NAME, - vm.current_step, - ratio, - ) - })?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - SIGNATURE_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) + .into()); } + Ok((used, size)) } pub fn get_used_instances( From 2a0db12fef180e39010aa341118da28987780bee Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 13:33:35 -0300 Subject: [PATCH 04/35] Misc fixes --- src/vm/runners/builtin_runner/keccak.rs | 19 +++++++++------- src/vm/runners/builtin_runner/mod.rs | 30 ++++++++++++------------- src/vm/runners/cairo_runner.rs | 20 ++++++++--------- src/vm/vm_core.rs | 2 +- 4 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index ad8152e572..c495c510af 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -261,7 +261,7 @@ mod tests { #[test] fn get_used_instances() { let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(10, vec![200; 8]), true).into(); + KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![1]); @@ -271,7 +271,8 @@ mod tests { #[test] fn final_stack() { - let mut builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::new(10, vec![200; 8]), true); + let mut builtin = + KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true); let mut vm = vm!(); @@ -294,7 +295,8 @@ mod tests { #[test] fn final_stack_error_stop_pointer() { - let mut builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::new(10, vec![200; 8]), true); + let mut builtin = + KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true); let mut vm = vm!(); @@ -321,7 +323,7 @@ mod tests { #[test] fn final_stack_error_when_not_included() { let mut builtin = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(10, vec![200; 8]), false); + KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), false); let mut vm = vm!(); @@ -344,7 +346,8 @@ mod tests { #[test] fn final_stack_error_non_relocatable() { - let mut builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::new(10, vec![200; 8]), true); + let mut builtin = + KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true); let mut vm = vm!(); @@ -368,7 +371,7 @@ mod tests { #[test] fn get_used_cells_and_allocated_size_test() { let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(10, vec![200; 8]), true).into(); + KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); let mut vm = vm!(); @@ -395,7 +398,7 @@ mod tests { #[test] fn get_allocated_memory_units() { let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(10, vec![200; 8]), true).into(); + KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); let mut vm = vm!(); vm.current_step = 10; @@ -639,7 +642,7 @@ mod tests { ((0, 35), 0) ]; - let keccak_instance = KeccakInstanceDef::new(2048, vec![1; 8]); + let keccak_instance = KeccakInstanceDef::new(Some(2048), vec![1; 8]); let builtin = KeccakBuiltinRunner::new(&keccak_instance, true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 25)), &memory); diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 41fd0e59e8..dbe379a09b 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -521,7 +521,7 @@ mod tests { #[test] fn get_n_input_cells_ecdsa() { - let signature = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(10), true); + let signature = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true); let builtin: BuiltinRunner = signature.clone().into(); assert_eq!(signature.n_input_cells, builtin.n_input_cells()) } @@ -563,7 +563,7 @@ mod tests { #[test] fn get_cells_per_instance_ecdsa() { - let signature = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(10), true); + let signature = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true); let builtin: BuiltinRunner = signature.clone().into(); assert_eq!(signature.cells_per_instance, builtin.cells_per_instance()) } @@ -612,7 +612,7 @@ mod tests { #[test] fn get_name_ecdsa() { - let signature = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(10), true); + let signature = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true); let builtin: BuiltinRunner = signature.into(); assert_eq!(SIGNATURE_BUILTIN_NAME, builtin.name()) } @@ -805,7 +805,7 @@ mod tests { #[test] fn get_allocated_memory_units_keccak_with_items() { let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new( - &KeccakInstanceDef::new(10, vec![200; 8]), + &KeccakInstanceDef::new(Some(10), vec![200; 8]), true, )); @@ -832,7 +832,7 @@ mod tests { #[test] fn get_allocated_memory_units_hash() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(1, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(1), true)); let vm = vm!(); assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); } @@ -881,7 +881,7 @@ mod tests { #[test] fn get_range_check_usage_hash() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(256, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(256), true)); let memory = memory![((0, 0), 1), ((0, 1), 2), ((0, 2), 3), ((0, 3), 4)]; assert_eq!(builtin.get_range_check_usage(&memory), None); } @@ -942,7 +942,7 @@ mod tests { #[test] fn get_used_diluted_check_units_hash() { - let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(16, true)); + let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(1), true)); assert_eq!(builtin.get_used_diluted_check_units(270, 7), 0); } @@ -966,7 +966,7 @@ mod tests { let ec_op_builtin: BuiltinRunner = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); assert_eq!(ec_op_builtin.get_memory_segment_addresses(), (0, None),); - let hash_builtin: BuiltinRunner = HashBuiltinRunner::new(8, true).into(); + let hash_builtin: BuiltinRunner = HashBuiltinRunner::new(Some(8), true).into(); assert_eq!(hash_builtin.get_memory_segment_addresses(), (0, None),); let output_builtin: BuiltinRunner = OutputBuiltinRunner::new(true).into(); assert_eq!(output_builtin.get_memory_segment_addresses(), (0, None),); @@ -1061,7 +1061,7 @@ mod tests { #[test] fn run_security_checks_hash_missing_memory_cells_with_offsets() { - let builtin: BuiltinRunner = HashBuiltinRunner::new(8, true).into(); + let builtin: BuiltinRunner = HashBuiltinRunner::new(Some(8), true).into(); let mut vm = vm!(); vm.segments.memory = memory![ @@ -1081,7 +1081,7 @@ mod tests { #[test] fn run_security_checks_hash_missing_memory_cells() { - let hash_builtin = HashBuiltinRunner::new(8, true); + let hash_builtin = HashBuiltinRunner::new(Some(8), true); let builtin: BuiltinRunner = hash_builtin.into(); @@ -1301,7 +1301,7 @@ mod tests { /// builtin is a HashBuiltinRunner. #[test] fn get_used_perm_range_check_units_hash() { - let builtin_runner: BuiltinRunner = HashBuiltinRunner::new(8, true).into(); + let builtin_runner: BuiltinRunner = HashBuiltinRunner::new(Some(8), true).into(); let mut vm = vm!(); vm.current_step = 8; @@ -1341,7 +1341,7 @@ mod tests { let ec_op_builtin: BuiltinRunner = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); assert_eq!(ec_op_builtin.ratio(), (Some(256)),); - let hash_builtin: BuiltinRunner = HashBuiltinRunner::new(8, true).into(); + let hash_builtin: BuiltinRunner = HashBuiltinRunner::new(Some(8), true).into(); assert_eq!(hash_builtin.ratio(), (Some(8)),); let output_builtin: BuiltinRunner = OutputBuiltinRunner::new(true).into(); assert_eq!(output_builtin.ratio(), None,); @@ -1378,7 +1378,7 @@ mod tests { let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); - let hash_builtin: BuiltinRunner = HashBuiltinRunner::new(8, true).into(); + let hash_builtin: BuiltinRunner = HashBuiltinRunner::new(Some(8), true).into(); assert_eq!(hash_builtin.get_used_instances(&vm.segments), Ok(2)); } @@ -1408,7 +1408,7 @@ mod tests { false, )), BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), false)), - BuiltinRunner::Hash(HashBuiltinRunner::new(1, false)), + BuiltinRunner::Hash(HashBuiltinRunner::new(Some(1), false)), BuiltinRunner::Output(OutputBuiltinRunner::new(false)), BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, false)), BuiltinRunner::Keccak(KeccakBuiltinRunner::new( @@ -1435,7 +1435,7 @@ mod tests { false, )), BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), false)), - BuiltinRunner::Hash(HashBuiltinRunner::new(1, false)), + BuiltinRunner::Hash(HashBuiltinRunner::new(Some(1), false)), BuiltinRunner::Output(OutputBuiltinRunner::new(false)), BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, false)), BuiltinRunner::Keccak(KeccakBuiltinRunner::new( diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index f49f28a23e..59373e274f 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -260,28 +260,28 @@ impl CairoRunner { match name { HASH_BUILTIN_NAME => vm .builtin_runners - .push((name, HashBuiltinRunner::new(32, true).into())), + .push((name, HashBuiltinRunner::new(Some(32), true).into())), RANGE_CHECK_BUILTIN_NAME => vm .builtin_runners - .push((name, RangeCheckBuiltinRunner::new(1, 8, true).into())), + .push((name, RangeCheckBuiltinRunner::new(Some(1), 8, true).into())), OUTPUT_BUILTIN_NAME => vm .builtin_runners .push((name, OutputBuiltinRunner::new(true).into())), SIGNATURE_BUILTIN_NAME => vm.builtin_runners.push(( name, - SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(1), true).into(), + SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(1)), true).into(), )), BITWISE_BUILTIN_NAME => vm.builtin_runners.push(( name, - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(1), true).into(), + BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(1)), true).into(), )), EC_OP_BUILTIN_NAME => vm.builtin_runners.push(( name, - EcOpBuiltinRunner::new(&EcOpInstanceDef::new(1), true).into(), + EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(1)), true).into(), )), KECCAK_BUILTIN_NAME => vm.builtin_runners.push(( name, - EcOpBuiltinRunner::new(&EcOpInstanceDef::new(1), true).into(), + EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(1)), true).into(), )), _ => {} } @@ -1080,7 +1080,7 @@ impl CairoRunner { .retain(|(name, _)| name != &"hash_builtin"); // Create, initialize and insert the new custom hash runner. - let mut builtin: BuiltinRunner = HashBuiltinRunner::new(32, true).into(); + let mut builtin: BuiltinRunner = HashBuiltinRunner::new(Some(32), true).into(); builtin.initialize_segments(&mut vm.segments); let segment_index = builtin.base() as isize; vm.builtin_runners.push(("hash_builtin", builtin)); @@ -3689,7 +3689,7 @@ mod tests { )))]]; vm.builtin_runners = vec![( RANGE_CHECK_BUILTIN_NAME, - RangeCheckBuiltinRunner::new(12, 5, true).into(), + RangeCheckBuiltinRunner::new(Some(12), 5, true).into(), )]; assert_matches!( @@ -4321,7 +4321,7 @@ mod tests { match value { BuiltinRunner::Hash(builtin) => { assert_eq!(builtin.base(), 0); - assert_eq!(builtin.ratio(), 32); + assert_eq!(builtin.ratio(), Some(32)); assert!(builtin.included); } _ => unreachable!(), @@ -4349,7 +4349,7 @@ mod tests { match value { BuiltinRunner::Hash(builtin) => { assert_eq!(builtin.base(), 1); - assert_eq!(builtin.ratio(), 32); + assert_eq!(builtin.ratio(), Some(32)); assert!(builtin.included); } _ => unreachable!(), diff --git a/src/vm/vm_core.rs b/src/vm/vm_core.rs index f580a70ae6..74c96c2441 100644 --- a/src/vm/vm_core.rs +++ b/src/vm/vm_core.rs @@ -4078,7 +4078,7 @@ mod tests { .current_step(12) .builtin_runners(vec![( "string", - BuiltinRunner::from(HashBuiltinRunner::new(12, true)), + BuiltinRunner::from(HashBuiltinRunner::new(Some(1)2, true)), )]) .run_context(RunContext { pc: Relocatable::from((0, 0)), From 0e123f0ea8fd9c0a615d0333de97a7835fb4b524 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 13:35:53 -0300 Subject: [PATCH 05/35] Misc fixes --- src/vm/runners/builtin_runner/bitwise.rs | 4 ++-- src/vm/runners/builtin_runner/ec_op.rs | 4 ++-- src/vm/runners/builtin_runner/hash.rs | 4 ++-- src/vm/runners/builtin_runner/keccak.rs | 4 ++-- src/vm/runners/builtin_runner/range_check.rs | 4 ++-- src/vm/runners/builtin_runner/signature.rs | 4 ++-- src/vm/vm_core.rs | 10 +++++----- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/vm/runners/builtin_runner/bitwise.rs b/src/vm/runners/builtin_runner/bitwise.rs index 1b1c43e5e6..ccdef1dd6b 100644 --- a/src/vm/runners/builtin_runner/bitwise.rs +++ b/src/vm/runners/builtin_runner/bitwise.rs @@ -116,8 +116,8 @@ impl BitwiseBuiltinRunner { // Dynamic layout has the exact number of instances it needs (up to a power of 2). let instances: usize = self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = ((instances / self.instances_per_component as usize) as usize) - .next_power_of_two(); + let components = + (instances / self.instances_per_component as usize).next_power_of_two(); Ok(self.cells_per_instance as usize * self.instances_per_component as usize * components) diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index f4ede9c8f7..32ac961991 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -218,8 +218,8 @@ impl EcOpBuiltinRunner { // Dynamic layout has the exact number of instances it needs (up to a power of 2). let instances: usize = self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = ((instances / self.instances_per_component as usize) as usize) - .next_power_of_two(); + let components = + (instances / self.instances_per_component as usize).next_power_of_two(); Ok(self.cells_per_instance as usize * self.instances_per_component as usize * components) diff --git a/src/vm/runners/builtin_runner/hash.rs b/src/vm/runners/builtin_runner/hash.rs index 7cb8c59ac2..967f14c884 100644 --- a/src/vm/runners/builtin_runner/hash.rs +++ b/src/vm/runners/builtin_runner/hash.rs @@ -120,8 +120,8 @@ impl HashBuiltinRunner { // Dynamic layout has the exact number of instances it needs (up to a power of 2). let instances: usize = self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = ((instances / self.instances_per_component as usize) as usize) - .next_power_of_two(); + let components = + (instances / self.instances_per_component as usize).next_power_of_two(); Ok(self.cells_per_instance as usize * self.instances_per_component as usize * components) diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index c495c510af..4bc63d9c0f 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -125,8 +125,8 @@ impl KeccakBuiltinRunner { // Dynamic layout has the exact number of instances it needs (up to a power of 2). let instances: usize = self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = ((instances / self.instances_per_component as usize) as usize) - .next_power_of_two(); + let components = + (instances / self.instances_per_component as usize).next_power_of_two(); Ok(self.cells_per_instance as usize * self.instances_per_component as usize * components) diff --git a/src/vm/runners/builtin_runner/range_check.rs b/src/vm/runners/builtin_runner/range_check.rs index edb88feebe..5654e83bcf 100644 --- a/src/vm/runners/builtin_runner/range_check.rs +++ b/src/vm/runners/builtin_runner/range_check.rs @@ -118,8 +118,8 @@ impl RangeCheckBuiltinRunner { // Dynamic layout has the exact number of instances it needs (up to a power of 2). let instances: usize = self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = ((instances / self.instances_per_component as usize) as usize) - .next_power_of_two(); + let components = + (instances / self.instances_per_component as usize).next_power_of_two(); Ok(self.cells_per_instance as usize * self.instances_per_component as usize * components) diff --git a/src/vm/runners/builtin_runner/signature.rs b/src/vm/runners/builtin_runner/signature.rs index 2789f53bb9..7364a78d21 100644 --- a/src/vm/runners/builtin_runner/signature.rs +++ b/src/vm/runners/builtin_runner/signature.rs @@ -164,8 +164,8 @@ impl SignatureBuiltinRunner { // Dynamic layout has the exact number of instances it needs (up to a power of 2). let instances: usize = self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = ((instances / self.instances_per_component as usize) as usize) - .next_power_of_two(); + let components = + (instances / self.instances_per_component as usize).next_power_of_two(); Ok(self.cells_per_instance as usize * self.instances_per_component as usize * components) diff --git a/src/vm/vm_core.rs b/src/vm/vm_core.rs index 74c96c2441..8924335586 100644 --- a/src/vm/vm_core.rs +++ b/src/vm/vm_core.rs @@ -3109,7 +3109,7 @@ mod tests { #[test] fn deduce_memory_cell_pedersen_builtin_valid() { let mut vm = vm!(); - let builtin = HashBuiltinRunner::new(8, true); + let builtin = HashBuiltinRunner::new(Some(8), true); vm.builtin_runners.push((HASH_BUILTIN_NAME, builtin.into())); vm.segments = segments![((0, 3), 32), ((0, 4), 72), ((0, 5), 0)]; assert_matches!( @@ -3159,7 +3159,7 @@ mod tests { fp_update: FpUpdate::Regular, opcode: Opcode::AssertEq, }; - let mut builtin = HashBuiltinRunner::new(8, true); + let mut builtin = HashBuiltinRunner::new(Some(8), true); builtin.base = 3; let mut vm = vm!(); vm.builtin_runners.push((HASH_BUILTIN_NAME, builtin.into())); @@ -3544,7 +3544,7 @@ mod tests { end */ fn verify_auto_deductions_pedersen() { - let mut builtin = HashBuiltinRunner::new(8, true); + let mut builtin = HashBuiltinRunner::new(Some(8), true); builtin.base = 3; let mut vm = vm!(); vm.builtin_runners.push((HASH_BUILTIN_NAME, builtin.into())); @@ -3675,7 +3675,7 @@ mod tests { #[test] fn test_get_builtin_runners() { let mut vm = vm!(); - let hash_builtin = HashBuiltinRunner::new(8, true); + let hash_builtin = HashBuiltinRunner::new(Some(8), true); let bitwise_builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); vm.builtin_runners .push((HASH_BUILTIN_NAME, hash_builtin.into())); @@ -4078,7 +4078,7 @@ mod tests { .current_step(12) .builtin_runners(vec![( "string", - BuiltinRunner::from(HashBuiltinRunner::new(Some(1)2, true)), + BuiltinRunner::from(HashBuiltinRunner::new(Some(1), true)), )]) .run_context(RunContext { pc: Relocatable::from((0, 0)), From 0057d69da2cd59120c21b8837aa2258306c8f033 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 13:38:56 -0300 Subject: [PATCH 06/35] Fix wrong impl --- src/vm/runners/builtin_runner/ec_op.rs | 15 ++++++++++++--- src/vm/runners/builtin_runner/hash.rs | 5 +++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index 32ac961991..1985ff8960 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -5,7 +5,7 @@ use crate::types::instance_definitions::ec_op_instance_def::{ EcOpInstanceDef, CELLS_PER_EC_OP, INPUT_CELLS_PER_EC_OP, }; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; -use crate::vm::errors::memory_errors::MemoryError; +use crate::vm::errors::memory_errors::{InsufficientAllocatedCellsError, MemoryError}; use crate::vm::errors::runner_errors::RunnerError; use crate::vm::vm_core::VirtualMachine; use crate::vm::vm_memory::memory::Memory; @@ -246,8 +246,17 @@ impl EcOpBuiltinRunner { &self, vm: &VirtualMachine, ) -> Result<(usize, usize), MemoryError> { - let size = self.get_used_cells(&vm.segments)?; - Ok((size, size)) + let used = self.get_used_cells(&vm.segments)?; + let size = self.get_allocated_memory_units(vm)?; + if used > size { + return Err(InsufficientAllocatedCellsError::BuiltinCells( + EC_OP_BUILTIN_NAME, + used, + size, + ) + .into()); + } + Ok((used, size)) } pub fn get_used_instances( diff --git a/src/vm/runners/builtin_runner/hash.rs b/src/vm/runners/builtin_runner/hash.rs index 967f14c884..915e57fa6d 100644 --- a/src/vm/runners/builtin_runner/hash.rs +++ b/src/vm/runners/builtin_runner/hash.rs @@ -14,7 +14,7 @@ use felt::Felt; use num_integer::{div_ceil, Integer}; use starknet_crypto::{pedersen_hash, FieldElement}; -use super::EC_OP_BUILTIN_NAME; +use super::{EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME}; #[derive(Debug, Clone)] pub struct HashBuiltinRunner { @@ -152,7 +152,7 @@ impl HashBuiltinRunner { let size = self.get_allocated_memory_units(vm)?; if used > size { return Err(InsufficientAllocatedCellsError::BuiltinCells( - EC_OP_BUILTIN_NAME, + HASH_BUILTIN_NAME, used, size, ) @@ -160,6 +160,7 @@ impl HashBuiltinRunner { } Ok((used, size)) } + pub fn get_used_instances( &self, segments: &MemorySegmentManager, From 0ac1ca81476be926dbe5564eb4f373c1d9a2e259 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 13:52:45 -0300 Subject: [PATCH 07/35] Fix tests --- src/vm/runners/cairo_runner.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index 59373e274f..e6190aeb47 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -3753,6 +3753,7 @@ mod tests { ap: (0, 0).into(), fp: (0, 0).into(), }]); + vm.compute_effective_sizes(); assert_matches!( cairo_runner.check_range_check_usage(&vm), @@ -3819,7 +3820,7 @@ mod tests { ap: (0, 0).into(), fp: (0, 0).into(), }]); - + vm.compute_effective_sizes(); assert_matches!( cairo_runner.check_used_cells(&vm), Err(VirtualMachineError::Memory( From f8894df2a71d6336c0d01e7ae346e44f4dc7e1d1 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 13:53:26 -0300 Subject: [PATCH 08/35] Fix tests --- src/vm/runners/cairo_runner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index e6190aeb47..ea067413d0 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -3366,7 +3366,7 @@ mod tests { .expect("Call to `Program::from_file()` failed."); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "all", true); + let mut cairo_runner = cairo_runner!(program, "plain", true); let mut vm = vm!(true); let end = cairo_runner.initialize(&mut vm).unwrap(); From 73d1e693dbb14808d83709982dcf037b148c0685 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 14:01:15 -0300 Subject: [PATCH 09/35] Add dynamic layout --- src/main.rs | 4 +++- .../instance_definitions/builtins_instance_def.rs | 12 ++++++++++++ src/types/layout.rs | 14 ++++++++++++++ src/vm/runners/cairo_runner.rs | 1 + 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 363dc933aa..4a6b919be7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,9 @@ struct Args { fn validate_layout(value: &str) -> Result<(), String> { match value { - "plain" | "small" | "dex" | "bitwise" | "perpetual_with_bitwise" | "all" => Ok(()), + "plain" | "small" | "dex" | "bitwise" | "perpetual_with_bitwise" | "all" | "dynamic" => { + Ok(()) + } _ => Err(format!("{value} is not a valid layout")), } } diff --git a/src/types/instance_definitions/builtins_instance_def.rs b/src/types/instance_definitions/builtins_instance_def.rs index 7b3c926fe7..a76b72cf8a 100644 --- a/src/types/instance_definitions/builtins_instance_def.rs +++ b/src/types/instance_definitions/builtins_instance_def.rs @@ -99,6 +99,18 @@ impl BuiltinsInstanceDef { keccak: None, } } + + pub(crate) fn dynamic() -> BuiltinsInstanceDef { + BuiltinsInstanceDef { + output: true, + pedersen: Some(PedersenInstanceDef::new(None, 4)), + range_check: Some(RangeCheckInstanceDef::new(None, 8)), + ecdsa: Some(EcdsaInstanceDef::new(None)), + bitwise: Some(BitwiseInstanceDef::new(None)), + ec_op: Some(EcOpInstanceDef::new(None)), + keccak: None, + } + } } #[cfg(test)] diff --git a/src/types/layout.rs b/src/types/layout.rs index 1e37c654a5..1efc882efc 100644 --- a/src/types/layout.rs +++ b/src/types/layout.rs @@ -114,6 +114,20 @@ impl CairoLayout { _cpu_instance_def: CpuInstanceDef::default(), } } + + pub(crate) fn dynamic_instance() -> CairoLayout { + CairoLayout { + _name: String::from("dynamic"), + _cpu_component_step: 1, + rc_units: 16, + builtins: BuiltinsInstanceDef::dynamic(), + _public_memory_fraction: 8, + _memory_units_per_step: 8, + diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), + _n_trace_colums: 73, + _cpu_instance_def: CpuInstanceDef::default(), + } + } } #[cfg(test)] diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index ea067413d0..c1906370c8 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -102,6 +102,7 @@ impl CairoRunner { "perpetual_with_bitwise" => CairoLayout::perpetual_with_bitwise_instance(), "bitwise" => CairoLayout::bitwise_instance(), "all" => CairoLayout::all_instance(), + "dynamic" => CairoLayout::dynamic_instance(), name => return Err(RunnerError::InvalidLayoutName(name.to_string())), }; Ok(CairoRunner { From 9b4af24ad2bc2c7977a97360a8ceb13d196165f5 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 16:07:16 -0300 Subject: [PATCH 10/35] Fix wrong impl --- src/vm/errors/memory_errors.rs | 2 ++ src/vm/runners/builtin_runner/bitwise.rs | 8 ++++++++ src/vm/runners/builtin_runner/ec_op.rs | 8 ++++++++ src/vm/runners/builtin_runner/hash.rs | 8 ++++++++ src/vm/runners/builtin_runner/keccak.rs | 8 ++++++++ src/vm/runners/builtin_runner/signature.rs | 8 ++++++++ src/vm/runners/cairo_runner.rs | 2 +- 7 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/vm/errors/memory_errors.rs b/src/vm/errors/memory_errors.rs index 67aefbc073..404fd41ede 100644 --- a/src/vm/errors/memory_errors.rs +++ b/src/vm/errors/memory_errors.rs @@ -89,6 +89,8 @@ pub enum MemoryError { #[derive(Debug, PartialEq, Eq, Error)] pub enum InsufficientAllocatedCellsError { + #[error("Number of steps must be at least {0} for the {1} builtin.")] + MinStepNotReached(usize, &'static str), #[error("The {0} builtin used {1} cells but the capacity is {2}.")] BuiltinCells(&'static str, usize, usize), #[error("There are only {0} cells to fill the range checks holes, but potentially {1} are required.")] diff --git a/src/vm/runners/builtin_runner/bitwise.rs b/src/vm/runners/builtin_runner/bitwise.rs index ccdef1dd6b..314bb726aa 100644 --- a/src/vm/runners/builtin_runner/bitwise.rs +++ b/src/vm/runners/builtin_runner/bitwise.rs @@ -123,6 +123,14 @@ impl BitwiseBuiltinRunner { * components) } Some(ratio) => { + let min_step = (ratio * self.instances_per_component) as usize; + if vm.current_step < min_step { + return Err(InsufficientAllocatedCellsError::MinStepNotReached( + min_step, + BITWISE_BUILTIN_NAME, + ) + .into()); + }; let value = safe_div_usize(vm.current_step, ratio as usize) .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; Ok(self.cells_per_instance as usize * value) diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index 1985ff8960..6144f9bd4f 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -225,6 +225,14 @@ impl EcOpBuiltinRunner { * components) } Some(ratio) => { + let min_step = (ratio * self.instances_per_component) as usize; + if vm.current_step < min_step { + return Err(InsufficientAllocatedCellsError::MinStepNotReached( + min_step, + EC_OP_BUILTIN_NAME, + ) + .into()); + }; let value = safe_div_usize(vm.current_step, ratio as usize) .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; Ok(self.cells_per_instance as usize * value) diff --git a/src/vm/runners/builtin_runner/hash.rs b/src/vm/runners/builtin_runner/hash.rs index 915e57fa6d..d8479900bd 100644 --- a/src/vm/runners/builtin_runner/hash.rs +++ b/src/vm/runners/builtin_runner/hash.rs @@ -127,6 +127,14 @@ impl HashBuiltinRunner { * components) } Some(ratio) => { + let min_step = (ratio * self.instances_per_component) as usize; + if vm.current_step < min_step { + return Err(InsufficientAllocatedCellsError::MinStepNotReached( + min_step, + HASH_BUILTIN_NAME, + ) + .into()); + }; let value = safe_div_usize(vm.current_step, ratio as usize) .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; Ok(self.cells_per_instance as usize * value) diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index 4bc63d9c0f..592f516274 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -132,6 +132,14 @@ impl KeccakBuiltinRunner { * components) } Some(ratio) => { + let min_step = (ratio * self.instances_per_component) as usize; + if vm.current_step < min_step { + return Err(InsufficientAllocatedCellsError::MinStepNotReached( + min_step, + KECCAK_BUILTIN_NAME, + ) + .into()); + }; let value = safe_div_usize(vm.current_step, ratio as usize) .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; Ok(self.cells_per_instance as usize * value) diff --git a/src/vm/runners/builtin_runner/signature.rs b/src/vm/runners/builtin_runner/signature.rs index 7364a78d21..3081d108bc 100644 --- a/src/vm/runners/builtin_runner/signature.rs +++ b/src/vm/runners/builtin_runner/signature.rs @@ -171,6 +171,14 @@ impl SignatureBuiltinRunner { * components) } Some(ratio) => { + let min_step = (ratio * self.instances_per_component) as usize; + if vm.current_step < min_step { + return Err(InsufficientAllocatedCellsError::MinStepNotReached( + min_step, + SIGNATURE_BUILTIN_NAME, + ) + .into()); + }; let value = safe_div_usize(vm.current_step, ratio as usize) .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; Ok(self.cells_per_instance as usize * value) diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index c1906370c8..cfab4df150 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -3367,7 +3367,7 @@ mod tests { .expect("Call to `Program::from_file()` failed."); let mut hint_processor = BuiltinHintProcessor::new_empty(); - let mut cairo_runner = cairo_runner!(program, "plain", true); + let mut cairo_runner = cairo_runner!(program, "all", true); let mut vm = vm!(true); let end = cairo_runner.initialize(&mut vm).unwrap(); From e1e014248938e37e8c246d343af1ef62fdda039d Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 7 Mar 2023 16:35:00 -0300 Subject: [PATCH 11/35] Fix tests --- src/vm/runners/builtin_runner/keccak.rs | 4 +-- src/vm/runners/builtin_runner/mod.rs | 40 +++++++++++++++++----- src/vm/runners/builtin_runner/signature.rs | 6 ++-- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index 592f516274..0087b7b67c 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -409,9 +409,9 @@ mod tests { KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); let mut vm = vm!(); - vm.current_step = 10; + vm.current_step = 160; - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(16)); + assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(256)); } #[test] diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index dbe379a09b..73ee6bf0cb 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -441,6 +441,7 @@ mod tests { use crate::types::instance_definitions::ecdsa_instance_def::EcdsaInstanceDef; use crate::types::instance_definitions::keccak_instance_def::KeccakInstanceDef; use crate::types::program::Program; + use crate::vm::errors::memory_errors::InsufficientAllocatedCellsError; use crate::vm::runners::cairo_runner::CairoRunner; use crate::{ types::instance_definitions::{ @@ -809,9 +810,26 @@ mod tests { true, )); + let mut vm = vm!(); + vm.current_step = 160; + assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(256)); + } + + #[test] + fn get_allocated_memory_units_keccak_min_steps_not_reached() { + let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new( + &KeccakInstanceDef::new(Some(10), vec![200; 8]), + true, + )); + let mut vm = vm!(); vm.current_step = 10; - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(16)); + assert_eq!( + builtin.get_allocated_memory_units(&vm), + Err(MemoryError::InsufficientAllocatedCells( + InsufficientAllocatedCellsError::MinStepNotReached(160, KECCAK_BUILTIN_NAME) + )) + ); } #[test] @@ -833,8 +851,9 @@ mod tests { #[test] fn get_allocated_memory_units_hash() { let builtin = BuiltinRunner::Hash(HashBuiltinRunner::new(Some(1), true)); - let vm = vm!(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); + let mut vm = vm!(); + vm.current_step = 1; + assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(3)); } #[test] @@ -843,16 +862,18 @@ mod tests { &BitwiseInstanceDef::default(), true, )); - let vm = vm!(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); + let mut vm = vm!(); + vm.current_step = 256; + assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(5)); } #[test] fn get_allocated_memory_units_ec_op() { let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true)); - let vm = vm!(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); + let mut vm = vm!(); + vm.current_step = 256; + assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(7)); } #[test] @@ -861,8 +882,9 @@ mod tests { &KeccakInstanceDef::default(), true, )); - let vm = vm!(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); + let mut vm = vm!(); + vm.current_step = 32768; + assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(256)); } #[test] diff --git a/src/vm/runners/builtin_runner/signature.rs b/src/vm/runners/builtin_runner/signature.rs index 3081d108bc..a31803c13b 100644 --- a/src/vm/runners/builtin_runner/signature.rs +++ b/src/vm/runners/builtin_runner/signature.rs @@ -521,13 +521,15 @@ mod tests { } #[test] - fn get_allocated_memory_units_safe_div_fail() { + fn get_allocated_memory_min_step_not_reached() { let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); let mut vm = vm!(); vm.current_step = 500; assert_eq!( builtin.get_allocated_memory_units(&vm), - Err(MemoryError::ErrorCalculatingMemoryUnits) + Err(MemoryError::InsufficientAllocatedCells( + InsufficientAllocatedCellsError::MinStepNotReached(512, SIGNATURE_BUILTIN_NAME) + )) ) } From f7e7ceb8cf18a5f52000640685af69384d19e29f Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 17 Mar 2023 17:40:32 -0300 Subject: [PATCH 12/35] Expose instances_per_component --- src/vm/runners/builtin_runner/bitwise.rs | 2 +- src/vm/runners/builtin_runner/ec_op.rs | 2 +- src/vm/runners/builtin_runner/hash.rs | 2 +- src/vm/runners/builtin_runner/keccak.rs | 2 +- src/vm/runners/builtin_runner/mod.rs | 13 +++++++++++++ src/vm/runners/builtin_runner/poseidon.rs | 2 +- src/vm/runners/builtin_runner/range_check.rs | 2 +- src/vm/runners/builtin_runner/signature.rs | 2 +- 8 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/vm/runners/builtin_runner/bitwise.rs b/src/vm/runners/builtin_runner/bitwise.rs index be91618448..177bd9e1f7 100644 --- a/src/vm/runners/builtin_runner/bitwise.rs +++ b/src/vm/runners/builtin_runner/bitwise.rs @@ -29,7 +29,7 @@ pub struct BitwiseBuiltinRunner { bitwise_builtin: BitwiseInstanceDef, pub(crate) stop_ptr: Option, pub(crate) included: bool, - instances_per_component: u32, + pub(crate) instances_per_component: u32, } impl BitwiseBuiltinRunner { diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index a8d7b34876..507571e12c 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -27,7 +27,7 @@ pub struct EcOpBuiltinRunner { ec_op_builtin: EcOpInstanceDef, pub(crate) stop_ptr: Option, pub(crate) included: bool, - instances_per_component: u32, + pub(crate) instances_per_component: u32, cache: RefCell>, } diff --git a/src/vm/runners/builtin_runner/hash.rs b/src/vm/runners/builtin_runner/hash.rs index de4c627f6c..5a9be881ef 100644 --- a/src/vm/runners/builtin_runner/hash.rs +++ b/src/vm/runners/builtin_runner/hash.rs @@ -24,7 +24,7 @@ pub struct HashBuiltinRunner { pub(crate) n_input_cells: u32, pub(crate) stop_ptr: Option, pub(crate) included: bool, - instances_per_component: u32, + pub(crate) instances_per_component: u32, // This act as a cache to optimize calls to deduce_memory_cell // Therefore need interior mutability pub(self) verified_addresses: RefCell>, diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index cca5cda82e..85205d5de0 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -26,7 +26,7 @@ pub struct KeccakBuiltinRunner { pub(crate) stop_ptr: Option, pub(crate) included: bool, state_rep: Vec, - instances_per_component: u32, + pub(crate) instances_per_component: u32, cache: RefCell>, } diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 961d7a3feb..00eb253bd0 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -306,6 +306,19 @@ impl BuiltinRunner { } } + fn instances_per_component(&self) -> u32 { + match self { + BuiltinRunner::Bitwise(builtin) => builtin.instances_per_component, + BuiltinRunner::EcOp(builtin) => builtin.instances_per_component, + BuiltinRunner::Hash(builtin) => builtin.instances_per_component, + BuiltinRunner::RangeCheck(builtin) => builtin.instances_per_component, + BuiltinRunner::Output(_) => 0, + BuiltinRunner::Keccak(builtin) => builtin.instances_per_component, + BuiltinRunner::Signature(builtin) => builtin.instances_per_component, + BuiltinRunner::Poseidon(builtin) => builtin.instances_per_component, + } + } + pub fn name(&self) -> &'static str { match self { BuiltinRunner::Bitwise(_) => BITWISE_BUILTIN_NAME, diff --git a/src/vm/runners/builtin_runner/poseidon.rs b/src/vm/runners/builtin_runner/poseidon.rs index 6888ac1c41..6aa6383caf 100644 --- a/src/vm/runners/builtin_runner/poseidon.rs +++ b/src/vm/runners/builtin_runner/poseidon.rs @@ -26,7 +26,7 @@ pub struct PoseidonBuiltinRunner { pub(crate) stop_ptr: Option, pub(crate) included: bool, cache: RefCell>, - instances_per_component: u32, + pub(crate) instances_per_component: u32, } impl PoseidonBuiltinRunner { diff --git a/src/vm/runners/builtin_runner/range_check.rs b/src/vm/runners/builtin_runner/range_check.rs index a45183698a..7d0b9fbe18 100644 --- a/src/vm/runners/builtin_runner/range_check.rs +++ b/src/vm/runners/builtin_runner/range_check.rs @@ -39,7 +39,7 @@ pub struct RangeCheckBuiltinRunner { pub _bound: Option, pub(crate) included: bool, n_parts: u32, - instances_per_component: u32, + pub(crate) instances_per_component: u32, } impl RangeCheckBuiltinRunner { diff --git a/src/vm/runners/builtin_runner/signature.rs b/src/vm/runners/builtin_runner/signature.rs index 9d6357e555..64e61e7bed 100644 --- a/src/vm/runners/builtin_runner/signature.rs +++ b/src/vm/runners/builtin_runner/signature.rs @@ -33,7 +33,7 @@ pub struct SignatureBuiltinRunner { pub(crate) n_input_cells: u32, _total_n_bits: u32, pub(crate) stop_ptr: Option, - instances_per_component: u32, + pub(crate) instances_per_component: u32, signatures: Rc>>, } From f7d3720dbff8fe2443650aa45abc857bd262788f Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 17 Mar 2023 17:45:55 -0300 Subject: [PATCH 13/35] Move get_used_cells_and_allocated_sizes to mod --- src/vm/runners/builtin_runner/bitwise.rs | 17 -------------- src/vm/runners/builtin_runner/ec_op.rs | 17 -------------- src/vm/runners/builtin_runner/hash.rs | 17 -------------- src/vm/runners/builtin_runner/keccak.rs | 17 -------------- src/vm/runners/builtin_runner/mod.rs | 27 ++++++++++++++--------- src/vm/runners/builtin_runner/output.rs | 8 ------- src/vm/runners/builtin_runner/poseidon.rs | 17 -------------- 7 files changed, 16 insertions(+), 104 deletions(-) diff --git a/src/vm/runners/builtin_runner/bitwise.rs b/src/vm/runners/builtin_runner/bitwise.rs index 177bd9e1f7..ad13fb7d89 100644 --- a/src/vm/runners/builtin_runner/bitwise.rs +++ b/src/vm/runners/builtin_runner/bitwise.rs @@ -149,23 +149,6 @@ impl BitwiseBuiltinRunner { .ok_or(MemoryError::MissingSegmentUsedSizes) } - pub fn get_used_cells_and_allocated_size( - &self, - vm: &VirtualMachine, - ) -> Result<(usize, usize), MemoryError> { - let used = self.get_used_cells(&vm.segments)?; - let size = self.get_allocated_memory_units(vm)?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - BITWISE_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) - } - pub fn get_used_diluted_check_units(&self, diluted_spacing: u32, diluted_n_bits: u32) -> usize { let total_n_bits = self.bitwise_builtin.total_n_bits; let mut partition = Vec::with_capacity(total_n_bits as usize); diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index 507571e12c..a4daea84f5 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -268,23 +268,6 @@ impl EcOpBuiltinRunner { .ok_or(MemoryError::MissingSegmentUsedSizes) } - pub fn get_used_cells_and_allocated_size( - &self, - vm: &VirtualMachine, - ) -> Result<(usize, usize), MemoryError> { - let used = self.get_used_cells(&vm.segments)?; - let size = self.get_allocated_memory_units(vm)?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - EC_OP_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) - } - pub fn get_used_instances( &self, segments: &MemorySegmentManager, diff --git a/src/vm/runners/builtin_runner/hash.rs b/src/vm/runners/builtin_runner/hash.rs index 5a9be881ef..316eaa3b9a 100644 --- a/src/vm/runners/builtin_runner/hash.rs +++ b/src/vm/runners/builtin_runner/hash.rs @@ -152,23 +152,6 @@ impl HashBuiltinRunner { .ok_or(MemoryError::MissingSegmentUsedSizes) } - pub fn get_used_cells_and_allocated_size( - &self, - vm: &VirtualMachine, - ) -> Result<(usize, usize), MemoryError> { - let used = self.get_used_cells(&vm.segments)?; - let size = self.get_allocated_memory_units(vm)?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - HASH_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) - } - pub fn get_used_instances( &self, segments: &MemorySegmentManager, diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index 85205d5de0..6002bd70f9 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -166,23 +166,6 @@ impl KeccakBuiltinRunner { .ok_or(MemoryError::MissingSegmentUsedSizes) } - pub fn get_used_cells_and_allocated_size( - &self, - vm: &VirtualMachine, - ) -> Result<(usize, usize), MemoryError> { - let used = self.get_used_cells(&vm.segments)?; - let size = self.get_allocated_memory_units(vm)?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - KECCAK_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) - } - pub fn get_used_instances( &self, segments: &MemorySegmentManager, diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 00eb253bd0..0dd796be75 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -1,6 +1,6 @@ use crate::stdlib::prelude::*; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; -use crate::vm::errors::memory_errors::{self, MemoryError}; +use crate::vm::errors::memory_errors::{self, InsufficientAllocatedCellsError, MemoryError}; use crate::vm::errors::runner_errors::RunnerError; use crate::vm::errors::vm_errors::VirtualMachineError; use crate::vm::vm_core::VirtualMachine; @@ -397,18 +397,23 @@ impl BuiltinRunner { vm: &VirtualMachine, ) -> Result<(usize, usize), MemoryError> { match self { - BuiltinRunner::Bitwise(ref bitwise) => bitwise.get_used_cells_and_allocated_size(vm), - BuiltinRunner::EcOp(ref ec) => ec.get_used_cells_and_allocated_size(vm), - BuiltinRunner::Hash(ref hash) => hash.get_used_cells_and_allocated_size(vm), - BuiltinRunner::Output(ref output) => output.get_used_cells_and_allocated_size(vm), - BuiltinRunner::RangeCheck(ref range_check) => { - range_check.get_used_cells_and_allocated_size(vm) + BuiltinRunner::Output(_) => { + let used = self.get_used_cells(&vm.segments)?; + Ok((used, used)) } - BuiltinRunner::Keccak(ref keccak) => keccak.get_used_cells_and_allocated_size(vm), - BuiltinRunner::Signature(ref signature) => { - signature.get_used_cells_and_allocated_size(vm) + _ => { + let used = self.get_used_cells(&vm.segments)?; + let size = self.get_allocated_memory_units(vm)?; + if used > size { + return Err(InsufficientAllocatedCellsError::BuiltinCells( + self.name(), + used, + size, + ) + .into()); + } + Ok((used, size)) } - BuiltinRunner::Poseidon(ref poseidon) => poseidon.get_used_cells_and_allocated_size(vm), } } diff --git a/src/vm/runners/builtin_runner/output.rs b/src/vm/runners/builtin_runner/output.rs index 131d23c4ee..5778ab02d7 100644 --- a/src/vm/runners/builtin_runner/output.rs +++ b/src/vm/runners/builtin_runner/output.rs @@ -64,14 +64,6 @@ impl OutputBuiltinRunner { .ok_or(MemoryError::MissingSegmentUsedSizes) } - pub fn get_used_cells_and_allocated_size( - &self, - vm: &VirtualMachine, - ) -> Result<(usize, usize), MemoryError> { - let used = self.get_used_cells(&vm.segments)?; - Ok((used, used)) - } - pub fn get_used_instances( &self, segments: &MemorySegmentManager, diff --git a/src/vm/runners/builtin_runner/poseidon.rs b/src/vm/runners/builtin_runner/poseidon.rs index 6aa6383caf..e719d21e5d 100644 --- a/src/vm/runners/builtin_runner/poseidon.rs +++ b/src/vm/runners/builtin_runner/poseidon.rs @@ -149,23 +149,6 @@ impl PoseidonBuiltinRunner { .ok_or(MemoryError::MissingSegmentUsedSizes) } - pub fn get_used_cells_and_allocated_size( - &self, - vm: &VirtualMachine, - ) -> Result<(usize, usize), MemoryError> { - let used = self.get_used_cells(&vm.segments)?; - let size = self.get_allocated_memory_units(vm)?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - POSEIDON_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) - } - pub fn get_used_instances( &self, segments: &MemorySegmentManager, From 5c1fa2f45e48b459a0582d14b6f3752f11aa45bb Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 17 Mar 2023 17:53:25 -0300 Subject: [PATCH 14/35] Move get_allocated_memory_units to mod --- src/vm/runners/builtin_runner/hash.rs | 49 +++++------------------ src/vm/runners/builtin_runner/keccak.rs | 30 +------------- src/vm/runners/builtin_runner/mod.rs | 37 ++++++++++++----- src/vm/runners/builtin_runner/poseidon.rs | 34 +--------------- 4 files changed, 40 insertions(+), 110 deletions(-) diff --git a/src/vm/runners/builtin_runner/hash.rs b/src/vm/runners/builtin_runner/hash.rs index 316eaa3b9a..74ec01c15f 100644 --- a/src/vm/runners/builtin_runner/hash.rs +++ b/src/vm/runners/builtin_runner/hash.rs @@ -1,20 +1,17 @@ use crate::stdlib::{cell::RefCell, prelude::*}; - -use crate::math_utils::safe_div_usize; use crate::types::instance_definitions::pedersen_instance_def::{ CELLS_PER_HASH, INPUT_CELLS_PER_HASH, }; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; -use crate::vm::errors::memory_errors::{InsufficientAllocatedCellsError, MemoryError}; +use crate::vm::errors::memory_errors::MemoryError; use crate::vm::errors::runner_errors::RunnerError; -use crate::vm::vm_core::VirtualMachine; use crate::vm::vm_memory::memory::Memory; use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use felt::Felt; use num_integer::{div_ceil, Integer}; use starknet_crypto::{pedersen_hash, FieldElement}; -use super::{EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME}; +use super::HASH_BUILTIN_NAME; #[derive(Debug, Clone)] pub struct HashBuiltinRunner { @@ -114,34 +111,6 @@ impl HashBuiltinRunner { Ok(None) } - pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - match self.ratio { - None => { - // Dynamic layout has the exact number of instances it needs (up to a power of 2). - let instances: usize = - self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = - (instances / self.instances_per_component as usize).next_power_of_two(); - Ok(self.cells_per_instance as usize - * self.instances_per_component as usize - * components) - } - Some(ratio) => { - let min_step = (ratio * self.instances_per_component) as usize; - if vm.current_step < min_step { - return Err(InsufficientAllocatedCellsError::MinStepNotReached( - min_step, - HASH_BUILTIN_NAME, - ) - .into()); - }; - let value = safe_div_usize(vm.current_step, ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) - } - } - } - pub fn get_memory_segment_addresses(&self) -> (usize, Option) { (self.base, self.stop_ptr) } @@ -167,14 +136,14 @@ impl HashBuiltinRunner { ) -> Result { if self.included { let stop_pointer_addr = - (pointer - 1).map_err(|_| RunnerError::NoStopPointer(EC_OP_BUILTIN_NAME))?; + (pointer - 1).map_err(|_| RunnerError::NoStopPointer(HASH_BUILTIN_NAME))?; let stop_pointer = segments .memory .get_relocatable(stop_pointer_addr) - .map_err(|_| RunnerError::NoStopPointer(EC_OP_BUILTIN_NAME))?; + .map_err(|_| RunnerError::NoStopPointer(HASH_BUILTIN_NAME))?; if self.base as isize != stop_pointer.segment_index { return Err(RunnerError::InvalidStopPointerIndex( - EC_OP_BUILTIN_NAME, + HASH_BUILTIN_NAME, stop_pointer, self.base, )); @@ -184,7 +153,7 @@ impl HashBuiltinRunner { let used = num_instances * self.cells_per_instance as usize; if stop_ptr != used { return Err(RunnerError::InvalidStopPointer( - EC_OP_BUILTIN_NAME, + HASH_BUILTIN_NAME, Relocatable::from((self.base as isize, used)), Relocatable::from((self.base as isize, stop_ptr)), )); @@ -275,7 +244,7 @@ mod tests { assert_eq!( builtin.final_stack(&vm.segments, pointer), Err(RunnerError::InvalidStopPointer( - EC_OP_BUILTIN_NAME, + HASH_BUILTIN_NAME, relocatable!(0, 999), relocatable!(0, 0) )) @@ -326,7 +295,7 @@ mod tests { assert_eq!( builtin.final_stack(&vm.segments, pointer), - Err(RunnerError::NoStopPointer(EC_OP_BUILTIN_NAME)) + Err(RunnerError::NoStopPointer(HASH_BUILTIN_NAME)) ); } @@ -379,7 +348,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units() { - let builtin = HashBuiltinRunner::new(Some(10), true); + let builtin: BuiltinRunner = HashBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index 6002bd70f9..fc8330ab9e 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -2,7 +2,7 @@ use crate::math_utils::safe_div_usize; use crate::stdlib::{collections::HashMap, prelude::*}; use crate::types::instance_definitions::keccak_instance_def::KeccakInstanceDef; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; -use crate::vm::errors::memory_errors::{InsufficientAllocatedCellsError, MemoryError}; +use crate::vm::errors::memory_errors::MemoryError; use crate::vm::errors::runner_errors::RunnerError; use crate::vm::vm_core::VirtualMachine; use crate::vm::vm_memory::memory::Memory; @@ -128,34 +128,6 @@ impl KeccakBuiltinRunner { Ok(self.cache.borrow().get(&address).map(|x| x.into())) } - pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - match self.ratio { - None => { - // Dynamic layout has the exact number of instances it needs (up to a power of 2). - let instances: usize = - self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = - (instances / self.instances_per_component as usize).next_power_of_two(); - Ok(self.cells_per_instance as usize - * self.instances_per_component as usize - * components) - } - Some(ratio) => { - let min_step = (ratio * self.instances_per_component) as usize; - if vm.current_step < min_step { - return Err(InsufficientAllocatedCellsError::MinStepNotReached( - min_step, - KECCAK_BUILTIN_NAME, - ) - .into()); - }; - let value = safe_div_usize(vm.current_step, ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) - } - } - } - pub fn get_memory_segment_addresses(&self) -> (usize, Option) { (self.base, self.stop_ptr) } diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 0dd796be75..40d9c4e0c2 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -1,3 +1,4 @@ +use crate::math_utils::safe_div_usize; use crate::stdlib::prelude::*; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use crate::vm::errors::memory_errors::{self, InsufficientAllocatedCellsError, MemoryError}; @@ -115,16 +116,34 @@ impl BuiltinRunner { vm: &VirtualMachine, ) -> Result { match *self { - BuiltinRunner::Bitwise(ref bitwise) => bitwise.get_allocated_memory_units(vm), - BuiltinRunner::EcOp(ref ec) => ec.get_allocated_memory_units(vm), - BuiltinRunner::Hash(ref hash) => hash.get_allocated_memory_units(vm), - BuiltinRunner::Output(ref output) => output.get_allocated_memory_units(vm), - BuiltinRunner::RangeCheck(ref range_check) => { - range_check.get_allocated_memory_units(vm) + BuiltinRunner::Output(_) => Ok(0), + _ => { + match self.ratio() { + None => { + // Dynamic layout has the exact number of instances it needs (up to a power of 2). + let instances: usize = + self.get_used_cells(&vm.segments)? / self.cells_per_instance() as usize; + let components = (instances / self.instances_per_component() as usize) + .next_power_of_two(); + Ok(self.cells_per_instance() as usize + * self.instances_per_component() as usize + * components) + } + Some(ratio) => { + let min_step = (ratio * self.instances_per_component()) as usize; + if vm.current_step < min_step { + return Err(InsufficientAllocatedCellsError::MinStepNotReached( + min_step, + self.name(), + ) + .into()); + }; + let value = safe_div_usize(vm.current_step, ratio as usize) + .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; + Ok(self.cells_per_instance() as usize * value) + } + } } - BuiltinRunner::Keccak(ref keccak) => keccak.get_allocated_memory_units(vm), - BuiltinRunner::Signature(ref signature) => signature.get_allocated_memory_units(vm), - BuiltinRunner::Poseidon(ref poseidon) => poseidon.get_allocated_memory_units(vm), } } diff --git a/src/vm/runners/builtin_runner/poseidon.rs b/src/vm/runners/builtin_runner/poseidon.rs index e719d21e5d..e67ee6262b 100644 --- a/src/vm/runners/builtin_runner/poseidon.rs +++ b/src/vm/runners/builtin_runner/poseidon.rs @@ -1,14 +1,12 @@ use std::cell::RefCell; use std::collections::HashMap; -use crate::math_utils::safe_div_usize; use crate::types::instance_definitions::poseidon_instance_def::{ CELLS_PER_POSEIDON, INPUT_CELLS_PER_POSEIDON, }; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; -use crate::vm::errors::memory_errors::{InsufficientAllocatedCellsError, MemoryError}; +use crate::vm::errors::memory_errors::MemoryError; use crate::vm::errors::runner_errors::RunnerError; -use crate::vm::vm_core::VirtualMachine; use crate::vm::vm_memory::memory::Memory; use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use felt::Felt; @@ -111,34 +109,6 @@ impl PoseidonBuiltinRunner { Ok(self.cache.borrow().get(&address).map(|x| x.into())) } - pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - match self.ratio { - None => { - // Dynamic layout has the exact number of instances it needs (up to a power of 2). - let instances: usize = - self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = - (instances / self.instances_per_component as usize).next_power_of_two(); - Ok(self.cells_per_instance as usize - * self.instances_per_component as usize - * components) - } - Some(ratio) => { - let min_step = (ratio * self.instances_per_component) as usize; - if vm.current_step < min_step { - return Err(InsufficientAllocatedCellsError::MinStepNotReached( - min_step, - POSEIDON_BUILTIN_NAME, - ) - .into()); - }; - let value = safe_div_usize(vm.current_step, ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) - } - } - } - pub fn get_memory_segment_addresses(&self) -> (usize, Option) { (self.base, self.stop_ptr) } @@ -365,7 +335,7 @@ mod tests { #[test] fn get_allocated_memory_units() { - let builtin = PoseidonBuiltinRunner::new(Some(10), true); + let builtin: BuiltinRunner = PoseidonBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); From f3d4c3458a9be3c85c9069c5486bfba904236f1e Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 17 Mar 2023 18:03:09 -0300 Subject: [PATCH 15/35] Finnish prev tasks --- src/vm/runners/builtin_runner/bitwise.rs | 38 +----------- src/vm/runners/builtin_runner/ec_op.rs | 36 ++--------- src/vm/runners/builtin_runner/mod.rs | 3 +- src/vm/runners/builtin_runner/range_check.rs | 59 ++---------------- src/vm/runners/builtin_runner/signature.rs | 63 +++----------------- 5 files changed, 21 insertions(+), 178 deletions(-) diff --git a/src/vm/runners/builtin_runner/bitwise.rs b/src/vm/runners/builtin_runner/bitwise.rs index ad13fb7d89..d622d5b867 100644 --- a/src/vm/runners/builtin_runner/bitwise.rs +++ b/src/vm/runners/builtin_runner/bitwise.rs @@ -1,6 +1,5 @@ use crate::stdlib::vec::Vec; use crate::{ - math_utils::safe_div_usize, types::{ instance_definitions::bitwise_instance_def::{ BitwiseInstanceDef, CELLS_PER_BITWISE, INPUT_CELLS_PER_BITWISE, @@ -8,11 +7,7 @@ use crate::{ relocatable::{MaybeRelocatable, Relocatable}, }, vm::{ - errors::{ - memory_errors::{InsufficientAllocatedCellsError, MemoryError}, - runner_errors::RunnerError, - }, - vm_core::VirtualMachine, + errors::{memory_errors::MemoryError, runner_errors::RunnerError}, vm_memory::{memory::Memory, memory_segments::MemorySegmentManager}, }, }; @@ -111,34 +106,6 @@ impl BitwiseBuiltinRunner { Ok(None) } - pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - match self.ratio { - None => { - // Dynamic layout has the exact number of instances it needs (up to a power of 2). - let instances: usize = - self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = - (instances / self.instances_per_component as usize).next_power_of_two(); - Ok(self.cells_per_instance as usize - * self.instances_per_component as usize - * components) - } - Some(ratio) => { - let min_step = (ratio * self.instances_per_component) as usize; - if vm.current_step < min_step { - return Err(InsufficientAllocatedCellsError::MinStepNotReached( - min_step, - BITWISE_BUILTIN_NAME, - ) - .into()); - }; - let value = safe_div_usize(vm.current_step, ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) - } - } - } - pub fn get_memory_segment_addresses(&self) -> (usize, Option) { (self.base, self.stop_ptr) } @@ -402,7 +369,8 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units() { - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); + let builtin: BuiltinRunner = + BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true).into(); let mut vm = vm!(); diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index a4daea84f5..0975ca1071 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -1,12 +1,11 @@ -use crate::math_utils::{ec_add, ec_double, safe_div_usize}; +use crate::math_utils::{ec_add, ec_double}; use crate::stdlib::{borrow::Cow, prelude::*}; use crate::types::instance_definitions::ec_op_instance_def::{ EcOpInstanceDef, CELLS_PER_EC_OP, INPUT_CELLS_PER_EC_OP, }; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; -use crate::vm::errors::memory_errors::{InsufficientAllocatedCellsError, MemoryError}; +use crate::vm::errors::memory_errors::MemoryError; use crate::vm::errors::runner_errors::RunnerError; -use crate::vm::vm_core::VirtualMachine; use crate::vm::vm_memory::memory::Memory; use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use felt::Felt; @@ -230,34 +229,6 @@ impl EcOpBuiltinRunner { } } - pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - match self.ratio { - None => { - // Dynamic layout has the exact number of instances it needs (up to a power of 2). - let instances: usize = - self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = - (instances / self.instances_per_component as usize).next_power_of_two(); - Ok(self.cells_per_instance as usize - * self.instances_per_component as usize - * components) - } - Some(ratio) => { - let min_step = (ratio * self.instances_per_component) as usize; - if vm.current_step < min_step { - return Err(InsufficientAllocatedCellsError::MinStepNotReached( - min_step, - EC_OP_BUILTIN_NAME, - ) - .into()); - }; - let value = safe_div_usize(vm.current_step, ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) - } - } - } - pub fn get_memory_segment_addresses(&self) -> (usize, Option) { (self.base, self.stop_ptr) } @@ -511,7 +482,8 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units() { - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true); + let builtin: BuiltinRunner = + EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true).into(); let mut vm = vm!(); diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 40d9c4e0c2..cec7500a96 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -281,7 +281,8 @@ impl BuiltinRunner { ) -> Result { match self { BuiltinRunner::RangeCheck(range_check) => { - range_check.get_used_perm_range_check_units(vm) + let (used_cells, _) = self.get_used_cells_and_allocated_size(vm)?; + Ok(used_cells * range_check.n_parts as usize) } _ => Ok(0), } diff --git a/src/vm/runners/builtin_runner/range_check.rs b/src/vm/runners/builtin_runner/range_check.rs index 7d0b9fbe18..5072753306 100644 --- a/src/vm/runners/builtin_runner/range_check.rs +++ b/src/vm/runners/builtin_runner/range_check.rs @@ -5,17 +5,12 @@ use crate::stdlib::{ }; use crate::{ - math_utils::safe_div_usize, types::{ instance_definitions::range_check_instance_def::CELLS_PER_RANGE_CHECK, relocatable::{MaybeRelocatable, Relocatable}, }, vm::{ - errors::{ - memory_errors::{InsufficientAllocatedCellsError, MemoryError}, - runner_errors::RunnerError, - }, - vm_core::VirtualMachine, + errors::{memory_errors::MemoryError, runner_errors::RunnerError}, vm_memory::{ memory::{Memory, ValidationRule}, memory_segments::MemorySegmentManager, @@ -38,7 +33,7 @@ pub struct RangeCheckBuiltinRunner { inner_rc_bound: usize, pub _bound: Option, pub(crate) included: bool, - n_parts: u32, + pub(crate) n_parts: u32, pub(crate) instances_per_component: u32, } @@ -114,26 +109,6 @@ impl RangeCheckBuiltinRunner { Ok(None) } - pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - match self.ratio { - None => { - // Dynamic layout has the exact number of instances it needs (up to a power of 2). - let instances: usize = - self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = - (instances / self.instances_per_component as usize).next_power_of_two(); - Ok(self.cells_per_instance as usize - * self.instances_per_component as usize - * components) - } - Some(ratio) => { - let value = safe_div_usize(vm.current_step, ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) - } - } - } - pub fn get_memory_segment_addresses(&self) -> (usize, Option) { (self.base, self.stop_ptr) } @@ -144,23 +119,6 @@ impl RangeCheckBuiltinRunner { .ok_or(MemoryError::MissingSegmentUsedSizes) } - pub fn get_used_cells_and_allocated_size( - &self, - vm: &VirtualMachine, - ) -> Result<(usize, usize), MemoryError> { - let used = self.get_used_cells(&vm.segments)?; - let size = self.get_allocated_memory_units(vm)?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - RANGE_CHECK_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) - } - pub fn get_range_check_usage(&self, memory: &Memory) -> Option<(usize, usize)> { let mut rc_bounds: Option<(usize, usize)> = None; let range_check_segment = memory.data.get(self.base)?; @@ -232,15 +190,6 @@ impl RangeCheckBuiltinRunner { Ok(pointer) } } - - /// Returns the number of range check units used by the builtin. - pub fn get_used_perm_range_check_units( - &self, - vm: &VirtualMachine, - ) -> Result { - let (used_cells, _) = self.get_used_cells_and_allocated_size(vm)?; - Ok(used_cells * self.n_parts as usize) - } } #[cfg(test)] @@ -423,7 +372,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units() { - let builtin = RangeCheckBuiltinRunner::new(Some(10), 12, true); + let builtin: BuiltinRunner = RangeCheckBuiltinRunner::new(Some(10), 12, true).into(); let mut vm = vm!(); @@ -628,7 +577,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_perm_range_check_units() { - let builtin_runner = RangeCheckBuiltinRunner::new(Some(8), 8, true); + let builtin_runner: BuiltinRunner = RangeCheckBuiltinRunner::new(Some(8), 8, true).into(); let mut vm = vm!(); vm.current_step = 8; diff --git a/src/vm/runners/builtin_runner/signature.rs b/src/vm/runners/builtin_runner/signature.rs index 64e61e7bed..38af15d13d 100644 --- a/src/vm/runners/builtin_runner/signature.rs +++ b/src/vm/runners/builtin_runner/signature.rs @@ -1,17 +1,12 @@ use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*, rc::Rc}; use crate::{ - math_utils::safe_div_usize, types::{ instance_definitions::ecdsa_instance_def::EcdsaInstanceDef, relocatable::{MaybeRelocatable, Relocatable}, }, vm::{ - errors::{ - memory_errors::{InsufficientAllocatedCellsError, MemoryError}, - runner_errors::RunnerError, - }, - vm_core::VirtualMachine, + errors::{memory_errors::MemoryError, runner_errors::RunnerError}, vm_memory::{ memory::{Memory, ValidationRule}, memory_segments::MemorySegmentManager, @@ -159,34 +154,6 @@ impl SignatureBuiltinRunner { self.ratio } - pub fn get_allocated_memory_units(&self, vm: &VirtualMachine) -> Result { - match self.ratio { - None => { - // Dynamic layout has the exact number of instances it needs (up to a power of 2). - let instances: usize = - self.get_used_cells(&vm.segments)? / self.cells_per_instance as usize; - let components = - (instances / self.instances_per_component as usize).next_power_of_two(); - Ok(self.cells_per_instance as usize - * self.instances_per_component as usize - * components) - } - Some(ratio) => { - let min_step = (ratio * self.instances_per_component) as usize; - if vm.current_step < min_step { - return Err(InsufficientAllocatedCellsError::MinStepNotReached( - min_step, - SIGNATURE_BUILTIN_NAME, - ) - .into()); - }; - let value = safe_div_usize(vm.current_step, ratio as usize) - .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance as usize * value) - } - } - } - pub fn get_memory_segment_addresses(&self) -> (usize, Option) { (self.base, self.stop_ptr) } @@ -197,23 +164,6 @@ impl SignatureBuiltinRunner { .ok_or(MemoryError::MissingSegmentUsedSizes) } - pub fn get_used_cells_and_allocated_size( - &self, - vm: &VirtualMachine, - ) -> Result<(usize, usize), MemoryError> { - let used = self.get_used_cells(&vm.segments)?; - let size = self.get_allocated_memory_units(vm)?; - if used > size { - return Err(InsufficientAllocatedCellsError::BuiltinCells( - SIGNATURE_BUILTIN_NAME, - used, - size, - ) - .into()); - } - Ok((used, size)) - } - pub fn get_used_instances( &self, segments: &MemorySegmentManager, @@ -269,7 +219,7 @@ mod tests { types::instance_definitions::ecdsa_instance_def::EcdsaInstanceDef, utils::test_utils::*, vm::{ - errors::memory_errors::MemoryError, + errors::memory_errors::{InsufficientAllocatedCellsError, MemoryError}, runners::builtin_runner::BuiltinRunner, vm_core::VirtualMachine, vm_memory::{memory::Memory, memory_segments::MemorySegmentManager}, @@ -281,7 +231,8 @@ mod tests { #[test] fn get_used_cells_and_allocated_size_valid() { - let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true); + let builtin: BuiltinRunner = + SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true).into(); let mut vm = vm!(); vm.current_step = 110; vm.segments.segment_used_sizes = Some(vec![1]); @@ -546,7 +497,8 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_min_step_not_reached() { - let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); + let builtin: BuiltinRunner = + SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); let mut vm = vm!(); vm.current_step = 500; assert_eq!( @@ -560,7 +512,8 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_and_allocated_size_insufficient_allocated() { - let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); + let builtin: BuiltinRunner = + SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![50]); vm.current_step = 512; From f79bbd0351c3ca5b859a4cbb912b5416153d2b39 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 17 Mar 2023 18:05:32 -0300 Subject: [PATCH 16/35] Fix test --- src/vm/runners/builtin_runner/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index cec7500a96..ddc46164eb 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -944,8 +944,9 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units_range_check() { let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, true)); - let vm = vm!(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); + let mut vm = vm!(); + vm.current_step = 8; + assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(1)); } #[test] From 5b76b60e9881d086cd02c9da5b32a284a1099147 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 12:27:58 -0300 Subject: [PATCH 17/35] Initial progress --- src/vm/runners/builtin_runner/mod.rs | 1 + .../runners/builtin_runner/segment_arena.rs | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/vm/runners/builtin_runner/segment_arena.rs diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index ddc46164eb..0457faf9a9 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -15,6 +15,7 @@ mod keccak; mod output; mod poseidon; mod range_check; +mod segment_arena; mod signature; pub use self::keccak::KeccakBuiltinRunner; diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs new file mode 100644 index 0000000000..a00875ca64 --- /dev/null +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -0,0 +1,47 @@ +use crate::vm::errors::memory_errors::MemoryError; +use crate::with_std::any::Any; +use crate::{ + types::relocatable::{MaybeRelocatable, Relocatable}, + vm::vm_memory::memory_segments::MemorySegmentManager, +}; + +const ARENA_BUILTIN_SIZE: u32 = 3; +// The size of the builtin segment at the time of its creation. +const INITIAL_SEGMENT_SIZE: u32 = ARENA_BUILTIN_SIZE; + +#[derive(Debug)] +pub struct SegmentArenaBuiltinRunner { + base: Relocatable, + included: bool, + ratio: Option, + cells_per_instance: u32, + n_input_cells_per_instance: u32, +} + +impl SegmentArenaBuiltinRunner { + pub(crate) fn new(included: bool) -> Self { + SegmentArenaBuiltinRunner { + base: Relocatable::from((0, 0)), + included, + ratio: None, + cells_per_instance: ARENA_BUILTIN_SIZE, + n_input_cells_per_instance: ARENA_BUILTIN_SIZE, + } + } + + pub fn initialize_segments( + &mut self, + segments: &mut MemorySegmentManager, + ) -> Result<(), MemoryError> { + let segment_start = segments.gen_arg(&[ + MaybeRelocatable::from(segments.add()), + MaybeRelocatable::from(0), + MaybeRelocatable::from(0), + ] as &dyn Any)?; + self.base = (segment_start + .get_relocatable() + .ok_or(MemoryError::AddressNotRelocatable)? + + INITIAL_SEGMENT_SIZE as usize)?; + Ok(()) + } +} From 993d06084e303ceaf61e03e9423f952ba88674d2 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 12:39:15 -0300 Subject: [PATCH 18/35] Add builtin specific methods --- src/vm/errors/memory_errors.rs | 3 +++ src/vm/runners/builtin_runner/segment_arena.rs | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/vm/errors/memory_errors.rs b/src/vm/errors/memory_errors.rs index caba4746bf..f188eb993e 100644 --- a/src/vm/errors/memory_errors.rs +++ b/src/vm/errors/memory_errors.rs @@ -91,6 +91,9 @@ pub enum MemoryError { ExpectedRelocatable(Relocatable), #[error("Unknown memory cell at address {0}")] UnknownMemoryCell(Relocatable), + // SegmentArenaBuiltin + #[error("segment_arena_builtin: assert used >= INITIAL_SEGMENT_SIZE")] + InvalidUsedSizeSegmentArena, } #[derive(Debug, PartialEq, Eq, Error)] diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index a00875ca64..6c703c3781 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -7,7 +7,7 @@ use crate::{ const ARENA_BUILTIN_SIZE: u32 = 3; // The size of the builtin segment at the time of its creation. -const INITIAL_SEGMENT_SIZE: u32 = ARENA_BUILTIN_SIZE; +const INITIAL_SEGMENT_SIZE: usize = ARENA_BUILTIN_SIZE as usize; #[derive(Debug)] pub struct SegmentArenaBuiltinRunner { @@ -44,4 +44,14 @@ impl SegmentArenaBuiltinRunner { + INITIAL_SEGMENT_SIZE as usize)?; Ok(()) } + + pub fn get_used_cells(&self, segments: &MemorySegmentManager) -> Result { + let used = segments + .get_segment_used_size(self.base.segment_index as usize) + .ok_or(MemoryError::MissingSegmentUsedSizes)?; + if used < INITIAL_SEGMENT_SIZE { + return Err(MemoryError::InvalidUsedSizeSegmentArena); + } + Ok(used - INITIAL_SEGMENT_SIZE) + } } From 3c8ef1d32a4cf6ce8793ecebffd0326d8644213e Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 15:04:32 -0300 Subject: [PATCH 19/35] Add SegmentArenaBuiltinRunner to BuiltinRunner enum --- src/vm/runners/builtin_runner/mod.rs | 57 +++++++++++++++++-- .../runners/builtin_runner/segment_arena.rs | 18 ++++-- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 0457faf9a9..7ee812e1d2 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -20,6 +20,7 @@ mod signature; pub use self::keccak::KeccakBuiltinRunner; pub use self::poseidon::PoseidonBuiltinRunner; +use self::segment_arena::SegmentArenaBuiltinRunner; pub use bitwise::BitwiseBuiltinRunner; pub use ec_op::EcOpBuiltinRunner; pub use hash::HashBuiltinRunner; @@ -36,6 +37,7 @@ pub const BITWISE_BUILTIN_NAME: &str = "bitwise"; pub const EC_OP_BUILTIN_NAME: &str = "ec_op"; pub const KECCAK_BUILTIN_NAME: &str = "keccak"; pub const POSEIDON_BUILTIN_NAME: &str = "poseidon"; +pub const SEGMENT_ARENA_BUilTIN_NAME: &str = "segment_area"; /* NB: this enum is no accident: we may need (and cairo-rs-py *does* need) * structs containing this to be `Send`. The only two ways to achieve that @@ -55,11 +57,15 @@ pub enum BuiltinRunner { Keccak(KeccakBuiltinRunner), Signature(SignatureBuiltinRunner), Poseidon(PoseidonBuiltinRunner), + SegmentArena(SegmentArenaBuiltinRunner), } impl BuiltinRunner { ///Creates the necessary segments for the builtin in the MemorySegmentManager and stores the first address on the builtin's base - pub fn initialize_segments(&mut self, segments: &mut MemorySegmentManager) { + pub fn initialize_segments( + &mut self, + segments: &mut MemorySegmentManager, + ) -> Result<(), MemoryError> { match *self { BuiltinRunner::Bitwise(ref mut bitwise) => bitwise.initialize_segments(segments), BuiltinRunner::EcOp(ref mut ec) => ec.initialize_segments(segments), @@ -71,7 +77,11 @@ impl BuiltinRunner { BuiltinRunner::Keccak(ref mut keccak) => keccak.initialize_segments(segments), BuiltinRunner::Signature(ref mut signature) => signature.initialize_segments(segments), BuiltinRunner::Poseidon(ref mut poseidon) => poseidon.initialize_segments(segments), + BuiltinRunner::SegmentArena(ref mut segment_arena) => { + segment_arena.initialize_segments(segments)? + } } + Ok(()) } pub fn initial_stack(&self) -> Vec { @@ -84,6 +94,7 @@ impl BuiltinRunner { BuiltinRunner::Keccak(ref keccak) => keccak.initial_stack(), BuiltinRunner::Signature(ref signature) => signature.initial_stack(), BuiltinRunner::Poseidon(ref poseidon) => poseidon.initial_stack(), + BuiltinRunner::SegmentArena(ref segment_arena) => segment_arena.initial_stack(), } } @@ -108,6 +119,9 @@ impl BuiltinRunner { BuiltinRunner::Poseidon(ref mut poseidon) => { poseidon.final_stack(segments, stack_pointer) } + BuiltinRunner::SegmentArena(ref mut segment_arena) => { + segment_arena.final_stack(segments, stack_pointer) + } } } @@ -117,7 +131,7 @@ impl BuiltinRunner { vm: &VirtualMachine, ) -> Result { match *self { - BuiltinRunner::Output(_) => Ok(0), + BuiltinRunner::Output(_) | BuiltinRunner::SegmentArena(_) => Ok(0), _ => { match self.ratio() { None => { @@ -159,6 +173,7 @@ impl BuiltinRunner { BuiltinRunner::Keccak(ref keccak) => keccak.base(), BuiltinRunner::Signature(ref signature) => signature.base(), BuiltinRunner::Poseidon(ref poseidon) => poseidon.base(), + BuiltinRunner::SegmentArena(ref segment_arena) => segment_arena.base(), } } @@ -167,7 +182,7 @@ impl BuiltinRunner { BuiltinRunner::Bitwise(bitwise) => bitwise.ratio(), BuiltinRunner::EcOp(ec) => ec.ratio(), BuiltinRunner::Hash(hash) => hash.ratio(), - BuiltinRunner::Output(_) => None, + BuiltinRunner::Output(_) | BuiltinRunner::SegmentArena(_) => None, BuiltinRunner::RangeCheck(range_check) => range_check.ratio(), BuiltinRunner::Keccak(keccak) => keccak.ratio(), BuiltinRunner::Signature(ref signature) => signature.ratio(), @@ -185,6 +200,9 @@ impl BuiltinRunner { BuiltinRunner::Keccak(ref keccak) => keccak.add_validation_rule(memory), BuiltinRunner::Signature(ref signature) => signature.add_validation_rule(memory), BuiltinRunner::Poseidon(ref poseidon) => poseidon.add_validation_rule(memory), + BuiltinRunner::SegmentArena(ref segment_arena) => { + segment_arena.add_validation_rule(memory) + } } } @@ -206,6 +224,9 @@ impl BuiltinRunner { signature.deduce_memory_cell(address, memory) } BuiltinRunner::Poseidon(ref poseidon) => poseidon.deduce_memory_cell(address, memory), + BuiltinRunner::SegmentArena(ref segment_arena) => { + segment_arena.deduce_memory_cell(address, memory) + } } } @@ -213,6 +234,9 @@ impl BuiltinRunner { &self, vm: &VirtualMachine, ) -> Result, MemoryError> { + if let BuiltinRunner::SegmentArena(_) = self { + return Ok(vec![]); + } let base = self.base(); let segment_size = vm .segments @@ -236,6 +260,9 @@ impl BuiltinRunner { BuiltinRunner::Keccak(ref keccak) => keccak.get_memory_segment_addresses(), BuiltinRunner::Signature(ref signature) => signature.get_memory_segment_addresses(), BuiltinRunner::Poseidon(ref poseidon) => poseidon.get_memory_segment_addresses(), + BuiltinRunner::SegmentArena(ref segment_arena) => { + segment_arena.get_memory_segment_addresses() + } } } @@ -249,6 +276,9 @@ impl BuiltinRunner { BuiltinRunner::Keccak(ref keccak) => keccak.get_used_cells(segments), BuiltinRunner::Signature(ref signature) => signature.get_used_cells(segments), BuiltinRunner::Poseidon(ref poseidon) => poseidon.get_used_cells(segments), + BuiltinRunner::SegmentArena(ref segment_arena) => { + segment_arena.get_used_cells(segments) + } } } @@ -265,6 +295,9 @@ impl BuiltinRunner { BuiltinRunner::Keccak(ref keccak) => keccak.get_used_instances(segments), BuiltinRunner::Signature(ref signature) => signature.get_used_instances(segments), BuiltinRunner::Poseidon(ref poseidon) => poseidon.get_used_instances(segments), + BuiltinRunner::SegmentArena(ref segment_arena) => { + segment_arena.get_used_instances(segments) + } } } @@ -311,6 +344,7 @@ impl BuiltinRunner { BuiltinRunner::Keccak(builtin) => builtin.cells_per_instance, BuiltinRunner::Signature(builtin) => builtin.cells_per_instance, BuiltinRunner::Poseidon(builtin) => builtin.cells_per_instance, + BuiltinRunner::SegmentArena(builtin) => builtin.cells_per_instance, } } @@ -324,6 +358,7 @@ impl BuiltinRunner { BuiltinRunner::Keccak(builtin) => builtin.n_input_cells, BuiltinRunner::Signature(builtin) => builtin.n_input_cells, BuiltinRunner::Poseidon(builtin) => builtin.n_input_cells, + BuiltinRunner::SegmentArena(builtin) => builtin.n_input_cells_per_instance, } } @@ -333,7 +368,7 @@ impl BuiltinRunner { BuiltinRunner::EcOp(builtin) => builtin.instances_per_component, BuiltinRunner::Hash(builtin) => builtin.instances_per_component, BuiltinRunner::RangeCheck(builtin) => builtin.instances_per_component, - BuiltinRunner::Output(_) => 0, + BuiltinRunner::Output(_) | BuiltinRunner::SegmentArena(_) => 1, BuiltinRunner::Keccak(builtin) => builtin.instances_per_component, BuiltinRunner::Signature(builtin) => builtin.instances_per_component, BuiltinRunner::Poseidon(builtin) => builtin.instances_per_component, @@ -350,11 +385,12 @@ impl BuiltinRunner { BuiltinRunner::Keccak(_) => KECCAK_BUILTIN_NAME, BuiltinRunner::Signature(_) => SIGNATURE_BUILTIN_NAME, BuiltinRunner::Poseidon(_) => POSEIDON_BUILTIN_NAME, + BuiltinRunner::SegmentArena(_) => SEGMENT_ARENA_BUilTIN_NAME, } } pub fn run_security_checks(&self, vm: &VirtualMachine) -> Result<(), VirtualMachineError> { - if let BuiltinRunner::Output(_) = self { + if let BuiltinRunner::Output(_) | BuiltinRunner::SegmentArena(_) = self { return Ok(()); } let cells_per_instance = self.cells_per_instance() as usize; @@ -418,7 +454,7 @@ impl BuiltinRunner { vm: &VirtualMachine, ) -> Result<(usize, usize), MemoryError> { match self { - BuiltinRunner::Output(_) => { + BuiltinRunner::Output(_) | BuiltinRunner::SegmentArena(_) => { let used = self.get_used_cells(&vm.segments)?; Ok((used, used)) } @@ -449,6 +485,9 @@ impl BuiltinRunner { BuiltinRunner::Keccak(ref mut keccak) => keccak.stop_ptr = Some(stop_ptr), BuiltinRunner::Signature(ref mut signature) => signature.stop_ptr = Some(stop_ptr), BuiltinRunner::Poseidon(ref mut poseidon) => poseidon.stop_ptr = Some(stop_ptr), + BuiltinRunner::SegmentArena(ref mut segment_arena) => { + segment_arena.stop_ptr = Some(stop_ptr) + } } } } @@ -501,6 +540,12 @@ impl From for BuiltinRunner { } } +impl From for BuiltinRunner { + fn from(runner: SegmentArenaBuiltinRunner) -> Self { + BuiltinRunner::SegmentArena(runner) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index 6c703c3781..f8e55bf628 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -9,13 +9,13 @@ const ARENA_BUILTIN_SIZE: u32 = 3; // The size of the builtin segment at the time of its creation. const INITIAL_SEGMENT_SIZE: usize = ARENA_BUILTIN_SIZE as usize; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct SegmentArenaBuiltinRunner { base: Relocatable, included: bool, - ratio: Option, - cells_per_instance: u32, - n_input_cells_per_instance: u32, + pub(crate) cells_per_instance: u32, + pub(crate) n_input_cells_per_instance: u32, + pub(crate) stop_ptr: Option, } impl SegmentArenaBuiltinRunner { @@ -23,9 +23,9 @@ impl SegmentArenaBuiltinRunner { SegmentArenaBuiltinRunner { base: Relocatable::from((0, 0)), included, - ratio: None, cells_per_instance: ARENA_BUILTIN_SIZE, n_input_cells_per_instance: ARENA_BUILTIN_SIZE, + stop_ptr: None, } } @@ -54,4 +54,12 @@ impl SegmentArenaBuiltinRunner { } Ok(used - INITIAL_SEGMENT_SIZE) } + + pub fn initial_stack(&self) -> Vec { + if self.included { + vec![MaybeRelocatable::from(self.base)] + } else { + vec![] + } + } } From 7b960af2726beb78b6ea1f79bba3f1b2828964a8 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 15:13:10 -0300 Subject: [PATCH 20/35] implement final_stack for segmen arena nuiltin runner --- src/vm/runners/builtin_runner/mod.rs | 4 +- .../runners/builtin_runner/segment_arena.rs | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 7ee812e1d2..532fe971f0 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -37,7 +37,7 @@ pub const BITWISE_BUILTIN_NAME: &str = "bitwise"; pub const EC_OP_BUILTIN_NAME: &str = "ec_op"; pub const KECCAK_BUILTIN_NAME: &str = "keccak"; pub const POSEIDON_BUILTIN_NAME: &str = "poseidon"; -pub const SEGMENT_ARENA_BUilTIN_NAME: &str = "segment_area"; +pub const SEGMENT_ARENA_BUILTIN_NAME: &str = "segment_area"; /* NB: this enum is no accident: we may need (and cairo-rs-py *does* need) * structs containing this to be `Send`. The only two ways to achieve that @@ -385,7 +385,7 @@ impl BuiltinRunner { BuiltinRunner::Keccak(_) => KECCAK_BUILTIN_NAME, BuiltinRunner::Signature(_) => SIGNATURE_BUILTIN_NAME, BuiltinRunner::Poseidon(_) => POSEIDON_BUILTIN_NAME, - BuiltinRunner::SegmentArena(_) => SEGMENT_ARENA_BUilTIN_NAME, + BuiltinRunner::SegmentArena(_) => SEGMENT_ARENA_BUILTIN_NAME, } } diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index f8e55bf628..8bac662aca 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -1,10 +1,13 @@ use crate::vm::errors::memory_errors::MemoryError; +use crate::vm::errors::runner_errors::RunnerError; use crate::with_std::any::Any; use crate::{ types::relocatable::{MaybeRelocatable, Relocatable}, vm::vm_memory::memory_segments::MemorySegmentManager, }; +use super::SEGMENT_ARENA_BUILTIN_NAME; + const ARENA_BUILTIN_SIZE: u32 = 3; // The size of the builtin segment at the time of its creation. const INITIAL_SEGMENT_SIZE: usize = ARENA_BUILTIN_SIZE as usize; @@ -62,4 +65,39 @@ impl SegmentArenaBuiltinRunner { vec![] } } + + pub fn final_stack( + &mut self, + segments: &MemorySegmentManager, + pointer: Relocatable, + ) -> Result { + if self.included { + let stop_pointer_addr = (pointer - 1) + .map_err(|_| RunnerError::NoStopPointer(SEGMENT_ARENA_BUILTIN_NAME))?; + let stop_pointer = segments + .memory + .get_relocatable(stop_pointer_addr) + .map_err(|_| RunnerError::NoStopPointer(SEGMENT_ARENA_BUILTIN_NAME))?; + if self.base.segment_index != stop_pointer.segment_index { + return Err(RunnerError::InvalidStopPointerIndex( + SEGMENT_ARENA_BUILTIN_NAME, + stop_pointer, + self.base.segment_index as usize, + )); + } + let used = self.get_used_cells(segments).map_err(RunnerError::Memory)?; + if stop_pointer != (self.base + used)? { + return Err(RunnerError::InvalidStopPointer( + SEGMENT_ARENA_BUILTIN_NAME, + Relocatable::from((self.base + used)?), + Relocatable::from(stop_pointer), + )); + } + self.stop_ptr = Some(stop_pointer); + Ok(stop_pointer_addr) + } else { + self.stop_ptr = Some(self.base); + Ok(pointer) + } + } } From 141507edf279660773a10c22b46ea88de481ae97 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 15:21:23 -0300 Subject: [PATCH 21/35] Add other methods --- src/vm/runners/builtin_runner/mod.rs | 1 + .../runners/builtin_runner/segment_arena.rs | 32 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 532fe971f0..2dd34075c8 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -173,6 +173,7 @@ impl BuiltinRunner { BuiltinRunner::Keccak(ref keccak) => keccak.base(), BuiltinRunner::Signature(ref signature) => signature.base(), BuiltinRunner::Poseidon(ref poseidon) => poseidon.base(), + //Warning, returns only the segment index, base offset will be 3 BuiltinRunner::SegmentArena(ref segment_arena) => segment_arena.base(), } } diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index 8bac662aca..b29e52ca04 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -1,5 +1,6 @@ use crate::vm::errors::memory_errors::MemoryError; use crate::vm::errors::runner_errors::RunnerError; +use crate::vm::vm_memory::memory::Memory; use crate::with_std::any::Any; use crate::{ types::relocatable::{MaybeRelocatable, Relocatable}, @@ -18,7 +19,7 @@ pub struct SegmentArenaBuiltinRunner { included: bool, pub(crate) cells_per_instance: u32, pub(crate) n_input_cells_per_instance: u32, - pub(crate) stop_ptr: Option, + pub(crate) stop_ptr: Option, } impl SegmentArenaBuiltinRunner { @@ -93,11 +94,36 @@ impl SegmentArenaBuiltinRunner { Relocatable::from(stop_pointer), )); } - self.stop_ptr = Some(stop_pointer); + self.stop_ptr = Some(stop_pointer.offset); Ok(stop_pointer_addr) } else { - self.stop_ptr = Some(self.base); + self.stop_ptr = Some(self.base.offset); Ok(pointer) } } + + pub fn get_used_instances( + &self, + segments: &MemorySegmentManager, + ) -> Result { + self.get_used_cells(segments) + } + + pub fn get_memory_segment_addresses(&self) -> (usize, Option) { + (self.base.segment_index as usize, self.stop_ptr) + } + + pub fn add_validation_rule(&self, _memory: &mut Memory) {} + + pub fn deduce_memory_cell( + &self, + _address: Relocatable, + _memory: &Memory, + ) -> Result, RunnerError> { + Ok(None) + } + + pub fn base(&self) -> usize { + self.base.segment_index as usize + } } From de988c51bd8ff826a791992110982ce7a61283aa Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 16:03:51 -0300 Subject: [PATCH 22/35] Integrate into runner --- src/vm/runners/builtin_runner/mod.rs | 2 +- .../runners/builtin_runner/segment_arena.rs | 11 +- src/vm/runners/cairo_runner.rs | 180 ++++++++++++++---- 3 files changed, 146 insertions(+), 47 deletions(-) diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 2dd34075c8..67fa5f7a80 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -20,7 +20,7 @@ mod signature; pub use self::keccak::KeccakBuiltinRunner; pub use self::poseidon::PoseidonBuiltinRunner; -use self::segment_arena::SegmentArenaBuiltinRunner; +pub use self::segment_arena::SegmentArenaBuiltinRunner; pub use bitwise::BitwiseBuiltinRunner; pub use ec_op::EcOpBuiltinRunner; pub use hash::HashBuiltinRunner; diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index b29e52ca04..f2d8bb476b 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -37,15 +37,16 @@ impl SegmentArenaBuiltinRunner { &mut self, segments: &mut MemorySegmentManager, ) -> Result<(), MemoryError> { - let segment_start = segments.gen_arg(&[ + let info = &vec![ MaybeRelocatable::from(segments.add()), MaybeRelocatable::from(0), MaybeRelocatable::from(0), - ] as &dyn Any)?; + ] as &dyn Any; + let segment_start = segments.gen_arg(info)?; self.base = (segment_start .get_relocatable() .ok_or(MemoryError::AddressNotRelocatable)? - + INITIAL_SEGMENT_SIZE as usize)?; + + INITIAL_SEGMENT_SIZE)?; Ok(()) } @@ -90,8 +91,8 @@ impl SegmentArenaBuiltinRunner { if stop_pointer != (self.base + used)? { return Err(RunnerError::InvalidStopPointer( SEGMENT_ARENA_BUILTIN_NAME, - Relocatable::from((self.base + used)?), - Relocatable::from(stop_pointer), + (self.base + used)?, + stop_pointer, )); } self.stop_ptr = Some(stop_pointer.offset); diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index 5d7eb6bbff..ed2abac34c 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -6,6 +6,7 @@ use crate::{ prelude::*, }, types::instance_definitions::keccak_instance_def::KeccakInstanceDef, + vm::runners::builtin_runner::{SegmentArenaBuiltinRunner, SEGMENT_ARENA_BUILTIN_NAME}, }; use crate::{ @@ -131,7 +132,7 @@ impl CairoRunner { pub fn initialize(&mut self, vm: &mut VirtualMachine) -> Result { self.initialize_builtins(vm)?; - self.initialize_segments(vm, None); + self.initialize_segments(vm, None)?; let end = self.initialize_main_entrypoint(vm)?; self.initialize_vm(vm)?; Ok(end) @@ -266,7 +267,11 @@ impl CairoRunner { // Initialize all the builtins. Values used are the original one from the CairoFunctionRunner // Values extracted from here: https://github.com/starkware-libs/cairo-lang/blob/4fb83010ab77aa7ead0c9df4b0c05e030bc70b87/src/starkware/cairo/common/cairo_function_runner.py#L28 - fn initialize_all_builtins(&self, vm: &mut VirtualMachine) -> Result<(), RunnerError> { + fn initialize_all_builtins( + &self, + vm: &mut VirtualMachine, + add_segment_arena_builtin: bool, + ) -> Result<(), RunnerError> { let starknet_preset_builtins = vec![ BuiltinName::pedersen, BuiltinName::range_check, @@ -322,6 +327,12 @@ impl CairoRunner { initialize_builtin(builtin_name, vm) } } + if add_segment_arena_builtin { + vm.builtin_runners.push(( + SEGMENT_ARENA_BUILTIN_NAME, + SegmentArenaBuiltinRunner::new(true).into(), + )) + } Ok(()) } @@ -330,15 +341,16 @@ impl CairoRunner { &mut self, vm: &mut VirtualMachine, program_base: Option, - ) { + ) -> Result<(), MemoryError> { self.program_base = match program_base { Some(base) => Some(base), None => Some(vm.segments.add()), }; self.execution_base = Some(vm.segments.add()); for (_key, builtin_runner) in vm.builtin_runners.iter_mut() { - builtin_runner.initialize_segments(&mut vm.segments); + builtin_runner.initialize_segments(&mut vm.segments)?; } + Ok(()) } fn initialize_state( @@ -1006,9 +1018,10 @@ impl CairoRunner { pub fn initialize_function_runner( &mut self, vm: &mut VirtualMachine, + add_segment_arena_builtin: bool, ) -> Result<(), RunnerError> { - self.initialize_all_builtins(vm)?; - self.initialize_segments(vm, self.program_base); + self.initialize_all_builtins(vm, add_segment_arena_builtin)?; + self.initialize_segments(vm, self.program_base)?; Ok(()) } @@ -1064,7 +1077,8 @@ impl CairoRunner { // Create, initialize and insert the new custom hash runner. let mut builtin: BuiltinRunner = HashBuiltinRunner::new(Some(32), true).into(); - builtin.initialize_segments(&mut vm.segments); + // initialize_segments for HashBuiltinRunner cn + builtin.initialize_segments(&mut vm.segments).unwrap(); let segment_index = builtin.base() as isize; vm.builtin_runners.push(("hash_builtin", builtin)); @@ -1231,7 +1245,9 @@ mod tests { let mut vm = vm!(); vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner.initialize_segments(&mut vm.segments); + builtin_runner + .initialize_segments(&mut vm.segments) + .unwrap(); (BuiltinName::output.name(), builtin_runner) }]; @@ -1278,7 +1294,9 @@ mod tests { }); add_segments!(vm, 6); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, program_base); + cairo_runner + .initialize_segments(&mut vm, program_base) + .unwrap(); assert_eq!( cairo_runner.program_base, Some(Relocatable { @@ -1307,7 +1325,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); assert_eq!( cairo_runner.program_base, Some(Relocatable { @@ -1566,7 +1584,7 @@ mod tests { cairo_runner.initial_ap = Some(relocatable!(1, 2)); cairo_runner.initial_fp = Some(relocatable!(1, 2)); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); vm.segments = segments![((2, 0), 23), ((2, 1), 233)]; assert_eq!(vm.builtin_runners[0].0, RANGE_CHECK_BUILTIN_NAME); assert_eq!(vm.builtin_runners[0].1.base(), 2); @@ -1595,7 +1613,7 @@ mod tests { cairo_runner.initial_ap = Some(relocatable!(1, 2)); cairo_runner.initial_fp = Some(relocatable!(1, 2)); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); vm.segments = segments![((2, 1), 23), ((2, 4), (-1))]; assert_eq!( @@ -1644,7 +1662,7 @@ mod tests { ); let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -1719,7 +1737,7 @@ mod tests { let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -1808,7 +1826,7 @@ mod tests { let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -1890,7 +1908,7 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); assert_eq!(end, Relocatable::from((3, 0))); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -1973,7 +1991,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2088,7 +2106,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2227,7 +2245,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2471,7 +2489,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); assert_matches!( @@ -2607,7 +2625,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); assert_matches!( @@ -2727,7 +2745,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); assert_eq!(vm.builtin_runners[0].0, OUTPUT_BUILTIN_NAME); assert_eq!(vm.builtin_runners[0].1.base(), 2); @@ -2782,7 +2800,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2823,7 +2841,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2845,7 +2863,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); assert_eq!(vm.builtin_runners[0].0, OUTPUT_BUILTIN_NAME); assert_eq!(vm.builtin_runners[0].1.base(), 2); vm.segments = segments![( @@ -2904,7 +2922,7 @@ mod tests { // Swap the first and second builtins (first should be `output`). vm.builtin_runners.swap(0, 1); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let end = cairo_runner .initialize_main_entrypoint(&mut vm) @@ -2995,7 +3013,7 @@ mod tests { let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -3062,7 +3080,7 @@ mod tests { let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -3135,7 +3153,7 @@ mod tests { let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -3260,7 +3278,9 @@ mod tests { vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner.initialize_segments(&mut vm.segments); + builtin_runner + .initialize_segments(&mut vm.segments) + .unwrap(); (BuiltinName::output.name(), builtin_runner) }]; @@ -3280,7 +3300,9 @@ mod tests { vm.segments.memory.mark_as_accessed((1, 2).into()); vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner.initialize_segments(&mut vm.segments); + builtin_runner + .initialize_segments(&mut vm.segments) + .unwrap(); (BuiltinName::output.name(), builtin_runner) }]; @@ -3905,7 +3927,9 @@ mod tests { vm.segments.memory.mark_as_accessed((1, 3).into()); vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner.initialize_segments(&mut vm.segments); + builtin_runner + .initialize_segments(&mut vm.segments) + .unwrap(); (BuiltinName::output.name(), builtin_runner) }]; @@ -3946,7 +3970,7 @@ mod tests { let mut vm = vm!(); cairo_runner - .initialize_all_builtins(&mut vm) + .initialize_all_builtins(&mut vm, false) .expect("Builtin initialization failed."); let given_output = vm.get_builtin_runners(); @@ -3958,6 +3982,7 @@ mod tests { assert_eq!(given_output[4].0, BITWISE_BUILTIN_NAME); assert_eq!(given_output[5].0, EC_OP_BUILTIN_NAME); assert_eq!(given_output[6].0, KECCAK_BUILTIN_NAME); + assert_eq!(given_output[7].0, POSEIDON_BUILTIN_NAME); } #[test] @@ -3973,7 +3998,35 @@ mod tests { let mut vm = vm!(); cairo_runner - .initialize_all_builtins(&mut vm) + .initialize_all_builtins(&mut vm, false) + .expect("Builtin initialization failed."); + + let given_output = vm.get_builtin_runners(); + + assert_eq!(given_output[0].0, HASH_BUILTIN_NAME); + assert_eq!(given_output[1].0, RANGE_CHECK_BUILTIN_NAME); + assert_eq!(given_output[2].0, SIGNATURE_BUILTIN_NAME); + assert_eq!(given_output[3].0, OUTPUT_BUILTIN_NAME); + assert_eq!(given_output[4].0, BITWISE_BUILTIN_NAME); + assert_eq!(given_output[5].0, EC_OP_BUILTIN_NAME); + assert_eq!(given_output[6].0, KECCAK_BUILTIN_NAME); + assert_eq!(given_output[7].0, POSEIDON_BUILTIN_NAME); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn initialize_all_builtins_maintain_program_order_add_segment_arena() { + let program = program![ + BuiltinName::pedersen, + BuiltinName::range_check, + BuiltinName::ecdsa + ]; + + let cairo_runner = cairo_runner!(program); + let mut vm = vm!(); + + cairo_runner + .initialize_all_builtins(&mut vm, true) .expect("Builtin initialization failed."); let given_output = vm.get_builtin_runners(); @@ -3985,6 +4038,8 @@ mod tests { assert_eq!(given_output[4].0, BITWISE_BUILTIN_NAME); assert_eq!(given_output[5].0, EC_OP_BUILTIN_NAME); assert_eq!(given_output[6].0, KECCAK_BUILTIN_NAME); + assert_eq!(given_output[7].0, POSEIDON_BUILTIN_NAME); + assert_eq!(given_output[8].0, SEGMENT_ARENA_BUILTIN_NAME); } #[test] @@ -3996,7 +4051,7 @@ mod tests { let mut vm = vm!(); cairo_runner - .initialize_function_runner(&mut vm) + .initialize_function_runner(&mut vm, false) .expect("initialize_function_runner failed."); let builtin_runners = vm.get_builtin_runners(); @@ -4027,6 +4082,47 @@ mod tests { assert_eq!(vm.segments.num_segments(), 10); } + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn initialize_function_runner_add_segment_arena_builtin() { + let program = program!(); + + let mut cairo_runner = cairo_runner!(program); + let mut vm = vm!(); + + cairo_runner + .initialize_function_runner(&mut vm, true) + .expect("initialize_function_runner failed."); + + let builtin_runners = vm.get_builtin_runners(); + + assert_eq!(builtin_runners[0].0, HASH_BUILTIN_NAME); + assert_eq!(builtin_runners[1].0, RANGE_CHECK_BUILTIN_NAME); + assert_eq!(builtin_runners[2].0, OUTPUT_BUILTIN_NAME); + assert_eq!(builtin_runners[3].0, SIGNATURE_BUILTIN_NAME); + assert_eq!(builtin_runners[4].0, BITWISE_BUILTIN_NAME); + assert_eq!(builtin_runners[5].0, EC_OP_BUILTIN_NAME); + assert_eq!(builtin_runners[6].0, KECCAK_BUILTIN_NAME); + assert_eq!(builtin_runners[7].0, POSEIDON_BUILTIN_NAME); + assert_eq!(builtin_runners[8].0, SEGMENT_ARENA_BUILTIN_NAME); + + assert_eq!( + cairo_runner.program_base, + Some(Relocatable { + segment_index: 0, + offset: 0, + }) + ); + assert_eq!( + cairo_runner.execution_base, + Some(Relocatable { + segment_index: 1, + offset: 0, + }) + ); + assert_eq!(vm.segments.num_segments(), 12); + } + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initialize_segments_incorrect_layout_plain_one_builtin() { @@ -4078,7 +4174,7 @@ mod tests { let mut runner = cairo_runner!(program); runner.proof_mode = true; let mut vm = vm!(); - runner.initialize_segments(&mut vm, None); + runner.initialize_segments(&mut vm, None).unwrap(); assert_eq!(runner.execution_base, Some(Relocatable::from((1, 0)))); assert_eq!(runner.program_base, Some(Relocatable::from((0, 0)))); assert_eq!( @@ -4103,7 +4199,7 @@ mod tests { runner.proof_mode = true; let mut vm = vm!(); runner.initialize_builtins(&mut vm).unwrap(); - runner.initialize_segments(&mut vm, None); + runner.initialize_segments(&mut vm, None).unwrap(); assert_eq!(runner.execution_base, Some(Relocatable::from((1, 0)))); assert_eq!(runner.program_base, Some(Relocatable::from((0, 0)))); assert_eq!( @@ -4362,7 +4458,7 @@ mod tests { .push((BuiltinName::output.name(), output_builtin.into())); vm.builtin_runners .push((BuiltinName::bitwise.name(), bitwise_builtin.into())); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); vm.segments.memory.data = vec![ vec![Some(MemoryCell::new(MaybeRelocatable::from((0, 0))))], vec![ @@ -4466,7 +4562,7 @@ mod tests { .unwrap(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); assert_matches!( cairo_runner.run_from_entrypoint( main_entrypoint, @@ -4486,7 +4582,9 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); new_cairo_runner.initialize_builtins(&mut new_vm).unwrap(); - new_cairo_runner.initialize_segments(&mut new_vm, None); + new_cairo_runner + .initialize_segments(&mut new_vm, None) + .unwrap(); let fib_entrypoint = program .identifiers @@ -4610,7 +4708,7 @@ mod tests { .unwrap(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None); + cairo_runner.initialize_segments(&mut vm, None).unwrap(); let result = cairo_runner.run_from_entrypoint( main_entrypoint, From 9bdf56246a525ec8587ab607aa1870017eff34c0 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 18:28:21 -0300 Subject: [PATCH 23/35] Remove error from initialize_segments --- src/vm/runners/builtin_runner/mod.rs | 8 +- .../runners/builtin_runner/segment_arena.rs | 32 ++++--- src/vm/runners/cairo_runner.rs | 86 ++++++++----------- 3 files changed, 57 insertions(+), 69 deletions(-) diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 67fa5f7a80..65557120e0 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -62,10 +62,7 @@ pub enum BuiltinRunner { impl BuiltinRunner { ///Creates the necessary segments for the builtin in the MemorySegmentManager and stores the first address on the builtin's base - pub fn initialize_segments( - &mut self, - segments: &mut MemorySegmentManager, - ) -> Result<(), MemoryError> { + pub fn initialize_segments(&mut self, segments: &mut MemorySegmentManager) { match *self { BuiltinRunner::Bitwise(ref mut bitwise) => bitwise.initialize_segments(segments), BuiltinRunner::EcOp(ref mut ec) => ec.initialize_segments(segments), @@ -78,10 +75,9 @@ impl BuiltinRunner { BuiltinRunner::Signature(ref mut signature) => signature.initialize_segments(segments), BuiltinRunner::Poseidon(ref mut poseidon) => poseidon.initialize_segments(segments), BuiltinRunner::SegmentArena(ref mut segment_arena) => { - segment_arena.initialize_segments(segments)? + segment_arena.initialize_segments(segments) } } - Ok(()) } pub fn initial_stack(&self) -> Vec { diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index f2d8bb476b..461d32cc1d 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -1,7 +1,6 @@ use crate::vm::errors::memory_errors::MemoryError; use crate::vm::errors::runner_errors::RunnerError; use crate::vm::vm_memory::memory::Memory; -use crate::with_std::any::Any; use crate::{ types::relocatable::{MaybeRelocatable, Relocatable}, vm::vm_memory::memory_segments::MemorySegmentManager, @@ -33,21 +32,15 @@ impl SegmentArenaBuiltinRunner { } } - pub fn initialize_segments( - &mut self, - segments: &mut MemorySegmentManager, - ) -> Result<(), MemoryError> { - let info = &vec![ + pub fn initialize_segments(&mut self, segments: &mut MemorySegmentManager) { + let info = &[ MaybeRelocatable::from(segments.add()), MaybeRelocatable::from(0), MaybeRelocatable::from(0), - ] as &dyn Any; - let segment_start = segments.gen_arg(info)?; - self.base = (segment_start - .get_relocatable() - .ok_or(MemoryError::AddressNotRelocatable)? - + INITIAL_SEGMENT_SIZE)?; - Ok(()) + ]; + let segment_start = gen_arg(segments, info); + // 0 + 3 can't fail + self.base = (segment_start + INITIAL_SEGMENT_SIZE).unwrap(); } pub fn get_used_cells(&self, segments: &MemorySegmentManager) -> Result { @@ -128,3 +121,16 @@ impl SegmentArenaBuiltinRunner { self.base.segment_index as usize } } + +//Specific use-case only for SegmentArenaBuiltinRunner::initialize_segments() +fn gen_arg(segments: &mut MemorySegmentManager, data: &[MaybeRelocatable; 3]) -> Relocatable { + let base = segments.add(); + for (num, value) in data.iter().enumerate() { + // 0 + 3 can't fail, inserting into newly created segment can't fail + segments + .memory + .insert(&(base + num).unwrap(), value) + .unwrap(); + } + base +} diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index ed2abac34c..0a6a410202 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -132,7 +132,7 @@ impl CairoRunner { pub fn initialize(&mut self, vm: &mut VirtualMachine) -> Result { self.initialize_builtins(vm)?; - self.initialize_segments(vm, None)?; + self.initialize_segments(vm, None); let end = self.initialize_main_entrypoint(vm)?; self.initialize_vm(vm)?; Ok(end) @@ -341,16 +341,15 @@ impl CairoRunner { &mut self, vm: &mut VirtualMachine, program_base: Option, - ) -> Result<(), MemoryError> { + ) { self.program_base = match program_base { Some(base) => Some(base), None => Some(vm.segments.add()), }; self.execution_base = Some(vm.segments.add()); for (_key, builtin_runner) in vm.builtin_runners.iter_mut() { - builtin_runner.initialize_segments(&mut vm.segments)?; + builtin_runner.initialize_segments(&mut vm.segments); } - Ok(()) } fn initialize_state( @@ -1021,7 +1020,7 @@ impl CairoRunner { add_segment_arena_builtin: bool, ) -> Result<(), RunnerError> { self.initialize_all_builtins(vm, add_segment_arena_builtin)?; - self.initialize_segments(vm, self.program_base)?; + self.initialize_segments(vm, self.program_base); Ok(()) } @@ -1077,8 +1076,7 @@ impl CairoRunner { // Create, initialize and insert the new custom hash runner. let mut builtin: BuiltinRunner = HashBuiltinRunner::new(Some(32), true).into(); - // initialize_segments for HashBuiltinRunner cn - builtin.initialize_segments(&mut vm.segments).unwrap(); + builtin.initialize_segments(&mut vm.segments); let segment_index = builtin.base() as isize; vm.builtin_runners.push(("hash_builtin", builtin)); @@ -1245,9 +1243,7 @@ mod tests { let mut vm = vm!(); vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner - .initialize_segments(&mut vm.segments) - .unwrap(); + builtin_runner.initialize_segments(&mut vm.segments); (BuiltinName::output.name(), builtin_runner) }]; @@ -1294,9 +1290,7 @@ mod tests { }); add_segments!(vm, 6); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner - .initialize_segments(&mut vm, program_base) - .unwrap(); + cairo_runner.initialize_segments(&mut vm, program_base); assert_eq!( cairo_runner.program_base, Some(Relocatable { @@ -1325,7 +1319,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); assert_eq!( cairo_runner.program_base, Some(Relocatable { @@ -1584,7 +1578,7 @@ mod tests { cairo_runner.initial_ap = Some(relocatable!(1, 2)); cairo_runner.initial_fp = Some(relocatable!(1, 2)); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); vm.segments = segments![((2, 0), 23), ((2, 1), 233)]; assert_eq!(vm.builtin_runners[0].0, RANGE_CHECK_BUILTIN_NAME); assert_eq!(vm.builtin_runners[0].1.base(), 2); @@ -1613,7 +1607,7 @@ mod tests { cairo_runner.initial_ap = Some(relocatable!(1, 2)); cairo_runner.initial_fp = Some(relocatable!(1, 2)); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); vm.segments = segments![((2, 1), 23), ((2, 4), (-1))]; assert_eq!( @@ -1662,7 +1656,7 @@ mod tests { ); let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -1737,7 +1731,7 @@ mod tests { let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -1826,7 +1820,7 @@ mod tests { let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -1908,7 +1902,7 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); assert_eq!(end, Relocatable::from((3, 0))); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -1991,7 +1985,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2106,7 +2100,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2245,7 +2239,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2489,7 +2483,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); assert_matches!( @@ -2625,7 +2619,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); assert_matches!( @@ -2745,7 +2739,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); assert_eq!(vm.builtin_runners[0].0, OUTPUT_BUILTIN_NAME); assert_eq!(vm.builtin_runners[0].1.base(), 2); @@ -2800,7 +2794,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2841,7 +2835,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); //Execution Phase @@ -2863,7 +2857,7 @@ mod tests { let mut cairo_runner = cairo_runner!(program); let mut vm = vm!(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); assert_eq!(vm.builtin_runners[0].0, OUTPUT_BUILTIN_NAME); assert_eq!(vm.builtin_runners[0].1.base(), 2); vm.segments = segments![( @@ -2922,7 +2916,7 @@ mod tests { // Swap the first and second builtins (first should be `output`). vm.builtin_runners.swap(0, 1); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let end = cairo_runner .initialize_main_entrypoint(&mut vm) @@ -3013,7 +3007,7 @@ mod tests { let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -3080,7 +3074,7 @@ mod tests { let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -3153,7 +3147,7 @@ mod tests { let mut vm = vm!(true); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); cairo_runner.initialize_main_entrypoint(&mut vm).unwrap(); cairo_runner.initialize_vm(&mut vm).unwrap(); @@ -3278,9 +3272,7 @@ mod tests { vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner - .initialize_segments(&mut vm.segments) - .unwrap(); + builtin_runner.initialize_segments(&mut vm.segments); (BuiltinName::output.name(), builtin_runner) }]; @@ -3300,9 +3292,7 @@ mod tests { vm.segments.memory.mark_as_accessed((1, 2).into()); vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner - .initialize_segments(&mut vm.segments) - .unwrap(); + builtin_runner.initialize_segments(&mut vm.segments); (BuiltinName::output.name(), builtin_runner) }]; @@ -3927,9 +3917,7 @@ mod tests { vm.segments.memory.mark_as_accessed((1, 3).into()); vm.builtin_runners = vec![{ let mut builtin_runner: BuiltinRunner = OutputBuiltinRunner::new(true).into(); - builtin_runner - .initialize_segments(&mut vm.segments) - .unwrap(); + builtin_runner.initialize_segments(&mut vm.segments); (BuiltinName::output.name(), builtin_runner) }]; @@ -4174,7 +4162,7 @@ mod tests { let mut runner = cairo_runner!(program); runner.proof_mode = true; let mut vm = vm!(); - runner.initialize_segments(&mut vm, None).unwrap(); + runner.initialize_segments(&mut vm, None); assert_eq!(runner.execution_base, Some(Relocatable::from((1, 0)))); assert_eq!(runner.program_base, Some(Relocatable::from((0, 0)))); assert_eq!( @@ -4199,7 +4187,7 @@ mod tests { runner.proof_mode = true; let mut vm = vm!(); runner.initialize_builtins(&mut vm).unwrap(); - runner.initialize_segments(&mut vm, None).unwrap(); + runner.initialize_segments(&mut vm, None); assert_eq!(runner.execution_base, Some(Relocatable::from((1, 0)))); assert_eq!(runner.program_base, Some(Relocatable::from((0, 0)))); assert_eq!( @@ -4458,7 +4446,7 @@ mod tests { .push((BuiltinName::output.name(), output_builtin.into())); vm.builtin_runners .push((BuiltinName::bitwise.name(), bitwise_builtin.into())); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); vm.segments.memory.data = vec![ vec![Some(MemoryCell::new(MaybeRelocatable::from((0, 0))))], vec![ @@ -4562,7 +4550,7 @@ mod tests { .unwrap(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); assert_matches!( cairo_runner.run_from_entrypoint( main_entrypoint, @@ -4582,9 +4570,7 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); new_cairo_runner.initialize_builtins(&mut new_vm).unwrap(); - new_cairo_runner - .initialize_segments(&mut new_vm, None) - .unwrap(); + new_cairo_runner.initialize_segments(&mut new_vm, None); let fib_entrypoint = program .identifiers @@ -4708,7 +4694,7 @@ mod tests { .unwrap(); cairo_runner.initialize_builtins(&mut vm).unwrap(); - cairo_runner.initialize_segments(&mut vm, None).unwrap(); + cairo_runner.initialize_segments(&mut vm, None); let result = cairo_runner.run_from_entrypoint( main_entrypoint, From a431769da8cd8c5fbb8672694161dd2f71b03d29 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 18:30:14 -0300 Subject: [PATCH 24/35] Fix comment --- src/vm/runners/builtin_runner/segment_arena.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index 461d32cc1d..cbc892b579 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -122,7 +122,7 @@ impl SegmentArenaBuiltinRunner { } } -//Specific use-case only for SegmentArenaBuiltinRunner::initialize_segments() +// Specific non-failling version of gen_arg used specifically for SegmentArenaBuiltinRunner fn gen_arg(segments: &mut MemorySegmentManager, data: &[MaybeRelocatable; 3]) -> Relocatable { let base = segments.add(); for (num, value) in data.iter().enumerate() { From 019ab32da151e1d9834c545fad2765c987c33640 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 18:35:31 -0300 Subject: [PATCH 25/35] Add test --- .../runners/builtin_runner/segment_arena.rs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index cbc892b579..1ba890573f 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -134,3 +134,23 @@ fn gen_arg(segments: &mut MemorySegmentManager, data: &[MaybeRelocatable; 3]) -> } base } + +#[cfg(test)] +mod tests { + use crate::utils::test_utils::*; + + use super::*; + + #[test] + fn gen_arg_test() { + let mut segments = MemorySegmentManager::new(); + let data = &[ + MaybeRelocatable::from(segments.add()), + MaybeRelocatable::from(0), + MaybeRelocatable::from(0), + ]; + let base = gen_arg(&mut segments, data); + assert_eq!(base, (1, 0).into()); + check_memory!(segments.memory, ((1, 0), (0, 0)), ((1, 1), 0), ((1, 2), 0)); + } +} From 2a9ae2f77cc7205440572f44f7526c517358000f Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 20 Mar 2023 18:57:00 -0300 Subject: [PATCH 26/35] Add tests for segment arena builtin --- .../runners/builtin_runner/segment_arena.rs | 254 +++++++++++++++++- 1 file changed, 252 insertions(+), 2 deletions(-) diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index 1ba890573f..c516277fd9 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -137,11 +137,15 @@ fn gen_arg(segments: &mut MemorySegmentManager, data: &[MaybeRelocatable; 3]) -> #[cfg(test)] mod tests { - use crate::utils::test_utils::*; - use super::*; + use crate::vm::vm_core::VirtualMachine; + use crate::with_std::collections::HashMap; + use crate::{relocatable, utils::test_utils::*, vm::runners::builtin_runner::BuiltinRunner}; + #[cfg(target_arch = "wasm32")] + use wasm_bindgen_test::*; #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn gen_arg_test() { let mut segments = MemorySegmentManager::new(); let data = &[ @@ -153,4 +157,250 @@ mod tests { assert_eq!(base, (1, 0).into()); check_memory!(segments.memory, ((1, 0), (0, 0)), ((1, 1), 0), ((1, 2), 0)); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_used_instances() { + let builtin = SegmentArenaBuiltinRunner::new(true); + + let mut vm = vm!(); + vm.segments.segment_used_sizes = Some(vec![3]); + + assert_eq!(builtin.get_used_instances(&vm.segments), Ok(0)); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn final_stack_error_stop_pointer() { + let mut builtin = SegmentArenaBuiltinRunner::new(true); + + let mut vm = vm!(); + + vm.segments = segments![ + ((0, 0), (0, 0)), + ((0, 1), (0, 1)), + ((2, 0), (0, 0)), + ((2, 1), (0, 0)) + ]; + + vm.segments.segment_used_sizes = Some(vec![6]); + + let pointer = Relocatable::from((2, 2)); + assert_eq!( + builtin.final_stack(&vm.segments, pointer), + Err(RunnerError::InvalidStopPointer( + SEGMENT_ARENA_BUILTIN_NAME, + relocatable!(0, 3), + relocatable!(0, 0) + )) + ); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn final_stack_valid() { + let mut builtin = SegmentArenaBuiltinRunner::new(false); + + let mut vm = vm!(); + + vm.segments = segments![ + ((0, 0), (0, 0)), + ((0, 1), (0, 1)), + ((2, 0), (0, 0)), + ((2, 1), (1, 0)) + ]; + + vm.segments.segment_used_sizes = Some(vec![0]); + + let pointer = Relocatable::from((2, 2)); + + assert_eq!( + builtin.final_stack(&vm.segments, pointer).unwrap(), + Relocatable::from((2, 2)) + ); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn final_stack_error_non_relocatable() { + let mut builtin = SegmentArenaBuiltinRunner::new(true); + + let mut vm = vm!(); + + vm.segments = segments![ + ((0, 0), (0, 0)), + ((0, 1), (0, 1)), + ((2, 0), (0, 0)), + ((2, 1), 2) + ]; + + vm.segments.segment_used_sizes = Some(vec![0]); + + let pointer = Relocatable::from((2, 2)); + + assert_eq!( + builtin.final_stack(&vm.segments, pointer), + Err(RunnerError::NoStopPointer(SEGMENT_ARENA_BUILTIN_NAME)) + ); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_used_cells_and_allocated_size_test() { + let builtin: BuiltinRunner = SegmentArenaBuiltinRunner::new(true).into(); + + let mut vm = vm!(); + + vm.segments.segment_used_sizes = Some(vec![3]); + + assert_eq!( + builtin.get_used_cells_and_allocated_size(&vm), + Ok((0_usize, 0)) + ); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn initialize_segments_for_output() { + let mut builtin = SegmentArenaBuiltinRunner::new(true); + let mut segments = MemorySegmentManager::new(); + builtin.initialize_segments(&mut segments); + assert_eq!(builtin.base, (1, 3).into()); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_initial_stack_for_output_with_base() { + let mut builtin = SegmentArenaBuiltinRunner::new(true); + builtin.base = relocatable!(1, 0); + let initial_stack = builtin.initial_stack(); + assert_eq!( + initial_stack[0].clone(), + MaybeRelocatable::RelocatableValue((builtin.base() as isize, 0).into()) + ); + assert_eq!(initial_stack.len(), 1); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_memory_segment_addresses() { + let builtin = SegmentArenaBuiltinRunner::new(true); + + assert_eq!(builtin.get_memory_segment_addresses(), (0, None),); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_memory_accesses_missing_segment_used_sizes() { + let builtin = BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(true)); + let vm = vm!(); + + assert_eq!(builtin.get_memory_accesses(&vm), Ok(vec![]),); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_memory_accesses_empty() { + let builtin = BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(true)); + let mut vm = vm!(); + + vm.segments.segment_used_sizes = Some(vec![0]); + assert_eq!(builtin.get_memory_accesses(&vm), Ok(vec![])); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_memory_accesses() { + let builtin = BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(true)); + let mut vm = vm!(); + + vm.segments.segment_used_sizes = Some(vec![4]); + assert_eq!(builtin.get_memory_accesses(&vm), Ok(vec![]),); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_used_cells_missing_segment_used_sizes() { + let builtin = BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(true)); + let vm = vm!(); + + assert_eq!( + builtin.get_used_cells(&vm.segments), + Err(MemoryError::MissingSegmentUsedSizes) + ); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_used_cells_empty() { + let builtin = BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(true)); + let mut vm = vm!(); + + vm.segments.segment_used_sizes = Some(vec![0]); + assert_eq!( + builtin.get_used_cells(&vm.segments), + Err(MemoryError::InvalidUsedSizeSegmentArena) + ); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_used_cells() { + let builtin = BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(true)); + let mut vm = vm!(); + + vm.segments.segment_used_sizes = Some(vec![4]); + assert_eq!(builtin.get_used_cells(&vm.segments), Ok(1)); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn test_get_used_instances_missing_segments() { + let builtin = BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(true)); + let memory_segment_manager = MemorySegmentManager::new(); + + assert_eq!( + builtin.get_used_instances(&memory_segment_manager), + Err(MemoryError::MissingSegmentUsedSizes) + ); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn test_get_used_instances_valid() { + let builtin = BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(true)); + let mut memory_segment_manager = MemorySegmentManager::new(); + memory_segment_manager.segment_used_sizes = Some(vec![6]); + + assert_eq!(builtin.get_used_instances(&memory_segment_manager), Ok(3)); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn test_deduce_memory_cell_output_builtin() { + let builtin = BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(true)); + let mut vm = vm!(); + + vm.segments = segments![ + ((0, 0), (0, 0)), + ((0, 1), (0, 1)), + ((2, 0), (0, 0)), + ((2, 1), 2) + ]; + + let pointer = Relocatable::from((2, 2)); + + assert_eq!( + builtin.deduce_memory_cell(pointer, &vm.segments.memory), + Ok(None) + ); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn test_add_validation_rule() { + let builtin = SegmentArenaBuiltinRunner::new(true); + let mut vm = vm!(); + builtin.add_validation_rule(&mut vm.segments.memory); + } } From a23245310bbf4e3ebb3af15f707be60576e90f02 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 21 Mar 2023 16:49:48 -0300 Subject: [PATCH 27/35] Fix --- src/vm/runners/builtin_runner/segment_arena.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index c516277fd9..f49c864d04 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -129,7 +129,7 @@ fn gen_arg(segments: &mut MemorySegmentManager, data: &[MaybeRelocatable; 3]) -> // 0 + 3 can't fail, inserting into newly created segment can't fail segments .memory - .insert(&(base + num).unwrap(), value) + .insert((base + num).unwrap(), value) .unwrap(); } base From 8d904f79a54638ab77ebe50aacb8fa3e03c939e5 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 22 Mar 2023 12:00:15 -0300 Subject: [PATCH 28/35] fix get_perm_range_check_limit --- src/vm/trace/mod.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/vm/trace/mod.rs b/src/vm/trace/mod.rs index 3e7178296e..f0d3bc0a76 100644 --- a/src/vm/trace/mod.rs +++ b/src/vm/trace/mod.rs @@ -1,3 +1,5 @@ +use core::ops::Shl; + use self::trace_entry::TraceEntry; use super::{ decoding::decoder::decode_instruction, @@ -9,7 +11,7 @@ use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use num_traits::ToPrimitive; pub mod trace_entry; - +const OFFSET_BITS: u32 = 16; /// Return the minimum and maximum values in the perm_range_check component. pub fn get_perm_range_check_limits( trace: &[TraceEntry], @@ -33,9 +35,9 @@ pub fn get_perm_range_check_limits( .transpose()?; let decoded_instruction = decode_instruction(instruction, immediate.as_ref())?; - let off0 = decoded_instruction.off0; - let off1 = decoded_instruction.off1; - let off2 = decoded_instruction.off2; + let off0 = decoded_instruction.off0 + 1_isize.shl(OFFSET_BITS - 1); + let off1 = decoded_instruction.off1 + 1_isize.shl(OFFSET_BITS - 1); + let off2 = decoded_instruction.off2 + 1_isize.shl(OFFSET_BITS - 1); let min_value = off0.min(off1).min(off2); let max_value = off0.max(off1).max(off2); From 7d48d4a814c1fa15306cb66438835b0cdc6a6cad Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 22 Mar 2023 14:27:25 -0300 Subject: [PATCH 29/35] Fix get_range_check_usage --- src/vm/runners/builtin_runner/range_check.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/vm/runners/builtin_runner/range_check.rs b/src/vm/runners/builtin_runner/range_check.rs index 5ee02efcdb..fdf8818c09 100644 --- a/src/vm/runners/builtin_runner/range_check.rs +++ b/src/vm/runners/builtin_runner/range_check.rs @@ -126,13 +126,9 @@ impl RangeCheckBuiltinRunner { let inner_rc_bound = Felt252::new(self.inner_rc_bound); for value in range_check_segment { //Split val into n_parts parts. + let mut val = value.as_ref()?.get_value().get_int_ref()?.clone(); for _ in 0..self.n_parts { - let part_val = value - .as_ref()? - .get_value() - .get_int_ref()? - .mod_floor(&inner_rc_bound) - .to_usize()?; + let part_val = val.mod_floor(&inner_rc_bound).to_usize()?; rc_bounds = Some(match rc_bounds { None => (part_val, part_val), Some((rc_min, rc_max)) => { @@ -142,6 +138,7 @@ impl RangeCheckBuiltinRunner { (rc_min, rc_max) } }); + val = val.div_floor(&inner_rc_bound) } } rc_bounds From 12736d953a567c21ef75b5226162dbf3ca928c42 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 22 Mar 2023 15:21:34 -0300 Subject: [PATCH 30/35] Fix test values --- src/vm/runners/builtin_runner/mod.rs | 2 +- src/vm/runners/builtin_runner/range_check.rs | 6 +++--- src/vm/runners/cairo_runner.rs | 4 ++-- src/vm/trace/mod.rs | 7 +++++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 805b2a5e9f..41f0110895 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -997,7 +997,7 @@ mod tests { fn get_range_check_usage_range_check() { let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(Some(8), 8, true)); let memory = memory![((0, 0), 1), ((0, 1), 2), ((0, 2), 3), ((0, 3), 4)]; - assert_eq!(builtin.get_range_check_usage(&memory), Some((1, 4))); + assert_eq!(builtin.get_range_check_usage(&memory), Some((0, 4))); } #[test] diff --git a/src/vm/runners/builtin_runner/range_check.rs b/src/vm/runners/builtin_runner/range_check.rs index fdf8818c09..8571db61e6 100644 --- a/src/vm/runners/builtin_runner/range_check.rs +++ b/src/vm/runners/builtin_runner/range_check.rs @@ -532,7 +532,7 @@ mod tests { fn get_range_check_usage_succesful_a() { let builtin = RangeCheckBuiltinRunner::new(Some(8), 8, true); let memory = memory![((0, 0), 1), ((0, 1), 2), ((0, 2), 3), ((0, 3), 4)]; - assert_eq!(builtin.get_range_check_usage(&memory), Some((1, 4))); + assert_eq!(builtin.get_range_check_usage(&memory), Some((0, 4))); } #[test] @@ -545,7 +545,7 @@ mod tests { ((0, 2), 31349610736_i64), ((0, 3), 413468326585859_i64) ]; - assert_eq!(builtin.get_range_check_usage(&memory), Some((6384, 62821))); + assert_eq!(builtin.get_range_check_usage(&memory), Some((0, 62821))); } #[test] @@ -560,7 +560,7 @@ mod tests { ((0, 4), 75346043276073460326_i128), ((0, 5), 87234598724867609478353436890268_i128) ]; - assert_eq!(builtin.get_range_check_usage(&memory), Some((10480, 42341))); + assert_eq!(builtin.get_range_check_usage(&memory), Some((0, 61576))); } #[test] diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index c9afffe17d..c60328c68c 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -3698,7 +3698,7 @@ mod tests { assert_matches!( cairo_runner.get_perm_range_check_limits(&vm), - Ok(Some((-31440, 16383))) + Ok(Some((1328, 49151))) ); } @@ -3727,7 +3727,7 @@ mod tests { assert_matches!( cairo_runner.get_perm_range_check_limits(&vm), - Ok(Some((-31440, 1328))) + Ok(Some((0, 33023))) ); } diff --git a/src/vm/trace/mod.rs b/src/vm/trace/mod.rs index f0d3bc0a76..142e7661ac 100644 --- a/src/vm/trace/mod.rs +++ b/src/vm/trace/mod.rs @@ -81,9 +81,12 @@ mod test { }]; let memory = memory![((0, 0), 0xFFFF_8000_0000_u64)]; + // off0 -32768 + // off1 0 + // off2 32767 assert_matches!( get_perm_range_check_limits(trace, &memory), - Ok(Some((-32768, 32767))) + Ok(Some((0, 65535))) ); } @@ -117,7 +120,7 @@ mod test { assert_matches!( get_perm_range_check_limits(trace, &memory), - Ok(Some((-31440, 16383))) + Ok(Some((1328, 49151))) ); } } From 30c75835f676c265a19cdeda4f9d7f42ba9a8eef Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 27 Mar 2023 14:46:55 -0300 Subject: [PATCH 31/35] expose program base for starknet reasons --- src/vm/runners/cairo_runner.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index bf651ca5e3..d2a07b3945 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -77,7 +77,7 @@ pub struct CairoRunner { pub(crate) program: Program, layout: CairoLayout, final_pc: Option, - pub(crate) program_base: Option, + pub program_base: Option, execution_base: Option, initial_ap: Option, initial_fp: Option, From ecd36199952b65141791000ecb6a21e5f87ca2dd Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Wed, 29 Mar 2023 17:42:03 -0300 Subject: [PATCH 32/35] Update src/vm/runners/builtin_runner/mod.rs Co-authored-by: Mario Rugiero --- src/vm/runners/builtin_runner/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 94bddef4fe..67663e47d9 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -37,7 +37,7 @@ pub const BITWISE_BUILTIN_NAME: &str = "bitwise"; pub const EC_OP_BUILTIN_NAME: &str = "ec_op"; pub const KECCAK_BUILTIN_NAME: &str = "keccak"; pub const POSEIDON_BUILTIN_NAME: &str = "poseidon"; -pub const SEGMENT_ARENA_BUILTIN_NAME: &str = "segment_area"; +pub const SEGMENT_ARENA_BUILTIN_NAME: &str = "segment_arena"; /* NB: this enum is no accident: we may need (and cairo-rs-py *does* need) * structs containing this to be `Send`. The only two ways to achieve that From ec087cfa1fdcd50124e0c6ee470d1e4ebea94403 Mon Sep 17 00:00:00 2001 From: Mario Rugiero Date: Thu, 30 Mar 2023 17:28:51 -0300 Subject: [PATCH 33/35] Fix build --- src/vm/runners/builtin_runner/mod.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index aa117432a1..67663e47d9 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -372,19 +372,6 @@ impl BuiltinRunner { } } - fn instances_per_component(&self) -> u32 { - match self { - BuiltinRunner::Bitwise(builtin) => builtin.instances_per_component, - BuiltinRunner::EcOp(builtin) => builtin.instances_per_component, - BuiltinRunner::Hash(builtin) => builtin.instances_per_component, - BuiltinRunner::RangeCheck(builtin) => builtin.instances_per_component, - BuiltinRunner::Output(_) => 0, - BuiltinRunner::Keccak(builtin) => builtin.instances_per_component, - BuiltinRunner::Signature(builtin) => builtin.instances_per_component, - BuiltinRunner::Poseidon(builtin) => builtin.instances_per_component, - } - } - pub fn name(&self) -> &'static str { match self { BuiltinRunner::Bitwise(_) => BITWISE_BUILTIN_NAME, From da58e605eeb6abd05be723b369398a3bb2c4e294 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 31 Mar 2023 12:31:24 -0300 Subject: [PATCH 34/35] Add nostd Vec import --- src/vm/runners/builtin_runner/segment_arena.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index f49c864d04..d6d8e0b692 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -6,6 +6,9 @@ use crate::{ vm::vm_memory::memory_segments::MemorySegmentManager, }; +#[cfg(all(not(feature = "std"), feature = "alloc"))] +use alloc::vec::Vec; + use super::SEGMENT_ARENA_BUILTIN_NAME; const ARENA_BUILTIN_SIZE: u32 = 3; From 99c9bb84702d00b3f0dbd8869f5736abe73dc3b2 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 31 Mar 2023 15:44:02 -0300 Subject: [PATCH 35/35] Fix wasm import --- src/vm/runners/builtin_runner/segment_arena.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/runners/builtin_runner/segment_arena.rs b/src/vm/runners/builtin_runner/segment_arena.rs index d6d8e0b692..027e70d541 100644 --- a/src/vm/runners/builtin_runner/segment_arena.rs +++ b/src/vm/runners/builtin_runner/segment_arena.rs @@ -141,8 +141,8 @@ fn gen_arg(segments: &mut MemorySegmentManager, data: &[MaybeRelocatable; 3]) -> #[cfg(test)] mod tests { use super::*; + use crate::stdlib::collections::HashMap; use crate::vm::vm_core::VirtualMachine; - use crate::with_std::collections::HashMap; use crate::{relocatable, utils::test_utils::*, vm::runners::builtin_runner::BuiltinRunner}; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*;