-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgmac.rs
More file actions
521 lines (471 loc) · 15.9 KB
/
algmac.rs
File metadata and controls
521 lines (471 loc) · 15.9 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use crate::error::{Error, SignatureError};
use algebra::{
bytes::{ToBytes, FromBytes},
curves::ProjectiveCurve,
groups::Group,
fields::PrimeField,
to_bytes, UniformRand,
};
use digest::Digest;
use rand::Rng;
use std::{
io::{Result as IoResult, Write, Read},
marker::PhantomData,
vec::Vec,
};
//TODO: switch generic type back to Group if able to fix serialization/hashing
pub struct GGM<G: ProjectiveCurve, D: Digest>(PhantomData<G>, PhantomData<D>);
pub struct PublicParams<G: ProjectiveCurve> {
pub g: G,
pub h: G,
}
//TODO: derive algebra::bytes::ToBytes from struct?
impl<G: ProjectiveCurve> ToBytes for PublicParams<G> {
fn write<W: Write>(self: &Self, mut writer: W) -> IoResult<()> {
self.g.write(&mut writer)?;
self.h.write(&mut writer)
}
}
// Public and private MAC key pair
#[derive(Clone)]
#[allow(non_snake_case)]
pub struct PubKey<G: ProjectiveCurve> {
pub CX0: G,
pub X1: G,
}
#[allow(non_snake_case)]
impl<G: ProjectiveCurve> FromBytes for PubKey<G> {
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let CX0 = G::read(&mut reader)?;
let X1 = G::read(&mut reader)?;
Ok(Self { CX0, X1 })
}
}
impl<G: ProjectiveCurve> ToBytes for PubKey<G> {
fn write<W: Write>(self: &Self, mut writer: W) -> IoResult<()> {
self.CX0.write(&mut writer)?;
self.X1.write(&mut writer)
}
}
pub struct SecretKey<G: ProjectiveCurve> {
pub x0: G::ScalarField,
pub x1: G::ScalarField,
pub xt: G::ScalarField,
pub pk: PubKey<G>,
}
impl<G: ProjectiveCurve> FromBytes for SecretKey<G> {
fn read<R: Read>(mut reader: R) -> IoResult<Self> {
let x0 = <G::ScalarField>::read(&mut reader)?;
let x1 = <G::ScalarField>::read(&mut reader)?;
let xt = <G::ScalarField>::read(&mut reader)?;
let pk = PubKey::<G>::read(&mut reader)?;
Ok(Self { x0, x1, xt, pk })
}
}
impl<G: ProjectiveCurve> ToBytes for SecretKey<G> {
fn write<W: Write>(self: &Self, mut writer: W) -> IoResult<()> {
self.x0.write(&mut writer)?;
self.x1.write(&mut writer)?;
self.xt.write(&mut writer)?;
self.pk.write(&mut writer)
}
}
#[derive(Clone)]
pub struct Mac<G: ProjectiveCurve> {
pub u0: G,
pub u1: G,
}
impl<G: ProjectiveCurve> ToBytes for Mac<G> {
fn write<W: Write>(self: &Self, mut writer: W) -> IoResult<()> {
self.u0.write(&mut writer)?;
self.u1.write(&mut writer)
}
}
#[allow(non_snake_case)]
pub struct MacProof<G: ProjectiveCurve, D: Digest> {
M_r: G,
z0: G::ScalarField,
z1: G::ScalarField,
zt: G::ScalarField,
zr: G::ScalarField,
c: G::ScalarField,
phantom: PhantomData<D>,
}
#[derive(Clone)]
#[allow(non_snake_case)]
pub struct BlindMacInput<G: ProjectiveCurve> {
pub D: G,
pub ct1: G,
pub ct2: G,
}
impl<G: ProjectiveCurve> ToBytes for BlindMacInput<G> {
fn write<W: Write>(self: &Self, mut writer: W) -> IoResult<()> {
self.D.write(&mut writer)?;
self.ct1.write(&mut writer)?;
self.ct2.write(&mut writer)
}
}
pub struct BlindMacState<G: ProjectiveCurve> {
pub delta: G::ScalarField,
pub r: G::ScalarField,
pub input: BlindMacInput<G>,
}
pub struct BlindMacOutput<G: ProjectiveCurve, D: Digest> {
u0: G,
ct1: G,
ct2: G,
proof: BlindMacProof<G, D>,
}
#[allow(non_snake_case)]
pub struct BlindMacProof<G: ProjectiveCurve, D: Digest> {
X_b1: G,
z_x0: G::ScalarField,
z_x1: G::ScalarField,
z_xt: G::ScalarField,
z_r: G::ScalarField,
z_b: G::ScalarField,
z_b1: G::ScalarField,
c: G::ScalarField,
phantom: PhantomData<D>,
}
impl<G: ProjectiveCurve, D: Digest> GGM<G, D> {
pub fn setup<R: Rng>(gen: &G, rng: &mut R) -> PublicParams<G> {
PublicParams {
g: gen.mul(&G::ScalarField::rand(rng)),
h: gen.mul(&G::ScalarField::rand(rng)),
}
}
pub fn keygen<R: Rng>(pp: &PublicParams<G>, rng: &mut R)
-> (PubKey<G>, SecretKey<G>) {
let x0 = G::ScalarField::rand(rng);
let x1 = G::ScalarField::rand(rng);
let xt = G::ScalarField::rand(rng);
let pk = PubKey {
CX0: pp.g.mul(&x0) + pp.h.mul(&xt),
X1: pp.h.mul(&x1),
};
let sk = SecretKey {
x0: x0,
x1: x1,
xt: xt,
pk: pk.clone(),
};
(pk, sk)
}
pub fn scalar_mac<R: Rng>(
pp: &PublicParams<G>,
sk: &SecretKey<G>,
m: &G::ScalarField,
rng: &mut R,
) -> Mac<G> {
Mac {
u0: pp.g,
u1: pp.g.mul(&(sk.x0 + &(sk.x1 * m))),
}.rerandomize(&G::ScalarField::rand(rng))
}
// MACs "M" where "M" in G
#[allow(non_snake_case)]
pub fn group_elem_mac<R: Rng>(
pp: &PublicParams<G>,
sk: &SecretKey<G>,
M: &G,
rng: &mut R,
) -> Mac<G> {
Mac {
u0: pp.g,
u1: M.mul(&sk.x1) + pp.g.mul(&sk.x0),
}.rerandomize(&G::ScalarField::rand(rng))
}
pub fn verify_mac(
_pp: &PublicParams<G>,
sk: &SecretKey<G>,
m: &G::ScalarField,
t: &Mac<G>
) -> bool {
t.u0.mul(&(*m * &sk.x1 + &sk.x0)) == t.u1
}
// MACs "M" where "M = g^m" and "m" is hidden
#[allow(non_snake_case)]
pub fn group_elem_mac_and_prove<R: Rng>(
pp: &PublicParams<G>,
sk: &SecretKey<G>,
M: &G,
rng: &mut R,
) -> Result<(Mac<G>, MacProof<G, D>), Error>{
// Create MAC with base g and rerandomize
let r = G::ScalarField::rand(rng);
let t = Mac {
u0: pp.g,
u1: M.mul(&sk.x1) + pp.g.mul(&sk.x0),
}.rerandomize(&r);
let M_r = M.mul(&r);
// Generate random commitments
let (r0, r1, rt, rr, c) = loop {
let r0 = G::ScalarField::rand(rng);
let r1 = G::ScalarField::rand(rng);
let rt = G::ScalarField::rand(rng);
let rr = G::ScalarField::rand(rng);
let s_u0 = pp.g.mul(&rr);
let s_mr = M.mul(&rr);
let s_u1 = t.u0.mul(&r0) + M_r.mul(&(r1));
let s_cx0 = pp.g.mul(&r0) + pp.h.mul(&rt);
let s_x1 = pp.h.mul(&r1);
// Hash statement and commitments to get challenge
// TODO: Does hash function create random bytes that maps to full scalar field?
let mut hash_input = Vec::new();
let hash_bytes = to_bytes![
pp,
sk.pk,
t,
M, M_r,
s_u0.into_affine(),
s_mr.into_affine(),
s_u1.into_affine(),
s_cx0.into_affine(),
s_x1.into_affine()
]?;
hash_input.extend_from_slice(&hash_bytes);
if let Some(c) = G::ScalarField::from_random_bytes(&D::digest(&hash_input)) {
break (r0, r1, rt, rr, c);
};
};
// Calculate prover response
let proof = MacProof {
M_r: M_r,
z0: r0 + &(c * &sk.x0),
z1: r1 + &(c * &sk.x1),
zt : rt + &(c * &sk.xt),
zr : rr + &(c * &r),
c: c,
phantom: PhantomData,
};
Ok((t, proof))
}
#[allow(non_snake_case)]
pub fn verify_mac_proof(
pp: &PublicParams<G>,
pk: &PubKey<G>,
M: &G,
t: &Mac<G>,
proof: &MacProof<G, D>
) -> Result<bool, Error> {
let s_u0 = pp.g.mul(&proof.zr) - &t.u0.mul(&proof.c);
let s_mr = M.mul(&proof.zr) - &proof.M_r.mul(&proof.c);
let s_u1 = t.u0.mul(&proof.z0) + &proof.M_r.mul(&(proof.z1)) - &t.u1.mul(&proof.c);
let s_cx0 = pp.g.mul(&proof.z0) + pp.h.mul(&proof.zt) - &pk.CX0.mul(&proof.c);
let s_x1 = pp.h.mul(&proof.z1) - &pk.X1.mul(&proof.c);
let mut hash_input = Vec::new();
let hash_bytes = to_bytes![
pp,
pk,
t,
M, &proof.M_r,
s_u0.into_affine(),
s_mr.into_affine(),
s_u1.into_affine(),
s_cx0.into_affine(),
s_x1.into_affine()
]?;
hash_input.extend_from_slice(&hash_bytes);
match G::ScalarField::from_random_bytes(&D::digest(&hash_input)) {
None => Err(Box::new(SignatureError::ProofVerificationFailed)),
Some(c) => {
if c == proof.c {
Ok(true)
} else {
Err(Box::new(SignatureError::ProofVerificationFailed))
}
},
}
}
#[allow(non_snake_case)]
pub fn blind_mac_input<R: Rng>(
pp: &PublicParams<G>,
m: &G::ScalarField,
rng: &mut R,
) -> (BlindMacState<G>, BlindMacInput<G>) {
let r = G::ScalarField::rand(rng);
let delta = G::ScalarField::rand(rng);
let D = pp.g.mul(&delta);
let input = BlindMacInput{
D: D,
ct1: pp.g.mul(&r),
ct2: pp.g.mul(m) + D.mul(&r),
};
(BlindMacState{delta: delta, r: r, input: input.clone()}, input)
}
#[allow(non_snake_case)]
pub fn blind_mac_eval<R: Rng>(
pp: &PublicParams<G>,
sk: &SecretKey<G>,
inp: &BlindMacInput<G>,
rng: &mut R,
) -> Result<BlindMacOutput<G, D>, Error> {
let b = G::ScalarField::rand(rng);
let r = G::ScalarField::rand(rng);
// Homomorphically evaluate encryption of MAC
let b1 = sk.x1 * &b;
let u0 = pp.g.mul(&b);
let ct1 = inp.ct1.mul(&b1) + pp.g.mul(&r);
let ct2 = inp.ct2.mul(&b1) + u0.mul(&sk.x0) + inp.D.mul(&r);
// Create auxiliary variable useful for proving discrete log products
let X_b1 = pp.h.mul(&b1);
// Generate random commitments and challenge for Sigma protocol
let (r_x0, r_x1, r_xt, r_r, r_b, r_b1, c) = loop {
let r_x0 = G::ScalarField::rand(rng);
let r_x1 = G::ScalarField::rand(rng);
let r_xt = G::ScalarField::rand(rng);
let r_r = G::ScalarField::rand(rng);
let r_b = G::ScalarField::rand(rng);
let r_b1 = G::ScalarField::rand(rng);
let s_x1 = pp.h.mul(&r_x1);
let s_cx0 = pp.g.mul(&r_x0) + pp.h.mul(&r_xt);
let s_xb1_a = sk.pk.X1.mul(&r_b);
let s_xb1_b = pp.h.mul(&r_b1);
let s_u0 = pp.g.mul(&r_b);
let s_ct1 = inp.ct1.mul(&r_b1) + pp.g.mul(&r_r);
let s_ct2 = inp.ct2.mul(&r_b1) + u0.mul(&r_x0) + inp.D.mul(&r_r);
let mut hash_input = Vec::new();
let hash_bytes = to_bytes![
pp,
sk.pk,
inp,
u0, X_b1,
ct1, ct2,
s_x1.into_affine(),
s_cx0.into_affine(),
s_xb1_a.into_affine(),
s_xb1_b.into_affine(),
s_u0.into_affine(),
s_ct1.into_affine(),
s_ct2.into_affine()
]?;
hash_input.extend_from_slice(&hash_bytes);
if let Some(c) = G::ScalarField::from_random_bytes(&D::digest(&hash_input)) {
break (r_x0, r_x1, r_xt, r_r, r_b, r_b1, c);
};
};
// Calculate prover response
let proof = BlindMacProof {
X_b1: X_b1,
z_x0: r_x0 + &(c * &sk.x0),
z_x1: r_x1 + &(c * &sk.x1),
z_xt: r_xt + &(c * &sk.xt),
z_r: r_r + &(c * &r),
z_b: r_b + &(c * &b),
z_b1: r_b1 + &(c * &b1),
c: c,
phantom: PhantomData,
};
let output = BlindMacOutput {
u0: u0,
ct1: ct1,
ct2: ct2,
proof: proof,
};
Ok(output)
}
pub fn blind_mac_verify_output(
pp: &PublicParams<G>,
pk: &PubKey<G>,
st: &BlindMacState<G>,
output: &BlindMacOutput<G, D>,
) -> Result<Mac<G>, Error> {
let s_x1 = pp.h.mul(&output.proof.z_x1) - &pk.X1.mul(&output.proof.c);
let s_cx0 = pp.g.mul(&output.proof.z_x0) + pp.h.mul(&output.proof.z_xt) - &pk.CX0.mul(&output.proof.c);
let s_xb1_a = pk.X1.mul(&output.proof.z_b) - &output.proof.X_b1.mul(&output.proof.c);
let s_xb1_b = pp.h.mul(&output.proof.z_b1) - &output.proof.X_b1.mul(&output.proof.c);
let s_u0 = pp.g.mul(&output.proof.z_b) - &output.u0.mul(&output.proof.c);
let s_ct1 = st.input.ct1.mul(&output.proof.z_b1) + pp.g.mul(&output.proof.z_r) - &output.ct1.mul(&output.proof.c);
let s_ct2 = st.input.ct2.mul(&output.proof.z_b1) + output.u0.mul(&output.proof.z_x0) + st.input.D.mul(&output.proof.z_r) - &output.ct2.mul(&output.proof.c);
let mut hash_input = Vec::new();
let hash_bytes = to_bytes![
pp,
pk,
st.input,
output.u0, output.proof.X_b1,
output.ct1, output.ct2,
s_x1.into_affine(),
s_cx0.into_affine(),
s_xb1_a.into_affine(),
s_xb1_b.into_affine(),
s_u0.into_affine(),
s_ct1.into_affine(),
s_ct2.into_affine()
]?;
hash_input.extend_from_slice(&hash_bytes);
match G::ScalarField::from_random_bytes(&D::digest(&hash_input)) {
None => Err(Box::new(SignatureError::ProofVerificationFailed)),
Some(c) => {
if c == output.proof.c {
let u1 = output.ct2 - &output.ct1.mul(&st.delta);
Ok(Mac{u0: output.u0, u1: u1})
} else {
Err(Box::new(SignatureError::ProofVerificationFailed))
}
},
}
}
}
impl<G: ProjectiveCurve> Mac<G> {
pub fn rerandomize(self: &Self, r: &G::ScalarField) -> Self {
Mac {
u0: self.u0.mul(&r),
u1: self.u1.mul(&r),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use algebra::{
curves::{
bls12_381::G1Projective,
ProjectiveCurve,
},
fields::bls12_381::Fr,
UniformRand,
};
use rand::{
SeedableRng,
rngs::StdRng,
};
use sha3::Sha3_256;
#[test]
#[allow(non_snake_case)]
fn mac_verify() {
let mut rng = StdRng::seed_from_u64(0u64);
type AlgMacG1 = GGM<G1Projective, Sha3_256>;
let pp = AlgMacG1::setup(&G1Projective::prime_subgroup_generator(), &mut rng);
let (_, sk) = AlgMacG1::keygen(&pp, &mut rng);
let m = Fr::rand(&mut rng);
let M = pp.g.mul(&m);
let t = AlgMacG1::group_elem_mac(&pp, &sk, &M, &mut rng);
assert!(AlgMacG1::verify_mac(&pp, &sk, &m, &t));
assert!(AlgMacG1::verify_mac(&pp, &sk, &m, &t.rerandomize(&Fr::rand(&mut rng))));
}
#[test]
#[allow(non_snake_case)]
fn mac_proof() {
let mut rng = StdRng::seed_from_u64(0u64);
type AlgMacG1 = GGM<G1Projective, Sha3_256>;
let pp = AlgMacG1::setup(&G1Projective::prime_subgroup_generator(), &mut rng);
let (pk, sk) = AlgMacG1::keygen(&pp, &mut rng);
let m = Fr::rand(&mut rng);
let M = pp.g.mul(&m);
let (t, proof) = AlgMacG1::group_elem_mac_and_prove(&pp, &sk, &M, &mut rng).unwrap();
assert!(AlgMacG1::verify_mac_proof(&pp, &pk, &M, &t, &proof).unwrap());
}
#[test]
fn blind_mac() {
let mut rng = StdRng::seed_from_u64(0u64);
type AlgMacG1 = GGM<G1Projective, Sha3_256>;
let pp = AlgMacG1::setup(&G1Projective::prime_subgroup_generator(), &mut rng);
let (pk, sk) = AlgMacG1::keygen(&pp, &mut rng);
let m = Fr::rand(&mut rng);
let (st, input) = AlgMacG1::blind_mac_input(&pp, &m, &mut rng);
let output = AlgMacG1::blind_mac_eval(&pp, &sk, &input, &mut rng).unwrap();
let t = AlgMacG1::blind_mac_verify_output(&pp, &pk, &st, &output).unwrap();
assert!(AlgMacG1::verify_mac(&pp, &sk, &m, &t));
}
}