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.

35 lines
1.1 KiB

4 years ago
  1. use ark_ec::{msm::VariableBaseMSM, AffineCurve, ProjectiveCurve};
  2. use ark_ff::{PrimeField, UniformRand, Zero};
  3. use rand::SeedableRng;
  4. use rand_xorshift::XorShiftRng;
  5. fn naive_var_base_msm<G: AffineCurve>(
  6. bases: &[G],
  7. scalars: &[<G::ScalarField as PrimeField>::BigInt],
  8. ) -> G::Projective {
  9. let mut acc = G::Projective::zero();
  10. for (base, scalar) in bases.iter().zip(scalars.iter()) {
  11. acc += &base.mul(*scalar);
  12. }
  13. acc
  14. }
  15. pub fn test_var_base_msm<G: AffineCurve>() {
  16. const SAMPLES: usize = 1 << 10;
  17. let mut rng = XorShiftRng::seed_from_u64(234872845u64);
  18. let v = (0..SAMPLES - 1)
  19. .map(|_| G::ScalarField::rand(&mut rng).into_repr())
  20. .collect::<Vec<_>>();
  21. let g = (0..SAMPLES)
  22. .map(|_| G::Projective::rand(&mut rng))
  23. .collect::<Vec<_>>();
  24. let g = <G::Projective as ProjectiveCurve>::batch_normalization_into_affine(&g);
  25. let naive = naive_var_base_msm(g.as_slice(), v.as_slice());
  26. let fast = VariableBaseMSM::multi_scalar_mul(g.as_slice(), v.as_slice());
  27. assert_eq!(naive.into_affine(), fast.into_affine());
  28. }