-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Basic test for MSM in Noir to catch performance improvements a…
…nd regressions (#7341) Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
- Loading branch information
1 parent
f0ce5c5
commit 668a476
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
test_programs/execution_success/multi_scalar_mul/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
test_programs/execution_success/multi_scalar_mul/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|