Skip to content

Commit

Permalink
Merge pull request #30 from iosis-tech/program_hash_pedersen
Browse files Browse the repository at this point in the history
pedersen commitment
  • Loading branch information
Okm165 authored Aug 1, 2024
2 parents 8e6b6a2 + 5d7752c commit 0c8131b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 50 deletions.
20 changes: 12 additions & 8 deletions crates/air/src/layout/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloc::vec;
use alloc::vec::Vec;
use global_values::{EcPoint, EcdsaSigConfig, GlobalValues, InteractionElements};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::{poseidon_hash_many, Felt};
use starknet_crypto::{pedersen_hash, Felt};
use swiftness_commitment::table::{commit::table_commit, decommit::table_decommit};
use swiftness_transcript::ensure;

Expand Down Expand Up @@ -379,20 +379,24 @@ impl LayoutTrait for Layout {
.flat_map(|v| vec![v.address, v.value])
.collect::<Vec<Felt>>();

// 1. Program segment
ensure!(initial_pc == INITIAL_PC, PublicInputError::MaxSteps);
ensure!(final_pc == INITIAL_PC + 4, PublicInputError::MaxSteps);

let program_end_pc = initial_fp - 2;
let program = &memory[initial_pc.to_bigint().try_into().unwrap()
..program_end_pc.to_bigint().try_into().unwrap()];

let program_hash = poseidon_hash_many(program);
let program: Vec<&Felt> = memory
.iter()
.skip(initial_pc.to_bigint().try_into().unwrap())
.step_by(2)
.take((program_end_pc - Felt::ONE).to_bigint().try_into().unwrap())
.collect();

let hash = program.iter().fold(Felt::ZERO, |acc, &e| pedersen_hash(&acc, e));
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into().unwrap();
// 3. Output segment
let output = &memory[memory.len() - output_len..memory.len()];
let output_hash = poseidon_hash_many(output);
let output = &memory[memory.len() - output_len..];
let output_hash = output.iter().fold(Felt::ZERO, |acc, e| pedersen_hash(&acc, e));

Ok((program_hash, output_hash))
}
Expand Down
20 changes: 12 additions & 8 deletions crates/air/src/layout/recursive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloc::vec;
use alloc::vec::Vec;
use global_values::{EcPoint, GlobalValues, InteractionElements};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::{poseidon_hash_many, Felt};
use starknet_crypto::{pedersen_hash, Felt};
use swiftness_commitment::table::{commit::table_commit, decommit::table_decommit};
use swiftness_transcript::ensure;

Expand Down Expand Up @@ -384,20 +384,24 @@ impl LayoutTrait for Layout {
.flat_map(|v| vec![v.address, v.value])
.collect::<Vec<Felt>>();

// 1. Program segment
ensure!(initial_pc == INITIAL_PC, PublicInputError::MaxSteps);
ensure!(final_pc == INITIAL_PC + 4, PublicInputError::MaxSteps);

let program_end_pc = initial_fp - 2;
let program = &memory[initial_pc.to_bigint().try_into().unwrap()
..program_end_pc.to_bigint().try_into().unwrap()];

let program_hash = poseidon_hash_many(program);
let program: Vec<&Felt> = memory
.iter()
.skip(initial_pc.to_bigint().try_into().unwrap())
.step_by(2)
.take((program_end_pc - Felt::ONE).to_bigint().try_into().unwrap())
.collect();

let hash = program.iter().fold(Felt::ZERO, |acc, &e| pedersen_hash(&acc, e));
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into().unwrap();
// 3. Output segment
let output = &memory[memory.len() - output_len..memory.len()];
let output_hash = poseidon_hash_many(output);
let output = &memory[memory.len() - output_len..];
let output_hash = output.iter().fold(Felt::ZERO, |acc, e| pedersen_hash(&acc, e));

Ok((program_hash, output_hash))
}
Expand Down
20 changes: 12 additions & 8 deletions crates/air/src/layout/recursive_with_poseidon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use alloc::vec;
use alloc::vec::Vec;
use global_values::{EcPoint, GlobalValues, InteractionElements};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::{poseidon_hash_many, Felt};
use starknet_crypto::{pedersen_hash, Felt};
use swiftness_commitment::table::{commit::table_commit, decommit::table_decommit};
use swiftness_transcript::ensure;

