Skip to content

Commit

Permalink
Renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaker committed Sep 2, 2023
1 parent 8ed454e commit cd86b2f
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 56 deletions.
4 changes: 2 additions & 2 deletions examples/bulletproof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use ark_bls12_381::G1Projective;
use ark_ec::{AffineRepr, CurveGroup, VariableBaseMSM};
use ark_ff::Field;
use ark_std::{log2, Zero};
use nimue::arkworks_plugins::{Absorbable, AlgebraicIO};
use nimue::ark_plugins::{Absorbable, AlgebraicIO};
use nimue::IOPattern;
use nimue::{
arkworks_plugins::{Absorbs, FieldChallenges},
ark_plugins::{Absorbs, FieldChallenges},
Arthur, Duplexer, InvalidTag, Merlin,
};
use rand::rngs::OsRng;
Expand Down
30 changes: 15 additions & 15 deletions examples/schnorr.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
use ark_ec::{AffineRepr, CurveGroup};
use ark_serialize::CanonicalSerialize;
use ark_std::UniformRand;
use nimue::arkworks_plugins::{Absorbable, Absorbs, AlgebraicIO, FieldChallenges};
use nimue::ark_plugins::{Absorbable, Absorbs, AlgebraicIO, FieldChallenges};
use nimue::{Arthur, Duplexer, IOPattern, InvalidTag, Merlin};

trait SchnorrIOPattern {
fn schnorr_statement<G, S: Duplexer>(&self) -> Self
fn schnorr_statement<G, H: Duplexer>(&self) -> Self
where
G: AffineRepr + Absorbable<S::L>;
G: AffineRepr + Absorbable<H::L>;

fn schnorr_io<G, S: Duplexer>(&self) -> Self
fn schnorr_io<G, H: Duplexer>(&self) -> Self
where
G: AffineRepr + Absorbable<S::L>;
G: AffineRepr + Absorbable<H::L>;
}

