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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use crate::types::GroupGT;

use super::ECCurve::fp12::{DENSE, FP12};
use super::ECCurve::fp4::FP4;
use super::ECCurve::pair::{another, ate, ate2, fexp, initmp, miller};
use crate::constants::GROUP_GT_SIZE;
use crate::errors::{SerzDeserzError, ValueError};
use crate::field_elem::FieldElement;
use crate::group_elem::GroupElement;
use crate::group_elem_g1::G1;
use crate::group_elem_g2::{parse_hex_as_fp2, G2};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Mul;

use serde::de::{Deserialize, Deserializer, Error as DError, Visitor};
use serde::ser::{Serialize, Serializer};
use std::str::SplitWhitespace;
use zeroize::Zeroize;

#[derive(Clone)]
pub struct GT {
    value: GroupGT,
}

impl fmt::Debug for GT {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut c = self.value.clone();
        write!(f, "{}", c.tostring())
    }
}

impl GT {
    pub fn new() -> Self {
        Self {
            value: GroupGT::new(),
        }
    }

    /// Reduced ate pairing. Returns `e(g1, g2)`
    pub fn ate_pairing(g1: &G1, g2: &G2) -> Self {
        // This check is temporary. Until amcl is fixed.
        if g1.is_identity() || g2.is_identity() {
            return Self::one();
        }
        let e = ate(&g2.to_ecp(), &g1.to_ecp());
        Self { value: fexp(&e) }
    }

    /// Reduced ate double pairing. Returns `e(g1, g2) * e(h1, h2)`
    pub fn ate_2_pairing(g1: &G1, g2: &G2, h1: &G1, h2: &G2) -> Self {
        // This check is temporary. Until amcl is fixed.
        if g1.is_identity() || g2.is_identity() {
            return Self::ate_pairing(h1, h2);
        }
        if h1.is_identity() || h2.is_identity() {
            return Self::ate_pairing(g1, g2);
        }
        let e = ate2(&g2.to_ecp(), &g1.to_ecp(), &h2.to_ecp(), &h1.to_ecp());
        Self { value: fexp(&e) }
    }

    /// Reduced ate multi pairing. Takes a vector of tuples of group elements G1 and G2 as Vec<(&G1, &G2)>.
    /// Returns the product of their pairings.
    /// More efficient than using ate_pairing or ate_2_pairing and multiplying results
    pub fn ate_multi_pairing(elems: Vec<(&G1, &G2)>) -> Self {
        let mut accum = initmp();
        for (g1, g2) in elems {
            if g1.is_identity() || g2.is_identity() {
                continue;
            }
            another(&mut accum, &g2.to_ecp(), &g1.to_ecp());
        }
        let e = miller(&accum);
        Self { value: fexp(&e) }
    }

    /// Inner product of 2 vectors in group G1 and G2.
    /// Equivalent to a multi-pairing
    pub fn inner_product(left: &[G1], right: &[G2]) -> Result<Self, ValueError> {
        check_vector_size_for_equality!(left, right)?;
        let mut accum = initmp();
        for (g1, g2) in left.iter().zip(right) {
            if g1.is_identity() || g2.is_identity() {
                continue;
            }
            another(&mut accum, &g2.to_ecp(), &g1.to_ecp());
        }
        let e = miller(&accum);
        Ok(Self { value: fexp(&e) })
    }

    pub fn product(a: &Self, b: &Self) -> Self {
        let mut m = FP12::new_copy(&a.value);
        m.mul(&b.value);
        Self { value: m }
    }

    pub fn pow(&self, e: &FieldElement) -> Self {
        Self {
            value: self.value.pow(&e.to_bignum()),
        }
    }

    /// Return inverse of itself
    pub fn inverse(&self) -> Self {
        let mut inv = self.value.clone();
        inv.inverse();
        Self { value: inv }
    }

    pub fn inverse_mut(&mut self) {
        self.value.inverse()
    }

