You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.5 KiB

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