Skip to content

Commit

Permalink
refactor(mont): test and benchmarks to use random BigInt values
Browse files Browse the repository at this point in the history
  • Loading branch information
moven0831 committed Jan 3, 2025
1 parent a6df775 commit a29e9c6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mopro-msm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ objc = { version = "=0.2.7" }
proptest = { version = "1.4.0" }
rayon = "1.5.1"
itertools = "0.13.0"
rand = "0.8.5"

[build-dependencies]
color-eyre = "0.6"
Expand Down
23 changes: 6 additions & 17 deletions mopro-msm/src/msm/metal_msm/tests/mont_backend/mont_benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::msm::metal_msm::utils::mont_params::{
use ark_bn254::Fr as ScalarField;
use ark_ff::{BigInt, PrimeField};
use metal::*;
use num_bigint::BigUint;
use num_bigint::{BigUint, RandBigInt};
use rand::thread_rng;
use stopwatch::Stopwatch;

#[test]
Expand Down Expand Up @@ -53,26 +54,14 @@ fn expensive_computation(
}

pub fn benchmark(log_limb_size: u32, shader_file: &str) -> Result<i64, String> {
let p = BigUint::parse_bytes(
b"30644E72E131A029B85045B68181585D2833E84879B9709143E1F593F0000001",
16,
)
.unwrap();
assert!(p == ScalarField::MODULUS.try_into().unwrap());
let p: BigUint = ScalarField::MODULUS.try_into().unwrap();

let p_bitwidth = calc_bitwidth(&p);
let num_limbs = calc_num_limbs(log_limb_size, p_bitwidth);

let a = BigUint::parse_bytes(
b"10ab655e9a2ca55660b44d1e5c37b00159aa76fed00000010a11800000000001",
16,
)
.unwrap();
let b = BigUint::parse_bytes(
b"11ab655e9a2ca55660b44d1e5c37b00159aa76fed00000010a11800000000001",
16,
)
.unwrap();
let mut rng = thread_rng();
let a = rng.gen_biguint_below(&p);
let b = rng.gen_biguint_below(&p);

let nsafe = calc_nsafe(log_limb_size);
if nsafe == 0 {
Expand Down
16 changes: 5 additions & 11 deletions mopro-msm/src/msm/metal_msm/tests/mont_backend/mont_mul_cios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::msm::metal_msm::utils::mont_params::{calc_mont_radix, calc_nsafe, cal
use ark_bn254::Fr as ScalarField;
use ark_ff::{BigInt, PrimeField};
use metal::*;
use num_bigint::BigUint;
use num_bigint::{BigUint, RandBigInt};
use rand::thread_rng;

#[test]
#[serial_test::serial]
Expand All @@ -34,16 +35,9 @@ pub fn do_test(log_limb_size: u32) {
let res = calc_rinv_and_n0(&p, &r, log_limb_size);
let n0 = res.1;

let a = BigUint::parse_bytes(
b"10ab655e9a2ca55660b44d1e5c37b00159aa76fed00000010a11800000000001",
16,
)
.unwrap();
let b = BigUint::parse_bytes(
b"11ab655e9a2ca55660b44d1e5c37b00159aa76fed00000010a11800000000001",
16,
)
.unwrap();
let mut rng = thread_rng();
let a = rng.gen_biguint_below(&p);
let b = rng.gen_biguint_below(&p);

let a_r = &a * &r % &p;
let b_r = &b * &r % &p;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::msm::metal_msm::utils::mont_params::{calc_mont_radix, calc_nsafe, cal
use ark_bn254::Fr as ScalarField;
use ark_ff::{BigInt, PrimeField};
use metal::*;
use num_bigint::BigUint;
use num_bigint::{BigUint, RandBigInt};
use rand::thread_rng;

#[test]
#[serial_test::serial]
Expand All @@ -34,16 +35,9 @@ pub fn do_test(log_limb_size: u32) {
let res = calc_rinv_and_n0(&p, &r, log_limb_size);
let n0 = res.1;

let a = BigUint::parse_bytes(
b"10ab655e9a2ca55660b44d1e5c37b00159aa76fed00000010a11800000000001",
16,
)
.unwrap();
let b = BigUint::parse_bytes(
b"11ab655e9a2ca55660b44d1e5c37b00159aa76fed00000010a11800000000001",
16,
)
.unwrap();
let mut rng = thread_rng();
let a = rng.gen_biguint_below(&p);
let b = rng.gen_biguint_below(&p);

let a_r = &a * &r % &p;
let b_r = &b * &r % &p;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use crate::msm::metal_msm::utils::mont_params::{calc_mont_radix, calc_rinv_and_n
use ark_bn254::Fr as ScalarField;
use ark_ff::{BigInt, PrimeField};
use metal::*;
use num_bigint::BigUint;
use num_bigint::{BigUint, RandBigInt};
use rand::thread_rng;

#[test]
#[serial_test::serial]
Expand All @@ -37,16 +38,9 @@ pub fn do_test(log_limb_size: u32) {
let res = calc_rinv_and_n0(&p, &r, log_limb_size);
let n0 = res.1;

let a = BigUint::parse_bytes(
b"10ab655e9a2ca55660b44d1e5c37b00159aa76fed00000010a11800000000001",
16,
)
.unwrap();
let b = BigUint::parse_bytes(
b"11ab655e9a2ca55660b44d1e5c37b00159aa76fed00000010a11800000000001",
16,
)
.unwrap();
let mut rng = thread_rng();
let a = rng.gen_biguint_below(&p);
let b = rng.gen_biguint_below(&p);

let a_r = &a * &r % &p;
let b_r = &b * &r % &p;
Expand Down

0 comments on commit a29e9c6

Please sign in to comment.