-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Ignore pesky pie zip files * Add zero segment * Handle zero segment when counting memory holes * Add Changelog entry * Fix macro * First draft of runner & instance_def * Complete struct * Impl initialize_segments + read_n_words_value * Implement read_inputs * Implement read_memory_vars * Impl fill_inputs * Impl fill_offsets * Impl write_n_words_value * Implement fill_value * Implement fill_memory * Integrate mod builtin under feature flag * Add test file * Impl hint * Push hint impl file * Add quick impl of run_additional_security_checks * Some fixes * Fixes * Fix bugs * Temp testing code * Refactor: Use a struct to represent inputs * Refactor: use a struct to represent memory vars * Refactor: Use a constant N_WORDS * Refactor: use fixed N_WORDS size arrays to ensure safe indexing * Store prime as NonZeroFelt * Refactor: Used a fixed-size array for shift_powers * Clippy * Clippy * Small improvments * Refactor: Use only offsets_ptr instead of all inputs in fill_offsets * Refactor(Simplification): Reduce the return type of read_memory_vars to only a b & c as the other values are not used * Fix(Refactor: Use BigUint to represent values as we have to accomodate values up to U384 * Refactor: Optimize fill_offsets loop so clippy does not complain about it * Add a fill_memory wrapper on the vm to avoid cloning the builtin runners * Fixes * Check batch size * Refactors: Reduce duplication in mod_builtin_fill_memory & combine loops in fill_value * Safety: Use saturating sub * Refactor: Avoid creating empty structs in fill_memory * Add integration test * Add error handling + integration tests * Fix bugged shift_powers creation * Improve error formatting * Simplify error string generation * Fix test * Add test using large batch size + hint impl for circuits using large batch size * Add test with large batch size * Fix hint impl + rename module * Improve error handling of run_additional_security_checks * Remove unused method BuiltinRunner::get_memory_accesses * Impl mark_accessed_memory * Mark cells created by fill_memory as accessed during execution * Remove mark_accessed_memory * Finalize zero segment * WIP: Move final_stack to enum impl * Remove unused methods from builtin runners * Remove commented code * Get rid of empty impls in builtin runners * Move get_memory_segment_addresses impl to enum impl * Impl builtin methods * Add placeholder value * Fix cells_per_instance * Remove temp testing code * Run modulo builtin security checks when running builtin security checks * Use realistic value for mod builtin ratio * Draft(private inputs) * Impl air_private_input * Update private input structure * Improve struct compatibility for serialization * Relocate zero_segment_address + Use BtreeMap keep batches sorted * Fix field names * Clippy * Remove unused attribute from modulo module + limit feature gated code * Fix test files EOF * Relocate ptrs when creating mod private input * Remove feature * Update requirements.txt * Update program + add changelog entry * Impl new hints * Adjust recursive_large_output params * Update tests * Remove allow(unused) * Push bench file * Add non-std imports * Remove unused func * Remove dead code * Box error contents to reduce errir size * Avoid needless to_string when reporting errors * Copy common lib modules so that we can run mod builtin tests in CI * Add mod_builtin to special features so we can run the tests in CI * Fix file * Fix get_used_instances for segment arena * Fix test * Add temp fix * Clippy * fmt * Add no-std import * Add no-std import * Push changelog * Push deleted comment * fmt * Fix typo * Compile with proof mode on CI + add air_private_input tests * Run proof mode integration tests * Push symlinks for convenience as these will be moved soon * Fix test * Fix test value * Fix test value * Fix * Use rea data in layoyts + adjust tests accordingly * Update air priv input tests value (Accounting for increased ratios in layout) * Remove feature mod_builtin * Revert "Remove feature mod_builtin" This reverts commit 5cc921c. * Remove dbg print * Add imports
- Loading branch information
Showing
47 changed files
with
2,061 additions
and
1,044 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// This file is a copy of common/modulo.cairo + added structs from common/cairo_builtins.cairo so that we can run modulo programs in CI | ||
from starkware.cairo.common.math import safe_div, unsigned_div_rem | ||
from starkware.cairo.common.registers import get_label_location | ||
|
||
// Represents a 384-bit unsigned integer d0 + 2**96 * d1 + 2**192 * d2 + 2**288 * d3 | ||
// where each di is in [0, 2**96). | ||
struct UInt384 { | ||
d0: felt, | ||
d1: felt, | ||
d2: felt, | ||
d3: felt, | ||
} | ||
|
||
// Specifies the Add and Mul Mod builtins memory structure. | ||
struct ModBuiltin { | ||
// The modulus. | ||
p: UInt384, | ||
// A pointer to input values, the intermediate results and the output. | ||
values_ptr: UInt384*, | ||
// A pointer to offsets inside the values array, defining the circuit. | ||
// The offsets array should contain 3 * n elements. | ||
offsets_ptr: felt*, | ||
// The number of operations to perform. | ||
n: felt, | ||
} | ||
|
||
const BATCH_SIZE = 1; | ||
|
||
// Returns the smallest felt 0 <= q < rc_bound such that x <= q * y. | ||
func div_ceil{range_check_ptr}(x: felt, y: felt) -> felt { | ||
let (q, r) = unsigned_div_rem(x, y); | ||
if (r != 0) { | ||
return q + 1; | ||
} else { | ||
return q; | ||
} | ||
} | ||
|
||
// Fills the first instance of the add_mod and mul_mod builtins and calls the fill_memory hint to | ||
// fill the rest of the instances and the missing values in the values table. | ||
// | ||
// This function uses a hardcoded value of batch_size=8, and asserts the instance definitions use | ||
// the same value. | ||
func run_mod_p_circuit_with_large_batch_size{ | ||
range_check_ptr, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin* | ||
}( | ||
p: UInt384, | ||
values_ptr: UInt384*, | ||
add_mod_offsets_ptr: felt*, | ||
add_mod_n: felt, | ||
mul_mod_offsets_ptr: felt*, | ||
mul_mod_n: felt, | ||
) { | ||
const BATCH_SIZE = 8; | ||
let add_mod_n_instances = div_ceil(add_mod_n, BATCH_SIZE); | ||
assert add_mod_ptr[0] = ModBuiltin( | ||
p=p, | ||
values_ptr=values_ptr, | ||
offsets_ptr=add_mod_offsets_ptr, | ||
n=add_mod_n_instances * BATCH_SIZE, | ||
); | ||
|
||
let mul_mod_n_instances = div_ceil(mul_mod_n, BATCH_SIZE); | ||
assert mul_mod_ptr[0] = ModBuiltin( | ||
p=p, | ||
values_ptr=values_ptr, | ||
offsets_ptr=mul_mod_offsets_ptr, | ||
n=mul_mod_n_instances * BATCH_SIZE, | ||
); | ||
|
||
%{ | ||
from starkware.cairo.lang.builtins.modulo.mod_builtin_runner import ModBuiltinRunner | ||
assert builtin_runners["add_mod_builtin"].instance_def.batch_size == ids.BATCH_SIZE | ||
assert builtin_runners["mul_mod_builtin"].instance_def.batch_size == ids.BATCH_SIZE | ||
ModBuiltinRunner.fill_memory( | ||
memory=memory, | ||
add_mod=(ids.add_mod_ptr.address_, builtin_runners["add_mod_builtin"], ids.add_mod_n), | ||
mul_mod=(ids.mul_mod_ptr.address_, builtin_runners["mul_mod_builtin"], ids.mul_mod_n), | ||
) | ||
%} | ||
|
||
let add_mod_ptr = &add_mod_ptr[add_mod_n_instances]; | ||
let mul_mod_ptr = &mul_mod_ptr[mul_mod_n_instances]; | ||
return (); | ||
} | ||
|
||
// Fills the first instance of the add_mod and mul_mod builtins and calls the fill_memory hint to | ||
// fill the rest of the instances and the missing values in the values table. | ||
// | ||
// This function uses a hardcoded value of batch_size=1, and asserts the instance definitions use | ||
// the same value. | ||
func run_mod_p_circuit{add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*}( | ||
p: UInt384, | ||
values_ptr: UInt384*, | ||
add_mod_offsets_ptr: felt*, | ||
add_mod_n: felt, | ||
mul_mod_offsets_ptr: felt*, | ||
mul_mod_n: felt, | ||
) { | ||
assert add_mod_ptr[0] = ModBuiltin( | ||
p=p, values_ptr=values_ptr, offsets_ptr=add_mod_offsets_ptr, n=add_mod_n | ||
); | ||
|
||
assert mul_mod_ptr[0] = ModBuiltin( | ||
p=p, values_ptr=values_ptr, offsets_ptr=mul_mod_offsets_ptr, n=mul_mod_n | ||
); | ||
|
||
%{ | ||
from starkware.cairo.lang.builtins.modulo.mod_builtin_runner import ModBuiltinRunner | ||
assert builtin_runners["add_mod_builtin"].instance_def.batch_size == 1 | ||
assert builtin_runners["mul_mod_builtin"].instance_def.batch_size == 1 | ||
ModBuiltinRunner.fill_memory( | ||
memory=memory, | ||
add_mod=(ids.add_mod_ptr.address_, builtin_runners["add_mod_builtin"], ids.add_mod_n), | ||
mul_mod=(ids.mul_mod_ptr.address_, builtin_runners["mul_mod_builtin"], ids.mul_mod_n), | ||
) | ||
%} | ||
|
||
let add_mod_ptr = &add_mod_ptr[add_mod_n]; | ||
let mul_mod_ptr = &mul_mod_ptr[mul_mod_n]; | ||
return (); | ||
} |
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,53 @@ | ||
%builtins range_check add_mod mul_mod | ||
// TODO: Import directly from common library once released | ||
from cairo_programs.mod_builtin_feature.common.modulo import ModBuiltin, UInt384, run_mod_p_circuit | ||
// from starkware.common.cairo_builtins import ModBuiltin, UInt384 | ||
// from starkware.cairo.common.modulo import run_mod_p_circuit | ||
from starkware.cairo.common.registers import get_label_location | ||
from starkware.cairo.common.alloc import alloc | ||
|
||
func main{range_check_ptr, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*}() { | ||
alloc_locals; | ||
|
||
let p = UInt384(d0=1, d1=1, d2=0, d3=0); | ||
let x1 = UInt384(d0=1, d1=0, d2=0, d3=0); | ||
let x2 = UInt384(d0=2, d1=1, d2=0, d3=0); | ||
let x3 = UInt384(d0=2, d1=0, d2=0, d3=0); | ||
let res = UInt384(d0=1, d1=0, d2=0, d3=0); | ||
|
||
let (local values_arr: UInt384*) = alloc(); | ||
assert values_arr[0] = x1; | ||
assert values_arr[1] = x2; | ||
assert values_arr[2] = x3; | ||
assert values_arr[7] = res; | ||
|
||
let (local add_mod_offsets_arr: felt*) = alloc(); | ||
assert add_mod_offsets_arr[0] = 0; // x1 | ||
assert add_mod_offsets_arr[1] = 12; // x2 - x1 | ||
assert add_mod_offsets_arr[2] = 4; // x2 | ||
assert add_mod_offsets_arr[3] = 16; // (x2 - x1) * x3 | ||
assert add_mod_offsets_arr[4] = 20; // x1 * x3 | ||
assert add_mod_offsets_arr[5] = 24; // (x2 - x1) * x3 + x1 * x3 | ||
|
||
let (local mul_mod_offsets_arr: felt*) = alloc(); | ||
assert mul_mod_offsets_arr[0] = 12; // x2 - x1 | ||
assert mul_mod_offsets_arr[1] = 8; // x3 | ||
assert mul_mod_offsets_arr[2] = 16; // (x2 - x1) * x3 | ||
assert mul_mod_offsets_arr[3] = 0; // x1 | ||
assert mul_mod_offsets_arr[4] = 8; // x3 | ||
assert mul_mod_offsets_arr[5] = 20; // x1 * x3 | ||
assert mul_mod_offsets_arr[6] = 8; // x3 | ||
assert mul_mod_offsets_arr[7] = 28; // ((x2 - x1) * x3 + x1 * x3) / x3 = x2 mod p | ||
assert mul_mod_offsets_arr[8] = 24; // (x2 - x1) * x3 + x1 * x3 | ||
|
||
run_mod_p_circuit( | ||
p=p, | ||
values_ptr=values_arr, | ||
add_mod_offsets_ptr=add_mod_offsets_arr, | ||
add_mod_n=2, | ||
mul_mod_offsets_ptr=mul_mod_offsets_arr, | ||
mul_mod_n=3, | ||
); | ||
|
||
return (); | ||
} |
53 changes: 53 additions & 0 deletions
53
cairo_programs/mod_builtin_feature/mod_builtin_failure.cairo
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,53 @@ | ||
%builtins range_check add_mod mul_mod | ||
// TODO: Import directly from common library once released | ||
from cairo_programs.mod_builtin_feature.common.modulo import ModBuiltin, UInt384, run_mod_p_circuit | ||
// from starkware.common.cairo_builtins import ModBuiltin, UInt384 | ||
// from starkware.cairo.common.modulo import run_mod_p_circuit | ||
from starkware.cairo.common.registers import get_label_location | ||
from starkware.cairo.common.alloc import alloc | ||
|
||
func main{range_check_ptr, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*}() { | ||
alloc_locals; | ||
|
||
let p = UInt384(d0=1, d1=1, d2=0, d3=0); | ||
let x1 = UInt384(d0=1, d1=0, d2=0, d3=0); | ||
let x2 = UInt384(d0=2, d1=1, d2=0, d3=0); | ||
let x3 = UInt384(d0=2, d1=0, d2=0, d3=0); | ||
let res = UInt384(d0=2, d1=0, d2=0, d3=0); | ||
|
||
let (local values_arr: UInt384*) = alloc(); | ||
assert values_arr[0] = x1; | ||
assert values_arr[1] = x2; | ||
assert values_arr[2] = x3; | ||
assert values_arr[7] = res; | ||
|
||
let (local add_mod_offsets_arr: felt*) = alloc(); | ||
assert add_mod_offsets_arr[0] = 0; // x1 | ||
assert add_mod_offsets_arr[1] = 12; // x2 - x1 | ||
assert add_mod_offsets_arr[2] = 4; // x2 | ||
assert add_mod_offsets_arr[3] = 16; // (x2 - x1) * x3 | ||
assert add_mod_offsets_arr[4] = 20; // x1 * x3 | ||
assert add_mod_offsets_arr[5] = 24; // (x2 - x1) * x3 + x1 * x3 | ||
|
||
let (local mul_mod_offsets_arr: felt*) = alloc(); | ||
assert mul_mod_offsets_arr[0] = 12; // x2 - x1 | ||
assert mul_mod_offsets_arr[1] = 8; // x3 | ||
assert mul_mod_offsets_arr[2] = 16; // (x2 - x1) * x3 | ||
assert mul_mod_offsets_arr[3] = 0; // x1 | ||
assert mul_mod_offsets_arr[4] = 8; // x3 | ||
assert mul_mod_offsets_arr[5] = 20; // x1 * x3 | ||
assert mul_mod_offsets_arr[6] = 8; // x3 | ||
assert mul_mod_offsets_arr[7] = 28; // ((x2 - x1) * x3 + x1 * x3) / x3 = x2 mod p | ||
assert mul_mod_offsets_arr[8] = 24; // (x2 - x1) * x3 + x1 * x3 | ||
|
||
run_mod_p_circuit( | ||
p=p, | ||
values_ptr=values_arr, | ||
add_mod_offsets_ptr=add_mod_offsets_arr, | ||
add_mod_n=2, | ||
mul_mod_offsets_ptr=mul_mod_offsets_arr, | ||
mul_mod_n=3, | ||
); | ||
|
||
return (); | ||
} |
53 changes: 53 additions & 0 deletions
53
cairo_programs/mod_builtin_feature/mod_builtin_large_batch_size.cairo
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,53 @@ | ||
%builtins range_check add_mod mul_mod | ||
// TODO: Import directly from common library once released | ||
from cairo_programs.mod_builtin_feature.common.modulo import ModBuiltin, UInt384, run_mod_p_circuit_with_large_batch_size | ||
// from starkware.common.cairo_builtins import ModBuiltin, UInt384 | ||
// from starkware.cairo.common.modulo import run_mod_p_circuit_with_large_batch_size | ||
from starkware.cairo.common.registers import get_label_location | ||
from starkware.cairo.common.alloc import alloc | ||
|
||
func main{range_check_ptr, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*}() { | ||
alloc_locals; | ||
|
||
let p = UInt384(d0=1, d1=1, d2=0, d3=0); | ||
let x1 = UInt384(d0=1, d1=0, d2=0, d3=0); | ||
let x2 = UInt384(d0=2, d1=1, d2=0, d3=0); | ||
let x3 = UInt384(d0=2, d1=0, d2=0, d3=0); | ||
let res = UInt384(d0=1, d1=0, d2=0, d3=0); | ||
|
||
let (local values_arr: UInt384*) = alloc(); | ||
assert values_arr[0] = x1; | ||
assert values_arr[1] = x2; | ||
assert values_arr[2] = x3; | ||
assert values_arr[7] = res; | ||
|
||
let (local add_mod_offsets_arr: felt*) = alloc(); | ||
assert add_mod_offsets_arr[0] = 0; // x1 | ||
assert add_mod_offsets_arr[1] = 12; // x2 - x1 | ||
assert add_mod_offsets_arr[2] = 4; // x2 | ||
assert add_mod_offsets_arr[3] = 16; // (x2 - x1) * x3 | ||
assert add_mod_offsets_arr[4] = 20; // x1 * x3 | ||
assert add_mod_offsets_arr[5] = 24; // (x2 - x1) * x3 + x1 * x3 | ||
|
||
let (local mul_mod_offsets_arr: felt*) = alloc(); | ||
assert mul_mod_offsets_arr[0] = 12; // x2 - x1 | ||
assert mul_mod_offsets_arr[1] = 8; // x3 | ||
assert mul_mod_offsets_arr[2] = 16; // (x2 - x1) * x3 | ||
assert mul_mod_offsets_arr[3] = 0; // x1 | ||
assert mul_mod_offsets_arr[4] = 8; // x3 | ||
assert mul_mod_offsets_arr[5] = 20; // x1 * x3 | ||
assert mul_mod_offsets_arr[6] = 8; // x3 | ||
assert mul_mod_offsets_arr[7] = 28; // ((x2 - x1) * x3 + x1 * x3) / x3 = x2 mod p | ||
assert mul_mod_offsets_arr[8] = 24; // (x2 - x1) * x3 + x1 * x3 | ||
|
||
run_mod_p_circuit_with_large_batch_size( | ||
p=p, | ||
values_ptr=values_arr, | ||
add_mod_offsets_ptr=add_mod_offsets_arr, | ||
add_mod_n=2, | ||
mul_mod_offsets_ptr=mul_mod_offsets_arr, | ||
mul_mod_n=3, | ||
); | ||
|
||
return (); | ||
} |
Oops, something went wrong.