Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Program::new_for_proof #1396

Merged
merged 13 commits into from
Aug 28, 2023
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
build-programs:
strategy:
matrix:
# NOTE: we build cairo_proof_programs so clippy can check the benchmarks too
# NOTE: we build cairo_bench_programs so clippy can check the benchmarks too
program-target: [
cairo_bench_programs,
cairo_proof_programs,
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* feat: Add `Program::new_for_proof` [#1396](https://github.com/lambdaclass/cairo-vm/pull/1396)

* BREAKING: Add `disable_trace_padding` to `CairoRunConfig`[#1233](https://github.com/lambdaclass/cairo-rs/pull/1233)

* feat: Implement `CairoRunner.get_cairo_pie`[#1375](https://github.com/lambdaclass/cairo-vm/pull/1375/files)
Expand Down
146 changes: 135 additions & 11 deletions vm/src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,9 @@ impl Program {
error_message_attributes: Vec<Attribute>,
instruction_locations: Option<HashMap<usize, InstructionLocation>>,
) -> Result<Program, ProgramError> {
let mut constants = HashMap::new();
for (key, value) in identifiers.iter() {
if value.type_.as_deref() == Some("const") {
let value = value
.value
.clone()
.ok_or_else(|| ProgramError::ConstWithoutValue(key.clone()))?;
constants.insert(key.clone(), value);
}
}
let hints: BTreeMap<_, _> = hints.into_iter().collect();
let constants = Self::extract_constants(&identifiers)?;

let hints: BTreeMap<_, _> = hints.into_iter().collect();
let hints_collection = HintsCollection::new(&hints, data.len())?;

let shared_program_data = SharedProgramData {
Expand All @@ -213,6 +204,40 @@ impl Program {
builtins,
})
}
#[allow(clippy::too_many_arguments)]
pub fn new_for_proof(
builtins: Vec<BuiltinName>,
data: Vec<MaybeRelocatable>,
start: usize,
end: usize,
hints: HashMap<usize, Vec<HintParams>>,
reference_manager: ReferenceManager,
identifiers: HashMap<String, Identifier>,
error_message_attributes: Vec<Attribute>,
instruction_locations: Option<HashMap<usize, InstructionLocation>>,
) -> Result<Program, ProgramError> {
let constants = Self::extract_constants(&identifiers)?;

let hints: BTreeMap<_, _> = hints.into_iter().collect();
let hints_collection = HintsCollection::new(&hints, data.len())?;

let shared_program_data = SharedProgramData {
data,
main: None,
start: Some(start),
end: Some(end),
hints_collection,
error_message_attributes,
instruction_locations,
identifiers,
reference_manager: Self::get_reference_list(&reference_manager),
};
Ok(Self {
shared_program_data: Arc::new(shared_program_data),
constants,
builtins,
})
}

#[cfg(feature = "std")]
pub fn from_file(path: &Path, entrypoint: Option<&str>) -> Result<Program, ProgramError> {
Expand Down Expand Up @@ -279,6 +304,22 @@ impl Program {
.collect()
}

pub(crate) fn extract_constants(
identifiers: &HashMap<String, Identifier>,
) -> Result<HashMap<String, Felt252>, ProgramError> {
let mut constants = HashMap::new();
for (key, value) in identifiers.iter() {
if value.type_.as_deref() == Some("const") {
let value = value
.value
.clone()
.ok_or_else(|| ProgramError::ConstWithoutValue(key.clone()))?;
constants.insert(key.clone(), value);
}
}
Ok(constants)
}

// Obtains a reduced version of the program
// Doesn't contain hints
// Can be used for verifying execution.
Expand Down Expand Up @@ -425,6 +466,52 @@ mod tests {
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn new_for_proof() {
let reference_manager = ReferenceManager {
references: Vec::new(),
};

let builtins: Vec<BuiltinName> = Vec::new();
let data: Vec<MaybeRelocatable> = vec![
mayberelocatable!(5189976364521848832),
mayberelocatable!(1000),
mayberelocatable!(5189976364521848832),
mayberelocatable!(2000),
mayberelocatable!(5201798304953696256),
mayberelocatable!(2345108766317314046),
];

let program = Program::new_for_proof(
builtins.clone(),
data.clone(),
0,
1,
HashMap::new(),
reference_manager,
HashMap::new(),
Vec::new(),
None,
)
.unwrap();

assert_eq!(program.builtins, builtins);
assert_eq!(program.shared_program_data.data, data);
assert_eq!(program.shared_program_data.main, None);
assert_eq!(program.shared_program_data.start, Some(0));
assert_eq!(program.shared_program_data.end, Some(1));
assert_eq!(program.shared_program_data.identifiers, HashMap::new());
assert_eq!(
program.shared_program_data.hints_collection.hints,
Vec::new()
);
assert_eq!(
program.shared_program_data.hints_collection.hints_ranges,
Vec::new()
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn new_program_with_hints() {
Expand Down Expand Up @@ -563,6 +650,43 @@ mod tests {
);
}

#[test]
fn extract_constants() {
let mut identifiers: HashMap<String, Identifier> = HashMap::new();

identifiers.insert(
String::from("__main__.main"),
Identifier {
pc: Some(0),
type_: Some(String::from("function")),
value: None,
full_name: None,
members: None,
cairo_type: None,
},
);

identifiers.insert(
String::from("__main__.main.SIZEOF_LOCALS"),
Identifier {
pc: None,
type_: Some(String::from("const")),
value: Some(Felt252::zero()),
full_name: None,
members: None,
cairo_type: None,
},
);

assert_eq!(
Program::extract_constants(&identifiers).unwrap(),
[("__main__.main.SIZEOF_LOCALS", Felt252::zero())]
.into_iter()
.map(|(key, value)| (key.to_string(), value))
.collect::<HashMap<_, _>>(),
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_prime() {
Expand Down