Skip to content

Commit 249102b

Browse files
committed
Fix examples.
1 parent effccf9 commit 249102b

10 files changed

+100
-258
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ ark-ff = {version="0.4.0", optional=true}
2828
ark-ec = {version="0.4.0", optional=true}
2929
ark-serialize = {version="0.4.2", optional=true}
3030
ark-crypto-primitives = {version="0.4.0", optional=true}
31-
curve25519-dalek = {version="4.0.0", optional=true}
31+
curve25519-dalek = {version="4.0.0", optional=true, features=["group"]}
3232
# anemoi = {git = "https://github.com/anemoi-hash/anemoi-rust", optional=true}
3333
group = {version="0.13.0", optional=true}
3434

3535
[features]
3636
default = []
37-
arkworks = ["dep:ark-ff", "dep:ark-ec", "dep:ark-serialize", "dep:ark-crypto-primitives"]
38-
dalek = ["dep:curve25519-dalek"]
39-
zkcrypto = ["dep:group"]
37+
ark = ["dep:ark-ff", "dep:ark-ec", "dep:ark-serialize", "dep:ark-crypto-primitives"]
38+
group = ["dep:group", "dep:curve25519-dalek"]
4039
# anemoi = ["dep:anemoi"]
4140

4241
[dev-dependencies]
@@ -47,17 +46,18 @@ ark-curve25519 = "0.4.0"
4746
hex = "0.4.3"
4847
anyhow = { version = "1.0.75", features = ["backtrace"] }
4948

49+
5050
[package.metadata.docs.rs]
5151
rustdoc-args = [
5252
"--html-in-header", "doc/katex-header.html",
5353
"--cfg", "docsrs",
5454
]
55-
features = ["arkworks", "dalek"]
55+
features = ["ark", "group"]
5656

5757
[[example]]
5858
name = "schnorr"
59-
required-features = ["arkworks"]
59+
required-features = ["ark"]
6060

6161
[[example]]
6262
name = "bulletproof"
63-
required-features = ["arkworks"]
63+
required-features = ["ark"]

examples/bulletproof.rs

+39-41
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,9 @@ use ark_ec::PrimeGroup;
22
use ark_ec::{AffineRepr, CurveGroup, VariableBaseMSM};
33
use ark_ff::Field;
44
use ark_std::log2;
5-
use nimue::plugins::arkworks::*;
6-
use nimue::{Arthur, Merlin, ProofError, ProofResult};
5+
use nimue::plugins::ark::*;
76
use rand::rngs::OsRng;
87

9-
fn fold_generators<A: AffineRepr>(
10-
a: &[A],
11-
b: &[A],
12-
x: &A::ScalarField,
13-
y: &A::ScalarField,
14-
) -> Vec<A> {
15-
a.iter()
16-
.zip(b.iter())
17-
.map(|(&a, &b)| (a * x + b * y).into_affine())
18-
.collect()
19-
}
20-
21-
/// Computes the inner prouct of vectors `a` and `b`.
22-
///
23-
/// Useless once https://github.com/arkworks-rs/algebra/pull/665 gets merged.
24-
fn dot_prod<F: Field>(a: &[F], b: &[F]) -> F {
25-
a.iter().zip(b.iter()).map(|(&a, &b)| a * b).sum()
26-
}
27-
28-
/// Folds together `(a, b)` using challenges `x` and `y`.
29-
fn fold<F: Field>(a: &[F], b: &[F], x: &F, y: &F) -> Vec<F> {
30-
a.iter()
31-
.zip(b.iter())
32-
.map(|(&a, &b)| a * x + b * y)
33-
.collect()
34-
}
35-
368
/// The IO Pattern of a bulleproof.
379
///
3810
/// Defining this as a trait allows us to "attach" the bulletproof IO to
@@ -42,10 +14,11 @@ trait BulletproofIOPattern<G: CurveGroup> {
4214
fn add_bulletproof(self, len: usize) -> Self;
4315
}
4416

