Skip to content

Commit

Permalink
Revisit the pow plugin and make it shorter and working.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaker committed Jun 2, 2024
1 parent 1ad84d3 commit ac49fa9
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 131 deletions.
37 changes: 26 additions & 11 deletions src/iopattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,45 @@ impl<H: DuplexHash<U>, U: Unit> IOPattern<H, U> {

/// Create a new IOPattern with the domain separator.
pub fn new(domsep: &str) -> Self {
assert!(!domsep.contains(SEP_BYTE), "Domain separator cannot contain the separator BYTE.");
assert!(
!domsep.contains(SEP_BYTE),
"Domain separator cannot contain the separator BYTE."
);
Self::from_string(domsep.to_string())
}

/// Absorb `count` native elements.
pub fn absorb(self, count: usize, label: &str) -> Self {
assert!(count > 0, "Count must be positive.");
assert!(!label.contains(SEP_BYTE), "Label cannot contain the separator BYTE.");
assert!(match label.chars().next() {
Some(char) => !char.is_ascii_digit(),
None => true,
}, "Label cannot start with a digit.");
assert!(
!label.contains(SEP_BYTE),
"Label cannot contain the separator BYTE."
);
assert!(
match label.chars().next() {
Some(char) => !char.is_ascii_digit(),
None => true,
},
"Label cannot start with a digit."
);

Self::from_string(self.io + SEP_BYTE + &format!("A{}", count) + label)
}

/// Squeeze `count` native elements.
pub fn squeeze(self, count: usize, label: &str) -> Self {
assert!(count > 0, "Count must be positive.");
assert!(!label.contains(SEP_BYTE), "Label cannot contain the separator BYTE.");
assert!(match label.chars().next() {
Some(char) => !char.is_ascii_digit(),
None => true,
}, "Label cannot start with a digit.");
assert!(
!label.contains(SEP_BYTE),
"Label cannot contain the separator BYTE."
);
assert!(
match label.chars().next() {
Some(char) => !char.is_ascii_digit(),
None => true,
},
"Label cannot start with a digit."
);

Self::from_string(self.io + SEP_BYTE + &format!("S{}", count) + label)
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ark/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rand::{CryptoRng, RngCore};
use super::{FieldChallenges, FieldPublic, GroupPublic};
use crate::plugins::bytes_uniform_modp;
use crate::{
Merlin, ByteChallenges, BytePublic, DuplexHash, IOPatternError, Arthur, ProofError,
Arthur, ByteChallenges, BytePublic, DuplexHash, IOPatternError, Merlin, ProofError,
ProofResult, Unit, UnitTranscript,
};

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ mod tests;
pub mod anemoi;

pub use crate::traits::*;
pub use crate::{hash::Unit, Merlin, DuplexHash, IOPattern, Arthur, ProofError, ProofResult, Safe};
pub use crate::{hash::Unit, Arthur, DuplexHash, IOPattern, Merlin, ProofError, ProofResult, Safe};

super::traits::field_traits!(ark_ff::Field);
super::traits::group_traits!(ark_ec::CurveGroup, Scalar: ark_ff::PrimeField);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/ark/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ark_serialize::CanonicalDeserialize;

use super::{FieldReader, GroupReader};
use crate::traits::*;
use crate::{DuplexHash, Arthur, ProofResult};
use crate::{Arthur, DuplexHash, ProofResult};

impl<'a, F, H> FieldReader<F> for Arthur<'a, H>
where
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ark/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(feature = "ark-bls12-381")]
use super::poseidon::PoseidonHash;
use crate::{DefaultHash, DuplexHash, IOPattern, Unit, UnitTranscript};
#[cfg(feature = "ark-bls12-381")]
use ark_bls12_381::Fr;
#[cfg(feature = "ark-bls12-381")]
use super::poseidon::PoseidonHash;

/// Test that the algebraic hashes do use the IV generated from the IO Pattern.
fn check_iv_is_used<H: DuplexHash<F>, F: Unit + Copy + Default + Eq + core::fmt::Debug>() {
Expand Down
2 changes: 1 addition & 1 deletion 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::{Merlin, DuplexHash, ProofResult, UnitTranscript};
use crate::{DuplexHash, Merlin, ProofResult, UnitTranscript};

impl<F: PrimeField, H: DuplexHash, R: RngCore + CryptoRng> FieldWriter<F> for Merlin<H, u8, R> {
fn add_scalars(&mut self, input: &[F]) -> ProofResult<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/group/reader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::FieldReader;
use crate::{ByteReader, DuplexHash, Arthur, ProofError};
use crate::{Arthur, ByteReader, DuplexHash, ProofError};
use group::ff::PrimeField;

impl<'a, F, H, const N: usize> FieldReader<F> for Arthur<'a, H>
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/group/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use group::{ff::PrimeField, Group, GroupEncoding};
use rand::{CryptoRng, RngCore};

use super::{FieldPublic, FieldWriter, GroupPublic, GroupWriter};
use crate::{Merlin, ByteWriter, DuplexHash, ProofResult};
use crate::{ByteWriter, DuplexHash, Merlin, ProofResult};

impl<F, H, R> FieldWriter<F> for Merlin<H, u8, R>
where
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Bindings for some popular libearies using zero-knowledge.
/// Extension traits macros, for both arkworks and group.
#[cfg(any(feature = "ark", feature = "group"))]
mod traits;

#[cfg(feature = "ark")]
Expand All @@ -12,15 +13,17 @@ pub mod ark;
/// This plugin is experimental and has not yet been thoroughly tested.
pub mod group;

/// Experimental PoW support
pub mod proof_of_work;
/// Proof of work (PoW) challenges.
pub mod pow;

/// Bits needed in order to obtain a (pseudo-random) uniform distribution in F.
#[allow(unused)]
pub(super) const fn bytes_uniform_modp(modulus_bits: u32) -> usize {
(modulus_bits as usize + 128) / 8
}

/// Bits needed in order to encode an element of F.
#[allow(unused)]
pub(super) const fn bytes_modp(modulus_bits: u32) -> usize {
(modulus_bits as usize + 7) / 8
}
Expand Down
108 changes: 0 additions & 108 deletions src/plugins/proof_of_work/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rand::RngCore;
use crate::hash::keccak::Keccak;
use crate::hash::legacy::DigestBridge;
use crate::{
Merlin, ByteChallenges, BytePublic, ByteReader, ByteWriter, DuplexHash, IOPattern, Safe,
ByteChallenges, BytePublic, ByteReader, ByteWriter, DuplexHash, IOPattern, Merlin, Safe,
};

type Sha2 = DigestBridge<sha2::Sha256>;
Expand Down
1 change: 0 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub trait UnitTranscript<U: Unit> {
fn fill_challenge_units(&mut self, output: &mut [U]) -> Result<(), IOPatternError>;
}


/// Absorbing bytes from the sponge, without reading or writing them into the protocol transcript.
///
/// This trait is trivial for byte-oriented sponges, but non-trivial for algebraic hashes.
Expand Down

0 comments on commit ac49fa9

Please sign in to comment.