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