-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathchaum_pedersen.rs
More file actions
422 lines (361 loc) · 15.8 KB
/
Copy pathchaum_pedersen.rs
File metadata and controls
422 lines (361 loc) · 15.8 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Chaum-Pedersen DLOG equality proof verification for encrypted Zone deposits.
//!
//! Verifies that the sequencer correctly derived the ECDH shared secret from the
//! depositor's ephemeral public key without revealing the sequencer's private key.
//!
//! Uses the NCC-audited [`k256`] crate (v0.13.4) for secp256k1 operations.
use alloy_primitives::Keccak256;
use k256::{
AffinePoint, FieldBytes, ProjectivePoint, Scalar,
elliptic_curve::{
ops::{LinearCombination, Reduce},
sec1::{FromEncodedPoint, ToEncodedPoint},
},
};
use tempo_precompiles::storage::StorageCtx;
/// Gas cost for Chaum-Pedersen proof verification (two EC muls + hashing).
const CP_VERIFY_GAS: u64 = 6_000;
/// Uncompressed SEC1 encoding of the secp256k1 generator, `0x04 || G.x || G.y`.
///
/// Pinned by `test_generator_uncompressed_constant`.
const GENERATOR_UNCOMPRESSED: [u8; 65] = [
0x04, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b,
0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17,
0x98, 0x48, 0x3a, 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4, 0xfb, 0xfc, 0x0e, 0x11, 0x08,
0xa8, 0xfd, 0x17, 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47, 0xd0, 0x8f, 0xfb, 0x10, 0xd4,
0xb8,
];
/// Chaum-Pedersen DLOG equality proof verifier.
///
/// Verifies that the sequencer knows `privSeq` such that:
/// - `pubSeq = privSeq * G` (their public key)
/// - `sharedSecretPoint = privSeq * ephemeralPub` (the ECDH computation)
///
/// Verification equations:
/// - `R1 = s*G - c*pubSeq`
/// - `R2 = s*ephemeralPub - c*sharedSecretPoint`
/// - `c' = keccak256(G, ephemeralPub, pubSeq, sharedSecretPoint, R1, R2)`
/// - Check: `c == c'`
pub struct ChaumPedersenVerify;
impl ChaumPedersenVerify {
/// Charge the gas cost for Chaum-Pedersen proof verification.
pub fn verify_chaum_pedersen_gas() -> tempo_precompiles::Result<()> {
StorageCtx::default().deduct_gas(CP_VERIFY_GAS)
}
/// Verify a Chaum-Pedersen DLOG equality proof on secp256k1.
///
/// Proves knowledge of scalar `x` such that `pubSeq = x*G` AND `sharedSecret = x*ephemeralPub`.
pub(crate) fn verify(
ephemeral_pub_x: &[u8; 32],
ephemeral_pub_y_parity: u8,
shared_secret_x: &[u8; 32],
shared_secret_y_parity: u8,
sequencer_pub_x: &[u8; 32],
sequencer_pub_y_parity: u8,
s_bytes: &[u8; 32],
c_bytes: &[u8; 32],
) -> bool {
// Recover points
let Some(ephemeral_pub) = recover_point(ephemeral_pub_x, ephemeral_pub_y_parity) else {
return false;
};
let Some(shared_secret_point) = recover_point(shared_secret_x, shared_secret_y_parity)
else {
return false;
};
let Some(sequencer_pub) = recover_point(sequencer_pub_x, sequencer_pub_y_parity) else {
return false;
};
// Deserialize proof scalars by reducing modulo the group order.
let s = <Scalar as Reduce<k256::U256>>::reduce_bytes(&(*s_bytes).into());
let c = <Scalar as Reduce<k256::U256>>::reduce_bytes(&(*c_bytes).into());
// Each equation is a two-term linear combination, so evaluate it with Shamir's trick
// (`lincomb`) instead of two independent scalar multiplications.
let neg_c = -c;
// R1 = s*G - c*pubSeq
let r1 = ProjectivePoint::lincomb(
&ProjectivePoint::GENERATOR,
&s,
&ProjectivePoint::from(sequencer_pub),
&neg_c,
);
// R2 = s*ephemeralPub - c*sharedSecretPoint
let r2 = ProjectivePoint::lincomb(
&ProjectivePoint::from(ephemeral_pub),
&s,
&ProjectivePoint::from(shared_secret_point),
&neg_c,
);
let r1_affine = AffinePoint::from(r1);
let r2_affine = AffinePoint::from(r2);
// Recompute challenge and compare
let c_prime = challenge_hash(
&ephemeral_pub,
&sequencer_pub,
&shared_secret_point,
&r1_affine,
&r2_affine,
);
c == c_prime
}
}
/// Recover a secp256k1 affine point from compressed form (x coordinate + y parity).
///
/// `y_parity` follows SEC1: `0x02` for even y, `0x03` for odd y. `0x05` Compact encoding for even
/// points is also accepted.
pub fn recover_point(x_bytes: &[u8; 32], y_parity: u8) -> Option<AffinePoint> {
let mut encoded = [0u8; 33];
encoded[0] = y_parity;
encoded[1..].copy_from_slice(x_bytes);
let point = k256::EncodedPoint::from_bytes(encoded).ok()?;
Option::from(AffinePoint::from_encoded_point(&point))
}
/// Compute the Chaum-Pedersen challenge hash.
///
/// `c = keccak256(G || ephemeralPub || pubSeq || sharedSecretPoint || R1 || R2)`
///
/// Shared between the verifier (precompile) and prover (ecies module).
pub fn challenge_hash(
ephemeral_pub: &AffinePoint,
sequencer_pub: &AffinePoint,
shared_secret: &AffinePoint,
r1: &AffinePoint,
r2: &AffinePoint,
) -> Scalar {
let mut hasher = Keccak256::new();
hasher.update(GENERATOR_UNCOMPRESSED);
for point in [ephemeral_pub, sequencer_pub, shared_secret, r1, r2] {
hasher.update(point.to_encoded_point(false).as_bytes());
}
let hash = hasher.finalize();
<Scalar as Reduce<k256::U256>>::reduce_bytes(FieldBytes::from_slice(&hash.0))
}
#[cfg(test)]
mod tests {
use super::*;
use k256::elliptic_curve::{Field, PrimeField};
#[test]
fn test_generator_uncompressed_constant() {
let g = AffinePoint::from(ProjectivePoint::GENERATOR);
assert_eq!(
g.to_encoded_point(false).as_bytes(),
GENERATOR_UNCOMPRESSED.as_slice()
);
}
#[test]
fn test_recover_point_generator() {
let g = AffinePoint::from(ProjectivePoint::GENERATOR);
let encoded = g.to_encoded_point(true);
let x: [u8; 32] = encoded.x().unwrap().as_slice().try_into().unwrap();
let parity = encoded.as_bytes()[0];
let recovered = recover_point(&x, parity).expect("should recover generator");
assert_eq!(recovered, g);
}
#[test]
fn test_chaum_pedersen_valid_proof() {
let mut rng = rand::thread_rng();
let priv_seq = Scalar::random(&mut rng);
let pub_seq = (ProjectivePoint::GENERATOR * priv_seq).to_affine();
let eph_priv = Scalar::random(&mut rng);
let eph_pub = (ProjectivePoint::GENERATOR * eph_priv).to_affine();
let shared_secret = (ProjectivePoint::from(eph_pub) * priv_seq).to_affine();
// Generate proof
let k = Scalar::random(&mut rng);
let r1 = (ProjectivePoint::GENERATOR * k).to_affine();
let r2 = (ProjectivePoint::from(eph_pub) * k).to_affine();
let c = challenge_hash(&eph_pub, &pub_seq, &shared_secret, &r1, &r2);
let s = k + c * priv_seq;
let eph_enc = eph_pub.to_encoded_point(true);
let ss_enc = shared_secret.to_encoded_point(true);
let ps_enc = pub_seq.to_encoded_point(true);
let valid = ChaumPedersenVerify::verify(
eph_enc.x().unwrap().as_slice().try_into().unwrap(),
eph_enc.as_bytes()[0],
ss_enc.x().unwrap().as_slice().try_into().unwrap(),
ss_enc.as_bytes()[0],
ps_enc.x().unwrap().as_slice().try_into().unwrap(),
ps_enc.as_bytes()[0],
&s.to_repr().into(),
&c.to_repr().into(),
);
assert!(valid, "valid Chaum-Pedersen proof should verify");
}
#[test]
fn test_chaum_pedersen_invalid_proof() {
let mut rng = rand::thread_rng();
let priv_seq = Scalar::random(&mut rng);
let pub_seq = (ProjectivePoint::GENERATOR * priv_seq).to_affine();
let eph_priv = Scalar::random(&mut rng);
let eph_pub = (ProjectivePoint::GENERATOR * eph_priv).to_affine();
let shared_secret = (ProjectivePoint::from(eph_pub) * priv_seq).to_affine();
let eph_enc = eph_pub.to_encoded_point(true);
let ss_enc = shared_secret.to_encoded_point(true);
let ps_enc = pub_seq.to_encoded_point(true);
let valid = ChaumPedersenVerify::verify(
eph_enc.x().unwrap().as_slice().try_into().unwrap(),
eph_enc.as_bytes()[0],
ss_enc.x().unwrap().as_slice().try_into().unwrap(),
ss_enc.as_bytes()[0],
ps_enc.x().unwrap().as_slice().try_into().unwrap(),
ps_enc.as_bytes()[0],
&[0xAAu8; 32],
&[0xBBu8; 32],
);
assert!(!valid, "invalid proof should not verify");
}
#[test]
fn test_recover_point_invalid_parity() {
let g = AffinePoint::from(ProjectivePoint::GENERATOR);
let encoded = g.to_encoded_point(true);
let x: [u8; 32] = encoded.x().unwrap().as_slice().try_into().unwrap();
assert!(recover_point(&x, 0x00).is_none());
assert!(recover_point(&x, 0x04).is_none());
assert!(recover_point(&x, 0xFF).is_none());
}
#[test]
fn test_chaum_pedersen_tampered_s() {
let mut rng = rand::thread_rng();
let priv_seq = Scalar::random(&mut rng);
let pub_seq = (ProjectivePoint::GENERATOR * priv_seq).to_affine();
let eph_priv = Scalar::random(&mut rng);
let eph_pub = (ProjectivePoint::GENERATOR * eph_priv).to_affine();
let shared_secret = (ProjectivePoint::from(eph_pub) * priv_seq).to_affine();
let k = Scalar::random(&mut rng);
let r1 = (ProjectivePoint::GENERATOR * k).to_affine();
let r2 = (ProjectivePoint::from(eph_pub) * k).to_affine();
let c = challenge_hash(&eph_pub, &pub_seq, &shared_secret, &r1, &r2);
let s = k + c * priv_seq;
let s_tampered = s + Scalar::ONE;
let eph_enc = eph_pub.to_encoded_point(true);
let ss_enc = shared_secret.to_encoded_point(true);
let ps_enc = pub_seq.to_encoded_point(true);
let valid = ChaumPedersenVerify::verify(
eph_enc.x().unwrap().as_slice().try_into().unwrap(),
eph_enc.as_bytes()[0],
ss_enc.x().unwrap().as_slice().try_into().unwrap(),
ss_enc.as_bytes()[0],
ps_enc.x().unwrap().as_slice().try_into().unwrap(),
ps_enc.as_bytes()[0],
&s_tampered.to_repr().into(),
&c.to_repr().into(),
);
assert!(!valid, "tampered s should not verify");
}
#[test]
fn test_chaum_pedersen_tampered_c() {
let mut rng = rand::thread_rng();
let priv_seq = Scalar::random(&mut rng);
let pub_seq = (ProjectivePoint::GENERATOR * priv_seq).to_affine();
let eph_priv = Scalar::random(&mut rng);
let eph_pub = (ProjectivePoint::GENERATOR * eph_priv).to_affine();
let shared_secret = (ProjectivePoint::from(eph_pub) * priv_seq).to_affine();
let k = Scalar::random(&mut rng);
let r1 = (ProjectivePoint::GENERATOR * k).to_affine();
let r2 = (ProjectivePoint::from(eph_pub) * k).to_affine();
let c = challenge_hash(&eph_pub, &pub_seq, &shared_secret, &r1, &r2);
let s = k + c * priv_seq;
let c_tampered = c + Scalar::ONE;
let eph_enc = eph_pub.to_encoded_point(true);
let ss_enc = shared_secret.to_encoded_point(true);
let ps_enc = pub_seq.to_encoded_point(true);
let valid = ChaumPedersenVerify::verify(
eph_enc.x().unwrap().as_slice().try_into().unwrap(),
eph_enc.as_bytes()[0],
ss_enc.x().unwrap().as_slice().try_into().unwrap(),
ss_enc.as_bytes()[0],
ps_enc.x().unwrap().as_slice().try_into().unwrap(),
ps_enc.as_bytes()[0],
&s.to_repr().into(),
&c_tampered.to_repr().into(),
);
assert!(!valid, "tampered c should not verify");
}
#[test]
fn test_chaum_pedersen_wrong_shared_secret_parity() {
let mut rng = rand::thread_rng();
let priv_seq = Scalar::random(&mut rng);
let pub_seq = (ProjectivePoint::GENERATOR * priv_seq).to_affine();
let eph_priv = Scalar::random(&mut rng);
let eph_pub = (ProjectivePoint::GENERATOR * eph_priv).to_affine();
let shared_secret = (ProjectivePoint::from(eph_pub) * priv_seq).to_affine();
let k = Scalar::random(&mut rng);
let r1 = (ProjectivePoint::GENERATOR * k).to_affine();
let r2 = (ProjectivePoint::from(eph_pub) * k).to_affine();
let c = challenge_hash(&eph_pub, &pub_seq, &shared_secret, &r1, &r2);
let s = k + c * priv_seq;
let eph_enc = eph_pub.to_encoded_point(true);
let ss_enc = shared_secret.to_encoded_point(true);
let ps_enc = pub_seq.to_encoded_point(true);
let ss_parity = ss_enc.as_bytes()[0];
let flipped_ss_parity = if ss_parity == 0x02 { 0x03 } else { 0x02 };
let valid = ChaumPedersenVerify::verify(
eph_enc.x().unwrap().as_slice().try_into().unwrap(),
eph_enc.as_bytes()[0],
ss_enc.x().unwrap().as_slice().try_into().unwrap(),
flipped_ss_parity,
ps_enc.x().unwrap().as_slice().try_into().unwrap(),
ps_enc.as_bytes()[0],
&s.to_repr().into(),
&c.to_repr().into(),
);
assert!(!valid, "wrong shared secret parity should not verify");
}
#[test]
fn test_chaum_pedersen_wrong_ephemeral_parity() {
let mut rng = rand::thread_rng();
let priv_seq = Scalar::random(&mut rng);
let pub_seq = (ProjectivePoint::GENERATOR * priv_seq).to_affine();
let eph_priv = Scalar::random(&mut rng);
let eph_pub = (ProjectivePoint::GENERATOR * eph_priv).to_affine();
let shared_secret = (ProjectivePoint::from(eph_pub) * priv_seq).to_affine();
let k = Scalar::random(&mut rng);
let r1 = (ProjectivePoint::GENERATOR * k).to_affine();
let r2 = (ProjectivePoint::from(eph_pub) * k).to_affine();
let c = challenge_hash(&eph_pub, &pub_seq, &shared_secret, &r1, &r2);
let s = k + c * priv_seq;
let eph_enc = eph_pub.to_encoded_point(true);
let ss_enc = shared_secret.to_encoded_point(true);
let ps_enc = pub_seq.to_encoded_point(true);
let eph_parity = eph_enc.as_bytes()[0];
let flipped_eph_parity = if eph_parity == 0x02 { 0x03 } else { 0x02 };
let valid = ChaumPedersenVerify::verify(
eph_enc.x().unwrap().as_slice().try_into().unwrap(),
flipped_eph_parity,
ss_enc.x().unwrap().as_slice().try_into().unwrap(),
ss_enc.as_bytes()[0],
ps_enc.x().unwrap().as_slice().try_into().unwrap(),
ps_enc.as_bytes()[0],
&s.to_repr().into(),
&c.to_repr().into(),
);
assert!(!valid, "wrong ephemeral pubkey parity should not verify");
}
#[test]
fn test_chaum_pedersen_identity_r1_r2() {
let mut rng = rand::thread_rng();
let priv_seq = Scalar::random(&mut rng);
let pub_seq = (ProjectivePoint::GENERATOR * priv_seq).to_affine();
let eph_priv = Scalar::random(&mut rng);
let eph_pub = (ProjectivePoint::GENERATOR * eph_priv).to_affine();
let shared_secret = (ProjectivePoint::from(eph_pub) * priv_seq).to_affine();
let c = Scalar::random(&mut rng);
let s = c * priv_seq;
let eph_enc = eph_pub.to_encoded_point(true);
let ss_enc = shared_secret.to_encoded_point(true);
let ps_enc = pub_seq.to_encoded_point(true);
let valid = ChaumPedersenVerify::verify(
eph_enc.x().unwrap().as_slice().try_into().unwrap(),
eph_enc.as_bytes()[0],
ss_enc.x().unwrap().as_slice().try_into().unwrap(),
ss_enc.as_bytes()[0],
ps_enc.x().unwrap().as_slice().try_into().unwrap(),
ps_enc.as_bytes()[0],
&s.to_repr().into(),
&c.to_repr().into(),
);
assert!(
!valid,
"degenerate proof with identity R1/R2 should not verify"
);
}
}