Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* refactor: Move hint code match from execute_hint to compile_hint [#2224](https://github.com/lambdaclass/cairo-vm/pull/2224)

* chore: Pin generic-array version to 0.14.7 or lower. [#2227](https://github.com/lambdaclass/cairo-vm/pull/2227)

* fix: Added `cairo_1_test_contracts` and `cairo_2_test_contracts` as dependencies for `test-extensive_hints` target [#2201](https://github.com/lambdaclass/cairo-vm/pull/2201)
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ benchmark-action: cairo_bench_programs
iai-benchmark-action: cairo_bench_programs
cargo bench --bench iai_benchmark

cairo0-benchmark: cairo_bench_programs
cargo bench --bench cairo0_benchmark --features cairo-0-secp-hints

flamegraph:
cargo flamegraph --root --bench criterion_benchmark -- --bench

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ Run only the `iai_benchmark` benchmark suite with cargo:
cargo bench --bench iai_benchmark
```

Run only the `cairo0_benchmark` benchmark suite with cargo:

```bash
cargo bench --bench cairo0_benchmark --features cairo-0-secp-hints
```

Benchmark the `cairo-vm` in a hyper-threaded environment with the [`examples/hyper_threading/ crate`](examples/hyper_threading/)
```bash
make hyper-threading-benchmarks
Expand Down
43 changes: 43 additions & 0 deletions bench/cairo0_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use cairo_vm::{
cairo_run::{cairo_run, CairoRunConfig},
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,
types::layout_name::LayoutName,
};
use criterion::{criterion_group, criterion_main, Criterion};

fn repeat_hint_cairo0(c: &mut Criterion) {
let program = include_bytes!("../cairo_programs/benchmarks/repeated_hint_cairo0.json");
let mut hint_processor = BuiltinHintProcessor::new_empty();
c.bench_function("repeat hint cairo 0", |b| {
b.iter(|| {
let _ = cairo_run(
program,
&CairoRunConfig {
layout: LayoutName::all_cairo,
..Default::default()
},
&mut hint_processor,
);
});
});
}

fn repeat_hint(c: &mut Criterion) {
let program = include_bytes!("../cairo_programs/benchmarks/repeated_hint.json");
let mut hint_processor = BuiltinHintProcessor::new_empty();
c.bench_function("repeat hint", |b| {
b.iter(|| {
let _ = cairo_run(
program,
&CairoRunConfig {
layout: LayoutName::all_cairo,
..Default::default()
},
&mut hint_processor,
);
});
});
}

criterion_group!(runner, repeat_hint_cairo0, repeat_hint);
criterion_main!(runner);
2 changes: 1 addition & 1 deletion cairo-vm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ readme.workspace = true
keywords.workspace = true

[dependencies]
cairo-vm = { workspace = true, features = ["std", "clap"] }
cairo-vm = { workspace = true, features = ["std", "clap", "test_utils"] }
cairo-vm-tracer = { workspace = true, optional = true }
clap = { version = "4.3.10", features = ["derive"] }
mimalloc = { version = "0.1.37", default-features = false, optional = true }
Expand Down
51 changes: 51 additions & 0 deletions cairo_programs/benchmarks/repeated_hint.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
%builtins range_check

from starkware.cairo.common.uint256 import Uint256

const P_low = 201385395114098847380338600778089168199;
const P_high = 64323764613183177041862057485226039389;

struct Uint512 {
d0: felt,
d1: felt,
d2: felt,
d3: felt,
}

func inv_mod_p_uint512{range_check_ptr}(x: Uint512) -> Uint256 {
alloc_locals;
local x_inverse_mod_p: Uint256;
local p: Uint256 = Uint256(P_low, P_high);
// To whitelist
%{
def pack_512(u, num_bits_shift: int) -> int:
limbs = (u.d0, u.d1, u.d2, u.d3)
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs))

x = pack_512(ids.x, num_bits_shift = 128)
p = ids.p.low + (ids.p.high << 128)
x_inverse_mod_p = pow(x,-1, p)

x_inverse_mod_p_split = (x_inverse_mod_p & ((1 << 128) - 1), x_inverse_mod_p >> 128)

ids.x_inverse_mod_p.low = x_inverse_mod_p_split[0]
ids.x_inverse_mod_p.high = x_inverse_mod_p_split[1]
%}

return x_inverse_mod_p;
}

func recursive_hint{range_check_ptr: felt}(n: felt) {
if (n == 100000) {
return ();
}
let x = Uint512(101, 2, 15, 61);
let y = inv_mod_p_uint512(x);

return recursive_hint(n + 1);
}

func main{range_check_ptr: felt}() {
recursive_hint(0);
return ();
}
52 changes: 52 additions & 0 deletions cairo_programs/benchmarks/repeated_hint_cairo0.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
%builtins range_check

from starkware.cairo.common.secp256r1.ec import (
EcPoint,
)
from starkware.cairo.common.secp256r1.bigint import nondet_bigint3
from starkware.cairo.common.cairo_secp.bigint3 import BigInt3

func main{range_check_ptr: felt}() {
recursive_hint(0);

return ();
}

func recursive_hint{range_check_ptr}(n: felt) {
if (n == 100000) {
return ();
}

let y = BigInt3(-1,-2,-3);
let z = try_get_point_from_x_prime(y, 0);

return recursive_hint(n + 1);
}

func try_get_point_from_x_prime{range_check_ptr}(x: BigInt3, v: felt) -> BigInt3 {
%{
from starkware.cairo.common.cairo_secp.secp_utils import SECP256R1, pack
from starkware.python.math_utils import y_squared_from_x

y_square_int = y_squared_from_x(
x=pack(ids.x, PRIME),
alpha=SECP256R1.alpha,
beta=SECP256R1.beta,
field_prime=SECP256R1.prime,
)

# Note that (y_square_int ** ((SECP256R1.prime + 1) / 4)) ** 2 =
# = y_square_int ** ((SECP256R1.prime + 1) / 2) =
# = y_square_int ** ((SECP256R1.prime - 1) / 2 + 1) =
# = y_square_int * y_square_int ** ((SECP256R1.prime - 1) / 2) = y_square_int * {+/-}1.
y = pow(y_square_int, (SECP256R1.prime + 1) // 4, SECP256R1.prime)

# We need to decide whether to take y or prime - y.
if ids.v % 2 == y % 2:
value = y
else:
value = (-y) % SECP256R1.prime
%}
let (y: BigInt3) = nondet_bigint3();
return y;
}
5 changes: 5 additions & 0 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ path = "../bench/criterion_benchmark.rs"
name = "criterion_benchmark"
harness = false

[[bench]]
path = "../bench/cairo0_benchmark.rs"
name = "cairo0_benchmark"
harness = false

[[example]]
name = "custom_hint"
path = "../examples/custom_hint/src/main.rs"
Expand Down
3 changes: 3 additions & 0 deletions vm/src/hint_processor/builtin_hint_processor/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn bigint_pack_div_mod_hint(
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let p: BigInt = BigInt3::from_var_name("P", vm, ids_data, ap_tracking)?.pack86();

Expand Down Expand Up @@ -76,6 +77,7 @@ pub fn bigint_safe_div_hint(
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let res = exec_scopes.get::<BigInt>("res")?;
let y = exec_scopes.get::<BigInt>("y")?;
Expand Down Expand Up @@ -103,6 +105,7 @@ mod test {
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::hint_processor::hint_processor_definition::{HintProcessorLogic, HintReference};
use crate::serde::deserialize_program::ApTracking;
use crate::types::exec_scope::ExecutionScopes;
use crate::utils::test_utils::*;
use assert_matches::assert_matches;
Expand Down
17 changes: 17 additions & 0 deletions vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::hint_processor::hint_processor_utils::felt_to_usize;
use crate::stdlib::{borrow::Cow, collections::HashMap, prelude::*};

use crate::types::errors::math_errors::MathError;
use crate::types::exec_scope::ExecutionScopes;
use crate::Felt252;
use crate::{
hint_processor::{
Expand Down Expand Up @@ -67,8 +68,10 @@ fn compute_blake2s_func(vm: &mut VirtualMachine, output_ptr: Relocatable) -> Res
*/
pub fn compute_blake2s(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let output = get_ptr_from_var_name("output", vm, ids_data, ap_tracking)?;
compute_blake2s_func(vm, output)
Expand Down Expand Up @@ -98,8 +101,10 @@ pub fn compute_blake2s(
*/
pub fn finalize_blake2s(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
const N_PACKED_INSTANCES: usize = 7;
let blake2s_ptr_end = get_ptr_from_var_name("blake2s_ptr_end", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -146,8 +151,10 @@ pub fn finalize_blake2s(
*/
pub fn finalize_blake2s_v3(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
const N_PACKED_INSTANCES: usize = 7;
let blake2s_ptr_end = get_ptr_from_var_name("blake2s_ptr_end", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -178,8 +185,10 @@ pub fn finalize_blake2s_v3(
*/
pub fn blake2s_add_uint256(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
//Get variables from ids
let data_ptr = get_ptr_from_var_name("data", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -214,8 +223,10 @@ pub fn blake2s_add_uint256(
*/
pub fn blake2s_add_uint256_bigend(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
//Get variables from ids
let data_ptr = get_ptr_from_var_name("data", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -250,8 +261,10 @@ memory[ap] = (ids.end != ids.packed_values) and (memory[ids.packed_values] < 2**
*/
pub fn is_less_than_63_bits_and_not_end(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let end = get_ptr_from_var_name("end", vm, ids_data, ap_tracking)?;
let packed_values = get_ptr_from_var_name("packed_values", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -282,8 +295,10 @@ for i in range(ids.packed_values_len):
*/
pub fn blake2s_unpack_felts(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let packed_values_len =
get_integer_from_var_name("packed_values_len", vm, ids_data, ap_tracking)?;
Expand Down Expand Up @@ -346,8 +361,10 @@ Note: This hint belongs to the blake2s lib in cario_examples
*/
pub fn example_blake2s_compress(
vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let blake2s_start = get_ptr_from_var_name("blake2s_start", vm, ids_data, ap_tracking)?;
let output = get_ptr_from_var_name("output", vm, ids_data, ap_tracking)?;
Expand Down
Loading
Loading