Skip to content

Commit

Permalink
Merge branch '0.11' into changes-layout-cairo-lang-0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrigada committed Mar 10, 2023
2 parents c3bb82b + 3366594 commit 489476d
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 47 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 27 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,33 @@ hooks = []

[dependencies]
mimalloc = { version = "0.1.29", default-features = false, optional = true }
num-bigint = { version = "0.4", features = ["serde"] }
num-traits = "0.2"
num-integer = "0.1.45"
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11.1"
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
hex = "0.4.3"
bincode = "1.2.1"
starknet-crypto = "0.2.0"
clap = { version = "3.2.5", features = ["derive"] }
sha3 = "0.10.1"
rand_core = "0.6.4"
lazy_static = "1.4.0"
nom = "7"
sha2 = { version = "0.10.2", features = ["compress"] }
thiserror = "1.0.32"
generic-array = "0.14.6"
keccak = "0.1.2"
num-bigint = { version = "0.4", features = ["serde"], default-features = false }
num-traits = { version = "0.2", default-features = false }
num-integer = { version = "0.1.45", default-features = false }
serde = { version = "1.0", features = ["derive"], default-features = false }
serde_bytes = { version = "0.11.9", default-features = false }
serde_json = { version = "1.0", features = [
"arbitrary_precision",
], default-features = false }
hex = { version = "0.4.3", default-features = false }
bincode = { tag = "v2.0.0-rc.2", git = "https://github.com/bincode-org/bincode.git", default-features = false, features = [
"serde",
] }
starknet-crypto = { version = "0.4.1", default-features = false, features = ["signature-display"] }
sha3 = { version = "0.10.1", default-features = false }
rand_core = { version = "0.6.4", default-features = false }
lazy_static = { version = "1.4.0", default-features = false, features = [
"spin_no_std",
] }
nom = { version = "7", default-features = false }
sha2 = { version = "0.10.2", features = ["compress"], default-features = false }
generic-array = { version = "0.14.6", default-features = false }
keccak = { version = "0.1.2", default-features = false }
hashbrown = { version = "0.13.2", features = ["serde"] }
anyhow = { version = "1.0.69", default-features = false }
thiserror = { version = "1.0.32", default-features = false }
thiserror-no-std = "2.0.2"

