Skip to content

Commit

Permalink
chore: Basic test for MSM in Noir to catch performance improvements a…
Browse files Browse the repository at this point in the history
…nd regressions (#7341)

Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
vezenovm and TomAFrench authored Feb 11, 2025
1 parent f0ce5c5 commit 668a476
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
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
}

0 comments on commit 668a476

Please sign in to comment.