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

Improve trait implementations for algebraic hashes #19

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Changes from 1 commit
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
Next Next commit
missing trait implementations
kustosz committed Nov 27, 2024
commit 001dd567ab3b7a77790c8572764cbe0809df59ab
1 change: 1 addition & 0 deletions nimue-pow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ blake3 = "1.5.4"
keccak = { version = "0.1.4"}
bytemuck = "1.17.1"
rayon = { version = "1.10.0", optional = true }
rand = "0.8.5"

[features]
default = ["parallel"]
23 changes: 14 additions & 9 deletions nimue-pow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
pub mod blake3;
pub mod keccak;

use nimue::{
Arthur, ByteChallenges, ByteIOPattern, ByteReader, ByteWriter, IOPattern, Merlin, ProofError,
ProofResult,
};
use nimue::{Arthur, ByteChallenges, ByteIOPattern, ByteReader, ByteWriter, DuplexHash, IOPattern, Merlin, ProofError, ProofResult, Unit};

/// [`IOPattern`] for proof-of-work challenges.
pub trait PoWIOPattern {
@@ -21,7 +18,10 @@ pub trait PoWIOPattern {
fn challenge_pow(self, label: &str) -> Self;
}

impl PoWIOPattern for IOPattern {
impl<IOPattern> PoWIOPattern for IOPattern
where
IOPattern: ByteIOPattern,
{
fn challenge_pow(self, label: &str) -> Self {
// 16 bytes challenge and 16 bytes nonce (that will be written)
self.challenge_bytes(32, label).add_bytes(8, "pow-nonce")
@@ -33,9 +33,12 @@ pub trait PoWChallenge {
fn challenge_pow<S: PowStrategy>(&mut self, bits: f64) -> ProofResult<()>;
}

impl PoWChallenge for Merlin
impl <H, U, R> PoWChallenge for Merlin<H, U, R>
where
Merlin: ByteWriter,
U: Unit,
H: DuplexHash<U>,
R: rand::CryptoRng + rand::RngCore,
Merlin<H, U, R>: ByteWriter + ByteChallenges,
{
fn challenge_pow<S: PowStrategy>(&mut self, bits: f64) -> ProofResult<()> {
let challenge = self.challenge_bytes()?;
@@ -47,9 +50,11 @@ where
}
}

impl<'a> PoWChallenge for Arthur<'a>
impl<'a, H, U> PoWChallenge for Arthur<'a, H, U>
where
Arthur<'a>: ByteReader,
U: Unit,
H: DuplexHash<U>,
Arthur<'a, H, U>: ByteReader + ByteChallenges,
{
fn challenge_pow<S: PowStrategy>(&mut self, bits: f64) -> ProofResult<()> {
let challenge = self.challenge_bytes()?;
25 changes: 23 additions & 2 deletions nimue/src/plugins/ark/common.rs
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ where
impl<F, T> FieldChallenges<F> for T
where
F: Field,
T: ByteChallenges,
T: UnitTranscript<u8>,
{
fn fill_challenge_scalars(&mut self, output: &mut [F]) -> ProofResult<()> {
let base_field_size = bytes_uniform_modp(F::BasePrimeField::MODULUS_BIT_SIZE);
@@ -96,6 +96,27 @@ where
}
}

impl<'a, H, C, const N: usize> FieldChallenges<Fp<C,N>> for Arthur<'a, H, Fp<C, N>>
where
C: FpConfig<N>,
H: DuplexHash<Fp<C, N>>,
{
fn fill_challenge_scalars(&mut self, output: &mut [Fp<C, N>]) -> ProofResult<()> {
self.fill_challenge_units(output).map_err(ProofError::InvalidIO)
}
}

impl<H, C, R, const N: usize> FieldChallenges<Fp<C,N>> for Merlin<H, Fp<C, N>, R>
where
C: FpConfig<N>,
H: DuplexHash<Fp<C, N>>,
R: CryptoRng + RngCore
{
fn fill_challenge_scalars(&mut self, output: &mut [Fp<C, N>]) -> ProofResult<()> {
self.fill_challenge_units(output).map_err(ProofError::InvalidIO)
}
}

// Field <-> Field interactions:

impl<F, H, R, C, const N: usize> FieldPublic<F> for Merlin<H, Fp<C, N>, R>
@@ -224,7 +245,7 @@ impl<'a, H, R, C, const N: usize> ByteChallenges for Merlin<H, Fp<C, N>, R>
where
C: FpConfig<N>,
H: DuplexHash<Fp<C, N>>,
R: CryptoRng + rand::RngCore,
R: CryptoRng + RngCore,
{
fn fill_challenge_bytes(&mut self, output: &mut [u8]) -> Result<(), IOPatternError> {
if output == &[] {
26 changes: 25 additions & 1 deletion nimue/src/plugins/ark/writer.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use ark_serialize::CanonicalSerialize;
use rand::{CryptoRng, RngCore};

use super::{FieldPublic, FieldWriter, GroupPublic, GroupWriter};
use crate::{DuplexHash, Merlin, ProofResult, UnitTranscript};
use crate::{Arthur, BytePublic, ByteReader, ByteWriter, DuplexHash, IOPatternError, Merlin, ProofResult, Unit, UnitTranscript};

impl<F: Field, H: DuplexHash, R: RngCore + CryptoRng> FieldWriter<F> for Merlin<H, u8, R> {
fn add_scalars(&mut self, input: &[F]) -> ProofResult<()> {
@@ -58,3 +58,27 @@ where
Ok(())
}
}

impl<H, R, C, const N: usize> ByteWriter for Merlin<H, Fp<C, N>, R>
where
H: DuplexHash<Fp<C, N>>,
C: FpConfig<N>,
R: RngCore + CryptoRng,
{
fn add_bytes(&mut self, input: &[u8]) -> Result<(), IOPatternError> {
self.public_bytes(input)?;
self.transcript.extend(input);
Ok(())
}
}

impl<'a, H, C, const N: usize> ByteReader for Arthur<'a, H, Fp<C, N>>
where
H: DuplexHash<Fp<C, N>>,
C: FpConfig<N>,
{
fn fill_next_bytes(&mut self, input: &mut [u8]) -> Result<(), IOPatternError> {
u8::read(&mut self.transcript, input)?;
self.public_bytes(input)
}
}