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