Skip to content

Commit b2f98ac

Browse files
add some documentation
1 parent 72ea00c commit b2f98ac

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/utils/montgomery.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
//! # Montgomery implementation for Felt252.
2-
//!
2+
//!
33
//! This module holds utility functions for performing arithmetic operations
44
//! inside the Montgomery space.
5-
//!
6-
//! Representing felts in the Montgomery space allows for optimizations when
7-
//! performing multiplication and division operations. This is because it
8-
//! avoids having to perform modulo operations and even divisions. Montgomery
9-
//! reduces these operations to shifts and simple arithmetic operation such as
5+
//!
6+
//! Representing felts in the Montgomery space allows for optimizations when
7+
//! performing multiplication and division operations. This is because it
8+
//! avoids having to perform modulo operations and even divisions. Montgomery
9+
//! reduces these operations to shifts and simple arithmetic operation such as
1010
//! additions and subtractions.
11-
//!
12-
//! The way this works is by representing a values as x' = x * r mod n. This
11+
//!
12+
//! The way this works is by representing a values as x' = x * r mod n. This
1313
//! introduces a new constant `r` which, for performance reasons, it is defined
1414
//! as r = 2^{k} where k should be big enough to satisfy r > n.
15-
//!
15+
//!
1616
//! For more information on check: https://en.wikipedia.org/wiki/Montgomery_modular_multiplication.
1717
1818
use std::sync::LazyLock;
@@ -31,14 +31,15 @@ use starknet_types_core::felt::Felt;
3131

3232
// R parameter for felts. R = 2^{256} which is the smallets power of 2 greater than prime.
3333
pub static MONTY_R: LazyLock<BigUint> = LazyLock::new(|| BigUint::from(1u64) << 256);
34-
// R2 parameter for felts. R2 = 2^{256 * 2} mod prime.
34+
// R2 parameter for felts. R2 = 2^{256 * 2} mod prime. This value is a U256 instead of a
35+
// BigUint to integrate with lambdaworks with ease.
3536
pub static MONTY_R2: LazyLock<U256> = LazyLock::new(|| {
3637
UnsignedInteger::from_hex_unchecked(
3738
"7FFD4AB5E008810FFFFFFFFFF6F800000000001330FFFFFFFFFFD737E000401",
3839
)
3940
});
4041
// MU parameter for felts. MU = -prime^{-1} mod 2^{64}. The variant is used to
41-
// allow a better integration with lambdaworks.
42+
// allow a better integration with lambdaworks.
4243
// Check: https://github.com/lambdaclass/lambdaworks/blob/main/crates/math/src/field/fields/montgomery_backed_prime_fields.rs#L60
4344
pub const MONTY_MU_U64: u64 = 18446744073709551615;
4445
// MU parameter for felts. MU = prime^{-1} mod R.

0 commit comments

Comments
 (0)