-
Notifications
You must be signed in to change notification settings - Fork 31
Port to whir PR #215 + PR #217 with SHA-256 #288
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2e3c6a6
Port to whir PR #215 transcript API
Bisht13 19a371a
Add release-fast profile, jemalloc allocator, and reduce tracing noise
Bisht13 d4fde5c
Fix debug-mode test failures and update whir rev to 22efa1b2
Bisht13 ec2b49e
Restore Skyscraper as Merkle tree hash via whir HashEngine
Bisht13 9b91018
Port to whir Weights dyn trait API (PR #217) with pre-transform optim…
Bisht13 ac651c1
Address PR #288 review comments
Bisht13 e79f619
Switch from Skyscraper to SHA-256 for Merkle tree hash and transcript…
Bisht13 e706322
Address second round of PR #288 review comments
Bisht13 6c38794
Remove jemalloc from default features for macOS compatibility
Bisht13 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
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 |
|---|---|---|
| @@ -1,56 +1,44 @@ | ||
| use { | ||
| skyscraper::pow::{solve, verify}, | ||
| spongefish_pow::PowStrategy, | ||
| spongefish_pow::{PoWSolution, PowStrategy}, | ||
| zerocopy::transmute, | ||
| }; | ||
|
|
||
| /// Skyscraper proof of work | ||
| #[derive(Clone, Copy)] | ||
| pub struct SkyscraperPoW { | ||
| challenge: [u64; 4], | ||
| challenge: [u8; 32], | ||
| bits: f64, | ||
| } | ||
|
|
||
| impl PowStrategy for SkyscraperPoW { | ||
| fn new(challenge: [u8; 32], bits: f64) -> Self { | ||
| assert!((0.0..60.0).contains(&bits), "bits must be smaller than 60"); | ||
| Self { | ||
| challenge: transmute!(challenge), | ||
| bits, | ||
| } | ||
| Self { challenge, bits } | ||
| } | ||
|
|
||
| fn check(&mut self, nonce: u64) -> bool { | ||
| verify(self.challenge, self.bits, nonce) | ||
| verify(transmute!(self.challenge), self.bits, nonce) | ||
| } | ||
|
|
||
| fn solution(&self, nonce: u64) -> PoWSolution { | ||
| PoWSolution { | ||
| challenge: self.challenge, | ||
| nonce, | ||
| } | ||
| } | ||
|
|
||
| fn solve(&mut self) -> Option<u64> { | ||
| Some(solve(self.challenge, self.bits)) | ||
| fn solve(&mut self) -> Option<PoWSolution> { | ||
| let nonce = solve(transmute!(self.challenge), self.bits); | ||
| Some(self.solution(nonce)) | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_pow_skyscraper() { | ||
Bisht13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| use { | ||
| spongefish::{ | ||
| ByteDomainSeparator, BytesToUnitDeserialize, BytesToUnitSerialize, DefaultHash, | ||
| DomainSeparator, | ||
| }, | ||
| spongefish_pow::{PoWChallenge, PoWDomainSeparator}, | ||
| }; | ||
|
|
||
| const BITS: f64 = 10.0; | ||
|
|
||
| let iopattern = DomainSeparator::<DefaultHash>::new("the proof of work lottery 🎰") | ||
| .add_bytes(1, "something") | ||
| .challenge_pow("rolling dices"); | ||
|
|
||
| let mut prover = iopattern.to_prover_state(); | ||
| prover.add_bytes(b"\0").expect("Invalid IOPattern"); | ||
| prover.challenge_pow::<SkyscraperPoW>(BITS).unwrap(); | ||
|
|
||
| let mut verifier = iopattern.to_verifier_state(prover.narg_string()); | ||
| let byte = verifier.next_bytes::<1>().unwrap(); | ||
| assert_eq!(&byte, b"\0"); | ||
| verifier.challenge_pow::<SkyscraperPoW>(BITS).unwrap(); | ||
| let challenge = [42u8; 32]; | ||
| let bits = 10.0; | ||
| let mut pow = SkyscraperPoW::new(challenge, bits); | ||
| let solution = pow.solve().expect("should find nonce"); | ||
| assert_eq!(solution.challenge, challenge); | ||
| assert!(pow.check(solution.nonce)); | ||
| } | ||
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 |
|---|---|---|
| @@ -1,60 +1,41 @@ | ||
| use { | ||
| crate::FieldElement, | ||
| ark_bn254::Fr, | ||
| ark_ff::{BigInt, PrimeField}, | ||
| spongefish::duplex_sponge::{DuplexSponge, Permutation}, | ||
| zeroize::Zeroize, | ||
| spongefish::{DuplexSponge, Permutation}, | ||
| }; | ||
|
|
||
| fn to_fr(x: FieldElement) -> Fr { | ||
| Fr::new(BigInt(x.into_bigint().0)) | ||
| } | ||
| fn from_fr(x: Fr) -> FieldElement { | ||
| FieldElement::new(x.into_bigint()) | ||
| } | ||
|
|
||
| fn bigint_from_bytes_le<const N: usize>(bytes: &[u8]) -> BigInt<N> { | ||
| let limbs = bytes | ||
| .chunks_exact(8) | ||
| .map(|s| u64::from_le_bytes(s.try_into().unwrap())) | ||
| .collect::<Vec<_>>(); | ||
| BigInt::new(limbs.try_into().unwrap()) | ||
| } | ||
|
|
||
| type State = [FieldElement; 2]; | ||
|
|
||
| #[derive(Clone, Default, Zeroize)] | ||
| pub struct Skyscraper { | ||
| state: State, | ||
| } | ||
|
|
||
| impl AsRef<[FieldElement]> for Skyscraper { | ||
| fn as_ref(&self) -> &[FieldElement] { | ||
| &self.state | ||
| fn bytes_to_fr(bytes: &[u8]) -> Fr { | ||
| let mut limbs = [0u64; 4]; | ||
| for (i, chunk) in bytes.chunks_exact(8).enumerate() { | ||
| limbs[i] = u64::from_le_bytes(chunk.try_into().unwrap()); | ||
| } | ||
| Fr::new(BigInt(limbs)) | ||
| } | ||
| impl AsMut<[FieldElement]> for Skyscraper { | ||
| fn as_mut(&mut self) -> &mut [FieldElement] { | ||
| &mut self.state | ||
|
|
||
| fn fr_to_bytes(f: Fr) -> [u8; 32] { | ||
| let limbs = f.into_bigint().0; | ||
| let mut out = [0u8; 32]; | ||
| for (i, &limb) in limbs.iter().enumerate() { | ||
| out[i * 8..(i + 1) * 8].copy_from_slice(&limb.to_le_bytes()); | ||
| } | ||
| out | ||
| } | ||
|
|
||
| impl Permutation for Skyscraper { | ||
| type U = FieldElement; | ||
| const N: usize = 2; | ||
| const R: usize = 1; | ||
| #[derive(Clone, Default)] | ||
| pub struct Skyscraper; | ||
|
|
||
| fn new(iv: [u8; 32]) -> Self { | ||
| let felt = FieldElement::new(bigint_from_bytes_le(&iv)); | ||
| Self { | ||
| state: [0.into(), felt], | ||
| } | ||
| } | ||
| impl Permutation<64> for Skyscraper { | ||
| type U = u8; | ||
|
|
||
| fn permute(&mut self) { | ||
| let (l2, r2) = skyscraper::reference::permute(to_fr(self.state[0]), to_fr(self.state[1])); | ||
| self.state = [from_fr(l2), from_fr(r2)]; | ||
| fn permute(&self, state: &[u8; 64]) -> [u8; 64] { | ||
| let left = bytes_to_fr(&state[..32]); | ||
| let right = bytes_to_fr(&state[32..]); | ||
| let (l2, r2) = skyscraper::reference::permute(left, right); | ||
| let mut out = [0u8; 64]; | ||
| out[..32].copy_from_slice(&fr_to_bytes(l2)); | ||
| out[32..].copy_from_slice(&fr_to_bytes(r2)); | ||
| out | ||
| } | ||
| } | ||
|
|
||
| pub type SkyscraperSponge = DuplexSponge<Skyscraper>; | ||
| pub type SkyscraperSponge = DuplexSponge<Skyscraper, 64, 32>; |
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.
Uh oh!
There was an error while loading. Please reload this page.