testudo/src/group.rs
src/group.rsRaw
1use crate::errors::ProofVerifyError;
2use ark_ec::msm::VariableBaseMSM;
3use ark_ff::PrimeField;
4
5
6use lazy_static::lazy_static;
7use num_bigint::BigInt;
8
9use super::scalar::Scalar;
10use ark_ec::{AffineCurve, ProjectiveCurve};
11use ark_serialize::*;
12use core::borrow::Borrow;
13
14pub type GroupElement = ark_bls12_377::G1Projective;
15pub type GroupElementAffine = ark_bls12_377::G1Affine;
16pub type Fq = ark_bls12_377::Fq;
17pub type Fr = ark_bls12_377::Fr;
18
19#[derive(Clone, Eq, PartialEq, Hash, Debug, CanonicalSerialize, CanonicalDeserialize)]
20pub struct CompressedGroup(pub Vec<u8>);
21
22lazy_static! {
23 pub static ref GROUP_BASEPOINT: GroupElement = GroupElement::prime_subgroup_generator();
24}
25
26pub trait CompressGroupElement {
27 fn compress(&self) -> CompressedGroup;
28}
29
30pub trait DecompressGroupElement {
31 fn decompress(encoded: &CompressedGroup) -> Option<GroupElement>;
32}
33
34pub trait UnpackGroupElement {
35 fn unpack(&self) -> Result<GroupElement, ProofVerifyError>;
36}
37
38impl CompressGroupElement for GroupElement {
39 fn compress(&self) -> CompressedGroup {
40 let mut point_encoding = Vec::new();
41 self.serialize(&mut point_encoding).unwrap();
42 // println!("in compress {:?}", point_encoding);;
43 CompressedGroup(point_encoding)
44 }
45}
46
47impl DecompressGroupElement for GroupElement {
48 fn decompress(encoded: &CompressedGroup) -> Option<Self> {
49 let res = GroupElement::deserialize(&*encoded.0);
50 if res.is_err() {
51 println!("{:?}", res);
52 None
53 } else {
54 Some(res.unwrap())
55 }
56 }
57}
58
59impl UnpackGroupElement for CompressedGroup {
60 fn unpack(&self) -> Result<GroupElement, ProofVerifyError> {
61 let encoded = self.0.clone();
62 GroupElement::decompress(self).ok_or_else(|| ProofVerifyError::DecompressionError(encoded))
63 }
64}
65
66pub trait VartimeMultiscalarMul {
67 fn vartime_multiscalar_mul(scalars: &[Scalar], points: &[GroupElement]) -> GroupElement;
68}
69
70impl VartimeMultiscalarMul for GroupElement {
71 fn vartime_multiscalar_mul(scalars: &[Scalar], points: &[GroupElement]) -> GroupElement {
72 let repr_scalars = scalars
73 .into_iter()
74 .map(|S| S.borrow().into_repr())
75 .collect::<Vec<<Scalar as PrimeField>::BigInt>>();
76 let aff_points = points
77 .into_iter()
78 .map(|P| P.borrow().into_affine())
79 .collect::<Vec<GroupElementAffine>>();
80 VariableBaseMSM::multi_scalar_mul(aff_points.as_slice(), repr_scalars.as_slice())
81 }
82}