Expand Down Expand Up @@ -426,20 +426,24 @@ impl LayoutTrait for Layout {
.flat_map(|v| vec![v.address, v.value])
.collect::<Vec<Felt>>();

// 1. Program segment
ensure!(initial_pc == INITIAL_PC, PublicInputError::MaxSteps);
ensure!(final_pc == INITIAL_PC + 4, PublicInputError::MaxSteps);

let program_end_pc = initial_fp - 2;
let program = &memory[initial_pc.to_bigint().try_into().unwrap()
..program_end_pc.to_bigint().try_into().unwrap()];

let program_hash = poseidon_hash_many(program);
let program: Vec<&Felt> = memory
.iter()
.skip(initial_pc.to_bigint().try_into().unwrap())
.step_by(2)
.take((program_end_pc - Felt::ONE).to_bigint().try_into().unwrap())
.collect();

let hash = program.iter().fold(Felt::ZERO, |acc, &e| pedersen_hash(&acc, e));
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into().unwrap();
// 3. Output segment
let output = &memory[memory.len() - output_len..memory.len()];
let output_hash = poseidon_hash_many(output);
let output = &memory[memory.len() - output_len..];
let output_hash = output.iter().fold(Felt::ZERO, |acc, e| pedersen_hash(&acc, e));

Ok((program_hash, output_hash))
}
Expand Down
20 changes: 12 additions & 8 deletions crates/air/src/layout/small/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloc::vec;
use alloc::vec::Vec;
use global_values::{EcPoint, EcdsaSigConfig, GlobalValues, InteractionElements};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::{poseidon_hash_many, Felt};
use starknet_crypto::{pedersen_hash, Felt};
use swiftness_commitment::table::{commit::table_commit, decommit::table_decommit};
use swiftness_transcript::ensure;

Expand Down Expand Up @@ -379,20 +379,24 @@ impl LayoutTrait for Layout {
.flat_map(|v| vec![v.address, v.value])
.collect::<Vec<Felt>>();

// 1. Program segment
ensure!(initial_pc == INITIAL_PC, PublicInputError::MaxSteps);
ensure!(final_pc == INITIAL_PC + 4, PublicInputError::MaxSteps);

let program_end_pc = initial_fp - 2;
let program = &memory[initial_pc.to_bigint().try_into().unwrap()
..program_end_pc.to_bigint().try_into().unwrap()];

let program_hash = poseidon_hash_many(program);
let program: Vec<&Felt> = memory
.iter()
.skip(initial_pc.to_bigint().try_into().unwrap())
.step_by(2)
.take((program_end_pc - Felt::ONE).to_bigint().try_into().unwrap())
.collect();

let hash = program.iter().fold(Felt::ZERO, |acc, &e| pedersen_hash(&acc, e));
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into().unwrap();
// 3. Output segment
let output = &memory[memory.len() - output_len..memory.len()];
let output_hash = poseidon_hash_many(output);
let output = &memory[memory.len() - output_len..];
let output_hash = output.iter().fold(Felt::ZERO, |acc, e| pedersen_hash(&acc, e));

Ok((program_hash, output_hash))
}
Expand Down
20 changes: 12 additions & 8 deletions crates/air/src/layout/starknet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use alloc::vec;
use alloc::vec::Vec;
use global_values::{CurveConfig, EcPoint, EcdsaSigConfig, GlobalValues, InteractionElements};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::{poseidon_hash_many, Felt};
use starknet_crypto::{pedersen_hash, Felt};
use swiftness_commitment::table::{commit::table_commit, decommit::table_decommit};
use swiftness_transcript::ensure;