    pub fn is_one(&self) -> bool {
        return self.value.isunity();
    }

    pub fn one() -> Self {
        let zero = FP4::new_int(0);
        let one = FP4::new_int(1);
        Self {
            value: FP12::new_fp4s(&one, &zero, &zero),
        }
    }

    pub fn to_fp12(&self) -> FP12 {
        self.value.clone()
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        let mut temp = FP12::new();
        temp.copy(&self.value);
        let mut bytes: [u8; GROUP_GT_SIZE] = [0; GROUP_GT_SIZE];
        temp.tobytes(&mut bytes);
        bytes.to_vec()
    }

    pub fn from_bytes(bytes: &[u8]) -> Result<Self, SerzDeserzError> {
        if bytes.len() != GROUP_GT_SIZE {
            return Err(SerzDeserzError::GTBytesIncorrectSize(
                bytes.len(),
                GROUP_GT_SIZE,
            ));
        }
        Ok(Self {
            value: FP12::frombytes(bytes),
        })
    }

    /// Writes bytes to given slice. Raises exception when given slice is not of
    /// desired length.
    pub fn write_to_slice(&self, target: &mut [u8]) -> Result<(), SerzDeserzError> {
        if target.len() != GROUP_GT_SIZE {
            return Err(SerzDeserzError::GTBytesIncorrectSize(
                target.len(),
                GROUP_GT_SIZE,
            ));
        }
        let mut temp = FP12::new();
        temp.copy(&self.value);
        temp.tobytes(target);
        Ok(())
    }

    /// Writes bytes to given slice. Will panic when given slice is not of
    /// desired length.
    pub fn write_to_slice_unchecked(&self, target: &mut [u8]) {
        let mut temp = FP12::new();
        temp.copy(&self.value);
        temp.tobytes(target);
    }

    pub fn to_hex(&self) -> String {
        self.value.to_hex()
    }

    pub fn from_hex(s: String) -> Result<Self, SerzDeserzError> {
        let mut iter = s.split_whitespace();
        let a = parse_hex_as_fp4(&mut iter)?;
        let b = parse_hex_as_fp4(&mut iter)?;
        let c = parse_hex_as_fp4(&mut iter)?;
        let mut value = FP12::new();
        value.seta(a);
        value.setb(b);
        value.setc(c);
        value.settype(DENSE);
        Ok(Self { value })
    }

    /// Return a random group element. Only for testing.
    #[cfg(test)]
    pub fn random() -> Self {
        let g1 = G1::random();
        let g2 = G2::random();
        GT::ate_pairing(&g1, &g2)
    }
}

/// Parse given hex string as FP4
pub fn parse_hex_as_fp4(iter: &mut SplitWhitespace) -> Result<FP4, SerzDeserzError> {
    // Logic almost copied from AMCL but with error handling and constant time execution.
    // Constant time is important as hex is used during serialization and deserialization.
    // A seemingly effortless solution is to filter string for errors and pad with 0s before
    // passing to AMCL but that would be expensive as the string is scanned twice
    let a = parse_hex_as_fp2(iter)?;
    let b = parse_hex_as_fp2(iter)?;
    let mut fp4 = FP4::new();
    fp4.seta(a);
    fp4.setb(b);
    Ok(fp4)
}

impl PartialEq for GT {
    fn eq(&self, other: &GT) -> bool {
        self.value.equals(&other.value)
    }
}

impl_group_elem_conversions!(GT, GroupGT, GROUP_GT_SIZE);

impl_group_elem_traits!(GT, GroupGT);

impl Mul<GT> for GT {
    type Output = Self;

    fn mul(self, other: GT) -> Self {
        GT::product(&self, &other)
    }
}

impl Mul<&GT> for GT {
    type Output = Self;

    fn mul(self, other: &GT) -> Self {
        GT::product(&self, other)
    }
}

