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

Fuzz tests for StorePacking #1296

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions packages/account/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ snforge_std.workspace = true
openzeppelin_testing = { path = "../testing" }
openzeppelin_test_common = { path = "../test_common" }

[features]
fuzzing = []

[lib]

[[target.starknet-contract]]
Expand Down
45 changes: 45 additions & 0 deletions packages/account/src/tests/test_secp256_point.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::utils::secp256_point::{
use starknet::SyscallResultTrait;
use starknet::secp256_trait::{Secp256PointTrait, Secp256Trait};
use starknet::secp256k1::Secp256k1Point;
use starknet::secp256r1::Secp256r1Point;

#[test]
fn test_pack_big_secp256k1_points() {
Expand Down Expand Up @@ -60,6 +61,50 @@ fn test_unpack_big_secp256k1_points() {
assert_eq!(y, expected_y);
}

#[test]
#[cfg(feature: 'fuzzing')]
fn test_pack_unpack_secp256k1_point(x: u256, y_parity_val: u8) {
let y_parity = y_parity_val % 2 == 0;
let point: Secp256k1Point =
match Secp256Trait::secp256_ec_get_point_from_x_syscall(x, y_parity) {
Result::Ok(point) => match point {
Option::Some(point) => point,
Option::None => { return; },
},
Result::Err => { return; },
};

let (expected_x, expected_y) = point.get_coordinates().unwrap_syscall();
let packed_value = StorePacking::pack(point);
let unpacked_point: Secp256k1Point = StorePacking::unpack(packed_value);
let (x, y) = unpacked_point.get_coordinates().unwrap_syscall();

assert_eq!(x, expected_x);
assert_eq!(y, expected_y);
}

#[test]
#[cfg(feature: 'fuzzing')]
fn test_pack_unpack_secp256r1_point(x: u256, y_parity_val: u8) {
let y_parity = y_parity_val % 2 == 0;
let point: Secp256r1Point =
match Secp256Trait::secp256_ec_get_point_from_x_syscall(x, y_parity) {
Result::Ok(point) => match point {
Option::Some(point) => point,
Option::None => { return; },
},
Result::Err => { return; },
};

let (expected_x, expected_y) = point.get_coordinates().unwrap_syscall();
let packed_value = StorePacking::pack(point);
let unpacked_point: Secp256r1Point = StorePacking::unpack(packed_value);
let (x, y) = unpacked_point.get_coordinates().unwrap_syscall();

assert_eq!(x, expected_x);
assert_eq!(y, expected_y);
}

#[test]
fn test_partial_eq() {
let (big_point_1, big_point_2) = get_points();
Expand Down
3 changes: 3 additions & 0 deletions packages/governance/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ snforge_std.workspace = true
openzeppelin_testing = { path = "../testing" }
openzeppelin_test_common = { path = "../test_common" }

[features]
fuzzing = []

[lib]

[[target.starknet-contract]]
Expand Down
5 changes: 4 additions & 1 deletion packages/governance/src/multisig/multisig.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#[starknet::component]
pub mod MultisigComponent {
use core::hash::{HashStateExTrait, HashStateTrait};
use core::num::traits::Zero;
use core::num::traits::{Bounded, Zero};
use core::panic_with_felt252;
use core::pedersen::PedersenTrait;
use crate::multisig::interface::{IMultisig, TransactionID, TransactionState};
Expand Down Expand Up @@ -128,6 +128,7 @@ pub mod MultisigComponent {
pub const ZERO_ADDRESS_SIGNER: felt252 = 'Multisig: zero address signer';
pub const ZERO_QUORUM: felt252 = 'Multisig: quorum cannot be 0';
pub const QUORUM_TOO_HIGH: felt252 = 'Multisig: quorum > signers';
pub const TOO_MANY_SIGNERS: felt252 = 'Multisig: too many signers';
}

//
Expand Down Expand Up @@ -569,6 +570,8 @@ pub mod MultisigComponent {

signers_count += 1;
};

assert(signers_count != Bounded::MAX, Errors::TOO_MANY_SIGNERS);
self.Multisig_signers_info.write(SignersInfo { quorum, signers_count });
}
self._change_quorum(new_quorum);
Expand Down
3 changes: 3 additions & 0 deletions packages/governance/src/multisig/storage_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub struct SignersInfo {
/// The packing is done as follows:
/// - `quorum` value occupies 32 bits in bit range [64..95].
/// - `signers_count` value occupies the highest 32 bits in bit range [96..127].
///
/// WARNING: If `signers_count` equals the maximum u32 value (0xffffffff or 4_294_967_295),
/// the unpacked quorum value will be invalid, exceeding the expected value by 1.
pub impl SignersInfoStorePacking of StorePacking<SignersInfo, u128> {
fn pack(value: SignersInfo) -> u128 {
let SignersInfo { quorum, signers_count } = value;
Expand Down
2 changes: 2 additions & 0 deletions packages/governance/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod governor;
#[cfg(feature: 'fuzzing')]
mod test_fuzz_packing;
mod test_multisig;
mod test_timelock;
mod test_utils;
Expand Down
53 changes: 53 additions & 0 deletions packages/governance/src/tests/test_fuzz_packing.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use core::num::traits::Bounded;
use crate::governor::ProposalCore;
use crate::multisig::storage_utils::{SignersInfo, TxInfo};
use starknet::storage_access::StorePacking;

#[test]
fn test_pack_unpack_tx_info(is_executed_val: u8, submitted_block: u64) {
let is_executed = is_executed_val % 2 == 0;
let packed_value = StorePacking::pack(TxInfo { is_executed, submitted_block });
let unpacked_tx_info: TxInfo = StorePacking::unpack(packed_value);

assert_eq!(unpacked_tx_info.is_executed, is_executed);
assert_eq!(unpacked_tx_info.submitted_block, submitted_block);
}

#[test]
fn test_pack_unpack_signers_info(quorum: u32, signers_count: u32) {
// Packing works incorrectly when the number of signers
// equals max u32 value (0xffffffff or 4_294_967_295).
if signers_count == Bounded::MAX {
return;
};
let packed_value = StorePacking::pack(SignersInfo { quorum, signers_count });
let unpacked_signers_info: SignersInfo = StorePacking::unpack(packed_value);

assert_eq!(unpacked_signers_info.quorum, quorum);
assert_eq!(unpacked_signers_info.signers_count, signers_count);
}

#[test]
fn test_pack_unpack_proposal_core(
proposer_val: felt252,
vote_start: u64,
vote_duration: u64,
executed_val: u8,
canceled_val: u8,
eta_seconds: u64,
) {
let proposer = match proposer_val.try_into() {
Option::Some(proposer) => proposer,
Option::None => { return; },
};
let executed = executed_val % 2 == 0;
let canceled = canceled_val % 2 == 0;
let initial_proposal_core = ProposalCore {
proposer, vote_start, vote_duration, executed, canceled, eta_seconds,
};

let packed_value = StorePacking::pack(initial_proposal_core);
let unpacked_proposal_core: ProposalCore = StorePacking::unpack(packed_value);

assert!(unpacked_proposal_core == initial_proposal_core);
}
Loading