testudo/src/group.rs
src/group.rsRaw
1use crate::errors::ProofVerifyError;
2use ark_ec::msm::VariableBaseMSM;
3use ark_ff::PrimeField;
4use digest::DynDigest;
5use lazy_static::lazy_static;
6use num_bigint::BigInt;
7
8use lazy_static::lazy_static;
9use num_bigint::BigInt;
10
11use super::scalar::Scalar;
12use ark_ec::{AffineCurve, ProjectiveCurve};
13use ark_ec::{AffineCurve, ProjectiveCurve};
14use ark_serialize::*;
15use ark_serialize::*;
16use core::borrow::Borrow;
17use core::ops::{Mul, MulAssign};
18
19pub type GroupElement = ark_bls12_377::G1Projective;
20pub type GroupElementAffine = ark_bls12_377::G1Affine;
21pub type CurveField = ark_bls12_377::Fq;
22
23#[derive(Clone, Eq, PartialEq, Hash, Debug, CanonicalSerialize, CanonicalDeserialize)]
24pub struct CompressedGroup(pub Vec<u8>);
25
26lazy_static! {
27 pub static ref GROUP_BASEPOINT: GroupElement = GroupElement::prime_subgroup_generator();
28}
29
30pub trait CompressGroupElement {
31 fn compress(&self) -> CompressedGroup;
32}
33
34pub trait DecompressGroupElement {
35 fn decompress(encoded: &CompressedGroup) -> Option<GroupElement>;
36}
37
38pub trait UnpackGroupElement {
39 fn unpack(&self) -> Result<GroupElement, ProofVerifyError>;
40}
41
42impl CompressGroupElement for GroupElement {
43 fn compress(&self) -> CompressedGroup {
44 let mut point_encoding = Vec::new();
45 self.serialize(&mut point_encoding).unwrap();
46 // println!("in compress {:?}", point_encoding);;
47 CompressedGroup(point_encoding)
48 }
49}
50
51impl DecompressGroupElement for GroupElement {
52 fn decompress(encoded: &CompressedGroup) -> Option<Self> {
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(scalars: &[Scalar], points: &[GroupElement]) -> GroupElement {
76 let repr_scalars = scalars
77 .into_iter()
78 .map(|S| S.borrow().into_repr())
79 .collect::<Vec<<Scalar as PrimeField>::BigInt>>();
80 let aff_points = points
81 .into_iter()
82 .map(|P| P.borrow().into_affine())
83 .collect::<Vec<GroupElementAffine>>();
84 VariableBaseMSM::multi_scalar_mul(aff_points.as_slice(), repr_scalars.as_slice())
85 }
86}