Skip to content

Commit bcaa978

Browse files
committed
More refactor
1 parent bac8105 commit bcaa978

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+685
-1056
lines changed

nimue-anemoi/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ license = "MIT/Apache-2.0"
66

77
[dependencies]
88
ark-ff = { workspace = true }
9-
nimue = { workspace = true, features = ["ark"] }
9+
nimue = { workspace = true, features = ["arkworks-algebra"] }
1010
zeroize = { workspace = true }
1111
anemoi = { workspace = true, features = ["bls12_381"] }
1212
ark-bls12-381 = { workspace = true }

nimue-poseidon/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
license = "MIT/Apache-2.0"
66

77
[dependencies]
8-
nimue = { workspace = true, features = ["ark"] }
8+
nimue = { workspace = true, features = ["arkworks-algebra"] }
99
ark-ff = { workspace = true }
1010
zeroize = { workspace = true }
1111
ark-bls12-381 = { workspace = true, optional = true }
@@ -21,8 +21,8 @@ blake2 = { workspace = true }
2121
[features]
2222
bn254 = ["ark-bn254"]
2323
solinas = []
24-
bls12-381 = ["nimue/ark", "dep:ark-bls12-381"]
25-
ark-bls12-381 = ["nimue/ark", "dep:ark-bls12-381"]
24+
bls12-381 = ["nimue/arkworks-algebra", "dep:ark-bls12-381"]
25+
ark-bls12-381 = ["nimue/arkworks-algebra", "dep:ark-bls12-381"]
2626

2727
[[example]]
2828
name = "schnorr_algebraic_hash"

nimue-poseidon/examples/schnorr_algebraic_hash.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
use ark_ec::{CurveGroup, PrimeGroup};
55
use ark_ff::PrimeField;
66
use ark_std::UniformRand;
7-
use nimue::codecs::ark::*;
7+
use nimue::codecs::arkworks_algebra::*;
88