# This crate has only one function `take_until_unbalanced` that is
# very useful for our parsing purposes:
# https://stackoverflow.com/questions/70630556/parse-allowing-nested-parentheses-in-nom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
add_segment, enter_scope, exit_scope, memcpy_continue_copying, memcpy_enter_scope,
},
memset_utils::{memset_continue_loop, memset_enter_scope},
poseidon_utils::{n_more_than_10, n_more_than_2},
poseidon_utils::{n_greater_than_10, n_greater_than_2},
pow_utils::pow,
secp::{
bigint_utils::{bigint_to_uint256, nondet_bigint3},
Expand Down Expand Up @@ -438,11 +438,11 @@ impl HintProcessor for BuiltinHintProcessor {
hint_code::VERIFY_ECDSA_SIGNATURE => {
verify_ecdsa_signature(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::NONDET_N_MORE_THAN_10 => {
n_more_than_10(vm, &hint_data.ids_data, &hint_data.ap_tracking)
hint_code::NONDET_N_GREATER_THAN_10 => {
n_greater_than_10(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::NONDET_N_MORE_THAN_2 => {
n_more_than_2(vm, &hint_data.ids_data, &hint_data.ap_tracking)
hint_code::NONDET_N_GREATER_THAN_2 => {
n_greater_than_2(vm, &hint_data.ids_data, &hint_data.ap_tracking)
}
#[cfg(feature = "skip_next_instruction_hint")]
hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm),
Expand Down
5 changes: 3 additions & 2 deletions src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ pub(crate) const TEMPORARY_ARRAY: &str = r#"ids.temporary_array = segments.add_t
pub(crate) const VERIFY_ECDSA_SIGNATURE: &str =
r#"ecdsa_builtin.add_signature(ids.ecdsa_ptr.address_, (ids.signature_r, ids.signature_s))"#;

pub(crate) const NONDET_N_MORE_THAN_10: &str = "memory[ap] = to_felt_or_relocatable(ids.n >= 10)";
pub(crate) const NONDET_N_MORE_THAN_2: &str = "memory[ap] = to_felt_or_relocatable(ids.n >= 2)";
pub(crate) const NONDET_N_GREATER_THAN_10: &str =
"memory[ap] = to_felt_or_relocatable(ids.n >= 10)";
pub(crate) const NONDET_N_GREATER_THAN_2: &str = "memory[ap] = to_felt_or_relocatable(ids.n >= 2)";
#[cfg(feature = "skip_next_instruction_hint")]
pub(crate) const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()";
28 changes: 17 additions & 11 deletions src/hint_processor/builtin_hint_processor/poseidon_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
use super::hint_utils::{get_integer_from_var_name, insert_value_into_ap};

// Implements hint: "memory[ap] = to_felt_or_relocatable(ids.n >= 10)"
pub fn n_more_than_10(
pub fn n_greater_than_10(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
Expand All @@ -28,7 +28,7 @@ pub fn n_more_than_10(
}

// Implements hint: "memory[ap] = to_felt_or_relocatable(ids.n >= 2)"
pub fn n_more_than_2(
pub fn n_greater_than_2(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
Expand Down Expand Up @@ -59,11 +59,14 @@ mod tests {
use crate::vm::vm_memory::memory_segments::MemorySegmentManager;
use crate::{hint_processor::builtin_hint_processor::hint_code, utils::test_utils::*};
use assert_matches::assert_matches;
use std::any::Any;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[test]
fn run_n_more_than_10_true() {
let hint_code = hint_code::NONDET_N_MORE_THAN_10;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_n_greater_than_10_true() {
let hint_code = hint_code::NONDET_N_GREATER_THAN_10;
let mut vm = vm!();
vm.set_ap(3);
vm.segments = segments![((1, 0), 21)];
Expand All @@ -75,8 +78,9 @@ mod tests {
}

#[test]
fn run_n_more_than_10_false() {
let hint_code = hint_code::NONDET_N_MORE_THAN_10;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_n_greater_than_10_false() {
let hint_code = hint_code::NONDET_N_GREATER_THAN_10;
let mut vm = vm!();
vm.set_ap(3);
vm.segments = segments![((1, 0), 9)];
Expand All @@ -88,8 +92,9 @@ mod tests {
}

#[test]
fn run_n_more_than_2_true() {
let hint_code = hint_code::NONDET_N_MORE_THAN_2;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_n_greater_than_2_true() {
let hint_code = hint_code::NONDET_N_GREATER_THAN_2;
let mut vm = vm!();
vm.set_ap(3);
vm.segments = segments![((1, 0), 6)];
Expand All @@ -101,8 +106,9 @@ mod tests {
}

#[test]
fn run_n_more_than_2_false() {
let hint_code = hint_code::NONDET_N_MORE_THAN_2;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_n_greater_than_2_false() {
let hint_code = hint_code::NONDET_N_GREATER_THAN_2;
let mut vm = vm!();
vm.set_ap(3);
vm.segments = segments![((1, 0), 1)];
Expand Down
21 changes: 20 additions & 1 deletion src/vm/runners/builtin_runner/ec_op.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::borrow::Cow;
use crate::stdlib::{borrow::Cow, prelude::*};
use std::cell::RefCell;
use std::collections::HashMap;

use crate::math_utils::{ec_add, ec_double, safe_div_usize};
use crate::types::instance_definitions::ec_op_instance_def::{
Expand Down Expand Up @@ -27,6 +29,7 @@ pub struct EcOpBuiltinRunner {
pub(crate) stop_ptr: Option<usize>,
pub(crate) included: bool,
instances_per_component: u32,
cache: RefCell<HashMap<Relocatable, Felt>>,
}

impl EcOpBuiltinRunner {
Expand All @@ -40,6 +43,7 @@ impl EcOpBuiltinRunner {
stop_ptr: None,
included,
instances_per_component: 1,
cache: RefCell::new(HashMap::new()),
}
}
///Returns True if the point (x, y) is on the elliptic curve defined as
Expand Down Expand Up @@ -154,6 +158,13 @@ impl EcOpBuiltinRunner {
return Ok(None);
}
let instance = Relocatable::from((address.segment_index, address.offset - index));
let x_addr = (instance + (&Felt::new(INPUT_CELLS_PER_EC_OP)))
.map_err(|_| RunnerError::Memory(MemoryError::ExpectedInteger(instance)))?;

if let Some(number) = self.cache.borrow().get(&address).cloned() {
return Ok(Some(MaybeRelocatable::Int(number)));
}

//All input cells should be filled, and be integer values
//If an input cell is not filled, return None
let mut input_cells = Vec::<&Felt>::with_capacity(self.n_input_cells as usize);
Expand Down Expand Up @@ -205,6 +216,14 @@ impl EcOpBuiltinRunner {
&prime,
self.ec_op_builtin.scalar_height,
)?;
self.cache
.borrow_mut()
.insert(x_addr, result.0.clone().into());
self.cache.borrow_mut().insert(
(x_addr + 1usize)
.map_err(|_| RunnerError::Memory(MemoryError::ExpectedInteger(x_addr)))?,
result.1.clone().into(),
);
match index - self.n_input_cells as usize {
0 => Ok(Some(MaybeRelocatable::Int(Felt::new(result.0)))),
_ => Ok(Some(MaybeRelocatable::Int(Felt::new(result.1)))),
Expand Down
4 changes: 2 additions & 2 deletions src/vm/runners/builtin_runner/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::vm::vm_memory::memory_segments::MemorySegmentManager;
use felt::Felt;
use num_integer::div_ceil;
use starknet_crypto::FieldElement;

use super::poseidon_utils::poseidon_hash::permute_comp;
use starknet_crypto::{poseidon_permute_comp, FieldElement};
use super::POSEIDON_BUILTIN_NAME;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -99,7 +99,7 @@ impl PoseidonBuiltinRunner {
}
// n_input_cells is fixed to 3, so this try_into will never fail
let mut poseidon_state: [FieldElement; 3] = input_felts.try_into().unwrap();
permute_comp(&mut poseidon_state);
poseidon_permute_comp(&mut poseidon_state);
for (i, elem) in poseidon_state.iter().enumerate() {
self.cache.borrow_mut().insert(
(first_output_addr + i)?,
Expand Down
32 changes: 32 additions & 0 deletions tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,3 +1347,35 @@ fn cairo_run_poseidon_hash() {
)
.expect("Couldn't run program");
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn cairo_run_poseidon_builtin() {
let mut hint_executor = BuiltinHintProcessor::new_empty();
let cairo_run_config = cairo_run::CairoRunConfig {
layout: "all",
..cairo_vm::cairo_run::CairoRunConfig::default()
};
cairo_run::cairo_run(
include_bytes!("../cairo_programs/poseidon_builtin.json"),
&cairo_run_config,
&mut hint_executor,
)
.expect("Couldn't run program");
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn cairo_run_poseidon_hash() {
let mut hint_executor = BuiltinHintProcessor::new_empty();
let cairo_run_config = cairo_run::CairoRunConfig {
layout: "all",
..cairo_vm::cairo_run::CairoRunConfig::default()
};
cairo_run::cairo_run(
include_bytes!("../cairo_programs/poseidon_hash.json"),
&cairo_run_config,
&mut hint_executor,
)
.expect("Couldn't run program");
}

0 comments on commit 489476d

Please sign in to comment.