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 dynamic layout #879

Merged
merged 26 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion cairo-vm-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ fn validate_layout(value: &str) -> Result<(), String> {
| "starknet_with_keccak"
| "recursive_large_output"
| "all_cairo"
| "all_solidity" => Ok(()),
| "all_solidity"
| "dynamic" => Ok(()),
_ => Err(format!("{value} is not a valid layout")),
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/types/instance_definitions/bitwise_instance_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ pub(crate) const INPUT_CELLS_PER_BITWISE: u32 = 2;

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct BitwiseInstanceDef {
pub(crate) ratio: u32,
pub(crate) ratio: Option<u32>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) ratio: Option<u32>,
pub(crate) ratio: Option<NonZeroU32>,

pub(crate) total_n_bits: u32,
}

impl BitwiseInstanceDef {
pub(crate) fn default() -> Self {
BitwiseInstanceDef {
ratio: 256,
ratio: Some(256),
total_n_bits: 251,
}
}

pub(crate) fn new(ratio: u32) -> Self {
pub(crate) fn new(ratio: Option<u32>) -> Self {
BitwiseInstanceDef {
ratio,
total_n_bits: 251,
Expand Down Expand Up @@ -56,17 +56,17 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_new() {
let builtin_instance = BitwiseInstanceDef {
ratio: 8,
ratio: Some(8),
total_n_bits: 251,
};
assert_eq!(BitwiseInstanceDef::new(8), builtin_instance);
assert_eq!(BitwiseInstanceDef::new(Some(8)), builtin_instance);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_default() {
let builtin_instance = BitwiseInstanceDef {
ratio: 256,
ratio: Some(256),
total_n_bits: 251,
};
assert_eq!(BitwiseInstanceDef::default(), builtin_instance);
Expand Down
55 changes: 34 additions & 21 deletions src/types/instance_definitions/builtins_instance_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ impl BuiltinsInstanceDef {
pub(crate) fn recursive() -> BuiltinsInstanceDef {
BuiltinsInstanceDef {
output: true,
pedersen: Some(PedersenInstanceDef::new(128, 1)),
pedersen: Some(PedersenInstanceDef::new(Some(128), 1)),
range_check: Some(RangeCheckInstanceDef::default()),
ecdsa: None,
bitwise: Some(BitwiseInstanceDef::new(8)),
bitwise: Some(BitwiseInstanceDef::new(Some(8))),
ec_op: None,
keccak: None,
poseidon: None,
Expand All @@ -73,11 +73,11 @@ impl BuiltinsInstanceDef {
pub(crate) fn starknet() -> BuiltinsInstanceDef {
BuiltinsInstanceDef {
output: true,
pedersen: Some(PedersenInstanceDef::new(32, 1)),
range_check: Some(RangeCheckInstanceDef::new(16, 8)),
ecdsa: Some(EcdsaInstanceDef::new(2048)),
bitwise: Some(BitwiseInstanceDef::new(64)),
ec_op: Some(EcOpInstanceDef::new(1024)),
pedersen: Some(PedersenInstanceDef::new(Some(32), 1)),
range_check: Some(RangeCheckInstanceDef::new(Some(16), 8)),
ecdsa: Some(EcdsaInstanceDef::new(Some(2048))),
bitwise: Some(BitwiseInstanceDef::new(Some(64))),
ec_op: Some(EcOpInstanceDef::new(Some(1024))),
keccak: None,
poseidon: Some(PoseidonInstanceDef::default()),
}
Expand All @@ -86,23 +86,23 @@ impl BuiltinsInstanceDef {
pub(crate) fn starknet_with_keccak() -> BuiltinsInstanceDef {
BuiltinsInstanceDef {
output: true,
pedersen: Some(PedersenInstanceDef::new(32, 1)),
range_check: Some(RangeCheckInstanceDef::new(16, 8)),
ecdsa: Some(EcdsaInstanceDef::new(2048)),
bitwise: Some(BitwiseInstanceDef::new(64)),
ec_op: Some(EcOpInstanceDef::new(1024)),
keccak: Some(KeccakInstanceDef::new(2048, vec![200; 8])),
pedersen: Some(PedersenInstanceDef::new(Some(32), 1)),
range_check: Some(RangeCheckInstanceDef::new(Some(16), 8)),
ecdsa: Some(EcdsaInstanceDef::new(Some(2048))),
bitwise: Some(BitwiseInstanceDef::new(Some(64))),
ec_op: Some(EcOpInstanceDef::new(Some(1024))),
keccak: Some(KeccakInstanceDef::new(Some(2048), vec![200; 8])),
poseidon: Some(PoseidonInstanceDef::default()),
}
}

pub(crate) fn recursive_large_output() -> BuiltinsInstanceDef {
BuiltinsInstanceDef {
output: true,
pedersen: Some(PedersenInstanceDef::new(32, 1)),
pedersen: Some(PedersenInstanceDef::new(Some(32), 1)),
range_check: Some(RangeCheckInstanceDef::default()),
ecdsa: None,
bitwise: Some(BitwiseInstanceDef::new(8)),
bitwise: Some(BitwiseInstanceDef::new(Some(8))),
ec_op: None,
keccak: None,
poseidon: None,
Expand All @@ -112,13 +112,13 @@ impl BuiltinsInstanceDef {
pub(crate) fn all_cairo() -> BuiltinsInstanceDef {
BuiltinsInstanceDef {
output: true,
pedersen: Some(PedersenInstanceDef::new(256, 1)),
pedersen: Some(PedersenInstanceDef::new(Some(256), 1)),
range_check: Some(RangeCheckInstanceDef::default()),
ecdsa: Some(EcdsaInstanceDef::new(2048)),
bitwise: Some(BitwiseInstanceDef::new(16)),
ec_op: Some(EcOpInstanceDef::new(1024)),
keccak: Some(KeccakInstanceDef::new(2048, vec![200; 8])),
poseidon: Some(PoseidonInstanceDef::new(256)),
ecdsa: Some(EcdsaInstanceDef::new(Some(2048))),
bitwise: Some(BitwiseInstanceDef::new(Some(16))),
ec_op: Some(EcOpInstanceDef::new(Some(1024))),
keccak: Some(KeccakInstanceDef::new(Some(2048), vec![200; 8])),
poseidon: Some(PoseidonInstanceDef::new(Some(256))),
}
}

Expand All @@ -134,6 +134,19 @@ impl BuiltinsInstanceDef {
poseidon: None,
}
}

pub(crate) fn dynamic() -> BuiltinsInstanceDef {
BuiltinsInstanceDef {
output: true,
pedersen: Some(PedersenInstanceDef::new(None, 4)),
range_check: Some(RangeCheckInstanceDef::new(None, 8)),
ecdsa: Some(EcdsaInstanceDef::new(None)),
bitwise: Some(BitwiseInstanceDef::new(None)),
ec_op: Some(EcOpInstanceDef::new(None)),
keccak: None,
poseidon: None,
}
}
}

#[cfg(test)]
Expand Down
12 changes: 6 additions & 6 deletions src/types/instance_definitions/ec_op_instance_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ pub(crate) const INPUT_CELLS_PER_EC_OP: u32 = 5;

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct EcOpInstanceDef {
pub(crate) ratio: u32,
pub(crate) ratio: Option<u32>,
pub(crate) scalar_height: u32,
pub(crate) _scalar_bits: u32,
}

impl EcOpInstanceDef {
pub(crate) fn default() -> Self {
EcOpInstanceDef {
ratio: 256,
ratio: Some(256),
scalar_height: 256,
_scalar_bits: 252,
}
}

pub(crate) fn new(ratio: u32) -> Self {
pub(crate) fn new(ratio: Option<u32>) -> Self {
EcOpInstanceDef {
ratio,
scalar_height: 256,
Expand Down Expand Up @@ -59,18 +59,18 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_new() {
let builtin_instance = EcOpInstanceDef {
ratio: 8,
ratio: Some(8),
scalar_height: 256,
_scalar_bits: 252,
};
assert_eq!(EcOpInstanceDef::new(8), builtin_instance);
assert_eq!(EcOpInstanceDef::new(Some(8)), builtin_instance);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_default() {
let builtin_instance = EcOpInstanceDef {
ratio: 256,
ratio: Some(256),
scalar_height: 256,
_scalar_bits: 252,
};
Expand Down
12 changes: 6 additions & 6 deletions src/types/instance_definitions/ecdsa_instance_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub(crate) const _INPUTCELLS_PER_SIGNATURE: u32 = 2;

#[derive(Debug, PartialEq)]
pub(crate) struct EcdsaInstanceDef {
pub(crate) ratio: u32,
pub(crate) ratio: Option<u32>,
pub(crate) _repetitions: u32,
pub(crate) _height: u32,
pub(crate) _n_hash_bits: u32,
Expand All @@ -12,14 +12,14 @@ pub(crate) struct EcdsaInstanceDef {
impl EcdsaInstanceDef {
pub(crate) fn default() -> Self {
EcdsaInstanceDef {
ratio: 512,
ratio: Some(512),
_repetitions: 1,
_height: 256,
_n_hash_bits: 251,
}
}

pub(crate) fn new(ratio: u32) -> Self {
pub(crate) fn new(ratio: Option<u32>) -> Self {
EcdsaInstanceDef {
ratio,
_repetitions: 1,
Expand Down Expand Up @@ -62,19 +62,19 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_new() {
let builtin_instance = EcdsaInstanceDef {
ratio: 8,
ratio: Some(8),
_repetitions: 1,
_height: 256,
_n_hash_bits: 251,
};
assert_eq!(EcdsaInstanceDef::new(8), builtin_instance);
assert_eq!(EcdsaInstanceDef::new(Some(8)), builtin_instance);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_default() {
let builtin_instance = EcdsaInstanceDef {
ratio: 512,
ratio: Some(512),
_repetitions: 1,
_height: 256,
_n_hash_bits: 251,
Expand Down
19 changes: 11 additions & 8 deletions src/types/instance_definitions/keccak_instance_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ use crate::stdlib::prelude::*;

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct KeccakInstanceDef {
pub(crate) _ratio: u32,
pub(crate) ratio: Option<u32>,
pub(crate) _state_rep: Vec<u32>,
pub(crate) _instance_per_component: u32,
}

impl Default for KeccakInstanceDef {
fn default() -> Self {
Self {
// _ratio should be equal to 2 ** 11 -> 2048
_ratio: 2048,
// ratio should be equal to 2 ** 11 -> 2048
ratio: Some(2048),
_state_rep: vec![200; 8],
_instance_per_component: 16,
}
}
}

impl KeccakInstanceDef {
pub(crate) fn new(_ratio: u32, _state_rep: Vec<u32>) -> Self {
pub(crate) fn new(ratio: Option<u32>, _state_rep: Vec<u32>) -> Self {
Self {
_ratio,
ratio,
_state_rep,
..Default::default()
}
Expand Down Expand Up @@ -61,18 +61,21 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_new() {
let builtin_instance = KeccakInstanceDef {
_ratio: 2048,
ratio: Some(2048),
_state_rep: vec![200; 8],
_instance_per_component: 16,
};
assert_eq!(KeccakInstanceDef::new(2048, vec![200; 8]), builtin_instance);
assert_eq!(
KeccakInstanceDef::new(Some(2048), vec![200; 8]),
builtin_instance
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_default() {
let builtin_instance = KeccakInstanceDef {
_ratio: 2048,
ratio: Some(2048),
_state_rep: vec![200; 8],
_instance_per_component: 16,
};
Expand Down
12 changes: 6 additions & 6 deletions src/types/instance_definitions/pedersen_instance_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(crate) const INPUT_CELLS_PER_HASH: u32 = 2;

#[derive(Debug, PartialEq)]
pub(crate) struct PedersenInstanceDef {
pub(crate) ratio: u32,
pub(crate) ratio: Option<u32>,
pub(crate) _repetitions: u32,
pub(crate) _element_height: u32,
pub(crate) _element_bits: u32,
Expand All @@ -16,7 +16,7 @@ pub(crate) struct PedersenInstanceDef {
impl PedersenInstanceDef {
pub(crate) fn default() -> Self {
PedersenInstanceDef {
ratio: 8,
ratio: Some(8),
_repetitions: 4,
_element_height: 256,
_element_bits: 252,
Expand All @@ -25,7 +25,7 @@ impl PedersenInstanceDef {
}
}

pub(crate) fn new(ratio: u32, _repetitions: u32) -> Self {
pub(crate) fn new(ratio: Option<u32>, _repetitions: u32) -> Self {
PedersenInstanceDef {
ratio,
_repetitions,
Expand Down Expand Up @@ -70,21 +70,21 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_new() {
let builtin_instance = PedersenInstanceDef {
ratio: 10,
ratio: Some(10),
_repetitions: 2,
_element_height: 256,
_element_bits: 252,
_n_inputs: 2,
_hash_limit: BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]),
};
assert_eq!(PedersenInstanceDef::new(10, 2), builtin_instance);
assert_eq!(PedersenInstanceDef::new(Some(10), 2), builtin_instance);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_default() {
let builtin_instance = PedersenInstanceDef {
ratio: 8,
ratio: Some(8),
_repetitions: 4,
_element_height: 256,
_element_bits: 252,
Expand Down
12 changes: 6 additions & 6 deletions src/types/instance_definitions/poseidon_instance_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ pub(crate) const INPUT_CELLS_PER_POSEIDON: u32 = 3;

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct PoseidonInstanceDef {
pub(crate) ratio: u32,
pub(crate) ratio: Option<u32>,
}

impl PoseidonInstanceDef {
pub(crate) fn default() -> Self {
PoseidonInstanceDef { ratio: 32 }
PoseidonInstanceDef { ratio: Some(32) }
}

pub(crate) fn new(ratio: u32) -> Self {
pub(crate) fn new(ratio: Option<u32>) -> Self {
PoseidonInstanceDef { ratio }
}
}
Expand All @@ -22,13 +22,13 @@ mod tests {

#[test]
fn test_new() {
let builtin_instance = PoseidonInstanceDef { ratio: 8 };
assert_eq!(PoseidonInstanceDef::new(8), builtin_instance);
let builtin_instance = PoseidonInstanceDef { ratio: Some(8) };
assert_eq!(PoseidonInstanceDef::new(Some(8)), builtin_instance);
}

#[test]
fn test_default() {
let builtin_instance = PoseidonInstanceDef { ratio: 32 };
let builtin_instance = PoseidonInstanceDef { ratio: Some(32) };
assert_eq!(PoseidonInstanceDef::default(), builtin_instance);
}
}
Loading