impl SchnorrIOPattern for IOPattern {
fn schnorr_statement<G, S: Duplexer>(&self) -> Self
fn schnorr_statement<G, H: Duplexer>(&self) -> Self
where
G: AffineRepr + Absorbable<S::L>,
G: AffineRepr + Absorbable<H::L>,
{
// the statement: generator and public key
AlgebraicIO::<S>::from(self)
AlgebraicIO::<H>::from(self)
.absorb_point::<G>(2)
// (optional) allow for preprocessing of the generators
.into()
}

/// A Schnorr signature's IO Pattern.
fn schnorr_io<G, S: Duplexer>(&self) -> IOPattern
fn schnorr_io<G, H: Duplexer>(&self) -> IOPattern
where
G: AffineRepr + Absorbable<S::L>,
G: AffineRepr + Absorbable<H::L>,
{
AlgebraicIO::<S>::from(self)
AlgebraicIO::<H>::from(self)
// absorb the commitment
.absorb_point::<G>(1)
// challenge in bytes
Expand All @@ -40,8 +40,8 @@ impl SchnorrIOPattern for IOPattern {
}
}

fn prove<S: Duplexer, G: AffineRepr + Absorbable<S::L>>(
transcript: &mut Arthur<S>,
fn prove<H: Duplexer, G: AffineRepr + Absorbable<H::L>>(
transcript: &mut Arthur<H>,
witness: G::ScalarField,
) -> Result<(G::ScalarField, G::ScalarField), InvalidTag> {
// Commitment: use the prover transcript to seed randomness.
Expand All @@ -56,8 +56,8 @@ fn prove<S: Duplexer, G: AffineRepr + Absorbable<S::L>>(
Ok(proof)
}

fn verify<S: Duplexer, G: AffineRepr + Absorbable<S::L>>(
transcript: &mut Merlin<S>,
fn verify<H: Duplexer, G: AffineRepr + Absorbable<H::L>>(
transcript: &mut Merlin<H>,
g: G,
pk: G,
proof: (G::ScalarField, G::ScalarField),
Expand Down
File renamed without changes.
File renamed without changes.
32 changes: 16 additions & 16 deletions src/arkworks_plugins/iopattern.rs → src/ark_plugins/iopattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use super::Absorbable;
/// An IOPattern
/// that is aware of the sponge used and understands arkworks types,
/// such as fields and group elements.
pub struct AlgebraicIO<S: Duplexer> {
_sponge: ::core::marker::PhantomData<S>,
pub struct AlgebraicIO<H: Duplexer> {
_sponge: ::core::marker::PhantomData<H>,
iop: IOPattern,
}

impl<S: Duplexer, B: Borrow<IOPattern>> From<B> for AlgebraicIO<S> {
impl<H: Duplexer, B: Borrow<IOPattern>> From<B> for AlgebraicIO<H> {
fn from(value: B) -> Self {
AlgebraicIO {
_sponge: Default::default(),
Expand All @@ -22,9 +22,9 @@ impl<S: Duplexer, B: Borrow<IOPattern>> From<B> for AlgebraicIO<S> {
}
}

impl<S> AlgebraicIO<S>
impl<H> AlgebraicIO<H>
where
S: Duplexer,
H: Duplexer,
{
pub fn new(domsep: &str) -> Self {
Self {
Expand All @@ -33,23 +33,23 @@ where
}
}

pub fn absorb<T: Absorbable<S::L>>(self, count: usize) -> Self {
pub fn absorb<T: Absorbable<H::L>>(self, count: usize) -> Self {
self.iop.absorb(T::absorb_size() * count, "nat").into()
}

pub fn absorb_bytes(self, count: usize) -> Self {
let count = crate::div_ceil!(count, S::L::compressed_size());
let count = crate::div_ceil!(count, H::L::compressed_size());
self.iop.absorb(count, "").into()
}

pub fn absorb_point<A>(self, count: usize) -> Self
where
A: AffineRepr + Absorbable<S::L>,
A: AffineRepr + Absorbable<H::L>,
{
self.iop.absorb(A::absorb_size() * count, "GG").into()
}

pub fn absorb_field<F: Field + Absorbable<S::L>>(self, count: usize) -> Self {
pub fn absorb_field<F: Field + Absorbable<H::L>>(self, count: usize) -> Self {
self.iop.absorb(F::absorb_size() * count, "GG").into()
}

Expand All @@ -58,7 +58,7 @@ where
}

pub fn squeeze_bytes(self, count: usize) -> Self {
let count = crate::div_ceil!(count, S::L::extractable_bytelen());
let count = crate::div_ceil!(count, H::L::extractable_bytelen());
self.iop.squeeze(count, "").into()
}

Expand All @@ -69,20 +69,20 @@ where
}
}

impl<S: Duplexer> From<AlgebraicIO<S>> for IOPattern {
fn from(value: AlgebraicIO<S>) -> Self {
impl<H: Duplexer> From<AlgebraicIO<H>> for IOPattern {
fn from(value: AlgebraicIO<H>) -> Self {
value.iop
}
}

impl<S: Duplexer> From<AlgebraicIO<S>> for Arthur<S> {
fn from(value: AlgebraicIO<S>) -> Self {
impl<H: Duplexer> From<AlgebraicIO<H>> for Arthur<H> {
fn from(value: AlgebraicIO<H>) -> Self {
IOPattern::from(value).into()
}
}

impl<S: Duplexer> From<AlgebraicIO<S>> for Merlin<S> {
fn from(value: AlgebraicIO<S>) -> Self {
impl<H: Duplexer> From<AlgebraicIO<H>> for Merlin<H> {
fn from(value: AlgebraicIO<H>) -> Self {
IOPattern::from(value).into()
}
}
4 changes: 2 additions & 2 deletions src/arkworks_plugins/mod.rs → src/ark_plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<const N: usize, C: FpConfig<N>, P: SWCurveConfig<BaseField = Fp<C, N>>> Abs

fn to_absorbable(&self) -> Vec<Fp<C, N>> {
let (x, y) = self.xy().unwrap();
// XXX. this is a hack just to make sure that we are compatible also
// XXX. this clone is a hack just to make sure that we are compatible also
// with HEAD on arkworks algebra, where .xy() returns references.
vec![x.clone(), y.clone()]
}
Expand Down Expand Up @@ -112,7 +112,7 @@ macro_rules! impl_absorbable {
macro_rules! impl_lane {
($t:ty, $n: expr) => {
impl Lane for $t {
fn random_bytes_size() -> usize {
const fn random_bytes_size() -> usize {
$n
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use ark_std::UniformRand;

use super::{DuplexSponge, Lane, SpongeConfig};
use crate::sponge::{
poseidon::{PoseidonConfig, PoseidonDefaultConfigField, PoseidonSponge},
CryptographicSponge,
};
use crate::sponge::CryptographicSponge;
use crate::sponge::poseidon::{PoseidonConfig, PoseidonDefaultConfigField, PoseidonSponge};

use ark_std::UniformRand;

impl<F: Lane + PoseidonDefaultConfigField> SpongeConfig for PoseidonSponge<F> {
type L = F;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ This crate doesn't support big-endian targets.

/// Extensions for arkworks types.
#[cfg(feature = "arkworks")]
pub mod arkworks_plugins;
pub mod ark_plugins;

/// Support for legacy hash functions (SHA2).
pub mod legacy;
Expand Down
18 changes: 9 additions & 9 deletions src/merlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ use core::borrow::Borrow;
/// Merlin is wrapper around a sponge that provides a secure
/// Fiat-Shamir implementation for public-coin protocols.
#[derive(Clone)]
pub struct Merlin<S = DefaultHash>
pub struct Merlin<H = DefaultHash>
where
S: Duplexer,
H: Duplexer,
{
safe: Safe<S>,
safe: Safe<H>,
leftovers: Vec<u8>,
}

impl<S: Duplexer> Merlin<S> {
impl<H: Duplexer> Merlin<H> {
/// Creates a new [`Merlin`] instance with the given sponge and IO Pattern.
///
/// The resulting object will act as the verifier in a zero-knowledge protocol.
pub fn new(io_pattern: &IOPattern) -> Self {
let safe = Safe::new(io_pattern);
let leftovers = Vec::with_capacity(S::L::extractable_bytelen());
let leftovers = Vec::with_capacity(H::L::extractable_bytelen());
Self { safe, leftovers }
}

/// Absorb a slice of lanes into the sponge.
pub fn append(&mut self, input: &[S::L]) -> Result<&mut Self, InvalidTag> {
pub fn append(&mut self, input: &[H::L]) -> Result<&mut Self, InvalidTag> {
self.leftovers.clear();
self.safe.absorb(input)?;
Ok(self)
Expand All @@ -39,7 +39,7 @@ impl<S: Duplexer> Merlin<S> {
}

/// Signals the end of the statement and returns the (compressed) sponge state.
pub fn divide_and_store(self) -> Result<Vec<S::L>, InvalidTag> {
pub fn divide_and_store(self) -> Result<Vec<H::L>, InvalidTag> {
self.safe.divide_and_store()
}

Expand All @@ -53,13 +53,13 @@ impl<S: Duplexer> Merlin<S> {
// }
}

impl<S: Duplexer, B: Borrow<IOPattern>> From<B> for Merlin<S> {
impl<H: Duplexer, B: Borrow<IOPattern>> From<B> for Merlin<H> {
fn from(io_pattern: B) -> Self {
Merlin::new(io_pattern.borrow())
}
}

impl<S: Duplexer> ::core::fmt::Debug for Merlin<S> {
impl<H: Duplexer> core::fmt::Debug for Merlin<H> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("Merlin").field(&self.safe).finish()
}
Expand Down
12 changes: 7 additions & 5 deletions src/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ impl IOPattern {
let mut stack = VecDeque::new();

// skip the domain separator
for part in io_pattern.split(|&b| b as char ==' ').into_iter().skip(1) {
for part in io_pattern.split(|&b| b as char == ' ').into_iter().skip(1) {
let next_id = part[0] as char;
let next_length = part[1..].into_iter().take_while(|x| x.is_ascii_digit()).fold(0, |acc, x| acc * 10 + (x - b'0') as usize);
let next_length = part[1..]
.into_iter()
.take_while(|x| x.is_ascii_digit())
.fold(0, |acc, x| acc * 10 + (x - b'0') as usize);

// check that next_length != 0 is performed internally on Op::new
let next_op = Op::new(next_id, Some(next_length))?;
Expand All @@ -108,7 +111,6 @@ impl IOPattern {
} else {
// guaranteed never to fail, since:
assert!(dst.len() > 0 && !stack.is_empty());

let previous = dst.pop_back().unwrap();
let next = stack.pop_front().unwrap();

Expand Down Expand Up @@ -306,15 +308,15 @@ impl<D: Duplexer> Safe<D> {
}
}

impl<S: Duplexer> Drop for Safe<S> {
impl<H: Duplexer> Drop for Safe<H> {
/// Destroy the sponge state.
fn drop(&mut self) {
assert!(self.stack.is_empty());
self.sponge.zeroize();
}
}

impl<S: Duplexer> ::core::fmt::Debug for Safe<S> {
impl<H: Duplexer> ::core::fmt::Debug for Safe<H> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
// Ensure that the state isn't accidentally logged,
// but provide the remaining IO Pattern for debugging.
Expand Down
4 changes: 3 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn test_unfinished_io() {
/// Challenges from the same transcript should be equal.
#[test]
fn test_deterministic() {
let iop = IOPattern::new("example.com").absorb(3, "elt").squeeze(16, "elt");
let iop = IOPattern::new("example.com")
.absorb(3, "elt")
.squeeze(16, "elt");
let mut first_merlin = Merlin::<Keccak>::new(&iop);
let mut second_merlin = Merlin::<Keccak>::new(&iop);

Expand Down

0 comments on commit cd86b2f

Please sign in to comment.