Expand Down Expand Up @@ -468,20 +468,24 @@ impl LayoutTrait for Layout {
.flat_map(|v| vec![v.address, v.value])
.collect::<Vec<Felt>>();

// 1. Program segment
ensure!(initial_pc == INITIAL_PC, PublicInputError::MaxSteps);
ensure!(final_pc == INITIAL_PC + 4, PublicInputError::MaxSteps);

let program_end_pc = initial_fp - 2;
let program = &memory[initial_pc.to_bigint().try_into().unwrap()
..program_end_pc.to_bigint().try_into().unwrap()];

let program_hash = poseidon_hash_many(program);
let program: Vec<&Felt> = memory
.iter()
.skip(initial_pc.to_bigint().try_into().unwrap())
.step_by(2)
.take((program_end_pc - Felt::ONE).to_bigint().try_into().unwrap())
.collect();

let hash = program.iter().fold(Felt::ZERO, |acc, &e| pedersen_hash(&acc, e));
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into().unwrap();
// 3. Output segment
let output = &memory[memory.len() - output_len..memory.len()];
let output_hash = poseidon_hash_many(output);
let output = &memory[memory.len() - output_len..];
let output_hash = output.iter().fold(Felt::ZERO, |acc, e| pedersen_hash(&acc, e));

Ok((program_hash, output_hash))
}
Expand Down
20 changes: 12 additions & 8 deletions crates/air/src/layout/starknet_with_keccak/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use alloc::vec;
use alloc::vec::Vec;
use global_values::{CurveConfig, EcPoint, EcdsaSigConfig, GlobalValues, InteractionElements};
use starknet_core::types::NonZeroFelt;
use starknet_crypto::{poseidon_hash_many, Felt};
use starknet_crypto::{pedersen_hash, Felt};
use swiftness_commitment::table::{commit::table_commit, decommit::table_decommit};
use swiftness_transcript::ensure;

Expand Down Expand Up @@ -502,20 +502,24 @@ impl LayoutTrait for Layout {
.flat_map(|v| vec![v.address, v.value])
.collect::<Vec<Felt>>();

// 1. Program segment
ensure!(initial_pc == INITIAL_PC, PublicInputError::MaxSteps);
ensure!(final_pc == INITIAL_PC + 4, PublicInputError::MaxSteps);

let program_end_pc = initial_fp - 2;
let program = &memory[initial_pc.to_bigint().try_into().unwrap()
..program_end_pc.to_bigint().try_into().unwrap()];

let program_hash = poseidon_hash_many(program);
let program: Vec<&Felt> = memory
.iter()
.skip(initial_pc.to_bigint().try_into().unwrap())
.step_by(2)
.take((program_end_pc - Felt::ONE).to_bigint().try_into().unwrap())
.collect();

let hash = program.iter().fold(Felt::ZERO, |acc, &e| pedersen_hash(&acc, e));
let program_hash = pedersen_hash(&hash, &Felt::from(program.len()));

let output_len: usize = (output_stop - output_start).to_bigint().try_into().unwrap();
// 3. Output segment
let output = &memory[memory.len() - output_len..memory.len()];
let output_hash = poseidon_hash_many(output);
let output = &memory[memory.len() - output_len..];
let output_hash = output.iter().fold(Felt::ZERO, |acc, e| pedersen_hash(&acc, e));

Ok((program_hash, output_hash))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/stark/src/tests/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ fn test_stark_proof_fibonacci_verify() {
assert_eq!(
program_hash,
Felt::from_hex_unchecked(
"0x603f45d671891116de1e763d11d71d25102ff93707dafc97a8d06e18145baf5"
"0x9f6693f4a5610a46b5d71ef573c43bef5f0d111fc1c5e506d509c458a29bae"
)
);
assert_eq!(
output_hash,
Felt::from_hex_unchecked(
"0x21e35055ced9a22156eed737abcea133f8406f7f7e78222cf4f0f1271216adb"
"0x615be5409121774c863b7dfefd55d0bcab6d1d09eeecbbc4ee3b88daaa69c81"
)
);
}

0 comments on commit 0c8131b

Please sign in to comment.