Skip to content

Commit e2c6c91

Browse files
Use_BTreeMap_in_pie_additional_data (#2162)
1 parent d3c649c commit e2c6c91

File tree

5 files changed

+49
-45
lines changed

5 files changed

+49
-45
lines changed

vm/src/tests/cairo_pie_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use wasm_bindgen_test::*;
99
use crate::{
1010
cairo_run::{cairo_run, CairoRunConfig},
1111
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,
12-
stdlib::collections::{BTreeMap, HashMap},
12+
stdlib::collections::BTreeMap,
1313
types::relocatable::Relocatable,
1414
vm::runners::{
1515
cairo_pie::{
@@ -83,8 +83,8 @@ fn pedersen_test() {
8383
(
8484
BuiltinName::output,
8585
BuiltinAdditionalData::Output(OutputBuiltinAdditionalData {
86-
pages: HashMap::new(),
87-
attributes: HashMap::new(),
86+
pages: BTreeMap::new(),
87+
attributes: BTreeMap::new(),
8888
}),
8989
),
9090
(
@@ -156,7 +156,7 @@ fn common_signature() {
156156
// additional_data
157157
let expected_additional_data = BTreeMap::from([(
158158
BuiltinName::ecdsa,
159-
BuiltinAdditionalData::Signature(HashMap::from([(
159+
BuiltinAdditionalData::Signature(BTreeMap::from([(
160160
Relocatable::from((2, 0)),
161161
(
162162
felt_str!(

vm/src/vm/runners/builtin_runner/output.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::stdlib::{collections::HashMap, prelude::*};
1+
use crate::stdlib::{collections::BTreeMap, prelude::*};
22
use crate::types::builtin_name::BuiltinName;
33
use crate::types::relocatable::{MaybeRelocatable, Relocatable};
44
use crate::vm::errors::memory_errors::MemoryError;
@@ -32,8 +32,8 @@ impl OutputBuiltinRunner {
3232
OutputBuiltinRunner {
3333
base: 0,
3434
base_offset: 0,
35-
pages: HashMap::default(),
36-
attributes: HashMap::default(),
35+
pages: BTreeMap::default(),
36+
attributes: BTreeMap::default(),
3737
stop_ptr: None,
3838
included,
3939
}
@@ -42,8 +42,8 @@ impl OutputBuiltinRunner {
4242
pub fn new_state(&mut self, base: usize, base_offset: usize, included: bool) {
4343
self.base = base;
4444
self.base_offset = base_offset;
45-
self.pages = HashMap::default();
46-
self.attributes = HashMap::default();
45+
self.pages = BTreeMap::default();
46+
self.attributes = BTreeMap::default();
4747
self.stop_ptr = None;
4848
self.included = included;
4949
}
@@ -209,7 +209,6 @@ impl Default for OutputBuiltinRunner {
209209
mod tests {
210210
use super::*;
211211
use crate::relocatable;
212-
use crate::stdlib::collections::HashMap;
213212

214213
use crate::{
215214
utils::test_utils::*,
@@ -477,8 +476,8 @@ mod tests {
477476
assert_eq!(
478477
builtin.get_additional_data(),
479478
BuiltinAdditionalData::Output(OutputBuiltinAdditionalData {
480-
pages: HashMap::default(),
481-
attributes: HashMap::default()
479+
pages: BTreeMap::default(),
480+
attributes: BTreeMap::default()
482481
})
483482
)
484483
}
@@ -500,8 +499,8 @@ mod tests {
500499
let new_state = OutputBuiltinState {
501500
base: 10,
502501
base_offset: 0,
503-
pages: HashMap::from([(1, PublicMemoryPage { start: 0, size: 3 })]),
504-
attributes: HashMap::from([("gps_fact_topology".to_string(), vec![0, 2, 0])]),
502+
pages: BTreeMap::from([(1, PublicMemoryPage { start: 0, size: 3 })]),
503+
attributes: BTreeMap::from([("gps_fact_topology".to_string(), vec![0, 2, 0])]),
505504
};
506505
builtin.set_state(new_state.clone());
507506

@@ -518,8 +517,8 @@ mod tests {
518517
let mut builtin = OutputBuiltinRunner {
519518
base: 10,
520519
base_offset: 0,
521-
pages: HashMap::from([(1, PublicMemoryPage { start: 0, size: 3 })]),
522-
attributes: HashMap::from([("gps_fact_topology".to_string(), vec![0, 2, 0])]),
520+
pages: BTreeMap::from([(1, PublicMemoryPage { start: 0, size: 3 })]),
521+
attributes: BTreeMap::from([("gps_fact_topology".to_string(), vec![0, 2, 0])]),
523522
stop_ptr: Some(10),
524523
included: true,
525524
};
@@ -553,7 +552,7 @@ mod tests {
553552

554553
assert_eq!(
555554
builtin.pages,
556-
HashMap::from([(1, PublicMemoryPage { start: 0, size: 3 }),])
555+
BTreeMap::from([(1, PublicMemoryPage { start: 0, size: 3 }),])
557556
)
558557
}
559558

@@ -580,7 +579,7 @@ mod tests {
580579
let values = vec![0, 12, 30];
581580
builtin.add_attribute(name.clone(), values.clone());
582581

583-
assert_eq!(builtin.attributes, HashMap::from([(name, values)]));
582+
assert_eq!(builtin.attributes, BTreeMap::from([(name, values)]));
584583
}
585584

586585
#[test]
@@ -624,8 +623,8 @@ mod tests {
624623
let builtin_a = OutputBuiltinRunner {
625624
base: 0,
626625
base_offset: 0,
627-
pages: HashMap::from([(1, PublicMemoryPage { start: 0, size: 3 })]),
628-
attributes: HashMap::from([("gps_fact_topology".to_string(), vec![0, 2, 0])]),
626+
pages: BTreeMap::from([(1, PublicMemoryPage { start: 0, size: 3 })]),
627+
attributes: BTreeMap::from([("gps_fact_topology".to_string(), vec![0, 2, 0])]),
629628
stop_ptr: None,
630629
included: true,
631630
};

vm/src/vm/runners/builtin_runner/signature.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use crate::air_private_input::{PrivateInput, PrivateInputSignature, SignatureInput};
22
use crate::math_utils::div_mod;
3-
use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*, rc::Rc};
3+
use crate::stdlib::{
4+
cell::RefCell,
5+
collections::{BTreeMap, HashMap},
6+
prelude::*,
7+
rc::Rc,
8+
};
49

510
use crate::types::builtin_name::BuiltinName;
611
use crate::types::instance_definitions::ecdsa_instance_def::CELLS_PER_SIGNATURE;
@@ -162,7 +167,7 @@ impl SignatureBuiltinRunner {
162167

163168
pub fn get_additional_data(&self) -> BuiltinAdditionalData {
164169
// Convert signatures to Felt tuple
165-
let signatures: HashMap<Relocatable, (Felt252, Felt252)> = self
170+
let signatures: BTreeMap<Relocatable, (Felt252, Felt252)> = self
166171
.signatures
167172
.borrow()
168173
.iter()
@@ -523,7 +528,7 @@ mod tests {
523528
},
524529
)]);
525530
builtin.signatures = Rc::new(RefCell::new(signatures));
526-
let signatures = HashMap::from([(
531+
let signatures = BTreeMap::from([(
527532
Relocatable::from((4, 0)),
528533
(felt_str!("45678"), felt_str!("1239")),
529534
)]);

vm/src/vm/runners/cairo_pie.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ impl From<&Vec<usize>> for PublicMemoryPage {
7575
}
7676

7777
// HashMap value based on starknet/core/os/output.cairo usage
78-
pub type Attributes = HashMap<String, Vec<usize>>;
79-
pub type Pages = HashMap<usize, PublicMemoryPage>;
78+
pub type Attributes = BTreeMap<String, Vec<usize>>;
79+
pub type Pages = BTreeMap<usize, PublicMemoryPage>;
8080

8181
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
8282
pub struct OutputBuiltinAdditionalData {
@@ -96,7 +96,7 @@ pub enum BuiltinAdditionalData {
9696
Output(OutputBuiltinAdditionalData),
9797
// Signatures are composed of (r, s) tuples
9898
#[serde(with = "serde_impl::signature_additional_data")]
99-
Signature(HashMap<Relocatable, (Felt252, Felt252)>),
99+
Signature(BTreeMap<Relocatable, (Felt252, Felt252)>),
100100
None,
101101
}
102102

@@ -765,7 +765,7 @@ pub(super) mod serde_impl {
765765
use super::*;
766766

767767
pub fn serialize<S>(
768-
values: &HashMap<Relocatable, (Felt252, Felt252)>,
768+
values: &BTreeMap<Relocatable, (Felt252, Felt252)>,
769769
serializer: S,
770770
) -> Result<S::Ok, S::Error>
771771
where
@@ -787,30 +787,30 @@ pub(super) mod serde_impl {
787787

788788
pub fn deserialize<'de, D>(
789789
d: D,
790-
) -> Result<HashMap<Relocatable, (Felt252, Felt252)>, D::Error>
790+
) -> Result<BTreeMap<Relocatable, (Felt252, Felt252)>, D::Error>
791791
where
792792
D: Deserializer<'de>,
793793
{
794794
let number_map = Vec::<((Number, Number), (Number, Number))>::deserialize(d)?;
795-
let mut res = HashMap::with_capacity(number_map.len());
796-
for ((index, offset), (r, s)) in number_map.into_iter() {
797-
let addr = Relocatable::from((
798-
index
795+
number_map
796+
.into_iter()
797+
.map(|((index, offset), (r, s))| {
798+
let idx = index
799799
.as_u64()
800800
.ok_or_else(|| D::Error::custom("Invalid address"))?
801-
as isize,
802-
offset
801+
as isize;
802+
let off = offset
803803
.as_u64()
804804
.ok_or_else(|| D::Error::custom("Invalid address"))?
805-
as usize,
806-
));
807-
let r = Felt252::from_dec_str(r.as_str())
808-
.map_err(|_| D::Error::custom("Invalid Felt252 value"))?;
809-
let s = Felt252::from_dec_str(s.as_str())
810-
.map_err(|_| D::Error::custom("Invalid Felt252 value"))?;
811-
res.insert(addr, (r, s));
812-
}
813-
Ok(res)
805+
as usize;
806+
let addr = Relocatable::from((idx, off));
807+
let r = Felt252::from_dec_str(r.as_str())
808+
.map_err(|_| D::Error::custom("Invalid Felt252 value"))?;
809+
let s = Felt252::from_dec_str(s.as_str())
810+
.map_err(|_| D::Error::custom("Invalid Felt252 value"))?;
811+
Ok((addr, (r, s)))
812+
})
813+
.collect::<Result<BTreeMap<_, _>, D::Error>>()
814814
}
815815
}
816816

vm/src/vm/runners/cairo_runner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ impl CairoRunner {
10721072
.unwrap_or(self.vm.current_step);
10731073
let n_memory_holes = self.get_memory_holes()?;
10741074

1075-
let mut builtin_instance_counter = HashMap::new();
1075+
let mut builtin_instance_counter = BTreeMap::new();
10761076
for builtin_runner in &self.vm.builtin_runners {
10771077
builtin_instance_counter.insert(
10781078
builtin_runner.name(),
@@ -1083,7 +1083,7 @@ impl CairoRunner {
10831083
Ok(ExecutionResources {
10841084
n_steps,
10851085
n_memory_holes,
1086-
builtin_instance_counter: builtin_instance_counter.into_iter().collect(),
1086+
builtin_instance_counter,
10871087
})
10881088
}
10891089

0 commit comments

Comments
 (0)