Skip to content

Commit 0c8740c

Browse files
authored
Improve MSM documentation & add vector size check (#75)
* chore: improve Multi-Scalar Multiplication module documentation and add vector size validation Enhanced the documentation for the MSM module to improve clarity and usability. Added a check to ensure scalar and point vectors have matching lengths, preventing potential runtime errors. * chore: rename module multi_scalar_mul to msm Renamed the module from multi_scalar_mul to msm for brevity and improved readability.
1 parent e4ed5fc commit 0c8740c

File tree

4 files changed

+61
-35
lines changed

4 files changed

+61
-35
lines changed

src/linear_relation/canonical.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use subtle::{Choice, ConstantTimeEq};
88

99
use super::{GroupMap, GroupVar, LinearCombination, LinearRelation, ScalarTerm, ScalarVar};
1010
use crate::errors::{Error, InvalidInstance};
11-
use crate::linear_relation::multi_scalar_mul::VariableMultiScalarMul;
11+
use crate::linear_relation::msm::VariableMultiScalarMul;
1212

1313
/// A normalized form of the [`LinearRelation`], which is used for serialization into the transcript.
1414
///

src/linear_relation/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ mod canonical;
2828
pub use canonical::CanonicalLinearRelation;
2929

3030
/// Implementation of multi-scalar multiplication (MSM) over scalars and points.
31-
mod multi_scalar_mul;
32-
pub use multi_scalar_mul::VariableMultiScalarMul;
31+
mod msm;
32+
pub use msm::VariableMultiScalarMul;
3333

3434
/// A wrapper representing an index for a scalar variable.
3535
///

src/linear_relation/msm.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use group::prime::PrimeGroup;
2+
3+
/// Trait for performing Multi-Scalar Multiplication (MSM).
4+
///
5+
/// MSM computes the sum:
6+
/// ```text
7+
/// result = Σ (scalar[i] * point[i])
8+
/// ```
9+
/// Implementations can override this with optimized algorithms for specific groups,
10+
/// while a default naive implementation is provided for all [`PrimeGroup`] types.
11+
pub trait VariableMultiScalarMul {
12+
/// The scalar field type associated with the group.
13+
type Scalar;
14+
/// The group element (point) type.
15+
type Point;
16+
17+
/// Computes the multi-scalar multiplication (MSM) over the provided scalars and points.
18+
///
19+
/// # Parameters
20+
/// - `scalars`: Slice of scalar multipliers.
21+
/// - `bases`: Slice of group elements to be multiplied by the scalars.
22+
///
23+
/// # Returns
24+
/// The resulting group element from the MSM computation.
25+
///
26+
/// # Panics
27+
/// Panics if `scalars.len() != bases.len()`.
28+
fn msm(scalars: &[Self::Scalar], bases: &[Self::Point]) -> Self;
29+
}
30+
31+
impl<G: PrimeGroup> VariableMultiScalarMul for G {
32+
type Scalar = G::Scalar;
33+
type Point = G;
34+
35+
/// Default naive MSM implementation for any [`PrimeGroup`].
36+
///
37+
/// This method performs a straightforward sum of scalar multiplications:
38+
/// ```text
39+
/// Σ (scalar[i] * point[i])
40+
/// ```
41+
/// Complexity: **O(n)** group multiplications and additions.
42+
///
43+
/// # Panics
44+
/// Panics if `scalars.len() != bases.len()`.
45+
fn msm(scalars: &[Self::Scalar], bases: &[Self::Point]) -> Self {
46+
assert_eq!(
47+
scalars.len(),
48+
bases.len(),
49+
"scalars and bases must have the same length"
50+
);
51+
52+
let mut acc = Self::identity();
53+
for (s, p) in scalars.iter().zip(bases.iter()) {
54+
acc += *p * s;
55+
}
56+
acc
57+
}
58+
}

src/linear_relation/multi_scalar_mul.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)