impl Mul<GT> for &GT {
    type Output = GT;

    fn mul(self, other: GT) -> GT {
        GT::product(&self, &other)
    }
}

impl Mul<&GT> for &GT {
    type Output = GT;

    fn mul(self, other: &GT) -> GT {
        GT::product(self, other)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::time::Instant;

    #[test]
    fn test_unity() {
        let one = GT::one();
        assert!(one.is_one());
    }

    #[test]
    fn test_inverse() {
        let minus_one = FieldElement::minus_one();
        for _ in 0..10 {
            let g1 = G1::random();
            let g2 = G2::random();
            let e = GT::ate_pairing(&g1, &g2);
            let e_inv = e.inverse();
            assert!(GT::product(&e, &e_inv).is_one());

            assert_eq!(GT::pow(&e, &minus_one), e_inv);
            assert_eq!(GT::pow(&e_inv, &minus_one), e);
        }
    }

    #[test]
    fn test_ate_pairing_identity() {
        let g1 = G1::random();
        let g2 = G2::random();
        let g1_identity = G1::identity();
        let g2_identity = G2::identity();

        // e(g1 + identity, g2) == e(g1, g2)*e(identity, g2)
        let lhs = GT::ate_pairing(&(&g1 + &g1_identity), &g2);
        let rhs = GT::product(
            &GT::ate_pairing(&g1, &g2),
            &GT::ate_pairing(&g1_identity, &g2),
        );
        assert_eq!(lhs, rhs);

        // e(g1, g2 + identity) == e(g1, g2)*e(g1, identity)
        let lhs = GT::ate_pairing(&g1, &(&g2 + &g2_identity));
        let rhs = GT::product(
            &GT::ate_pairing(&g1, &g2),
            &GT::ate_pairing(&g1, &g2_identity),
        );
        assert_eq!(lhs, rhs);

        let h1 = G1::random();
        let h2 = G2::random();

        // e(g1, g2)*e(identity, h2) == e(g1, g2)
        let lhs = GT::product(
            &GT::ate_pairing(&g1, &g2),
            &GT::ate_pairing(&g1_identity, &h2),
        );
        let rhs = GT::ate_pairing(&g1, &g2);
        assert_eq!(lhs, rhs);

        // e(identity, g2)*e(h1, h2) == e(h1, h2)
        let lhs = GT::product(
            &GT::ate_pairing(&g1_identity, &g2),
            &GT::ate_pairing(&h1, &h2),
        );
        let rhs = GT::ate_pairing(&h1, &h2);
        assert_eq!(lhs, rhs);

        assert!(GT::ate_pairing(&g1_identity, &g2_identity).is_one());

        // 2-pairing
        assert_eq!(GT::ate_2_pairing(&g1, &g2, &g1_identity, &h2), GT::ate_pairing(&g1, &g2));
        assert_eq!(GT::ate_2_pairing(&g1, &g2, &h1, &g2_identity), GT::ate_pairing(&g1, &g2));
        assert_eq!(GT::ate_2_pairing(&g1_identity, &g2, &h1, &h2), GT::ate_pairing(&h1, &h2));
        assert_eq!(GT::ate_2_pairing(&g1, &g2_identity, &h1, &h2), GT::ate_pairing(&h1, &h2));
        assert!(GT::ate_2_pairing(&g1_identity, &g2_identity, &g1_identity, &g2_identity).is_one());

        let k1 = G1::random();
        let k2 = G2::random();

        // multi-pairing
        assert!(
            GT::ate_multi_pairing(vec![(&g1, &g2), (&h1, &h2), (&g1_identity, &k2)])
                == GT::ate_multi_pairing(vec![(&g1, &g2), (&h1, &h2)]),
        );

        assert!(
            GT::ate_multi_pairing(vec![(&g1, &g2), (&h1, &h2), (&k1, &g2_identity)])
                == GT::ate_multi_pairing(vec![(&g1, &g2), (&h1, &h2)]),
        );

        assert!(
            GT::ate_multi_pairing(vec![(&g1, &g2), (&g1_identity, &h2), (&k1, &k2)])
                == GT::ate_multi_pairing(vec![(&g1, &g2), (&k1, &k2)]),
        );

        assert!(
            GT::ate_multi_pairing(vec![(&g1, &g2), (&g1_identity, &h2), (&k1, &k2)])
                == GT::ate_multi_pairing(vec![(&g1, &g2), (&k1, &k2)]),
        );

        assert!(GT::ate_multi_pairing(vec![
            (&g1_identity, &g2_identity),
            (&g1_identity, &g2_identity),
            (&g1_identity, &g2_identity)
        ])
        .is_one());
    }

    #[test]
    fn test_ate_pairing_negative() {
        let g1 = G1::random();
        let g2 = G2::random();
        let g1_neg = -&g1;
        let g2_neg = -&g2;

        // e(g1, -g2) == e(-g1, g2)
        let lhs = GT::ate_pairing(&g1, &g2_neg);
        let rhs = GT::ate_pairing(&g1_neg, &g2);
        assert_eq!(lhs, rhs);

        // e(g1, -g2) == e(-g1, g2) == e(g1, g2)^-1
        let e = GT::ate_pairing(&g1, &g2);
        let e_inv = e.inverse();
        assert_eq!(lhs, e_inv);

        let p = GT::ate_pairing(&g1, &g2);

        // e(g1, g2) = e(-g1, g2)^-1 => e(g1, g2) * e(-g1, g2) == 1
        assert_eq!(GT::product(&p, &lhs), GT::one());

        // e(g1, g2) = e(g1, -g2)^-1 => e(g1, g2) * e(g1, -g2) == 1
        assert_eq!(GT::product(&p, &rhs), GT::one());
    }

    #[test]
    fn test_ate_pairing_product() {
        let g1 = G1::random();
        let g2 = G2::random();
        let h1 = G1::random();
        let h2 = G2::random();
        let r1 = GT::ate_pairing(&g1, &g2);
        let r2 = GT::ate_pairing(&h1, &h2);
        let r3 = GT::product(&r1, &r2);
        let r4 = r1 * r2;
        assert_eq!(r3, r4);
    }

    #[test]
    fn test_ate_pairing() {
        let g1 = G1::random();
        let h1 = G1::random();
        let g2 = G2::random();
        let h2 = G2::random();

        // e(g1 + h1, g2) == e(g1, g2)*e(h1, g2)
        let lhs = GT::ate_pairing(&(&g1 + &h1), &g2);
        let rhs = GT::product(&GT::ate_pairing(&g1, &g2), &GT::ate_pairing(&h1, &g2));
        let rhs_1 = GT::ate_2_pairing(&g1, &g2, &h1, &g2);
        let rhs_2 = GT::ate_multi_pairing(vec![(&g1, &g2), (&h1, &g2)]);
        let rhs_3 = GT::inner_product(
            vec![g1.clone(), h1.clone()].as_slice(),
            vec![g2.clone(), g2.clone()].as_slice(),
        )
        .unwrap();
        assert_eq!(lhs, rhs);
        assert_eq!(rhs_1, rhs);
        assert_eq!(rhs_2, rhs);
        assert_eq!(rhs_3, rhs);

        // e(g1, g2+h2) == e(g1, g2)*e(g1, h2)
        let lhs = GT::ate_pairing(&g1, &(&g2 + &h2));
        let rhs = GT::product(&GT::ate_pairing(&g1, &g2), &GT::ate_pairing(&g1, &h2));
        let rhs_1 = GT::ate_2_pairing(&g1, &g2, &g1, &h2);
        let rhs_2 = GT::ate_multi_pairing(vec![(&g1, &g2), (&g1, &h2)]);
        let rhs_3 = GT::inner_product(
            vec![g1.clone(), g1.clone()].as_slice(),
            vec![g2.clone(), h2.clone()].as_slice(),
        )
        .unwrap();
        assert_eq!(lhs, rhs);
        assert_eq!(rhs_1, rhs);
        assert_eq!(rhs_2, rhs);
        assert_eq!(rhs_3, rhs);

        let r = FieldElement::random();
        // e(g1, g2^r) == e(g1^r, g2) == e(g1, g2)^r
        let p1 = GT::ate_pairing(&g1, &(&g2 * &r));
        let p2 = GT::ate_pairing(&(&g1 * &r), &g2);
        let mut p = GT::ate_pairing(&g1, &g2);
        p = p.pow(&r);
        assert_eq!(p1, p2);
        assert_eq!(p1, p);
    }

    #[test]
    fn timing_ate_multi_pairing() {
        let count = 10;
        let g1_vec = (0..count).map(|_| G1::random()).collect::<Vec<G1>>();
        let g2_vec = (0..count).map(|_| G2::random()).collect::<Vec<G2>>();
        let mut tuple_vec = vec![];

        let start = Instant::now();
        let mut accum = GT::ate_pairing(&g1_vec[0], &g2_vec[0]);
        tuple_vec.push((&g1_vec[0], &g2_vec[0]));
        for i in 1..count {
            let e = GT::ate_pairing(&g1_vec[i], &g2_vec[i]);
            accum = GT::product(&accum, &e);
            tuple_vec.push((&g1_vec[i], &g2_vec[i]));
        }
        println!(
            "Time to compute {} pairings naively is {:?}",
            count,
            start.elapsed()
        );

        let start = Instant::now();
        let multi = GT::ate_multi_pairing(tuple_vec);
        println!(
            "Time to compute {} pairings using multi-pairings is {:?}",
            count,
            start.elapsed()
        );
        assert_eq!(accum, multi);

        let ip = GT::inner_product(&g1_vec, &g2_vec).unwrap();
        assert_eq!(accum, ip);
    }

    #[test]
    fn timing_pairing_pow() {
        // Compare cost of e(g1, g2)^r with e(g1^r, g2) and e(g1, g2^r)
        let count = 10;
        let g1_vec = (0..count).map(|_| G1::random()).collect::<Vec<G1>>();
        let g2_vec = (0..count).map(|_| G2::random()).collect::<Vec<G2>>();
        let r_vec = (0..count)
            .map(|_| FieldElement::random())
            .collect::<Vec<FieldElement>>();

        //e(g1, g2)^r
        let mut pairing_exp = vec![];

        // e(g1^r, g2)
        let mut g1_exp = vec![];

        // e(g1, g2^r)
        let mut g2_exp = vec![];

        let start = Instant::now();
        for i in 0..count {
            pairing_exp.push(GT::pow(&GT::ate_pairing(&g1_vec[i], &g2_vec[i]), &r_vec[i]));
        }
        println!(
            "Time to compute {} pairing and then exponentiation is {:?}",
            count,
            start.elapsed()
        );

        let start = Instant::now();
        for i in 0..count {
            g1_exp.push(GT::ate_pairing(&(&g1_vec[i] * &r_vec[i]), &g2_vec[i]));
        }
        println!(
            "Time to compute {} pairing after exponentiation in G1 is {:?}",
            count,
            start.elapsed()
        );

        let start = Instant::now();
        for i in 0..count {
            g2_exp.push(GT::ate_pairing(&g1_vec[i], &(&g2_vec[i] * &r_vec[i])));
        }
        println!(
            "Time to compute {} pairing after exponentiation in G2 is {:?}",
            count,
            start.elapsed()
        );

        for i in 0..count {
            assert_eq!(pairing_exp[i], g1_exp[i]);
            assert_eq!(pairing_exp[i], g2_exp[i]);
        }
    }
}