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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
use bls12_381::{self, *};
use ff::{PrimeField, PrimeFieldRepr};
use std::io::{Error, ErrorKind, Read, Result, Write};
use CurveAffine;
use CurveProjective;
use EncodedPoint;
type Compressed = bool;

/// Serialization support for group elements.
pub trait SerDes: Sized {
    /// Serialize a struct to a writer with a flag of compressness.
    fn serialize<W: Write>(&self, writer: &mut W, compressed: Compressed) -> Result<()>;

    /// Deserialize a struct; give an indicator if the element was compressed or not.
    /// Returns an error is the encoding does not match the indicator.
    fn deserialize<R: Read>(reader: &mut R, compressed: Compressed) -> Result<Self>;
}

impl SerDes for Fr {
    /// The compressed parameter has no effect since Fr element will always be compressed.
    fn serialize<W: Write>(&self, writer: &mut W, _compressed: Compressed) -> Result<()> {
        self.into_repr().write_be(writer)
    }

    /// The compressed parameter has no effect since Fr element will always be compressed.
    fn deserialize<R: Read>(reader: &mut R, _compressed: Compressed) -> Result<Self> {
        let mut r = FrRepr::default();
        r.read_be(reader)?;
        match Fr::from_repr(r) {
            Err(e) => Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => Ok(p),
        }
    }
}

impl SerDes for Fq12 {
    /// The compressed parameter has no effect since Fr element will always be compressed.
    fn serialize<W: Write>(&self, writer: &mut W, _compressed: Compressed) -> Result<()> {
        let mut buf: Vec<u8> = vec![];

        match self.c0.c0.c0.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c0.c0.c1.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c0.c1.c0.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c0.c1.c1.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c0.c2.c0.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c0.c2.c1.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c1.c0.c0.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };

        match self.c1.c0.c1.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c1.c1.c0.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c1.c1.c1.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c1.c2.c0.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        match self.c1.c2.c1.into_repr().write_be(&mut buf) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(p) => p,
        };
        writer.write_all(&buf)?;
        Ok(())
    }

    /// The compressed parameter has no effect since Fr element will always be compressed.
    fn deserialize<R: Read>(mut reader: &mut R, _compressed: Compressed) -> Result<Self> {
        let mut q = FqRepr::default();
        q.read_be(&mut reader)?;
        let c000 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c001 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c010 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c011 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c020 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c021 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c100 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c101 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c110 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c111 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c120 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        q.read_be(&mut reader)?;
        let c121 = match Fq::from_repr(q) {
            Err(e) => return Err(Error::new(ErrorKind::Other, e)),
            Ok(q) => q,
        };
        Ok(Fq12 {
            c0: Fq6 {
                c0: Fq2 { c0: c000, c1: c001 },

                c1: Fq2 { c0: c010, c1: c011 },

                c2: Fq2 { c0: c020, c1: c021 },
            },
            c1: Fq6 {
                c0: Fq2 { c0: c100, c1: c101 },

                c1: Fq2 { c0: c110, c1: c111 },

                c2: Fq2 { c0: c120, c1: c121 },
            },
        })
    }
}

impl SerDes for G1 {
    /// Convert a G1 point to a blob.
    fn serialize<W: Write>(&self, writer: &mut W, compressed: Compressed) -> Result<()> {
        let t = self.into_affine();

        // convert element into an (un)compressed byte string
        let buf = {
            if compressed {
                let tmp = bls12_381::G1Compressed::from_affine(t);
                tmp.as_ref().to_vec()
            } else {
                let tmp = bls12_381::G1Uncompressed::from_affine(t);
                tmp.as_ref().to_vec()
            }
        };

        // format the output
        writer.write_all(&buf)?;
        Ok(())
    }

