Skip to content

Commit 7d19956

Browse files
JulianGCalderonAlon-TiyuvalswOmriEshhar1
authoredSep 30, 2024··
Fix modulo builtin constraints (#1841)
* Fix Zero segment location. * Fixed has_zero_segment naming * Fix prover input. * Fixed version reading when no version is supplied * Added change to changelog. * fix test_from_serializable() * fix panic_impl error * fix cairo version * Add dummy changelog * Pin wasm-bindgen * Register change in CHANGELOG * Update Cargo.lock * Remove changes from CHANGELOG * Add argument parsing for layout params file * Add dynamic support (no implement) * Add cairo_layout_params_file.example.json * Implement dynamic layout creation * Update CHANGELOG * Add cli dynamic support for cairo 1 * Make wasm compatible * Use public_memory_fraction = 4 vy default * Deserialize bool from int * Add comparison with python-vm (failing) * Rebuild .rs files in makefile * Use 8 as dynamic public_memory_fraction The same value is used in python-vm * Use None ratio for dynamic unused builtins * Add rangecheck96 to private inputs * Make dyn py files depend on params_file * Use cpu_component_step=1 by default * Fix typo in private inputs * Add range check value to air private input test * Fix zero segment location * Use zero builtin instead of None * Add debug scripts * Remove dup makefile recipes * remove outdated test * Enable ensure-no_std on test * Fix tests * Add correct test * Rename tset * Add comment * Add debugging document * Update cairo layout params file * Remove duplicated range check * Remove dup * Remove debugging and scrippts (moveed to another branch) * Add comment * Add tests * Add dynamic test to cairo-vm-cli * Add parse test * Remove compare all dynamic * Add script for comparing with dynamic layouts * Add tests to workflow * Delete logic changes They are going to be moved to another branch * Delete more logic changes * Update rust.yml * Rename compare_outputs_dynamic_layout.sh to compare_outputs_dynamic_layouts.sh * Update test script * Enforce prover constraints in add, sub, and mul * Remove debug prints * Add tests * Update CHANGELOG.md * Fix serialization * Comment failing test * Uncomment test * Fix tests * Remove operation struct and use builtin type instead * Add unit tests to modulo builtin operations * Fix security error message * Test custom serde impl * Upload mod_builtin coverage --------- Co-authored-by: Alon Titelman <[email protected]> Co-authored-by: Yuval Goldberg <[email protected]> Co-authored-by: Omri Eshhar <[email protected]>
1 parent 14ec3e3 commit 7d19956

File tree

5 files changed

+306
-75
lines changed

5 files changed

+306
-75
lines changed
 

‎.github/workflows/rust.yml

+30
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,36 @@ jobs:
600600
key: codecov-cache-test-no_std-extensive_hints-${{ github.sha }}
601601
fail-on-cache-miss: true
602602

603+
- name: Fetch results for tests with stdlib (w/mod_builtin; part. 1)
604+
uses: actions/cache/restore@v3
605+
with:
606+
path: lcov-test#1-mod_builtin.info
607+
key: codecov-cache-test#1-mod_builtin-${{ github.sha }}
608+
fail-on-cache-miss: true
609+
- name: Fetch results for tests with stdlib (w/mod_builtin; part. 2)
610+
uses: actions/cache/restore@v3
611+
with:
612+
path: lcov-test#2-mod_builtin.info
613+
key: codecov-cache-test#2-mod_builtin-${{ github.sha }}
614+
fail-on-cache-miss: true
615+
- name: Fetch results for tests with stdlib (w/mod_builtin; part. 3)
616+
uses: actions/cache/restore@v3
617+
with:
618+
path: lcov-test#3-mod_builtin.info
619+
key: codecov-cache-test#3-mod_builtin-${{ github.sha }}
620+
fail-on-cache-miss: true
621+
- name: Fetch results for tests with stdlib (w/mod_builtin; part. 4)
622+
uses: actions/cache/restore@v3
623+
with:
624+
path: lcov-test#4-mod_builtin.info
625+
key: codecov-cache-test#4-mod_builtin-${{ github.sha }}
626+
fail-on-cache-miss: true
627+
- name: Fetch results for tests without stdlib (w/mod_builtin)
628+
uses: actions/cache/restore@v3
629+
with:
630+
path: lcov-no_std-mod_builtin.info
631+
key: codecov-cache-test-no_std-mod_builtin-${{ github.sha }}
632+
fail-on-cache-miss: true
603633

604634
- name: Upload coverage to codecov.io
605635
uses: codecov/codecov-action@v3

‎CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#### Upcoming Changes
44

5+
* fix: [#1841](https://github.com/lambdaclass/cairo-vm/pull/1841):
6+
* Fix modulo builtin to comply with prover constraints
7+
58
* feat(BREAKING): [#1824](https://github.com/lambdaclass/cairo-vm/pull/1824):
69
* Add support for dynamic layout
710
* CLI change(BREAKING): The flag `cairo_layout_params_file` must be specified when using dynamic layout.

‎vm/src/air_private_input.rs

+84
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ pub struct ModInputInstance {
126126
pub values_ptr: usize,
127127
pub offsets_ptr: usize,
128128
pub n: usize,
129+
#[serde(deserialize_with = "mod_input_instance_batch_serde::deserialize")]
130+
#[serde(serialize_with = "mod_input_instance_batch_serde::serialize")]
129131
pub batch: BTreeMap<usize, ModInputMemoryVars>,
130132
}
131133

@@ -205,6 +207,88 @@ impl AirPrivateInputSerializable {
205207
}
206208
}
207209

210+
mod mod_input_instance_batch_serde {
211+
use super::*;
212+
213+
use serde::{Deserializer, Serializer};
214+
215+
pub(crate) fn serialize<S: Serializer>(
216+
value: &BTreeMap<usize, ModInputMemoryVars>,
217+
s: S,
218+
) -> Result<S::Ok, S::Error> {
219+
let value = value.iter().map(|v| v.1).collect::<Vec<_>>();
220+
221+
value.serialize(s)
222+
}
223+
224+
pub(crate) fn deserialize<'de, D: Deserializer<'de>>(
225+
d: D,
226+
) -> Result<BTreeMap<usize, ModInputMemoryVars>, D::Error> {
227+
let value = Vec::<ModInputMemoryVars>::deserialize(d)?;
228+
229+
Ok(value.into_iter().enumerate().collect())
230+
}
231+
232+
#[cfg(feature = "std")]
233+
#[test]
234+
fn test_serde() {
235+
let input_value = vec![
236+
(
237+
0,
238+
ModInputMemoryVars {
239+
a_offset: 5,
240+
b_offset: 5,
241+
c_offset: 5,
242+
a0: Felt252::from(5u32),
243+
a1: Felt252::from(5u32),
244+
a2: Felt252::from(5u32),
245+
a3: Felt252::from(5u32),
246+
b0: Felt252::from(5u32),
247+
b1: Felt252::from(5u32),
248+
b2: Felt252::from(5u32),
249+
b3: Felt252::from(5u32),
250+
c0: Felt252::from(5u32),
251+
c1: Felt252::from(5u32),
252+
c2: Felt252::from(5u32),
253+
c3: Felt252::from(5u32),
254+
},
255+
),
256+
(
257+
1,
258+
ModInputMemoryVars {
259+
a_offset: 7,
260+
b_offset: 7,
261+
c_offset: 7,
262+
a0: Felt252::from(7u32),
263+
a1: Felt252::from(7u32),
264+
a2: Felt252::from(7u32),
265+
a3: Felt252::from(7u32),
266+
b0: Felt252::from(7u32),
267+
b1: Felt252::from(7u32),
268+
b2: Felt252::from(7u32),
269+
b3: Felt252::from(7u32),
270+
c0: Felt252::from(7u32),
271+
c1: Felt252::from(7u32),
272+
c2: Felt252::from(7u32),
273+
c3: Felt252::from(7u32),
274+
},
275+
),
276+
]
277+
.into_iter()
278+
.collect::<BTreeMap<usize, _>>();
279+
280+
let bytes = Vec::new();
281+
let mut serializer = serde_json::Serializer::new(bytes);
282+
serialize(&input_value, &mut serializer).unwrap();
283+
let bytes = serializer.into_inner();
284+
285+
let mut deserializer = serde_json::Deserializer::from_slice(&bytes);
286+
let output_value = deserialize(&mut deserializer).unwrap();
287+
288+
assert_eq!(input_value, output_value);
289+
}
290+
}
291+
208292
#[cfg(test)]
209293
mod tests {
210294
use crate::types::layout_name::LayoutName;

‎vm/src/tests/compare_outputs_dynamic_layouts.sh

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ CASES=(
8282
"cairo_programs/proof_programs/sha256.json;double_all_cairo"
8383
"cairo_programs/proof_programs/keccak.json;all_cairo"
8484
"cairo_programs/proof_programs/keccak.json;double_all_cairo"
85+
"cairo_programs/mod_builtin_feature/proof/mod_builtin.json;all_cairo"
86+
"cairo_programs/mod_builtin_feature/proof/mod_builtin_failure.json;all_cairo"
87+
"cairo_programs/mod_builtin_feature/proof/apply_poly.json;all_cairo"
8588
)
8689

8790
passed_tests=0

0 commit comments

Comments
 (0)
Please sign in to comment.