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

Add mod builtin #1673

Merged
merged 138 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
138 commits
Select commit Hold shift + click to select a range
d9e0fb0
Ignore pesky pie zip files
fmoletta Mar 15, 2024
025c2d4
Add zero segment
fmoletta Mar 15, 2024
8b562ef
Handle zero segment when counting memory holes
fmoletta Mar 15, 2024
0221923
Add Changelog entry
fmoletta Mar 15, 2024
dd577c6
Merge branch 'main' into add-zero-segment
fmoletta Mar 15, 2024
4e0e3a7
Fix macro
fmoletta Mar 15, 2024
8a562c8
Merge branch 'add-zero-segment' of github.com:lambdaclass/cairo-vm in…
fmoletta Mar 15, 2024
57c57e3
First draft of runner & instance_def
fmoletta Mar 15, 2024
d3f403c
Complete struct
fmoletta Mar 15, 2024
28fcdbc
Impl initialize_segments + read_n_words_value
fmoletta Mar 15, 2024
d1f9e9b
Implement read_inputs
fmoletta Mar 15, 2024
c4502ff
Implement read_memory_vars
fmoletta Mar 15, 2024
7cc8544
Impl fill_inputs
fmoletta Mar 18, 2024
9365407
Impl fill_offsets
fmoletta Mar 18, 2024
2064ea7
Impl write_n_words_value
fmoletta Mar 18, 2024
81de0cb
Implement fill_value
fmoletta Mar 18, 2024
ae038b4
Implement fill_memory
fmoletta Mar 18, 2024
c634c15
Integrate mod builtin under feature flag
fmoletta Mar 18, 2024
4b5ff49
Add test file
fmoletta Mar 18, 2024
a1a347a
Impl hint
fmoletta Mar 18, 2024
74bdfb8
Push hint impl file
fmoletta Mar 18, 2024
a081774
Add quick impl of run_additional_security_checks
fmoletta Mar 19, 2024
41739ff
Some fixes
fmoletta Mar 19, 2024
bc84fba
Fixes
fmoletta Mar 19, 2024
c9dc9a1
Fix bugs
fmoletta Mar 19, 2024
f0f85d0
Temp testing code
fmoletta Mar 19, 2024
c35104c
Refactor: Use a struct to represent inputs
fmoletta Mar 19, 2024
599f970
Refactor: use a struct to represent memory vars
fmoletta Mar 19, 2024
a09275d
Refactor: Use a constant N_WORDS
fmoletta Mar 19, 2024
14b9988
Refactor: use fixed N_WORDS size arrays to ensure safe indexing
fmoletta Mar 19, 2024
3c68767
Store prime as NonZeroFelt
fmoletta Mar 19, 2024
fc829a1
Refactor: Used a fixed-size array for shift_powers
fmoletta Mar 19, 2024
a2eb079
Clippy
fmoletta Mar 19, 2024
a5d0a0e
Clippy
fmoletta Mar 19, 2024
2e22046
Merge branch 'add-mod-builtin-runner' of github.com:lambdaclass/cairo…
fmoletta Mar 19, 2024
61a8d5a
Small improvments
fmoletta Mar 19, 2024
7c67516
Refactor: Use only offsets_ptr instead of all inputs in fill_offsets
fmoletta Mar 19, 2024
d3b2dfa
Refactor(Simplification): Reduce the return type of read_memory_vars …
fmoletta Mar 19, 2024
74180f3
Fix(Refactor: Use BigUint to represent values as we have to accomodat…
fmoletta Mar 19, 2024
154ed62
Refactor: Optimize fill_offsets loop so clippy does not complain abou…
fmoletta Mar 19, 2024
2c8cc82
Merge branch 'add-mod-builtin-runner' of github.com:lambdaclass/cairo…
fmoletta Mar 19, 2024
99096c2
Add a fill_memory wrapper on the vm to avoid cloning the builtin runners
fmoletta Mar 20, 2024
dd20cad
Fixes
fmoletta Mar 20, 2024
6f999da
Check batch size
fmoletta Mar 20, 2024
7d2dc20
Refactors: Reduce duplication in mod_builtin_fill_memory & combine lo…
fmoletta Mar 20, 2024
6266c00
Safety: Use saturating sub
fmoletta Mar 20, 2024
6ffbabd
Refactor: Avoid creating empty structs in fill_memory
fmoletta Mar 20, 2024
b7975a6
Add integration test
fmoletta Mar 20, 2024
baacc41
Add error handling + integration tests
fmoletta Mar 20, 2024
ea29015
Fix bugged shift_powers creation
fmoletta Mar 20, 2024
5bf94b4
Improve error formatting
fmoletta Mar 20, 2024
85cbc1b
Simplify error string generation
fmoletta Mar 20, 2024
3ea5910
Fix test
fmoletta Mar 20, 2024
0ba9d5a
Add test using large batch size + hint impl for circuits using large …
fmoletta Mar 20, 2024
9709b22
Add test with large batch size
fmoletta Mar 20, 2024
16d7917
Fix hint impl + rename module
fmoletta Mar 20, 2024
2106e94
Improve error handling of run_additional_security_checks
fmoletta Mar 21, 2024
38483d9
Remove unused method BuiltinRunner::get_memory_accesses
fmoletta Mar 21, 2024
fc43ac7
Impl mark_accessed_memory
fmoletta Mar 21, 2024
7ec7dcd
Mark cells created by fill_memory as accessed during execution
fmoletta Mar 21, 2024
e8fe69d
Remove mark_accessed_memory
fmoletta Mar 21, 2024
8e097a0
Finalize zero segment
fmoletta Mar 21, 2024
e31308d
WIP: Move final_stack to enum impl
fmoletta Mar 21, 2024
206accd
Remove unused methods from builtin runners
fmoletta Mar 21, 2024
fc9bf80
Remove commented code
fmoletta Mar 21, 2024
07606ab
Get rid of empty impls in builtin runners
fmoletta Mar 21, 2024
4da6d62
Move get_memory_segment_addresses impl to enum impl
fmoletta Mar 21, 2024
f6ddacd
Impl builtin methods
fmoletta Mar 21, 2024
49b71cc
Add placeholder value
fmoletta Mar 21, 2024
8c2d02a
Fix cells_per_instance
fmoletta Mar 22, 2024
46b2fc6
Remove temp testing code
fmoletta Mar 22, 2024
501777d
Run modulo builtin security checks when running builtin security checks
fmoletta Mar 22, 2024
86daf47
Use realistic value for mod builtin ratio
fmoletta Mar 22, 2024
8be7f27
Draft(private inputs)
fmoletta Mar 22, 2024
8995e2f
Impl air_private_input
fmoletta Mar 22, 2024
2ee44c3
Update private input structure
fmoletta Mar 22, 2024
798420f
Improve struct compatibility for serialization
fmoletta Mar 22, 2024
1a61d20
Relocate zero_segment_address + Use BtreeMap keep batches sorted
fmoletta Mar 22, 2024
fe4b8d9
Fix field names
fmoletta Mar 22, 2024
ae5b499
Clippy
fmoletta Mar 22, 2024
16fcd66
Remove unused attribute from modulo module + limit feature gated code
fmoletta Mar 22, 2024
579d7ac
Fix test files EOF
fmoletta Mar 22, 2024
58e049e
Relocate ptrs when creating mod private input
fmoletta Mar 25, 2024
ff811c0
Remove feature
fmoletta Mar 25, 2024
6295613
Update requirements.txt
fmoletta Mar 25, 2024
8fa52f2
Update program + add changelog entry
fmoletta Mar 25, 2024
8d6ac20
Impl new hints
fmoletta Mar 25, 2024
5923735
Adjust recursive_large_output params
fmoletta Mar 25, 2024
66be88f
Update tests
fmoletta Mar 25, 2024
55530de
Merge branch 'main' into add-mod-builtin-runner
fmoletta Mar 25, 2024
a75bdac
Remove allow(unused)
fmoletta Mar 25, 2024
01a95e2
Merge branch 'add-mod-builtin-runner' of https://github.com/lambdacla…
fmoletta Mar 25, 2024
b9fef8b
Merge branch 'update-cairo_lang-0.13.1' into add-mod-builtin-runner
fmoletta Mar 25, 2024
c86ce87
Push bench file
fmoletta Mar 26, 2024
b8081ba
Add non-std imports
fmoletta Mar 26, 2024
44528a9
Remove unused func
fmoletta Mar 26, 2024
c345915
Remove dead code
fmoletta Mar 26, 2024
d02c21a
Box error contents to reduce errir size
fmoletta Mar 26, 2024
65894c6
Avoid needless to_string when reporting errors
fmoletta Mar 26, 2024
edfd0fd
Copy common lib modules so that we can run mod builtin tests in CI
fmoletta Mar 26, 2024
434d53a
Add mod_builtin to special features so we can run the tests in CI
fmoletta Mar 26, 2024
c2e6384
Fix file
fmoletta Mar 26, 2024
0b6777a
Fix get_used_instances for segment arena
fmoletta Mar 26, 2024
169a12c
Fix test
fmoletta Mar 26, 2024
d87eb7d
Add temp fix
fmoletta Mar 26, 2024
46a69eb
Clippy
fmoletta Mar 26, 2024
4772c88
fmt
fmoletta Mar 26, 2024
49da224
Add no-std import
fmoletta Mar 26, 2024
e12c1ab
Add no-std import
fmoletta Mar 26, 2024
88330a6
Push changelog
fmoletta Mar 26, 2024
784eb0a
Push deleted comment
fmoletta Mar 26, 2024
2628db1
fmt
fmoletta Mar 26, 2024
448da93
Merge branch 'main' into add-mod-builtin-runner
fmoletta Mar 26, 2024
57868de
Merge branch 'main' of https://github.com/lambdaclass/cairo-vm into a…
fmoletta Mar 27, 2024
9a07ffb
Merge branch 'add-mod-builtin-runner' of https://github.com/lambdacla…
fmoletta Mar 27, 2024
28516b0
Fix typo
fmoletta Mar 27, 2024
79bdc27
Compile with proof mode on CI + add air_private_input tests
fmoletta Mar 27, 2024
302ce5b
Run proof mode integration tests
fmoletta Mar 27, 2024
91c35fc
Push symlinks for convenience as these will be moved soon
fmoletta Mar 27, 2024
0cda56c
Fix test
fmoletta Mar 27, 2024
f7fd9e6
Fix test value
fmoletta Mar 27, 2024
3f58b96
Fix test value
fmoletta Mar 27, 2024
52f5bfd
Merge branch 'main' into add-mod-builtin-runner
fmoletta Apr 4, 2024
8cb80a4
Merge branch 'main' of https://github.com/lambdaclass/cairo-vm into a…
fmoletta Apr 4, 2024
e6307a3
Merge branch 'main' into add-mod-builtin-runner
fmoletta Apr 5, 2024
f613112
Fix
fmoletta Apr 5, 2024
eaa99e1
Merge branch 'main' into add-mod-builtin-runner
fmoletta Apr 8, 2024
779c83b
Use rea data in layoyts + adjust tests accordingly
fmoletta Apr 8, 2024
4e910b8
Update air priv input tests value (Accounting for increased ratios in…
fmoletta Apr 8, 2024
5809cc3
Merge branch 'add-mod-builtin-runner' of https://github.com/lambdacla…
fmoletta Apr 8, 2024
deacbea
Merge branch 'main' into add-mod-builtin-runner
fmoletta Apr 8, 2024
5cc921c
Remove feature mod_builtin
fmoletta Apr 9, 2024
3a621f8
Merge branch 'add-mod-builtin-runner' of https://github.com/lambdacla…
fmoletta Apr 9, 2024
01d4011
Revert "Remove feature mod_builtin"
fmoletta Apr 9, 2024
158cccf
Merge branch 'main' into add-mod-builtin-runner
fmoletta Apr 9, 2024
d4d3c33
Remove dbg print
fmoletta Apr 9, 2024
eaeb175
Merge branch 'add-mod-builtin-runner' of https://github.com/lambdacla…
fmoletta Apr 9, 2024
e80822f
Add imports
fmoletta Apr 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ jobs:
strategy:
fail-fast: false
matrix:
special_features: ["", "extensive_hints"]
special_features: ["", "extensive_hints", "mod_builtin"]
target: [ test#1, test#2, test#3, test#4, test-no_std#1, test-no_std#2, test-no_std#3, test-no_std#4, test-wasm ]
name: Run tests
runs-on: ubuntu-22.04
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

#### Upcoming Changes

* feat(BREAKING): Add mod builtin [#1673](https://github.com/lambdaclass/cairo-vm/pull/1673)

Main Changes:
* Add the new `ModBuiltinRunner`, implementing the builtins `add_mod` & `mul_mod`
* Adds `add_mod` & `mul_mod` to the `all_cairo` & `dynamic` layouts under the `mod_builtin` feature flag. This will be added to the main code in a future update.
* Add method `VirtualMachine::fill_memory` in order to perform the new builtin's main logic from within hints
* Add hints to run arithmetic circuits using `add_mod` and/or `mul_mod` builtins

Other Changes:
* BREAKING: BuiltinRunner method signature change from
`air_private_input(&self, memory: &Memory) -> Vec<PrivateInput>` to `pub fn air_private_input(&self, segments: &MemorySegmentManager) -> Vec<PrivateInput>`
* Add `MayleRelocatable::sub_usize`
Oppen marked this conversation as resolved.
Show resolved Hide resolved
* Implement `Add<u32> for Relocatable`
* Add `Memory::get_usize`
* BREAKING: Clean up unused/duplicated code from builtins module:
* Remove unused method `get_memory_segment_addresses` from all builtin runners & the enum
* Remove empty implementations of `deduce_memory_cell` & `add_validation_rules` from all builtin runners
* Remove duplicated implementation of `final_stack` from all builtin runners except output and move it to the enum implementation

* bugfix(BREAKING): Handle off2 immediate case in `get_integer_from_reference`[#1701](https://github.com/lambdaclass/cairo-vm/pull/1701)
* `get_integer_from_reference` & `get_integer_from_var_name` output changed from `Result<Cow<'a, Felt252>, HintError>` to `Result<Felt252, HintError>`

Expand Down
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ PROOF_BENCH_DIR=cairo_programs/benchmarks
PROOF_BENCH_FILES:=$(wildcard $(PROOF_BENCH_DIR)/*.cairo)
PROOF_COMPILED_BENCHES:=$(patsubst $(PROOF_BENCH_DIR)/%.cairo, $(PROOF_BENCH_DIR)/%.json, $(PROOF_BENCH_FILES))

MOD_BUILTIN_TEST_PROOF_DIR=cairo_programs/mod_builtin_feature/proof
MOD_BUILTIN_TEST_PROOF_FILES:=$(wildcard $(MOD_BUILTIN_TEST_PROOF_DIR)/*.cairo)
COMPILED_MOD_BUILTIN_PROOF_TESTS:=$(patsubst $(MOD_BUILTIN_TEST_PROOF_DIR)/%.cairo, $(MOD_BUILTIN_TEST_PROOF_DIR)/%.json, $(MOD_BUILTIN_TEST_PROOF_FILES))

$(TEST_PROOF_DIR)/%.json: $(TEST_PROOF_DIR)/%.cairo
cairo-compile --cairo_path="$(TEST_PROOF_DIR):$(PROOF_BENCH_DIR)" $< --output $@ --proof_mode

Expand All @@ -61,6 +65,9 @@ $(TEST_PROOF_DIR)/%.trace $(TEST_PROOF_DIR)/%.memory $(TEST_PROOF_DIR)/%.air_pub
$(PROOF_BENCH_DIR)/%.json: $(PROOF_BENCH_DIR)/%.cairo
cairo-compile --cairo_path="$(TEST_PROOF_DIR):$(PROOF_BENCH_DIR)" $< --output $@ --proof_mode

$(MOD_BUILTIN_TEST_PROOF_DIR)/%.json: $(MOD_BUILTIN_TEST_PROOF_DIR)/%.cairo
cairo-compile --cairo_path="$(MOD_BUILTIN_TEST_PROOF_DIR):$(MOD_BUILTIN_TEST_PROOF_DIR)" $< --output $@ --proof_mode

# ======================
# Run without proof mode
# ======================
Expand All @@ -87,6 +94,10 @@ PRINT_TEST_DIR=cairo_programs/print_feature
PRINT_TEST_FILES:=$(wildcard $(PRINT_TEST_DIR)/*.cairo)
COMPILED_PRINT_TESTS:=$(patsubst $(PRINT_TEST_DIR)/%.cairo, $(PRINT_TEST_DIR)/%.json, $(PRINT_TEST_FILES))

MOD_BUILTIN_TEST_DIR=cairo_programs/mod_builtin_feature
MOD_BUILTIN_TEST_FILES:=$(wildcard $(MOD_BUILTIN_TEST_DIR)/*.cairo)
COMPILED_MOD_BUILTIN_TESTS:=$(patsubst $(MOD_BUILTIN_TEST_DIR)/%.cairo, $(MOD_BUILTIN_TEST_DIR)/%.json, $(MOD_BUILTIN_TEST_FILES))

NORETROCOMPAT_DIR:=cairo_programs/noretrocompat
NORETROCOMPAT_FILES:=$(wildcard $(NORETROCOMPAT_DIR)/*.cairo)
COMPILED_NORETROCOMPAT_TESTS:=$(patsubst $(NORETROCOMPAT_DIR)/%.cairo, $(NORETROCOMPAT_DIR)/%.json, $(NORETROCOMPAT_FILES))
Expand Down Expand Up @@ -228,8 +239,8 @@ run:
check:
cargo check

cairo_test_programs: $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) $(COMPILED_PRINT_TESTS)
cairo_proof_programs: $(COMPILED_PROOF_TESTS)
cairo_test_programs: $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) $(COMPILED_PRINT_TESTS) $(COMPILED_MOD_BUILTIN_TESTS)
cairo_proof_programs: $(COMPILED_PROOF_TESTS) $(COMPILED_MOD_BUILTIN_PROOF_TESTS)
cairo_bench_programs: $(COMPILED_BENCHES)
cairo_1_test_contracts: $(CAIRO_1_COMPILED_CASM_CONTRACTS)
cairo_2_test_contracts: $(CAIRO_2_COMPILED_CASM_CONTRACTS)
Expand Down
3 changes: 1 addition & 2 deletions cairo-vm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ rstest = "0.17.0"

[features]
default = ["with_mimalloc"]

with_mimalloc = ["dep:mimalloc"]
with_tracer = ["cairo-vm/tracer", "cairo-vm-tracer"]

mod_builtin = ["cairo-vm/mod_builtin"]
Oppen marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion cairo1-run/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,11 @@ fn create_entry_code(
BuiltinName::ec_op => EcOpType::ID,
BuiltinName::poseidon => PoseidonType::ID,
BuiltinName::segment_arena => SegmentArenaType::ID,
BuiltinName::keccak | BuiltinName::ecdsa | BuiltinName::output => return fp_loc,
BuiltinName::keccak
| BuiltinName::ecdsa
| BuiltinName::output
| BuiltinName::add_mod
| BuiltinName::mul_mod => return fp_loc,
};
signature
.ret_types
Expand Down
124 changes: 124 additions & 0 deletions cairo_programs/mod_builtin_feature/common/modulo.cairo
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 ();
}
53 changes: 53 additions & 0 deletions cairo_programs/mod_builtin_feature/mod_builtin.cairo
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 cairo_programs/mod_builtin_feature/mod_builtin_failure.cairo
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 ();
}
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 ();
}
Loading
Loading