    /// Deserialize a G1 element from a blob.
    /// Returns an error if deserialization fails.
    fn deserialize<R: Read>(reader: &mut R, compressed: Compressed) -> Result<Self> {
        // read into buf of compressed size
        let mut buf = vec![0u8; G1Compressed::size()];
        reader.read_exact(&mut buf)?;

        // check the first bit of buf[0] to decide if the point is compressed
        // or not
        // first bit is 1 => compressed mode
        // first bit is 0 => uncompressed mode
        if ((buf[0] & 0x80) == 0x80) != compressed {
            return Err(Error::new(ErrorKind::InvalidData, "Invalid compressness"));
        }

        if compressed {
            // convert the blob into a group element
            let mut g_buf = G1Compressed::empty();
            g_buf.as_mut().copy_from_slice(&buf);
            let g = match g_buf.into_affine() {
                Ok(p) => p.into_projective(),
                Err(e) => return Err(Error::new(ErrorKind::InvalidData, e)),
            };
            Ok(g)
        } else {
            // read the next uncompressed - compressed size
            let mut buf2 = vec![0u8; G1Uncompressed::size() - G1Compressed::size()];
            reader.read_exact(&mut buf2)?;
            // now buf holds the whole uncompressed bytes
            buf.append(&mut buf2);
            // convert the buf into a group element
            let mut g_buf = G1Uncompressed::empty();
            g_buf.as_mut().copy_from_slice(&buf);
            let g = match g_buf.into_affine() {
                Ok(p) => p.into_projective(),
                Err(e) => return Err(Error::new(ErrorKind::InvalidData, e)),
            };
            Ok(g)
        }
    }
}

impl SerDes for G2 {
    /// Convert a G2 point to a blob.
    fn serialize<W: Write>(&self, writer: &mut W, compressed: Compressed) -> Result<()> {
        let t = self.into_affine();
        // convert element into an (un)compressed byte string
        let buf = {
            if compressed {
                let tmp = bls12_381::G2Compressed::from_affine(t);
                tmp.as_ref().to_vec()
            } else {
                let tmp = bls12_381::G2Uncompressed::from_affine(t);
                tmp.as_ref().to_vec()
            }
        };

        // format the output
        writer.write_all(&buf)?;
        Ok(())
    }

    /// Deserialize a G2 element from a blob.
    /// Returns an error if deserialization fails.
    fn deserialize<R: Read>(reader: &mut R, compressed: Compressed) -> Result<Self> {
        // read into buf of compressed size
        let mut buf = vec![0u8; G2Compressed::size()];
        reader.read_exact(&mut buf)?;

        // check the first bit of buf[0] to decide if the point is compressed
        // or not
        // first bit is 1 => compressed mode
        // first bit is 0 => uncompressed mode
        if ((buf[0] & 0x80) == 0x80) != compressed {
            return Err(Error::new(ErrorKind::InvalidData, "Invalid compressness"));
        }

        if compressed {
            // convert the buf into a group element
            let mut g_buf = G2Compressed::empty();
            g_buf.as_mut().copy_from_slice(&buf);
            let g = match g_buf.into_affine() {
                Ok(p) => p.into_projective(),
                Err(e) => return Err(Error::new(ErrorKind::InvalidData, e)),
            };
            Ok(g)
        } else {
            // read the next uncompressed - compressed size
            let mut buf2 = vec![0u8; G2Uncompressed::size() - G2Compressed::size()];
            reader.read_exact(&mut buf2)?;
            // now buf holds the whole uncompressed bytes
            buf.append(&mut buf2);
            // convert the buf into a group element
            let mut g_buf = G2Uncompressed::empty();
            g_buf.as_mut().copy_from_slice(&buf);
            let g = match g_buf.into_affine() {
                Ok(p) => p.into_projective(),
                Err(e) => return Err(Error::new(ErrorKind::InvalidData, e)),
            };
            Ok(g)
        }
    }
}

