Skip to content

Commit

Permalink
Merge branch 'main' into hyper-threading-workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana authored Apr 9, 2024
2 parents fdc4e4c + 15f9dc0 commit a1e2cfc
Show file tree
Hide file tree
Showing 52 changed files with 2,147 additions and 1,064 deletions.
90 changes: 81 additions & 9 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ jobs:
smoke:
needs: merge-caches
name: Make sure all builds work
strategy:
fail-fast: false
matrix:
crate: ["vm", "cairo-vm-cli", "cairo1-run"]
runs-on: ubuntu-22.04
steps:
- name: Install Rust
Expand All @@ -210,6 +214,7 @@ jobs:
uses: taiki-e/install-action@v2
with:
tool: cargo-all-features

- name: Checkout
uses: actions/checkout@v3

Expand All @@ -226,19 +231,86 @@ jobs:
fail-on-cache-miss: true

# NOTE: we do this separately because --workspace operates in weird ways
- name: Check all features (vm)
- name: Check all features (${{ matrix.crate }})
run: |
cd vm
cd ${{ matrix.crate }}
cargo check-all-features
cargo check-all-features --workspace --all-targets
smoke-workspace:
needs: merge-caches
name: Make sure all builds work (workspace)
strategy:
fail-fast: false
matrix:
chunk: [1, 2, 3, 4, 5, 6]
runs-on: ubuntu-22.04
steps:
- name: Install Rust
uses: dtolnay/[email protected]
with:
targets: wasm32-unknown-unknown

- name: Set up cargo cache
uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Install cargo-all-features
uses: taiki-e/install-action@v2
with:
tool: cargo-all-features

- name: Checkout
uses: actions/checkout@v3

- name: Download proof programs symlinks
uses: actions/download-artifact@master
with:
name: proof_programs
path: cairo_programs/proof_programs/

- name: Fetch programs
uses: actions/cache/restore@v3
with:
path: ${{ env.CAIRO_PROGRAMS_PATH }}
key: all-programs-cache-${{ hashFiles('cairo_programs/**/*.cairo', 'examples/wasm-demo/src/array_sum.cairo') }}
fail-on-cache-miss: true

- name: Check all features (CLI)
run: |
cd cairo-vm-cli
cargo check-all-features
- name: Check all features (workspace)
run: |
cargo check-all-features --workspace --all-targets
cargo check-all-features --n-chunks 6 --chunk ${{ matrix.chunk }} --workspace --all-targets
smoke-no-std:
needs: merge-caches
name: Make sure all builds work (no_std)
runs-on: ubuntu-22.04
steps:
- name: Install Rust
uses: dtolnay/[email protected]
with:
targets: wasm32-unknown-unknown

- name: Set up cargo cache
uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Checkout
uses: actions/checkout@v3

- name: Download proof programs symlinks
uses: actions/download-artifact@master
with:
name: proof_programs
path: cairo_programs/proof_programs/

- name: Fetch programs
uses: actions/cache/restore@v3
with:
path: ${{ env.CAIRO_PROGRAMS_PATH }}
key: all-programs-cache-${{ hashFiles('cairo_programs/**/*.cairo', 'examples/wasm-demo/src/array_sum.cairo') }}
fail-on-cache-miss: true

- name: Check no-std
run: |
Expand All @@ -251,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`
* 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
2 changes: 0 additions & 2 deletions bench/criterion_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use cairo_vm::{
};
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};

#[cfg(feature = "with_mimalloc")]
use mimalloc::MiMalloc;

#[cfg(feature = "with_mimalloc")]
#[global_allocator]
static ALLOC: MiMalloc = MiMalloc;

Expand Down
2 changes: 0 additions & 2 deletions bench/iai_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ use cairo_vm::{
vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine},
};

#[cfg(feature = "with_mimalloc")]
use mimalloc::MiMalloc;

#[cfg(feature = "with_mimalloc")]
#[global_allocator]
static ALLOC: MiMalloc = MiMalloc;

Expand Down
5 changes: 3 additions & 2 deletions cairo-vm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ rstest = "0.17.0"

[features]
default = ["with_mimalloc"]
with_mimalloc = ["cairo-vm/with_mimalloc", "dep:mimalloc"]
with_tracer = ["cairo-vm/with_tracer", "cairo-vm-tracer"]
with_mimalloc = ["dep:mimalloc"]
with_tracer = ["cairo-vm/tracer", "cairo-vm-tracer"]
mod_builtin = ["cairo-vm/mod_builtin"]
1 change: 0 additions & 1 deletion cairo-vm-tracer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ readme.workspace = true
default = ["std"]
std = []
alloc = []
tracer = []

[dependencies]
cairo-vm = { workspace = true, features = ["arbitrary", "std"] }
Expand Down
2 changes: 1 addition & 1 deletion cairo1-run/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ num-traits = { version = "0.2", default-features = false }

[features]
default = ["with_mimalloc"]
with_mimalloc = ["cairo-vm/with_mimalloc", "dep:mimalloc"]
with_mimalloc = ["dep:mimalloc"]
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 ();
}
Loading

0 comments on commit a1e2cfc

Please sign in to comment.