1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! Implementation of the generic Schnorr Sigma Protocol over a [`group::Group`].
//!
//! This module defines the [`SchnorrProof`] structure, which implements
//! a Sigma protocol proving different types of discrete logarithm relations (eg. Schnorr, Pedersen's commitments)
//! through a group morphism abstraction (see [Maurer09](https://crypto-test.ethz.ch/publications/files/Maurer09.pdf)).
use crate::errors::{Error, Result};
use crate::linear_relation::CanonicalLinearRelation;
use crate::traits::{ScalarRng, SigmaProtocol, SigmaProtocolSimulator, Transcript};
use crate::{LinearRelation, MultiScalarMul, Nizk};
use alloc::vec::Vec;
use group::prime::PrimeGroup;
use spongefish::{Decoding, Encoding, NargDeserialize, NargSerialize};
fn protocol_identifier_for_group<G>() -> [u8; 64] {
let _ = core::marker::PhantomData::<G>;
#[cfg(feature = "p256")]
if core::any::type_name::<G>() == core::any::type_name::<p256::ProjectivePoint>() {
return pad_identifier(b"sigma-proofs_Shake128_P256");
}
#[cfg(feature = "bls12_381")]
if core::any::type_name::<G>() == core::any::type_name::<bls12_381::G1Projective>() {
return pad_identifier(b"sigma-proofs_Shake128_BLS12381");
}
pad_identifier(b"ietf sigma proof linear relation")
}
fn pad_identifier(identifier: &[u8]) -> [u8; 64] {
assert!(
identifier.len() <= 64,
"identifier must fit within 64 bytes"
);
let mut padded = [0u8; 64];
padded[..identifier.len()].copy_from_slice(identifier);
padded
}
impl<G> SigmaProtocol for CanonicalLinearRelation<G>
where
G: PrimeGroup + Encoding<[u8]> + NargSerialize + NargDeserialize + MultiScalarMul,
G::Scalar: Encoding<[u8]> + NargSerialize + NargDeserialize + Decoding<[u8]>,
{
type Commitment = G;
type ProverState = (Vec<G::Scalar>, Vec<G::Scalar>);
type Response = G::Scalar;
type Witness = Vec<G::Scalar>;
type Challenge = G::Scalar;
/// Prover's first message: generates a commitment using random nonces.
///
/// # Parameters
/// - `witness`: A vector of scalars that satisfy the linear map relation.
/// - `rng`: A cryptographically secure random number generator.
///
/// # Returns
/// - A tuple containing:
/// - The commitment (a vector of group elements).
/// - The prover state (random nonces and witness) used to compute the response.
///
/// # Errors
///
/// -[`Error::InvalidInstanceWitnessPair`] if the witness vector length is less than the number of scalar variables.
/// If the witness vector is larger, extra variables are ignored.
fn prover_commit(
&self,
witness: &Self::Witness,
rng: &mut impl ScalarRng,
) -> Result<(Vec<Self::Commitment>, Self::ProverState)> {
if witness.len() < self.num_scalars {
return Err(Error::InvalidInstanceWitnessPair);
}
let nonces = rng.random_scalars_vec::<G>(self.num_scalars);
let commitment = self.evaluate(&nonces);
let prover_state = (nonces.to_vec(), witness.to_vec());
Ok((commitment, prover_state))
}
/// Computes the prover's response (second message) using the challenge.
///
/// # Parameters
/// - `state`: The prover state returned by `prover_commit`, typically containing randomness and witness components.
/// - `challenge`: The verifier's challenge scalar.
///
/// # Returns
/// - A vector of scalars forming the prover's response.
///
/// # Errors
/// - Returns [`Error::InvalidInstanceWitnessPair`] if the prover state vectors have incorrect lengths.
fn prover_response(
&self,
prover_state: Self::ProverState,
challenge: &Self::Challenge,
) -> Result<Vec<Self::Response>> {
let (nonces, witness) = prover_state;
let responses = nonces
.into_iter()
.zip(witness)
.map(|(r, w)| r + w * challenge)
.collect();
Ok(responses)
}
/// Verifies the correctness of the proof.
///
/// # Parameters
/// - `commitment`: The prover's commitment vector (group elements).
/// - `challenge`: The challenge scalar.
/// - `response`: The prover's response vector.
///
/// # Returns
/// - `Ok(())` if the proof is valid.
/// - `Err(Error::VerificationFailure)` if the proof is invalid.
/// - `Err(Error::InvalidInstanceWitnessPair)` if the lengths of commitment or response do not match the expected counts.
///
/// # Errors
/// -[`Error::VerificationFailure`] if the computed relation
/// does not hold for the provided challenge and response, indicating proof invalidity.
/// -[`Error::InvalidInstanceWitnessPair`] if the commitment or response length is incorrect.
fn verifier(
&self,
commitment: &[Self::Commitment],
challenge: &Self::Challenge,
response: &[Self::Response],
) -> Result<()> {
if commitment.len() != self.image.len() || response.len() != self.num_scalars {
return Err(Error::InvalidInstanceWitnessPair);
}
let lhs = self.evaluate(response);
let mut rhs = Vec::new();
for (img, g) in self.image_elements().zip(commitment) {
rhs.push(img * challenge + g);
}
if lhs == rhs {
Ok(())
} else {
Err(Error::VerificationFailure)
}
}
fn commitment_len(&self) -> usize {
self.image.len()
}
fn response_len(&self) -> usize {
self.num_scalars
}
fn instance_label(&self) -> impl AsRef<[u8]> {
self.label()
}
fn protocol_identifier(&self) -> [u8; 64] {
protocol_identifier_for_group::<G>()
}
}
impl<G> CanonicalLinearRelation<G>
where
G: PrimeGroup + Encoding<[u8]> + NargSerialize + NargDeserialize + MultiScalarMul,
G::Scalar: Encoding<[u8]> + NargSerialize + NargDeserialize + Decoding<[u8]>,
{
/// Convert this LinearRelation into a non-interactive zero-knowledge protocol
/// using the ShakeCodec and a specified context/domain separator.
///
/// # Parameters
/// - `context`: Domain separator bytes for the Fiat-Shamir transform
///
/// # Returns
/// A `Nizk` instance ready for proving and verification
///
/// # Example
/// ```
/// # #[cfg(feature = "curve25519-dalek")] {
/// # use sigma_proofs::{LinearRelation, Nizk};
/// # use curve25519_dalek::RistrettoPoint as G;
/// # use curve25519_dalek::scalar::Scalar;
/// # use rand::rngs::OsRng;
/// # use group::Group;
///
/// let mut relation = LinearRelation::<G>::new();
/// let x_var = relation.allocate_scalar();
/// let g_var = relation.allocate_element();
/// let p_var = relation.allocate_eq(x_var * g_var);
///
/// relation.set_element(g_var, G::generator());
/// let x = Scalar::random(&mut OsRng);
/// relation.compute_image(&[x]).unwrap();
///
/// // Convert to NIZK with custom context
/// let nizk = relation.into_nizk(b"my-protocol-v1").unwrap();
/// let proof = nizk.prove_batchable(&vec![x], &mut OsRng).unwrap();
/// assert!(nizk.verify_batchable(&proof).is_ok());
/// # }
/// ```
pub fn into_nizk(self, session_identifier: &[u8]) -> Result<Nizk<CanonicalLinearRelation<G>>> {
Ok(Nizk::new(session_identifier, self))
}
}
impl<G> LinearRelation<G>
where
G: PrimeGroup + Encoding<[u8]> + NargSerialize + NargDeserialize + MultiScalarMul,
G::Scalar: Encoding<[u8]> + NargSerialize + NargDeserialize + Decoding<[u8]>,
{
/// Convert this LinearRelation into a non-interactive zero-knowledge protocol
/// using the Fiat-Shamir transform.
///
/// This is a convenience method that combines `.canonical()` and `.into_nizk()`.
///
/// # Parameters
/// - `session_identifier`: Domain separator bytes for the Fiat-Shamir transform
///
/// # Returns
/// A `Nizk` instance ready for proving and verification
///
/// # Example
/// ```
/// # #[cfg(feature = "curve25519-dalek")] {
/// # use sigma_proofs::{LinearRelation, Nizk};
/// # use curve25519_dalek::RistrettoPoint as G;
/// # use curve25519_dalek::scalar::Scalar;
/// # use rand::rngs::OsRng;
/// # use group::Group;
///
/// let mut relation = LinearRelation::<G>::new();
/// let x_var = relation.allocate_scalar();
/// let g_var = relation.allocate_element();
/// let p_var = relation.allocate_eq(x_var * g_var);
///
/// relation.set_element(g_var, G::generator());
/// let x = Scalar::random(&mut OsRng);
/// relation.compute_image(&[x]).unwrap();
///
/// // Convert to NIZK directly
/// let nizk = relation.into_nizk(b"my-protocol-v1").unwrap();
/// let proof = nizk.prove_batchable(&vec![x], &mut OsRng).unwrap();
/// assert!(nizk.verify_batchable(&proof).is_ok());
/// # }
/// ```
pub fn into_nizk(
self,
session_identifier: &[u8],
) -> crate::errors::Result<crate::Nizk<CanonicalLinearRelation<G>>>
where
G: PrimeGroup + Encoding<[u8]> + NargSerialize + NargDeserialize,
G::Scalar: Encoding<[u8]> + NargSerialize + NargDeserialize + Decoding<[u8]>,
{
self.canonical()
.map_err(|_| crate::errors::Error::InvalidInstanceWitnessPair)?
.into_nizk(session_identifier)
}
}
impl<G> SigmaProtocolSimulator for CanonicalLinearRelation<G>
where
G: PrimeGroup + Encoding<[u8]> + NargSerialize + NargDeserialize + MultiScalarMul,
G::Scalar: Encoding<[u8]> + NargSerialize + NargDeserialize + Decoding<[u8]>,
{
/// Simulates a valid transcript for a given challenge without a witness.
///
/// # Parameters
/// - `challenge`: A scalar value representing the challenge.
/// - `rng`: A cryptographically secure RNG.
///
/// # Returns
/// - A commitment and response forming a valid proof for the given challenge.
fn simulate_response(&self, rng: &mut impl ScalarRng) -> Vec<Self::Response> {
rng.random_scalars_vec::<G>(self.num_scalars)
}
/// Simulates a full proof transcript using a randomly generated challenge.
///
/// # Parameters
/// - `rng`: A cryptographically secure RNG.
///
/// # Returns
/// - A tuple `(commitment, challenge, response)` forming a valid proof.
fn simulate_transcript(&self, rng: &mut impl ScalarRng) -> Result<Transcript<Self>> {
let [challenge] = rng.random_scalars::<G, _>();
let response = self.simulate_response(rng);
let commitment = self.simulate_commitment(&challenge, &response)?;
Ok((commitment, challenge, response))
}
/// Recomputes the commitment from the challenge and response (used in compact proofs).
///
/// # Parameters
/// - `challenge`: The challenge scalar issued by the verifier or derived via Fiat–Shamir.
/// - `response`: The prover's response vector.
///
/// # Returns
/// - A vector of group elements representing the simulated commitment (one per linear constraint).
///
/// # Errors
/// - [`Error::InvalidInstanceWitnessPair`] if the response length does not match the expected number of scalars.
fn simulate_commitment(
&self,
challenge: &Self::Challenge,
response: &[Self::Response],
) -> Result<Vec<Self::Commitment>> {
if response.len() != self.num_scalars {
return Err(Error::InvalidInstanceWitnessPair);
}
let response_image = self.evaluate(response);
let commitment = response_image
.iter()
.zip(self.image_elements())
.map(|(res, img)| *res - img * challenge)
.collect::<Vec<_>>();
Ok(commitment)
}
}