99
/// Extend the IO pattern with the Schnorr protocol.
10-
trait SchnorrIOPattern<G: CurveGroup> {
10+
trait SchnorrDomainSeparator<G: CurveGroup> {
1111
/// Adds the entire Schnorr protocol to the IO pattern (statement and proof).
1212
fn add_schnorr_io(self) -> Self;
1313
}
1414

15-
impl<G, H, U> SchnorrIOPattern<G> for IOPattern<H, U>
15+
impl<G, H, U> SchnorrDomainSeparator<G> for DomainSeparator<H, U>
1616
where
1717
G: CurveGroup,
1818
U: Unit,
1919
H: DuplexInterface<U>,
20-
IOPattern<H, U>: GroupIOPattern<G> + ByteIOPattern,
20+
DomainSeparator<H, U>: GroupDomainSeparator<G> + ByteDomainSeparator,
2121
{
2222
fn add_schnorr_io(self) -> Self {
2323
self.add_points(1, "generator (P)")
@@ -39,15 +39,15 @@ fn keygen<G: CurveGroup>() -> (G::ScalarField, G) {
3939
}
4040

4141
/// The prove algorithm takes as input
42-
/// - the prover state `ProverTranscript`, that has access to a random oracle `H` and can absorb/squeeze elements from the group `G`.
42+
/// - the prover state `ProverState`, that has access to a random oracle `H` and can absorb/squeeze elements from the group `G`.
4343
/// - The generator `P` in the group.
4444
/// - the secret key $x \in \mathbb{Z}_p$
4545
/// It returns a zero-knowledge proof of knowledge of `x` as a sequence of bytes.
4646
#[allow(non_snake_case)]
4747
fn prove<G, H, U>(
4848
// the hash function `H` works over bytes.
4949
// Algebraic hashes over a particular domain can be denoted with an additional type argument implementing `nimue::Unit`.
50-
merlin: &mut ProverTranscript<H, U>,
50+
merlin: &mut ProverState<H, U>,
5151
// the generator
5252
P: G,
5353
// the secret key
@@ -58,9 +58,9 @@ where
5858
G::BaseField: PrimeField,
5959
H: DuplexInterface<U>,
6060
G: CurveGroup,
61-
ProverTranscript<H, U>: GroupWriter<G> + FieldWriter<G::BaseField> + ByteChallenges,
61+
ProverState<H, U>: ProverMessageGroup<G> + ProverMessageField<G::BaseField> + VerifierMessageBytes,
6262
{
63-
// `ProverTranscript` types implement a cryptographically-secure random number generator that is tied to the protocol transcript
63+
// `ProverState` types implement a cryptographically-secure random number generator that is tied to the protocol transcript
6464
// and that can be accessed via the `rng()` function.
6565
let k = G::ScalarField::rand(merlin.rng());
6666
let K = P * k;
@@ -79,18 +79,18 @@ where
7979
merlin.add_scalars(&[r_q])?;
8080

8181
// Output the current protocol transcript as a sequence of bytes.
82-
Ok(merlin.transcript())
82+
Ok(merlin.narg_string())
8383
}
8484

8585
/// The verify algorithm takes as input
86-
/// - the verifier state `VerifierTranscript`, that has access to a random oracle `H` and can deserialize/squeeze elements from the group `G`.
86+
/// - the verifier state `VerifierState`, that has access to a random oracle `H` and can deserialize/squeeze elements from the group `G`.
8787
/// - the secret key `witness`
8888
/// It returns a zero-knowledge proof of knowledge of `witness` as a sequence of bytes.
8989
#[allow(non_snake_case)]
9090
fn verify<'a, G, H, U>(
9191
// `ArkGroupMelin` contains the veirifier state, including the messages currently read. In addition, it is aware of the group `G`
9292
// from which it can serialize/deserialize elements.
93-
arthur: &mut VerifierTranscript<'a, H, U>,
93+
arthur: &mut VerifierState<'a, H, U>,
9494
// The group generator `P``
9595
P: G,
9696
// The public key `X`
@@ -101,7 +101,7 @@ where
101101
G::BaseField: PrimeField,
102102
G: CurveGroup,
103103
H: DuplexInterface<U>,
104-
VerifierTranscript<'a, H, U>: GroupReader<G> + FieldReader<G::BaseField> + ByteChallenges,
104+
VerifierState<'a, H, U>: DeserializeGroup<G> + DeserializeField<G::BaseField> + VerifierMessageBytes,
105105
{
106106
// Read the protocol from the transcript:
107107
let [K] = arthur.next_points()?;
@@ -137,15 +137,15 @@ fn main() {
137137
// type U = ark_bls12_381::Fq;
138138

139139
// Set up the IO for the protocol transcript with domain separator "nimue::examples::schnorr"
140-
let io = IOPattern::<H, U>::new("nimue::examples::schnorr");
141-
let io = SchnorrIOPattern::<G>::add_schnorr_io(io);
140+
let domain_separator = DomainSeparator::<H, U>::new("nimue::examples::schnorr");
141+
let domain_separator = SchnorrDomainSeparator::<G>::add_schnorr_io(domain_separator);
142142

143143
// Set up the elements to prove
144144
let P = G::generator();
145145
let (x, X) = keygen();
146146

147147
// Create the prover transcript, add the statement to it, and then invoke the prover.
148-
let mut merlin = io.to_merlin();
148+
let mut merlin = domain_separator.to_merlin();
149149
merlin.public_points(&[P, X]).unwrap();
150150
merlin.ratchet().unwrap();
151151
let proof = prove(&mut merlin, P, x).expect("Invalid proof");
@@ -154,7 +154,7 @@ fn main() {
154154
println!("Here's a Schnorr signature:\n{}", hex::encode(proof));
155155

156156
// Verify the proof: create the verifier transcript, add the statement to it, and invoke the verifier.
157-
let mut arthur = VerifierTranscript::<H, U>::new(&io, &proof);
157+
let mut arthur = VerifierState::<H, U>::new(&domain_separator, &proof);
158158
arthur.public_points(&[P, X]).unwrap();
159159
arthur.ratchet().unwrap();
160160
verify(&mut arthur, P, X).expect("Invalid proof");

nimue-poseidon/src/bls12_381.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use nimue::duplex_sponge::DuplexSponge;
22

3-
poseidon_sponge!(255, PoseidonPermx5_255_3, x5_255_3);
4-
poseidon_sponge!(255, PoseidonPermx5_255_5, x5_255_5);
3+
poseidon_permutation!(255, PoseidonPermx5_255_3, x5_255_3);
4+
poseidon_permutation!(255, PoseidonPermx5_255_5, x5_255_5);
55

66
pub type Poseidonx5_255_3 = DuplexSponge<PoseidonPermx5_255_3>;
77
pub type Poseidonx5_255_5 = DuplexSponge<PoseidonPermx5_255_5>;

nimue-poseidon/src/bn254.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
poseidon_sponge!(254, PoseidonPermx5_254_3, x5_254_3);
2-
poseidon_sponge!(254, PoseidonPermx5_254_5, x5_254_5);
1+
poseidon_permutation!(254, PoseidonPermx5_254_3, x5_254_3);
2+
poseidon_permutation!(254, PoseidonPermx5_254_5, x5_254_5);
33
mod x5_254_3 {
44
use ark_ff::MontFp;
55
pub type Field = ark_bn254::Fr;

nimue-poseidon/src/f64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub struct FConfig64;
77

88
pub type Field64 = Fp64<MontBackend<FConfig64, 1>>;
99

10-
poseidon_sponge!(64, PoseidonPermx3_64_24, x3_64_24);
10+
poseidon_permutation!(64, PoseidonPermx3_64_24, x3_64_24);
1111

1212
mod x3_64_24 {
1313

nimue-poseidon/src/lib.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use nimue::duplex_sponge::Unit;
1212
/// The `NAME` const is to distinbuish between different bitsizes of the same Field.
1313
/// For instance Bls12_381 and Bn254 both have field type Fp<MontBackend<FrConfig, 4>, 4> but are different fields.
1414
#[derive(Clone)]
15-
pub struct PoseidonSponge<const NAME: u32, F: PrimeField, const R: usize, const N: usize> {
15+
pub struct PoseidonPermutation<const NAME: u32, F: PrimeField, const R: usize, const N: usize> {
1616
/// Number of rounds in a full-round operation.
1717
pub full_rounds: usize,
1818
/// Number of rounds in a partial-round operation.
@@ -25,30 +25,30 @@ pub struct PoseidonSponge<const NAME: u32, F: PrimeField, const R: usize, const
2525
/// Maximally Distance Separating (MDS) Matrix.
2626
pub mds: &'static [[F; N]],
2727

28-
/// Sponge state
28+
/// Permutation state
2929
pub state: [F; N],
3030
}
3131

3232
pub type PoseidonHash<const NAME: u32, F, const R: usize, const N: usize> =
33-
DuplexSponge<PoseidonSponge<NAME, F, R, N>>;
33+
DuplexSponge<PoseidonPermutation<NAME, F, R, N>>;
3434

3535
impl<const NAME: u32, F: PrimeField, const R: usize, const N: usize> AsRef<[F]>
36-
for PoseidonSponge<NAME, F, R, N>
36+
for PoseidonPermutation<NAME, F, R, N>
3737
{
3838
fn as_ref(&self) -> &[F] {
3939
&self.state
4040
}
4141
}
4242

4343
impl<const NAME: u32, F: PrimeField, const R: usize, const N: usize> AsMut<[F]>
44-
for PoseidonSponge<NAME, F, R, N>
44+
for PoseidonPermutation<NAME, F, R, N>
4545
{
4646
fn as_mut(&mut self) -> &mut [F] {
4747
&mut self.state
4848
}
4949
}
5050

51-
impl<const NAME: u32, F: PrimeField, const R: usize, const N: usize> PoseidonSponge<NAME, F, R, N> {
51+
impl<const NAME: u32, F: PrimeField, const R: usize, const N: usize> PoseidonPermutation<NAME, F, R, N> {
5252
fn apply_s_box(&self, state: &mut [F], is_full_round: bool) {
5353
// Full rounds apply the S Box (x^alpha) to every element of state
5454
if is_full_round {
@@ -84,17 +84,17 @@ impl<const NAME: u32, F: PrimeField, const R: usize, const N: usize> PoseidonSpo
8484
}
8585

8686
impl<const NAME: u32, F: PrimeField, const R: usize, const N: usize> zeroize::Zeroize
87-
for PoseidonSponge<NAME, F, R, N>
87+
for PoseidonPermutation<NAME, F, R, N>
8888
{
8989
fn zeroize(&mut self) {
9090
self.state.zeroize();
9191
}
9292
}
9393

9494
impl<const NAME: u32, F, const R: usize, const N: usize> Permutation
95-
for PoseidonSponge<NAME, F, R, N>
95+
for PoseidonPermutation<NAME, F, R, N>
9696
where
97-
PoseidonSponge<NAME, F, R, N>: Default,
97+
PoseidonPermutation<NAME, F, R, N>: Default,
9898
F: PrimeField + Unit,
9999
{
100100
type U = F;
@@ -134,7 +134,7 @@ where
134134
}
135135

136136
impl<const NAME: u32, F: PrimeField, const R: usize, const N: usize> Debug
137-
for PoseidonSponge<NAME, F, R, N>
137+
for PoseidonPermutation<NAME, F, R, N>
138138
{
139139
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140140
self.state.fmt(f)
@@ -143,9 +143,9 @@ impl<const NAME: u32, F: PrimeField, const R: usize, const N: usize> Debug
143143

144144
/// Initialization of constants.
145145
#[allow(unused)]
146-
macro_rules! poseidon_sponge {
146+
macro_rules! poseidon_permutation {
147147
($bits: expr, $name: ident, $path: tt) => {
148-
pub type $name = crate::PoseidonSponge<$bits, $path::Field, { $path::R }, { $path::N }>;
148+
pub type $name = crate::PoseidonPermutation<$bits, $path::Field, { $path::R }, { $path::N }>;
149149

150150
impl Default for $name {
151151
fn default() -> Self {

nimue-poseidon/src/tests.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ where
1414
#[cfg(feature = "bls12-381")]
1515
#[test]
1616
fn test_squeeze_bytes_from_algebraic_hash() {
17-
use nimue::ByteChallenges;
17+
use nimue::VerifierMessageBytes;
1818

1919
type F = ark_bls12_381::Fr;
2020
type H = crate::bls12_381::Poseidonx5_255_3;
2121

22-
let io = nimue::IOPattern::<H, F>::new("test").absorb(1, "in");
23-
let io = <nimue::IOPattern<H, F> as nimue::codecs::ark::ByteIOPattern>::challenge_bytes(
24-
io, 2048, "out",
22+
let domain_separator = nimue::DomainSeparator::<H, F>::new("test").absorb(1, "in");
23+
let domain_separator = <nimue::DomainSeparator<H, F> as nimue::codecs::arkworks_algebra::ByteDomainSeparator>::challenge_bytes(
24+
domain_separator, 2048, "out",
2525
);
26-
let mut merlin = io.to_merlin();
26+
let mut merlin = domain_separator.to_merlin();
2727
merlin.add_units(&[F::from(0x42)]).unwrap();
2828

2929
let mut merlin_challenges = [0u8; 2048];
3030
merlin.fill_challenge_bytes(&mut merlin_challenges).unwrap();
3131

32-
let mut arthur = io.to_arthur(merlin.transcript());
32+
let mut arthur = domain_separator.to_verifier_state(merlin.narg_string());
3333
// write the unit to an throw-away array
3434
arthur.fill_next_units(&mut [F::from(0)]).unwrap();
3535
let arthur_challenges: [u8; 2048] = arthur.challenge_bytes().unwrap();
@@ -51,7 +51,7 @@ fn test_squeeze_bytes_from_algebraic_hash() {
5151
fn test_poseidon_bls12_381() {
5252
use crate::bls12_381::{PoseidonPermx5_255_3, PoseidonPermx5_255_5};
5353
use ark_ff::MontFp;
54-
use nimue::IOPattern;
54+
use nimue::DomainSeparator;
5555
use nimue::UnitTranscript;
5656

5757
type F = ark_bls12_381::Fr;
@@ -86,10 +86,10 @@ fn test_poseidon_bls12_381() {
8686

8787
// Check that poseidon can indeed be instantiated and doesn't do terribly stupid things like give 0 challenges.
8888
use crate::bls12_381::Poseidonx5_255_3;
89-
let io = IOPattern::<Poseidonx5_255_3, F>::new("test")
89+
let domain_separator = DomainSeparator::<Poseidonx5_255_3, F>::new("test")
9090
.absorb(1, "in")
9191
.squeeze(10, "out");
92-
let mut merlin = io.to_merlin();
92+
let mut merlin = domain_separator.to_merlin();
9393
merlin.add_units(&[F::from(0x42)]).unwrap();
9494

9595
let mut challenges = [F::from(0); 10];

nimue-pow/src/blake3.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,20 @@ impl Blake3PoW {
144144

145145
#[test]
146146
fn test_pow_blake3() {
147-
use crate::{ByteIOPattern, ByteReader, ByteWriter, PoWChallenge, PoWIOPattern};
148-
use nimue::{DefaultHash, IOPattern};
147+
use crate::{ByteDomainSeparator, ByteReader, ByteWriter, PoWChallenge, PoWDomainSeparator};
148+
use nimue::{DefaultHash, DomainSeparator};
149149

150150
const BITS: f64 = 10.0;
151151

152-
let iopattern = IOPattern::<DefaultHash>::new("the proof of work lottery 🎰")
152+
let domain_separator = DomainSeparator::<DefaultHash>::new("the proof of work lottery 🎰")
153153
.add_bytes(1, "something")
154154
.challenge_pow("rolling dices");
155155

156-
let mut prover = iopattern.to_merlin();
157-
prover.add_bytes(b"\0").expect("Invalid IOPattern");
156+
let mut prover = domain_separator.to_merlin();
157+
prover.add_bytes(b"\0").expect("Invalid DomainSeparator");
158158
prover.challenge_pow::<Blake3PoW>(BITS).unwrap();
159159

160-
let mut verifier = iopattern.to_arthur(prover.transcript());
160+
let mut verifier = domain_separator.to_verifier_state(prover.narg_string());
161161
let byte = verifier.next_bytes::<1>().unwrap();
162162
assert_eq!(&byte, b"\0");
163163
verifier.challenge_pow::<Blake3PoW>(BITS).unwrap();

nimue-pow/src/keccak.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ impl PowStrategy for KeccakPoW {
3030

3131
#[test]
3232
fn test_pow_keccak() {
33-
use crate::{ByteIOPattern, ByteReader, ByteWriter, PoWChallenge, PoWIOPattern};
34-
use nimue::{DefaultHash, IOPattern};
33+
use crate::{ByteDomainSeparator, ByteReader, ByteWriter, PoWChallenge, PoWDomainSeparator};
34+
use nimue::{DefaultHash, DomainSeparator};
3535

3636
const BITS: f64 = 10.0;
3737

38-
let iopattern = IOPattern::<DefaultHash>::new("the proof of work lottery 🎰")
38+
let domain_separator = DomainSeparator::<DefaultHash>::new("the proof of work lottery 🎰")
3939
.add_bytes(1, "something")
4040
.challenge_pow("rolling dices");
4141

42-
let mut prover = iopattern.to_merlin();
43-
prover.add_bytes(b"\0").expect("Invalid IOPattern");
42+
let mut prover = domain_separator.to_merlin();
43+
prover.add_bytes(b"\0").expect("Invalid DomainSeparator");
4444
prover.challenge_pow::<KeccakPoW>(BITS).unwrap();
4545

46-
let mut verifier = iopattern.to_arthur(prover.transcript());
46+
let mut verifier = domain_separator.to_verifier_state(prover.narg_string());
4747
let byte = verifier.next_bytes::<1>().unwrap();
4848
assert_eq!(&byte, b"\0");
4949
verifier.challenge_pow::<KeccakPoW>(BITS).unwrap();

0 commit comments

Comments
 (0)