impl SerDes for G1Affine {
    /// Convert a G1 point to a blob.
    fn serialize<W: Write>(&self, writer: &mut W, compressed: Compressed) -> Result<()> {
        // convert element into an (un)compressed byte string
        let buf = {
            if compressed {
                let tmp = bls12_381::G1Compressed::from_affine(*self);
                tmp.as_ref().to_vec()
            } else {
                let tmp = bls12_381::G1Uncompressed::from_affine(*self);
                tmp.as_ref().to_vec()
            }
        };

        // format the output
        writer.write_all(&buf)?;
        Ok(())
    }

    /// Deserialize a G1 element from a blob.
    /// Returns an error if deserialization fails.
    fn deserialize<R: Read>(reader: &mut R, compressed: Compressed) -> Result<Self> {
        // read into buf of compressed size
        let mut buf = vec![0u8; G1Compressed::size()];
        reader.read_exact(&mut buf)?;

        // check the first bit of buf[0] to decide if the point is compressed
        // or not
        // first bit is 1 => compressed mode
        // first bit is 0 => uncompressed mode
        if ((buf[0] & 0x80) == 0x80) != compressed {
            return Err(Error::new(ErrorKind::InvalidData, "Invalid compressness"));
        }

        if compressed {
            // convert the blob into a group element
            let mut g_buf = G1Compressed::empty();
            g_buf.as_mut().copy_from_slice(&buf);
            let g = match g_buf.into_affine() {
                Ok(p) => p,
                Err(e) => return Err(Error::new(ErrorKind::InvalidData, e)),
            };
            Ok(g)
        } else {
            // read the next uncompressed - compressed size
            let mut buf2 = vec![0u8; G1Uncompressed::size() - G1Compressed::size()];
            reader.read_exact(&mut buf2)?;
            // now buf holds the whole uncompressed bytes
            buf.append(&mut buf2);
            // convert the buf into a group element
            let mut g_buf = G1Uncompressed::empty();
            g_buf.as_mut().copy_from_slice(&buf);
            let g = match g_buf.into_affine() {
                Ok(p) => p,
                Err(e) => return Err(Error::new(ErrorKind::InvalidData, e)),
            };
            Ok(g)
        }
    }
}

impl SerDes for G2Affine {
    /// Convert a G2 point to a blob.
    fn serialize<W: Write>(&self, writer: &mut W, compressed: Compressed) -> Result<()> {
        // convert element into an (un)compressed byte string
        let buf = {
            if compressed {
                let tmp = bls12_381::G2Compressed::from_affine(*self);
                tmp.as_ref().to_vec()
            } else {
                let tmp = bls12_381::G2Uncompressed::from_affine(*self);
                tmp.as_ref().to_vec()
            }
        };

        // format the output
        writer.write_all(&buf)?;
        Ok(())
    }

    /// Deserialize a G2 element from a blob.
    /// Returns an error if deserialization fails.
    fn deserialize<R: Read>(reader: &mut R, compressed: Compressed) -> Result<Self> {
        // read into buf of compressed size
        let mut buf = vec![0u8; G2Compressed::size()];
        reader.read_exact(&mut buf)?;

        // check the first bit of buf[0] to decide if the point is compressed
        // or not
        // first bit is 1 => compressed mode
        // first bit is 0 => uncompressed mode
        if ((buf[0] & 0x80) == 0x80) != compressed {
            return Err(Error::new(ErrorKind::InvalidData, "Invalid compressness"));
        }

        if compressed {
            // convert the buf into a group element
            let mut g_buf = G2Compressed::empty();
            g_buf.as_mut().copy_from_slice(&buf);
            let g = match g_buf.into_affine() {
                Ok(p) => p,
                Err(e) => return Err(Error::new(ErrorKind::InvalidData, e)),
            };
            Ok(g)
        } else {
            // read the next uncompressed - compressed size
            let mut buf2 = vec![0u8; G2Uncompressed::size() - G2Compressed::size()];
            reader.read_exact(&mut buf2)?;
            // now buf holds the whole uncompressed bytes
            buf.append(&mut buf2);
            // convert the buf into a group element
            let mut g_buf = G2Uncompressed::empty();
            g_buf.as_mut().copy_from_slice(&buf);
            let g = match g_buf.into_affine() {
                Ok(p) => p,
                Err(e) => return Err(Error::new(ErrorKind::InvalidData, e)),
            };
            Ok(g)
        }
    }
}