45-
impl<G: CurveGroup> BulletproofIOPattern<G> for ArkGroupIOPattern<G> {
46-
/// The IO of the bulletproof statement (the sole commitment)
17+
impl<G, H> BulletproofIOPattern<G> for IOPattern<H>
18+
where G: CurveGroup, H: DuplexHash, IOPattern<H>: GroupIOPattern<G>{
19+
/// The IO of the bulletproof statement
4720
fn bulletproof_statement(self) -> Self {
48-
self.add_points(1, "Ped-commit")
21+
self.add_points(1, "Pedersen commitment")
4922
}
5023

5124
/// The IO of the bulletproof protocol
@@ -143,6 +116,33 @@ fn verify<G: CurveGroup>(
143116
}
144117
}
145118

119+
fn fold_generators<A: AffineRepr>(
120+
a: &[A],
121+
b: &[A],
122+
x: &A::ScalarField,
123+
y: &A::ScalarField,
124+
) -> Vec<A> {
125+
a.iter()
126+
.zip(b.iter())
127+
.map(|(&a, &b)| (a * x + b * y).into_affine())
128+
.collect()
129+
}
130+
131+
/// Computes the inner prouct of vectors `a` and `b`.
132+
///
133+
/// Useless once https://github.com/arkworks-rs/algebra/pull/665 gets merged.
134+
fn dot_prod<F: Field>(a: &[F], b: &[F]) -> F {
135+
a.iter().zip(b.iter()).map(|(&a, &b)| a * b).sum()
136+
}
137+
138+
/// Folds together `(a, b)` using challenges `x` and `y`.
139+
fn fold<F: Field>(a: &[F], b: &[F], x: &F, y: &F) -> Vec<F> {
140+
a.iter()
141+
.zip(b.iter())
142+
.map(|(&a, &b)| a * x + b * y)
143+
.collect()
144+
}
145+
146146
fn main() {
147147
use ark_curve25519::EdwardsProjective as G;
148148
use ark_std::UniformRand;
@@ -154,13 +154,11 @@ fn main() {
154154
let size = 8;
155155

156156
// initialize the IO Pattern putting the domain separator ("example.com")
157-
let io_pattern = ArkGroupIOPattern::<G>::new("example.com")
158-
// add the IO of the bulletproof statement (the commitment)
159-
.bulletproof_statement()
160-
// (optional) process the data so far, filling the block till the end.
161-
.ratchet()
162-
// add the IO of the bulletproof protocol (the transcript)
163-
.add_bulletproof(size);
157+
let iopattern = IOPattern::new("example.com");
158+
// add the IO of the bulletproof statement
159+
let iopattern = BulletproofIOPattern::<G>::bulletproof_statement(iopattern).ratchet();
160+
// add the IO of the bulletproof protocol (the transcript)
161+
let iopattern = BulletproofIOPattern::<G>::add_bulletproof(iopattern, size);
164162

165163
// the test vectors
166164
let a = (0..size).map(|x| F::from(x as u32)).collect::<Vec<_>>();
@@ -181,7 +179,7 @@ fn main() {
181179
let statement = G::msm_unchecked(&g, &a) + G::msm_unchecked(&h, &b) + u * ab;
182180
let witness = (&a[..], &b[..]);
183181

184-
let mut arthur = io_pattern.to_arthur();
182+
let mut arthur = iopattern.to_arthur();
185183
arthur.public_points(&[statement]).unwrap();
186184
arthur.ratchet().unwrap();
187185
let proof = prove(&mut arthur, generators, &statement, witness).expect("Error proving");
@@ -191,7 +189,7 @@ fn main() {
191189
hex::encode(proof)
192190
);
193191

194-
let mut verifier_transcript = io_pattern.to_merlin(proof);
192+
let mut verifier_transcript = iopattern.to_merlin(proof);
195193
verifier_transcript.public_points(&[statement]).unwrap();
196194
verifier_transcript.ratchet().unwrap();
197195
verify(&mut verifier_transcript, generators, size, &statement).expect("Invalid proof");

examples/schnorr.rs

+37-16
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,30 @@
55
///
66
use ark_ec::{CurveGroup, PrimeGroup};
77
use ark_std::UniformRand;
8-
use nimue::plugins::arkworks::*;
9-
use nimue::Arthur;
10-
use nimue::{DuplexHash, ProofResult};
8+
use nimue::plugins::ark::*;
119
use rand::rngs::OsRng;
1210

11+
/// Extend the IO pattern with the Schnorr protocol.
12+
trait SchnorrIOPattern<G: CurveGroup> {
13+
/// Adds the entire Schnorr protocol to the IO pattern (statement and proof).
14+
fn add_schnorr_io(self) -> Self;
15+
}
16+
17+
impl<G, H> SchnorrIOPattern<G> for IOPattern<H>
18+
where
19+
G: CurveGroup,
20+
H: DuplexHash,
21+
IOPattern<H>: GroupIOPattern<G> {
22+
fn add_schnorr_io(self) -> Self {
23+
self.add_points(1, "P")
24+
.add_points(1, "X")
25+
.ratchet()
26+
.add_points(1, "commitment (K)")
27+
.challenge_scalars(1, "challenge (c)")
28+
.add_scalars(1, "response (r)")
29+
}
30+
}
31+
1332
/// The key generation algorithm otuputs
1433
/// a secret key `sk` in $\mathbb{Z}_p$
1534
/// and its respective public key `pk` in $\mathbb{G}$.
@@ -72,16 +91,23 @@ where
7291
for<'a> Merlin<'a, H>: GroupReader<G>,
7392
{
7493
// Read the protocol from the transcript:
75-
let [K]: [G; 1] = merlin.next_points().unwrap();
76-
let [c]: [G::ScalarField; 1] = merlin.challenge_scalars().unwrap();
77-
let [r]: [G::ScalarField; 1] = merlin.challenge_scalars().unwrap();
94+
let [K] = merlin.next_points().unwrap();
95+
let [c] = merlin.challenge_scalars().unwrap();
96+
let [r] = merlin.next_scalars().unwrap();
7897

7998
// Check the verification equation, otherwise return a verification error.
99+
// The type ProofError is an enum that can report:
100+
// - InvalidProof: the proof is not valid
101+
// - InvalidIO: the transcript does not match the IO pattern
102+
// - SerializationError: there was an error serializing/deserializing an element
80103
if P * r == K + X * c {
81104
Ok(())
82105
} else {
83106
Err(nimue::ProofError::InvalidProof)
84107
}
108+
109+
// from here, another proof can be verified using the same merlin instance
110+
// and proofs can be composed.
85111
}
86112

87113
#[allow(non_snake_case)]
@@ -90,18 +116,13 @@ fn main() {
90116
// Set the group:
91117
type G = ark_curve25519::EdwardsProjective;
92118
// Set the hash function (commented out other valid choices):
93-
type H = nimue::hash::Keccak;
94-
// type H = nimue::legacy::DigestBridge<blake2::Blake2s256>;
95-
// type H = nimue::legacy::DigestBridge<sha2::Sha256>;
119+
// type H = nimue::hash::Keccak;
120+
type H = nimue::hash::legacy::DigestBridge<blake2::Blake2s256>;
121+
// type H = nimue::hash::legacy::DigestBridge<sha2::Sha256>;
96122

97123
// Set up the IO for the protocol transcript with domain separator "nimue::examples::schnorr"
98-
let io = ArkGroupIOPattern::<G, H>::new("nimue::examples::schnorr")
99-
.add_points(1, "P")
100-
.add_points(1, "X")
101-
.ratchet()
102-
.add_points(1, "commitment (K)")
103-
.challenge_scalars(1, "challenge (c)")
104-
.add_scalars(1, "response (r)");
124+
let io = IOPattern::<H>::new("nimue::examples::schnorr");
125+
let io = SchnorrIOPattern::<G>::add_schnorr_io(io);
105126

106127
// Set up the elements to prove
107128
let P = G::generator();

src/hash/legacy.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
//! `squeeze_unchecked` will use the squeeze oracle to output `output.len()` bytes,
1717
//! and finally `squeeze_end` will set the state `cv` to the current squeeze digest and length.
1818
//!
19-
20-
use core::mem::size_of;
21-
2219
use digest::{core_api::BlockSizeUser, typenum::Unsigned, Digest, FixedOutputReset, Reset};
2320
use generic_array::GenericArray;
2421
use zeroize::Zeroize;
@@ -119,7 +116,7 @@ impl<D: BlockSizeUser + Digest + Clone + FixedOutputReset> Default for DigestBri
119116

120117
impl<D: BlockSizeUser + Digest + Clone + FixedOutputReset> DuplexHash<u8> for DigestBridge<D> {
121118
fn new(tag: [u8; 32]) -> Self {
122-
debug_assert!(size_of::<D::OutputSize>() >= 32);
119+
// debug_assert!(size_of::<D::OutputSize>() >= 32);
123120
let mut bridge = Self::default();
124121
bridge.absorb_unchecked(&tag);
125122
bridge

src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ mod arthur;
128128
mod errors;
129129
/// Hash functions traits and implmentations.
130130
pub mod hash;
131+
/// IO Pattern
132+
mod iopattern;
131133
/// Verifier state and transcript deserialization.
132134
mod merlin;
133135
/// APIs for common zkp libraries.
@@ -143,8 +145,9 @@ pub mod traits;
143145
pub use arthur::Arthur;
144146
pub use errors::{IOPatternError, ProofError, ProofResult};
145147
pub use hash::{DuplexHash, Unit};
148+
pub use iopattern::IOPattern;
146149
pub use merlin::Merlin;
147-
pub use safe::{IOPattern, Safe};
150+
pub use safe::Safe;
148151
pub use traits::*;
149152

150153
/// Default random number generator used ([`rand::rngs::OsRng`]).

src/merlin.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::errors::IOPatternError;
22
use crate::hash::{DuplexHash, Unit};
3-
use crate::safe::{IOPattern, Safe};
3+
use crate::iopattern::IOPattern;
4+
use crate::safe::Safe;
45
use crate::traits::{ByteTranscript, ByteTranscriptReader};
56
use crate::DefaultHash;
67

0 commit comments

Comments
 (0)