Skip to content

Commit

Permalink
missing trait implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
kustosz committed Nov 27, 2024
1 parent b28eb12 commit 001dd56
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
1 change: 1 addition & 0 deletions nimue-pow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
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 {
Expand All @@ -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")
Expand All @@ -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()?;
Expand All @@ -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()?;
Expand Down
25 changes: 23 additions & 2 deletions nimue/src/plugins/ark/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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>
Expand Down Expand Up @@ -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 == &[] {
Expand Down
26 changes: 25 additions & 1 deletion nimue/src/plugins/ark/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 001dd56

Please sign in to comment.