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