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