/ algorithms / src / msm / tests.rs
tests.rs
 1  // Copyright (c) 2025-2026 ACDC Network
 2  // This file is part of the alphavm library.
 3  //
 4  // Alpha Chain | Delta Chain Protocol
 5  // International Monetary Graphite.
 6  //
 7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
 8  // They built world-class ZK infrastructure. We installed the EASY button.
 9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
11  //
12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
14  // No rights reserved. No permission required. No warranty. No refunds.
15  //
16  // https://creativecommons.org/publicdomain/zero/1.0/
17  // SPDX-License-Identifier: CC0-1.0
18  
19  use crate::msm::*;
20  use alphavm_curves::{
21      bls12_377::{Fr, G1Projective},
22      traits::{AffineCurve, ProjectiveCurve},
23  };
24  use alphavm_fields::{PrimeField, Zero};
25  use alphavm_utilities::{
26      rand::{TestRng, Uniform},
27      BitIteratorBE,
28  };
29  
30  fn naive_variable_base_msm<G: AffineCurve>(
31      bases: &[G],
32      scalars: &[<G::ScalarField as PrimeField>::BigInteger],
33  ) -> G::Projective {
34      let mut acc = G::Projective::zero();
35  
36      for (base, scalar) in bases.iter().zip(scalars.iter()) {
37          acc += base.mul_bits(BitIteratorBE::new(*scalar));
38      }
39      acc
40  }
41  
42  #[test]
43  fn variable_base_test_with_bls12() {
44      const SAMPLES: usize = 1 << 10;
45  
46      let mut rng = TestRng::default();
47  
48      let v = (0..SAMPLES).map(|_| Fr::rand(&mut rng).to_bigint()).collect::<Vec<_>>();
49      let g = (0..SAMPLES).map(|_| G1Projective::rand(&mut rng).to_affine()).collect::<Vec<_>>();
50  
51      let naive = naive_variable_base_msm(g.as_slice(), v.as_slice());
52      let fast = VariableBase::msm(g.as_slice(), v.as_slice());
53  
54      assert_eq!(naive.to_affine(), fast.to_affine());
55  }
56  
57  #[test]
58  fn variable_base_test_with_bls12_unequal_numbers() {
59      const SAMPLES: usize = 1 << 10;
60  
61      let mut rng = TestRng::default();
62  
63      let v = (0..SAMPLES - 100).map(|_| Fr::rand(&mut rng).to_bigint()).collect::<Vec<_>>();
64      let g = (0..SAMPLES).map(|_| G1Projective::rand(&mut rng).to_affine()).collect::<Vec<_>>();
65  
66      let naive = naive_variable_base_msm(g.as_slice(), v.as_slice());
67      let fast = VariableBase::msm(g.as_slice(), v.as_slice());
68  
69      assert_eq!(naive.to_affine(), fast.to_affine());
70  }