Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Basic test for MSM in Noir to catch performance improvements and regressions #7341

Merged
merged 8 commits into from
Feb 11, 2025
6 changes: 6 additions & 0 deletions test_programs/execution_success/multi_scalar_mul/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "multi_scalar_mul"
type = "bin"
authors = [""]

[dependencies]
26 changes: 26 additions & 0 deletions test_programs/execution_success/multi_scalar_mul/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
scalars = ["0", "0", "0", "0", "0"]

[[points]]
is_infinite = "0"
x = "0x0000000000000000000000000000000000000000000000000000000000000001"
y = "0x0000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c"

[[points]]
is_infinite = "0"
x = "0x0000000000000000000000000000000000000000000000000000000000000001"
y = "0x0000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c"

[[points]]
is_infinite = "0"
x = "0x0000000000000000000000000000000000000000000000000000000000000001"
y = "0x0000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c"

[[points]]
is_infinite = "0"
x = "0x0000000000000000000000000000000000000000000000000000000000000001"
y = "0x0000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c"

[[points]]
is_infinite = "0"
x = "0x0000000000000000000000000000000000000000000000000000000000000001"
y = "0x0000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c"
45 changes: 45 additions & 0 deletions test_programs/execution_success/multi_scalar_mul/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// This test provides a basic implementation of a MSM in Noir, that allows us to check
// performance improvements and regressions.
use std::embedded_curve_ops::embedded_curve_add_unsafe;
use std::embedded_curve_ops::EmbeddedCurvePoint;

// `main` must be marked unconstrained as the function uses `break` internally
unconstrained fn main(
points: [EmbeddedCurvePoint; 5],
scalars: [Field; 5],
) -> pub EmbeddedCurvePoint {
// EmbeddedCurveScalar are two 128-bit numbers
let mut acc = EmbeddedCurvePoint::point_at_infinity();
for i in 0..1 {
// These should probably be EmbeddedCurveScalars
// let full_scalar: Field = scalars[i].hi * 2.pow_32(128) + scalars[i].lo;
let full_scalar = scalars[i];
let full_scalar_bits: [u1; 254] = full_scalar.to_be_bits();
let mut index_of_msb = 0;
// Iterates in BE
for j in 0..254 {
if full_scalar_bits[j] == 1 {
index_of_msb = j;
break;
}
}

let mut temp = points[i];
let mut res = EmbeddedCurvePoint::point_at_infinity();
// When iterative backwards we want to go to bits.len() - 2
for j in 0..(254 - index_of_msb) {
let k = 253 - j;

// Add
if full_scalar_bits[k] == 1 {
res = embedded_curve_add_unsafe(res, temp);
}
// Double
temp = embedded_curve_add_unsafe(temp, temp);
}

acc = embedded_curve_add_unsafe(acc, res);
}
acc
}

Loading