Skip to content

Commit

Permalink
renegade_contracts: merkle: poseidon: using spongemode, fuzz tests pa…
Browse files Browse the repository at this point in the history
…ssing
  • Loading branch information
akirillo committed Aug 2, 2023
1 parent 3f25469 commit efed026
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 44 deletions.
18 changes: 0 additions & 18 deletions .github/actions/install_cairo/action.yml

This file was deleted.

5 changes: 2 additions & 3 deletions .github/workflows/test_devnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ jobs:
uses: actions-rs/cargo@v1
env:
ARTIFACTS_PATH: ${{ github.workspace }}/target/release
RUST_LOG: "tests=trace,katana_core=warn"
RUST_LOG: "tests=trace,katana_core=debug"
CAIRO_STEP_LIMIT: 10000000
with:
command: test
args: -p tests --all-targets --lib --verbose -- --nocapture


56 changes: 41 additions & 15 deletions src/merkle/poseidon.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ const T: usize = 3;
// TODO: Hardcode all MDS entries (only 9)
// TODO: Hardcode all round constants

#[derive(Drop)]
enum SpongeMode {
/// Signifies that the sponge is currently absorbing input at the given index
Absorbing: usize,
/// Signifies that the sponge is currently squeezing output from the given index
Squeezing: usize,
}

#[derive(Destruct)]
struct PoseidonSponge {
state: NullableVec<Scalar>,
absorb_index: usize,
squeeze_index: usize,
mode: SpongeMode,
round_constants: Array<Array<Scalar>>,
mds: Array<Array<Scalar>>,
}
Expand All @@ -54,46 +61,63 @@ impl PoseidonImpl of PoseidonTrait {
let round_constants = round_constants();
let mds = mds();

PoseidonSponge { state, absorb_index: 0, squeeze_index: 0, round_constants, mds, }
PoseidonSponge { state, mode: SpongeMode::Absorbing(0), round_constants, mds, }
}

fn absorb(ref self: PoseidonSponge, input: Span<Scalar>) {
let PoseidonSponge{mut state, mut absorb_index, squeeze_index, round_constants, mds } =
self;
let PoseidonSponge{mut state, mut mode, round_constants, mds } = self;

let round_constants_span = round_constants.deep_span();
let mds_span = mds.deep_span();

let mut absorb_index = match mode {
SpongeMode::Absorbing(i) => i,
SpongeMode::Squeezing(_) => {
permute(ref state, round_constants_span, mds_span);
0
}
};

let mut i = 0;
loop {
if i == input.len() {
break;
}

// Only permute if we're not done absorbing
if absorb_index == RATE {
permute(ref state, round_constants_span, mds_span);
absorb_index = 0;
}

let mut state_i = state[CAPACITY + absorb_index];
state_i += *input[i];
state.set(CAPACITY + absorb_index, state_i);

absorb_index += 1;

if absorb_index == RATE {
permute(ref state, round_constants_span, mds_span);
absorb_index = 0;
}

i += 1;
};

self = PoseidonSponge { state, absorb_index, squeeze_index, round_constants, mds };
mode = SpongeMode::Absorbing(absorb_index);

self = PoseidonSponge { state, mode, round_constants, mds };
}

fn squeeze(ref self: PoseidonSponge, num_elements: usize) -> Array<Scalar> {
let PoseidonSponge{mut state, absorb_index, mut squeeze_index, round_constants, mds } =
self;
let PoseidonSponge{mut state, mut mode, round_constants, mds } = self;

let round_constants_span = round_constants.deep_span();
let mds_span = mds.deep_span();

let mut squeeze_index = match mode {
SpongeMode::Absorbing(_) => {
permute(ref state, round_constants_span, mds_span);
0
},
SpongeMode::Squeezing(i) => i,
};

let mut output = ArrayTrait::new();

let mut i = 0;
Expand All @@ -108,14 +132,16 @@ impl PoseidonImpl of PoseidonTrait {
squeeze_index = 0;
}

output.append(state[squeeze_index]);
output.append(state[CAPACITY + squeeze_index]);

squeeze_index += 1;

i += 1;
};

self = PoseidonSponge { state, absorb_index, squeeze_index, round_constants, mds };
mode = SpongeMode::Squeezing(squeeze_index);

self = PoseidonSponge { state, mode, round_constants, mds };

output
}
Expand Down
12 changes: 6 additions & 6 deletions tests/src/poseidon/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use tracing::debug;

use crate::utils::{call_contract, global_setup, invoke_contract, ARTIFACTS_PATH_ENV_VAR};

pub const FUZZ_ROUNDS: usize = 1;
const MAX_INPUT_SIZE: usize = 10;
const MAX_OUTPUT_SIZE: usize = 10;
pub const FUZZ_ROUNDS: usize = 10;
const MAX_INPUT_SIZE: usize = 16;
const MAX_OUTPUT_SIZE: usize = 16;

const POSEIDON_FULL_ROUNDS: usize = 2; // DUMMY VALUE
const POSEIDON_PARTIAL_ROUNDS: usize = 4; // DUMMY VALUE
Expand Down Expand Up @@ -126,10 +126,10 @@ pub fn random_input(len: usize) -> Vec<Scalar> {
pub async fn get_random_input_hashes(
account: &ScriptAccount,
) -> Result<(Vec<Scalar>, Vec<Scalar>)> {
let input_len = thread_rng().gen_range(0..MAX_INPUT_SIZE);
let input_len = thread_rng().gen_range(1..MAX_INPUT_SIZE);
let input = random_input(input_len);
let ark_input: Vec<Scalar::Field> = input.iter().map(|s| s.inner()).collect();
let num_elements = thread_rng().gen_range(0..MAX_OUTPUT_SIZE);
let num_elements = thread_rng().gen_range(1..MAX_OUTPUT_SIZE);

debug!(
"Absorbing {} elements, squeezing {} elements",
Expand Down Expand Up @@ -181,7 +181,7 @@ fn round_constants() -> Vec<Vec<Scalar::Field>> {

fn ark_poseidon_params() -> PoseidonConfig<Scalar::Field> {
PoseidonConfig::new(
POSEIDON_FULL_ROUNDS,
POSEIDON_FULL_ROUNDS * 2,
POSEIDON_PARTIAL_ROUNDS,
POSEIDON_ALPHA,
mds(),
Expand Down
2 changes: 0 additions & 2 deletions tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ fn get_test_starknet_config() -> StarknetConfig {
let invoke_max_steps = env::var(CAIRO_STEP_LIMIT_ENV_VAR)
.map_or(DEFAULT_INVOKE_MAX_STEPS, |s| s.parse::<u32>().unwrap());

debug!("Running sequencer with {} max steps", invoke_max_steps);

StarknetConfig {
env: Environment {
invoke_max_steps,
Expand Down

0 comments on commit efed026

Please sign in to comment.