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
Show file tree
Hide file tree
Changes from all commits
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
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
5 changes: 3 additions & 2 deletions nimue-pow/src/blake3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ impl Blake3PoW {

#[test]
fn test_pow_blake3() {
use crate::{ByteIOPattern, ByteReader, ByteWriter, IOPattern, PoWChallenge, PoWIOPattern};
use crate::{ByteIOPattern, ByteReader, ByteWriter, PoWChallenge, PoWIOPattern};
use nimue::{DefaultHash, IOPattern};

const BITS: f64 = 10.0;

let iopattern = IOPattern::new("the proof of work lottery 🎰")
let iopattern = IOPattern::<DefaultHash>::new("the proof of work lottery 🎰")
.add_bytes(1, "something")
.challenge_pow("rolling dices");

Expand Down
5 changes: 3 additions & 2 deletions nimue-pow/src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ impl PowStrategy for KeccakPoW {

#[test]
fn test_pow_keccak() {
use crate::{ByteIOPattern, ByteReader, ByteWriter, IOPattern, PoWChallenge, PoWIOPattern};
use crate::{ByteIOPattern, ByteReader, ByteWriter, PoWChallenge, PoWIOPattern};
use nimue::{DefaultHash, IOPattern};

const BITS: f64 = 10.0;

let iopattern = IOPattern::new("the proof of work lottery 🎰")
let iopattern = IOPattern::<DefaultHash>::new("the proof of work lottery 🎰")
.add_bytes(1, "something")
.challenge_pow("rolling dices");

Expand Down
22 changes: 15 additions & 7 deletions nimue-pow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ pub mod blake3;
pub mod keccak;

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

/// [`IOPattern`] for proof-of-work challenges.
Expand All @@ -21,7 +21,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 +36,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 +53,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
35 changes: 29 additions & 6 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,29 @@ where
}
}

impl<H, C, const N: usize> FieldChallenges<Fp<C, N>> for Arthur<'_, 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 @@ -193,7 +216,7 @@ where

// Field <-> Bytes interactions:

impl<'a, H, C, const N: usize> BytePublic for Arthur<'a, H, Fp<C, N>>
impl<H, C, const N: usize> BytePublic for Arthur<'_, H, Fp<C, N>>
where
C: FpConfig<N>,
H: DuplexHash<Fp<C, N>>,
Expand All @@ -206,7 +229,7 @@ where
}
}

impl<'a, H, R, C, const N: usize> BytePublic for Merlin<H, Fp<C, N>, R>
impl<H, R, C, const N: usize> BytePublic for Merlin<H, Fp<C, N>, R>
where
C: FpConfig<N>,
H: DuplexHash<Fp<C, N>>,
Expand All @@ -220,11 +243,11 @@ where
}
}

impl<'a, H, R, C, const N: usize> ByteChallenges for Merlin<H, Fp<C, N>, R>
impl<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 All @@ -246,7 +269,7 @@ where
}

/// XXX. duplicate code
impl<'a, H, C, const N: usize> ByteChallenges for Arthur<'a, H, Fp<C, N>>
impl<H, C, const N: usize> ByteChallenges for Arthur<'_, H, Fp<C, N>>
where
C: FpConfig<N>,
H: DuplexHash<Fp<C, N>>,
Expand Down
29 changes: 28 additions & 1 deletion nimue/src/plugins/ark/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ 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 +61,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)
}
}
Loading