Skip to content

Commit d8bbdb5

Browse files
add docs on the algorithms used and fix typo
1 parent a65f366 commit d8bbdb5

File tree

1 file changed

+26
-2
lines changed
  • crates/starknet-types-core/src/qm31

1 file changed

+26
-2
lines changed

crates/starknet-types-core/src/qm31/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl fmt::Display for QM31Error {
3333
}
3434
}
3535

36-
/// Definition of a Quadruple Merseene 31.
36+
/// Definition of a Quadruple Mersenne 31.
3737
///
3838
/// The internal representation is composed of 4 limbs, following a big-endian ordering.
3939
/// Each of this limbs can be represented by 36 bits.
@@ -50,7 +50,7 @@ impl QM31 {
5050
}
5151

5252
/// Creates a [QM31] in its reduced form from four u64 coordinates.
53-
///
53+
///
5454
/// A coordinate refers to a value in the M31 field.
5555
pub fn from_coordinates(coordinates: [u64; 4]) -> QM31 {
5656
Self([
@@ -106,6 +106,11 @@ impl QM31 {
106106
}
107107

108108
/// Computes the addition of two [QM31] elements in reduced form.
109+
///
110+
/// In reduced form, a QM31 is composed of 4 limbs, each represented a value from the Mersenne 31 field.
111+
///
112+
/// The algorithm was taken from the following papper: https://github.com/ingonyama-zk/papers/blob/main/Mersenne31_polynomial_arithmetic.pdf
113+
/// Section 1.1.2.
109114
pub fn add(&self, rhs: &QM31) -> QM31 {
110115
let coordinates1 = self.inner();
111116
let coordinates2 = rhs.inner();
@@ -119,6 +124,11 @@ impl QM31 {
119124
}
120125

121126
/// Computes the negative of a [QM31] element in reduced form.
127+
///
128+
/// In reduced form, a QM31 is composed of 4 limbs, each represented a value from the Mersenne 31 field.
129+
///
130+
/// The algorithm was taken from the following papper: https://github.com/ingonyama-zk/papers/blob/main/Mersenne31_polynomial_arithmetic.pdf
131+
/// Section 1.1.2.
122132
pub fn neg(&self) -> QM31 {
123133
let coordinates = self.inner();
124134
Self::from_coordinates([
@@ -130,6 +140,11 @@ impl QM31 {
130140
}
131141

132142
/// Computes the subtraction of two [QM31] elements in reduced form.
143+
///
144+
/// In reduced form, a QM31 is composed of 4 limbs, each represented a value from the Mersenne 31 field.
145+
///
146+
/// The algorithm was taken from the following papper: https://github.com/ingonyama-zk/papers/blob/main/Mersenne31_polynomial_arithmetic.pdf
147+
/// Section 1.1.2.
133148
pub fn sub(&self, rhs: &QM31) -> QM31 {
134149
let coordinates1 = self.inner();
135150
let coordinates2 = rhs.inner();
@@ -143,6 +158,8 @@ impl QM31 {
143158
}
144159

145160
/// Computes the multiplication of two [QM31] elements in reduced form.
161+
///
162+
/// In reduced form, a QM31 is composed of 4 limbs, each represented a value from the Mersenne 31 field.
146163
pub fn mul(&self, rhs: &QM31) -> QM31 {
147164
let coordinates1_u64 = self.inner();
148165
let coordinates2_u64 = rhs.inner();
@@ -190,6 +207,11 @@ impl QM31 {
190207
}
191208

192209
/// Computes `v^(2^n) modulo STWO_PRIME`.
210+
///
211+
/// In reduced form, a QM31 is composed of 4 limbs, each represented a value from the Mersenne 31 field.
212+
///
213+
/// The algorithm was taken from the following papper: https://github.com/ingonyama-zk/papers/blob/main/Mersenne31_polynomial_arithmetic.pdf
214+
/// Section 1.1.3.
193215
fn sqn(v: u64, n: usize) -> u64 {
194216
let mut u = v;
195217
for _ in 0..n {
@@ -200,6 +222,8 @@ impl QM31 {
200222

201223
/// Computes the inverse of a [QM31] element in reduced form.
202224
///
225+
/// In reduced form, a QM31 is composed of 4 limbs, each represented a value from the Mersenne 31 field.
226+
///
203227
/// Returns an error if the operand is equal to zero.
204228
pub fn inverse(&self) -> Result<QM31, QM31Error> {
205229
if *self == Self::ZERO {

0 commit comments

Comments
 (0)