-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
3,194 additions
and
17 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
stwo_cairo_prover/crates/prover/src/input/builtin_segments.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::collections::HashMap; | ||
|
||
use cairo_vm::air_public_input::MemorySegmentAddresses; | ||
use cairo_vm::types::builtin_name::BuiltinName; | ||
|
||
/// This struct holds the builtins used in a Cairo program. | ||
#[derive(Debug, Default)] | ||
pub struct BuiltinSegments { | ||
pub add_mod: Option<MemorySegmentAddresses>, | ||
pub bitwise: Option<MemorySegmentAddresses>, | ||
pub ec_op: Option<MemorySegmentAddresses>, | ||
pub ecdsa: Option<MemorySegmentAddresses>, | ||
pub keccak: Option<MemorySegmentAddresses>, | ||
pub mul_mod: Option<MemorySegmentAddresses>, | ||
pub pedersen: Option<MemorySegmentAddresses>, | ||
pub poseidon: Option<MemorySegmentAddresses>, | ||
pub range_check_bits_96: Option<MemorySegmentAddresses>, | ||
pub range_check_bits_128: Option<MemorySegmentAddresses>, | ||
} | ||
|
||
impl BuiltinSegments { | ||
pub fn add_segment( | ||
&mut self, | ||
builtin_name: BuiltinName, | ||
segment: Option<MemorySegmentAddresses>, | ||
) { | ||
match builtin_name { | ||
BuiltinName::range_check => self.range_check_bits_128 = segment, | ||
BuiltinName::pedersen => self.pedersen = segment, | ||
BuiltinName::ecdsa => self.ecdsa = segment, | ||
BuiltinName::keccak => self.keccak = segment, | ||
BuiltinName::bitwise => self.bitwise = segment, | ||
BuiltinName::ec_op => self.ec_op = segment, | ||
BuiltinName::poseidon => self.poseidon = segment, | ||
BuiltinName::range_check96 => self.range_check_bits_96 = segment, | ||
BuiltinName::add_mod => self.add_mod = segment, | ||
BuiltinName::mul_mod => self.mul_mod = segment, | ||
// Not builtins. | ||
BuiltinName::output | BuiltinName::segment_arena => {} | ||
} | ||
} | ||
|
||
/// Creates a new `BuiltinSegments` struct from a map of memory segment names to addresses. | ||
pub fn from_memory_segments(memory_segments: &HashMap<&str, MemorySegmentAddresses>) -> Self { | ||
let mut res = BuiltinSegments::default(); | ||
for (name, value) in memory_segments.iter() { | ||
if let Some(builtin_name) = BuiltinName::from_str(name) { | ||
// Filter empty segments. | ||
let segment = if value.begin_addr == value.stop_ptr { | ||
None | ||
} else { | ||
assert!( | ||
value.begin_addr < value.stop_ptr, | ||
"Invalid segment addresses for builtin: {}", | ||
name | ||
); | ||
Some((value.begin_addr, value.stop_ptr).into()) | ||
}; | ||
res.add_segment(builtin_name, segment); | ||
}; | ||
} | ||
res | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_builtin_segments { | ||
use std::path::PathBuf; | ||
|
||
use cairo_vm::air_public_input::PublicInput; | ||
|
||
use crate::input::BuiltinSegments; | ||
|
||
#[test] | ||
fn test_builtin_segments() { | ||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
.join("src/input/test_builtins_segments/test_public_input.json"); | ||
let pub_data_string = std::fs::read_to_string(&path) | ||
.unwrap_or_else(|_| panic!("Unable to read file: {}", path.display())); | ||
let pub_data: PublicInput<'_> = | ||
sonic_rs::from_str(&pub_data_string).expect("Unable to parse JSON"); | ||
|
||
let builtins_segments = BuiltinSegments::from_memory_segments(&pub_data.memory_segments); | ||
assert_eq!(builtins_segments.add_mod, None); | ||
assert_eq!(builtins_segments.bitwise, Some((23581, 23901).into())); | ||
assert_eq!(builtins_segments.ec_op, None); | ||
assert_eq!(builtins_segments.ecdsa, None); | ||
assert_eq!(builtins_segments.keccak, None); | ||
assert_eq!(builtins_segments.mul_mod, None); | ||
assert_eq!(builtins_segments.pedersen, None); | ||
assert_eq!(builtins_segments.poseidon, None); | ||
assert_eq!(builtins_segments.range_check_bits_96, None); | ||
assert_eq!( | ||
builtins_segments.range_check_bits_128, | ||
Some((7069, 7187).into()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.