-
Notifications
You must be signed in to change notification settings - Fork 3
Add sponge ops #86
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
Closed
Closed
Add sponge ops #86
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ad48adf
WIP: Add sponge ops
hratoanina 516346d
Move hash defs
hratoanina 38b1776
Empty private data
hratoanina 0dd3984
Remove sponge private data
hratoanina e2cb3c8
Restrict generics to the run function
hratoanina dbdb031
Clean up
hratoanina 384b22c
Merge branch 'main' into hamy/sponge_op
hratoanina 18c01fb
Fix merge
hratoanina e90a381
Fix merge and clean up
hratoanina 64a0cae
Cargo sort
hratoanina 2c7d7d9
Clippy
hratoanina c892877
Clippy again
hratoanina b4c48d6
Clean up comments
hratoanina 7c13921
Merge branch 'main' into hamy/sponge_op
hratoanina e6d7aac
Merge branch 'main' into hamy/sponge_op
hratoanina 798737b
Merge branch 'main' into hamy/sponge_op
hratoanina c9edd70
Defer trace generation to the multiprover
hratoanina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| use core::array; | ||
|
|
||
| /// Hash verification circuit: prove the correct computation of a hash. | ||
| /// Public inputs: inputs, output | ||
| use p3_baby_bear::{BabyBear, Poseidon2BabyBear}; | ||
| use p3_circuit::Challenger; | ||
| use p3_circuit::builder::CircuitBuilder; | ||
| use p3_circuit_prover::MultiTableProver; | ||
| use p3_circuit_prover::config::babybear_config::build_standard_config_babybear; | ||
| use p3_circuit_prover::prover::{ProverError, TablePacking}; | ||
| use p3_symmetric::{CryptographicHasher, PaddingFreeSponge}; | ||
| use rand::rngs::SmallRng; | ||
| use rand::{Rng, SeedableRng}; | ||
|
|
||
| type F = BabyBear; | ||
|
|
||
| const HASH_RATE: usize = 8; | ||
| const HASH_CAPACITY: usize = 8; | ||
| const HASH_STATE_SIZE: usize = HASH_RATE + HASH_CAPACITY; | ||
|
|
||
| fn main() -> Result<(), ProverError> { | ||
| let mut rng = SmallRng::seed_from_u64(1); | ||
| let perm = Poseidon2BabyBear::<HASH_STATE_SIZE>::new_from_rng_128(&mut rng); | ||
|
|
||
| let mut builder = CircuitBuilder::<F>::new(); | ||
|
|
||
| let mut challenger = Challenger::default(); | ||
|
|
||
| // Public inputs: hash inputs and hash output | ||
| let inputs: [_; HASH_STATE_SIZE] = array::from_fn(|_| builder.add_public_input()); | ||
| let outputs: [_; HASH_RATE] = array::from_fn(|_| builder.add_public_input()); | ||
|
|
||
| // Add hash operations | ||
| challenger.add_inputs(&inputs); | ||
| challenger.squeeze(&mut builder, &outputs); | ||
|
|
||
| let circuit = builder.build()?; | ||
| let mut runner = circuit.runner(); | ||
|
|
||
| // Generate random inputs | ||
| let input_values: [_; 16] = array::from_fn(|_| rng.random::<F>()); | ||
|
|
||
| // Set public inputs | ||
| let hasher = PaddingFreeSponge::<_, HASH_STATE_SIZE, HASH_RATE, HASH_RATE>::new(perm.clone()); | ||
| let output_values = hasher.hash_iter(input_values); | ||
| let public_inputs = input_values | ||
| .into_iter() | ||
| .chain(output_values) | ||
| .collect::<Vec<_>>(); | ||
| runner.set_public_inputs(&public_inputs)?; | ||
|
|
||
| let traces = runner.run::<_, HASH_STATE_SIZE, HASH_RATE, HASH_CAPACITY>(perm)?; | ||
| let config = build_standard_config_babybear(); | ||
| let table_packing = TablePacking::from_counts(4, 1); | ||
| let multi_prover = MultiTableProver::new(config).with_table_packing(table_packing); | ||
| let proof = multi_prover.prove_all_tables(&traces)?; | ||
| multi_prover.verify_all_tables(&proof)?; | ||
|
|
||
| println!("✅ Verified hash({:?}) = {:?}", input_values, output_values); | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could also do
default_babybear_poseidon2_16()