#[cfg(test)]
mod serdes_test {
    use super::*;
    use rand_core::SeedableRng;
    #[test]
    fn test_g1_serialization_rand() {
        let mut rng = rand_xorshift::XorShiftRng::from_seed([
            0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
            0xbc, 0xe5,
        ]);

        // G1::zero, compressed
        let g1_zero = G1::zero();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_zero.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 48, "length of blob is incorrect");
        let g1_zero_recover = G1::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g1_zero, g1_zero_recover);

        // G1::one, compressed
        let g1_one = G1::one();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_one.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 48, "length of blob is incorrect");
        let g1_one_recover = G1::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g1_one, g1_one_recover);

        // G1::rand, compressed
        let g1_rand = G1::random(&mut rng);
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_rand.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 48, "length of blob is incorrect");
        let g1_rand_recover = G1::deserialize(&mut buf[..].as_ref(), true).unwrap();

        assert_eq!(g1_rand, g1_rand_recover);

        // G1::zero, uncompressed
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_zero.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g1_zero_recover = G1::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g1_zero, g1_zero_recover);

        // G1::one, uncompressed
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_one.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g1_one_recover = G1::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g1_one, g1_one_recover);

        // G1::rand, uncompressed
        let g1_rand = G1::random(&mut rng);
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_rand.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g1_rand_recover = G1::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g1_rand, g1_rand_recover);
    }

    #[test]
    fn test_g2_serialization_rand() {
        let mut rng = rand_xorshift::XorShiftRng::from_seed([
            0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
            0xbc, 0xe5,
        ]);
        // G2::zero, compressed
        let g2_zero = G2::zero();
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_zero.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g2_zero_recover = G2::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g2_zero, g2_zero_recover);

        // G2::one, compressed
        let g2_one = G2::one();
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_one.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g2_one_recover = G2::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g2_one, g2_one_recover);

        // G2::rand, compressed
        let g2_rand = G2::random(&mut rng);
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_rand.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g2_rand_recover = G2::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g2_rand, g2_rand_recover);

        // G2::zero, uncompressed
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_zero.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 192, "length of blob is incorrect");
        let g2_zero_recover = G2::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g2_zero, g2_zero_recover);

        // G2::one, uncompressed
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_one.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 192, "length of blob is incorrect");
        let g2_one_recover = G2::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g2_one, g2_one_recover);

        // G2::rand uncompressed
        let g2_rand = G2::random(&mut rng);
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_rand.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 192, "length of blob is incorrect");
        let g2_rand_recover = G2::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g2_rand, g2_rand_recover);
    }

    #[test]
    fn test_g1affine_serialization_rand() {
        let mut rng = rand_xorshift::XorShiftRng::from_seed([
            0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
            0xbc, 0xe5,
        ]);

        // G1::zero, compressed
        let g1_zero = G1::zero().into_affine();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_zero.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 48, "length of blob is incorrect");
        let g1_zero_recover = G1Affine::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g1_zero, g1_zero_recover);

        // G1::one, compressed
        let g1_one = G1::one().into_affine();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_one.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 48, "length of blob is incorrect");
        let g1_one_recover = G1Affine::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g1_one, g1_one_recover);

        // G1::rand, compressed
        let g1_rand = G1::random(&mut rng).into_affine();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_rand.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 48, "length of blob is incorrect");
        let g1_rand_recover = G1Affine::deserialize(&mut buf[..].as_ref(), true).unwrap();

        assert_eq!(g1_rand, g1_rand_recover);

        // G1::zero, uncompressed
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_zero.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g1_zero_recover = G1Affine::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g1_zero, g1_zero_recover);

        // G1::one, uncompressed
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_one.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g1_one_recover = G1Affine::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g1_one, g1_one_recover);

        // G1::rand, uncompressed
        let g1_rand = G1::random(&mut rng).into_affine();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(g1_rand.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g1_rand_recover = G1Affine::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g1_rand, g1_rand_recover);
    }

    #[test]
    fn test_g2affine_serialization_rand() {
        let mut rng = rand_xorshift::XorShiftRng::from_seed([
            0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
            0xbc, 0xe5,
        ]);
        // G2::zero, compressed
        let g2_zero = G2::zero().into_affine();
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_zero.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g2_zero_recover = G2Affine::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g2_zero, g2_zero_recover);

        // G2::one, compressed
        let g2_one = G2::one().into_affine();
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_one.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g2_one_recover = G2Affine::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g2_one, g2_one_recover);

        // G2::rand, compressed
        let g2_rand = G2::random(&mut rng).into_affine();
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_rand.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 96, "length of blob is incorrect");
        let g2_rand_recover = G2Affine::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(g2_rand, g2_rand_recover);

        // G2::zero, uncompressed
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_zero.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 192, "length of blob is incorrect");
        let g2_zero_recover = G2Affine::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g2_zero, g2_zero_recover);

        // G2::one, uncompressed
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_one.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 192, "length of blob is incorrect");
        let g2_one_recover = G2Affine::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g2_one, g2_one_recover);

        // G2::rand uncompressed
        let g2_rand = G2::random(&mut rng).into_affine();
        let mut buf: Vec<u8> = vec![];
        // serialize a G2 element into buffer
        assert!(g2_rand.serialize(&mut buf, false).is_ok());
        assert_eq!(buf.len(), 192, "length of blob is incorrect");
        let g2_rand_recover = G2Affine::deserialize(&mut buf[..].as_ref(), false).unwrap();
        assert_eq!(g2_rand, g2_rand_recover);
    }

    #[test]
    fn test_fr_serialization_rand() {
        use ff::Field;
        let mut rng = rand_xorshift::XorShiftRng::from_seed([
            0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
            0xbc, 0xe5,
        ]);
        // fr::zero
        let fr_zero = Fr::zero();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(fr_zero.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 32, "length of blob is incorrect");
        let fr_zero_recover = Fr::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(fr_zero, fr_zero_recover);

        // fr::one
        let fr_one = Fr::one();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(fr_one.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 32, "length of blob is incorrect");
        let fr_one_recover = Fr::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(fr_one, fr_one_recover);

        // fr::rand
        let fr_rand = Fr::random(&mut rng);
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(fr_rand.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 32, "length of blob is incorrect");
        let fr_rand_recover = Fr::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(fr_rand, fr_rand_recover);
    }

    #[test]
    fn test_fq12_serialization_rand() {
        use ff::Field;
        let mut rng = rand_xorshift::XorShiftRng::from_seed([
            0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
            0xbc, 0xe5,
        ]);
        // fq12::zero
        let fq12_zero = Fq12::zero();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(fq12_zero.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 48 * 12, "length of blob is incorrect");
        let fq12_zero_recover = Fq12::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(fq12_zero, fq12_zero_recover);

        // fq12::one
        let fq12_one = Fq12::one();
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(fq12_one.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 48 * 12, "length of blob is incorrect");
        let fq12_one_recover = Fq12::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(fq12_one, fq12_one_recover);

        // fr::rand
        let fq12_rand = Fq12::random(&mut rng);
        let mut buf: Vec<u8> = vec![];
        // serialize a G1 element into buffer
        assert!(fq12_rand.serialize(&mut buf, true).is_ok());
        assert_eq!(buf.len(), 48 * 12, "length of blob is incorrect");
        let fq12_rand_recover = Fq12::deserialize(&mut buf[..].as_ref(), true).unwrap();
        assert_eq!(fq12_rand, fq12_rand_recover);
    }
}