Skip to content

Commit

Permalink
Merge branch 'main' into dynamic-layout-2
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianGCalderon committed Sep 30, 2024
2 parents 1e2e36f + 7d19956 commit 4c90ff4
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ clippy:
cargo clippy --manifest-path fuzzer/Cargo.toml --all-targets

coverage: cairo_proof_programs cairo_test_programs cairo_1_test_contracts cairo_2_test_contracts
cargo llvm-cov --html --workspace --features "test_utils, cairo-1-hints, mod_builtin"
cargo llvm-cov --html --workspace --features "test_utils, cairo-1-hints"

coverage-clean:
cargo llvm-cov clean
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ The cairo-vm-cli supports the following optional arguments:

- `run_from_cairo_pie`: Runs a Cairo PIE instead of a compiled json file. The name of the file will be the first argument received by the CLI (as if it were to run a normal compiled program). Can only be used if proof_mode is not enabled.

- `cairo_layout_params_file`: Only used with dynamic layout. Receives the name of a json file with the dynamic layout parameters.

For example, to obtain the air public inputs from a fibonacci program run, we can run :

```bash
Expand Down
4 changes: 2 additions & 2 deletions cairo-vm-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
proof_mode: args.proof_mode,
secure_run: args.secure_run,
allow_missing_builtins: args.allow_missing_builtins,
cairo_layout_params,
dynamic_layout_params: cairo_layout_params,
..Default::default()
};

Expand Down Expand Up @@ -423,7 +423,7 @@ mod tests {
args.extend_from_slice(&["--layout".to_string(), "dynamic".to_string()]);
args.extend_from_slice(&[
"--cairo_layout_params_file".to_string(),
"../cairo_layout_params_file.json".to_string(),
"../vm/src/tests/cairo_layout_params_file.json".to_string(),
]);
args.push("../cairo_programs/proof_programs/fibonacci.json".to_string());

Expand Down
6 changes: 3 additions & 3 deletions cairo1-run/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct Cairo1RunConfig<'a> {
pub relocate_mem: bool,
/// Cairo layout chosen for the run
pub layout: LayoutName,
pub cairo_layout_params: Option<CairoLayoutParams>,
pub dynamic_layout_params: Option<CairoLayoutParams>,
/// Run in proof_mode
pub proof_mode: bool,
/// Should be true if either air_public_input or cairo_pie_output are needed
Expand All @@ -107,7 +107,7 @@ impl Default for Cairo1RunConfig<'_> {
proof_mode: false,
finalize_builtins: false,
append_return_values: false,
cairo_layout_params: None,
dynamic_layout_params: None,
}
}
}
Expand Down Expand Up @@ -250,7 +250,7 @@ pub fn cairo_run_program(
let mut runner = CairoRunner::new_v2(
&program,
cairo_run_config.layout,
cairo_run_config.cairo_layout_params.clone(),
cairo_run_config.dynamic_layout_params.clone(),
runner_mode,
cairo_run_config.trace_enabled,
)?;
Expand Down
4 changes: 2 additions & 2 deletions cairo1-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, Error> {
args: &args.args.0,
finalize_builtins: args.air_public_input.is_some() || args.cairo_pie_output.is_some(),
append_return_values: args.append_return_values,
cairo_layout_params,
dynamic_layout_params: cairo_layout_params,
};

// Try to parse the file as a sierra program
Expand Down Expand Up @@ -496,7 +496,7 @@ mod tests {
args.extend_from_slice(&["--layout".to_string(), "dynamic".to_string()]);
args.extend_from_slice(&[
"--cairo_layout_params_file".to_string(),
"../cairo_layout_params_file.json".to_string(),
"../vm/src/tests/cairo_layout_params_file.json".to_string(),
]);
args.push("../cairo_programs/cairo-1-programs/fibonacci.cairo".to_string());

Expand Down
12 changes: 7 additions & 5 deletions vm/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ pub struct CairoRunConfig<'a> {
pub trace_enabled: bool,
pub relocate_mem: bool,
pub layout: LayoutName,
pub cairo_layout_params: Option<CairoLayoutParams>,
/// The `dynamic_layout_params` argument should only be used with dynamic layout.
/// It is ignored otherwise.
pub dynamic_layout_params: Option<CairoLayoutParams>,
pub proof_mode: bool,
pub secure_run: Option<bool>,
pub disable_trace_padding: bool,
Expand All @@ -47,7 +49,7 @@ impl<'a> Default for CairoRunConfig<'a> {
secure_run: None,
disable_trace_padding: false,
allow_missing_builtins: None,
cairo_layout_params: None,
dynamic_layout_params: None,
}
}
}
Expand All @@ -70,7 +72,7 @@ pub fn cairo_run_program_with_initial_scope(
let mut cairo_runner = CairoRunner::new(
program,
cairo_run_config.layout,
cairo_run_config.cairo_layout_params.clone(),
cairo_run_config.dynamic_layout_params.clone(),
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
)?;
Expand Down Expand Up @@ -157,7 +159,7 @@ pub fn cairo_run_pie(
let mut cairo_runner = CairoRunner::new(
&program,
cairo_run_config.layout,
cairo_run_config.cairo_layout_params.clone(),
cairo_run_config.dynamic_layout_params.clone(),
false,
cairo_run_config.trace_enabled,
)?;
Expand Down Expand Up @@ -230,7 +232,7 @@ pub fn cairo_run_fuzzed_program(
let mut cairo_runner = CairoRunner::new(
&program,
cairo_run_config.layout,
cairo_run_config.cairo_layout_params.clone(),
cairo_run_config.dynamic_layout_params.clone(),
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
)?;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion vm/src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ fn run_program_with_custom_mod_builtin_params(
let mut cairo_runner = CairoRunner::new(
&program,
cairo_run_config.layout,
cairo_run_config.cairo_layout_params,
cairo_run_config.dynamic_layout_params,
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
)
Expand Down
12 changes: 10 additions & 2 deletions vm/src/tests/compare_outputs_dynamic_layouts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,17 @@ EOF
# Build cases to execute
CASES=(
"cairo_programs/proof_programs/factorial.json;all_cairo"
"cairo_programs/proof_programs/fibonacci.json;all_cairo"
"cairo_programs/proof_programs/factorial.json;double_all_cairo"
"cairo_programs/proof_programs/fibonacci.json;all_cairo"
"cairo_programs/proof_programs/fibonacci.json;double_all_cairo"
"cairo_programs/proof_programs/bigint.json;all_cairo"
"cairo_programs/proof_programs/bigint.json;double_all_cairo"
"cairo_programs/proof_programs/dict.json;all_cairo"
"cairo_programs/proof_programs/dict.json;double_all_cairo"
"cairo_programs/proof_programs/sha256.json;all_cairo"
"cairo_programs/proof_programs/sha256.json;double_all_cairo"
"cairo_programs/proof_programs/keccak.json;all_cairo"
"cairo_programs/proof_programs/keccak.json;double_all_cairo"
"cairo_programs/mod_builtin_feature/proof/mod_builtin.json;all_cairo"
"cairo_programs/mod_builtin_feature/proof/mod_builtin_failure.json;all_cairo"
"cairo_programs/mod_builtin_feature/proof/apply_poly.json;all_cairo"
Expand All @@ -160,7 +168,7 @@ for case in "${CASES[@]}"; do

# Run cairo-vm
echo "Running cairo-vm with case: $case"
cargo run -p cairo-vm-cli --release -- "$full_program" \
cargo run -p cairo-vm-cli --features mod_builtin --release -- "$full_program" \
--layout "dynamic" --cairo_layout_params_file "$full_layout" --proof_mode \
--trace_file program_rs.trace --memory_file program_rs.memory --air_public_input program_rs.air_public_input --air_private_input program_rs.air_private_input

Expand Down
6 changes: 6 additions & 0 deletions vm/src/types/instance_definitions/builtins_instance_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl BuiltinsInstanceDef {
params.range_check96_ratio_den,
)),
});
#[cfg(feature = "mod_builtin")]
let add_mod = Some(ModInstanceDef {
ratio: Some(LowRatio::new(
params.add_mod_ratio,
Expand All @@ -231,6 +232,7 @@ impl BuiltinsInstanceDef {
word_bit_len: 96,
batch_size: 1,
});
#[cfg(feature = "mod_builtin")]
let mul_mod = Some(ModInstanceDef {
ratio: Some(LowRatio::new(
params.mul_mod_ratio,
Expand All @@ -239,6 +241,10 @@ impl BuiltinsInstanceDef {
word_bit_len: 96,
batch_size: 1,
});
#[cfg(not(feature = "mod_builtin"))]
let add_mod = None;
#[cfg(not(feature = "mod_builtin"))]
let mul_mod = None;

BuiltinsInstanceDef {
output: true,
Expand Down
109 changes: 74 additions & 35 deletions vm/src/types/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::types::layout_name::LayoutName;

use super::instance_definitions::{
builtins_instance_def::BuiltinsInstanceDef, diluted_pool_instance_def::DilutedPoolInstanceDef,
use crate::{types::layout_name::LayoutName, vm::errors::runner_errors::RunnerError};

use super::{
builtin_name::BuiltinName,
instance_definitions::{
builtins_instance_def::BuiltinsInstanceDef,
diluted_pool_instance_def::DilutedPoolInstanceDef,
},
};

pub(crate) const DEFAULT_MEMORY_UNITS_PER_STEP: u32 = 8;
Expand Down Expand Up @@ -225,38 +229,58 @@ pub struct RawCairoLayoutParams {
}

impl TryFrom<RawCairoLayoutParams> for CairoLayoutParams {
type Error = &'static str;
type Error = RunnerError;

fn try_from(value: RawCairoLayoutParams) -> Result<Self, Self::Error> {
if !value.uses_pedersen_builtin && value.pedersen_ratio != 0 {
return Err("pedersen ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::pedersen,
));
}
if !value.uses_range_check_builtin && value.range_check_ratio != 0 {
return Err("range_check ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::range_check,
));
}
if !value.uses_ecdsa_builtin && value.ecdsa_ratio != 0 {
return Err("ecdsa ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::ecdsa,
));
}
if !value.uses_bitwise_builtin && value.bitwise_ratio != 0 {
return Err("bitwise ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::bitwise,
));
}
if !value.uses_ec_op_builtin && value.ec_op_ratio != 0 {
return Err("ec_op ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::ec_op,
));
}
if !value.uses_keccak_builtin && value.keccak_ratio != 0 {
return Err("keccak ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::keccak,
));
}
if !value.uses_poseidon_builtin && value.poseidon_ratio != 0 {
return Err("poseidon ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::poseidon,
));
}
if !value.uses_range_check96_builtin && value.range_check96_ratio != 0 {
return Err("range_check96 ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::range_check96,
));
}
if !value.uses_add_mod_builtin && value.add_mod_ratio != 0 {
return Err("add_mod ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::add_mod,
));
}
if !value.uses_mul_mod_builtin && value.mul_mod_ratio != 0 {
return Err("mul_mod ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::mul_mod,
));
}

Ok(CairoLayoutParams {
Expand Down Expand Up @@ -301,16 +325,17 @@ where

#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "mod_builtin")]
use crate::types::instance_definitions::mod_instance_def::ModInstanceDef;

use crate::types::instance_definitions::{
bitwise_instance_def::BitwiseInstanceDef, ec_op_instance_def::EcOpInstanceDef,
ecdsa_instance_def::EcdsaInstanceDef, keccak_instance_def::KeccakInstanceDef,
mod_instance_def::ModInstanceDef, pedersen_instance_def::PedersenInstanceDef,
poseidon_instance_def::PoseidonInstanceDef,
pedersen_instance_def::PedersenInstanceDef, poseidon_instance_def::PoseidonInstanceDef,
range_check_instance_def::RangeCheckInstanceDef, LowRatio,
};

use super::*;

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

Expand Down Expand Up @@ -509,22 +534,36 @@ mod tests {
ratio: Some(LowRatio::new(8, 16))
})
);
assert_eq!(
layout.builtins.mul_mod,
Some(ModInstanceDef {
ratio: Some(LowRatio::new(32, 16)),
word_bit_len: 96, // hardcoded
batch_size: 1 // hardcoded
})
);
assert_eq!(
layout.builtins.add_mod,
Some(ModInstanceDef {
ratio: Some(LowRatio::new(8, 16)),
word_bit_len: 96, // hardcoded
batch_size: 1 // hardcoded
})
);
#[cfg(feature = "mod_builtin")]
{
assert_eq!(
layout.builtins.mul_mod,
Some(ModInstanceDef {
ratio: Some(LowRatio {
numerator: 8,
denominator: 16
}),
word_bit_len: 96, // hardcoded
batch_size: 1 // hardcoded
}),
);
assert_eq!(
layout.builtins.add_mod,
Some(ModInstanceDef {
ratio: Some(LowRatio {
numerator: 8,
denominator: 16
}),
word_bit_len: 96, // hardcoded
batch_size: 1 // hardcoded
})
);
}
#[cfg(not(feature = "mod_builtin"))]
{
assert_eq!(layout.builtins.mul_mod, None,);
assert_eq!(layout.builtins.add_mod, None,);
}
}

#[test]
Expand Down
6 changes: 4 additions & 2 deletions vm/src/vm/errors/runner_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ pub enum RunnerError {
CairoPieProofMode,
#[error("{0}: Invalid additional data")]
InvalidAdditionalData(BuiltinName),
#[error("Bad dynamic layout params: {0}")]
BadDynamicLayoutParams(String),
#[error("dynamic layout params is missing")]
MissingDynamicLayoutParams,
#[error("dynamic layout {0} ratio should be 0 when disabled")]
BadDynamicLayoutBuiltinRatio(BuiltinName),
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 4c90ff4

Please sign in to comment.