Skip to content

Commit da29962

Browse files
Make QM31 functions internal (#2181)
* Make QM31 functions internal Also, allow dead code in neg function since this will be removed and replaced with types-rs implementation (along with the other QM31 functions of the VM). * Update CHANGELOG * Fix typo in CHANGELOG Co-authored-by: Franco Giachetta <[email protected]> --------- Co-authored-by: Franco Giachetta <[email protected]>
1 parent c42f95a commit da29962

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* feat: Make QM31 functions internal [#2181](https://github.com/lambdaclass/cairo-vm/pull/2181)
6+
57
* feat: Add `--fill-holes` CLI flag instead of relying on `--proof-mode` [#2165](https://github.com/lambdaclass/cairo-vm/pull/2165)
68

79
* feat: Use BTreeMap in PIE additional data [#2162](https://github.com/lambdaclass/cairo-vm/pull/2162)

vm/src/math_utils/mod.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn qm31_packed_reduced_read_coordinates(felt: Felt252) -> Result<[u64; 4], MathE
110110
/// Reduces four u64 coordinates and packs them into a single Felt252.
111111
/// STWO_PRIME fits in 36 bits, hence each coordinate can be represented by 36 bits and a QM31
112112
/// element can be stored in the first 144 bits of a Felt252.
113-
pub fn qm31_coordinates_to_packed_reduced(coordinates: [u64; 4]) -> Felt252 {
113+
pub(crate) fn qm31_coordinates_to_packed_reduced(coordinates: [u64; 4]) -> Felt252 {
114114
let bytes_part1 = ((coordinates[0] % STWO_PRIME) as u128
115115
+ (((coordinates[1] % STWO_PRIME) as u128) << 36))
116116
.to_le_bytes();
@@ -127,7 +127,10 @@ pub fn qm31_coordinates_to_packed_reduced(coordinates: [u64; 4]) -> Felt252 {
127127
/// QM31 operations are to be relocated into https://github.com/lambdaclass/lambdaworks.
128128
/// Computes the addition of two QM31 elements in reduced form.
129129
/// Returns an error if either operand is not reduced.
130-
pub fn qm31_packed_reduced_add(felt1: Felt252, felt2: Felt252) -> Result<Felt252, MathError> {
130+
pub(crate) fn qm31_packed_reduced_add(
131+
felt1: Felt252,
132+
felt2: Felt252,
133+
) -> Result<Felt252, MathError> {
131134
let coordinates1 = qm31_packed_reduced_read_coordinates(felt1)?;
132135
let coordinates2 = qm31_packed_reduced_read_coordinates(felt2)?;
133136
let result_unreduced_coordinates = [
@@ -145,7 +148,8 @@ pub fn qm31_packed_reduced_add(felt1: Felt252, felt2: Felt252) -> Result<Felt252
145148
/// QM31 operations are to be relocated into https://github.com/lambdaclass/lambdaworks.
146149
/// Computes the negative of a QM31 element in reduced form.
147150
/// Returns an error if the input is not reduced.
148-
pub fn qm31_packed_reduced_neg(felt: Felt252) -> Result<Felt252, MathError> {
151+
#[allow(dead_code)]
152+
pub(crate) fn qm31_packed_reduced_neg(felt: Felt252) -> Result<Felt252, MathError> {
149153
let coordinates = qm31_packed_reduced_read_coordinates(felt)?;
150154
Ok(qm31_coordinates_to_packed_reduced([
151155
STWO_PRIME - coordinates[0],
@@ -159,7 +163,10 @@ pub fn qm31_packed_reduced_neg(felt: Felt252) -> Result<Felt252, MathError> {
159163
/// QM31 operations are to be relocated into https://github.com/lambdaclass/lambdaworks.
160164
/// Computes the subtraction of two QM31 elements in reduced form.
161165
/// Returns an error if either operand is not reduced.
162-
pub fn qm31_packed_reduced_sub(felt1: Felt252, felt2: Felt252) -> Result<Felt252, MathError> {
166+
pub(crate) fn qm31_packed_reduced_sub(
167+
felt1: Felt252,
168+
felt2: Felt252,
169+
) -> Result<Felt252, MathError> {
163170
let coordinates1 = qm31_packed_reduced_read_coordinates(felt1)?;
164171
let coordinates2 = qm31_packed_reduced_read_coordinates(felt2)?;
165172
let result_unreduced_coordinates = [
@@ -177,7 +184,10 @@ pub fn qm31_packed_reduced_sub(felt1: Felt252, felt2: Felt252) -> Result<Felt252
177184
/// QM31 operations are to be relocated into https://github.com/lambdaclass/lambdaworks.
178185
/// Computes the multiplication of two QM31 elements in reduced form.
179186
/// Returns an error if either operand is not reduced.
180-
pub fn qm31_packed_reduced_mul(felt1: Felt252, felt2: Felt252) -> Result<Felt252, MathError> {
187+
pub(crate) fn qm31_packed_reduced_mul(
188+
felt1: Felt252,
189+
felt2: Felt252,
190+
) -> Result<Felt252, MathError> {
181191
let coordinates1_u64 = qm31_packed_reduced_read_coordinates(felt1)?;
182192
let coordinates2_u64 = qm31_packed_reduced_read_coordinates(felt2)?;
183193
let coordinates1 = coordinates1_u64.map(u128::from);
@@ -214,7 +224,7 @@ pub fn qm31_packed_reduced_mul(felt1: Felt252, felt2: Felt252) -> Result<Felt252
214224
/// M31 operations are to be relocated into https://github.com/lambdaclass/lambdaworks.
215225
/// Computes the inverse in the M31 field using Fermat's little theorem, i.e., returns
216226
/// `v^(STWO_PRIME-2) modulo STWO_PRIME`, which is the inverse of v unless v % STWO_PRIME == 0.
217-
pub fn pow2147483645(v: u64) -> u64 {
227+
pub(crate) fn pow2147483645(v: u64) -> u64 {
218228
let t0 = (sqn(v, 2) * v) % STWO_PRIME;
219229
let t1 = (sqn(t0, 1) * t0) % STWO_PRIME;
220230
let t2 = (sqn(t1, 3) * t0) % STWO_PRIME;
@@ -239,7 +249,7 @@ fn sqn(v: u64, n: usize) -> u64 {
239249
/// QM31 operations are to be relocated into https://github.com/lambdaclass/lambdaworks.
240250
/// Computes the inverse of a QM31 element in reduced form.
241251
/// Returns an error if the denominator is zero or either operand is not reduced.
242-
pub fn qm31_packed_reduced_inv(felt: Felt252) -> Result<Felt252, MathError> {
252+
pub(crate) fn qm31_packed_reduced_inv(felt: Felt252) -> Result<Felt252, MathError> {
243253
if felt.is_zero() {
244254
return Err(MathError::DividedByZero);
245255
}
@@ -281,7 +291,10 @@ pub fn qm31_packed_reduced_inv(felt: Felt252) -> Result<Felt252, MathError> {
281291
/// QM31 operations are to be relocated into https://github.com/lambdaclass/lambdaworks.
282292
/// Computes the division of two QM31 elements in reduced form.
283293
/// Returns an error if the input is zero.
284-
pub fn qm31_packed_reduced_div(felt1: Felt252, felt2: Felt252) -> Result<Felt252, MathError> {
294+
pub(crate) fn qm31_packed_reduced_div(
295+
felt1: Felt252,
296+
felt2: Felt252,
297+
) -> Result<Felt252, MathError> {
285298
let felt2_inv = qm31_packed_reduced_inv(felt2)?;
286299
qm31_packed_reduced_mul(felt1, felt2_inv)
287300
}

0 commit comments

Comments
 (0)