diff --git a/stwo_cairo_verifier/src/utils.cairo b/stwo_cairo_verifier/src/utils.cairo index 7894cf83..03754571 100644 --- a/stwo_cairo_verifier/src/utils.cairo +++ b/stwo_cairo_verifier/src/utils.cairo @@ -6,6 +6,7 @@ use core::dict::Felt252DictEntryTrait; use core::dict::Felt252DictTrait; use core::iter::Iterator; use core::num::traits::BitSize; +use stwo_cairo_verifier::fields::qm31::{QM31, qm31}; use stwo_cairo_verifier::BaseField; use core::traits::DivRem; @@ -126,9 +127,35 @@ pub fn find(n: u32, a: Span) -> bool { res } +pub fn pow_qm31(base: QM31, mut exponent: u32) -> QM31 { + let mut result = qm31(1, 0, 0, 0); + let mut base_power = base; + loop { + if exponent & 1 == 1 { + result = result * base_power; + } + exponent = exponent / 2; + if exponent == 0 { + break; + } + base_power = base_power * base_power; + }; + result +} + +pub fn qm31_zero_array(n: u32) -> Array { + let mut result = array![]; + let mut i = 0; + while i < n { + result.append(qm31(0, 0, 0, 0)); + i += 1; + }; + result +} + #[cfg(test)] mod tests { - use super::{pow, bit_reverse_index}; + use super::{pow, pow_qm31, qm31, bit_reverse_index}; #[test] fn test_pow() { @@ -163,5 +190,27 @@ mod tests { // 31 bits assert_eq!(16448250, bit_reverse_index(800042880, 31)); } + + + #[test] + fn test_pow_qm31_1() { + let result = pow_qm31(qm31(1, 2, 3, 4), 0); + let expected_result = qm31(1, 0, 0, 0); + assert_eq!(expected_result, result) + } + + #[test] + fn test_pow_qm31_2() { + let result = pow_qm31(qm31(1, 2, 3, 4), 1); + let expected_result = qm31(1, 2, 3, 4); + assert_eq!(expected_result, result) + } + + #[test] + fn test_pow_qm31_3() { + let result = pow_qm31(qm31(1, 2, 3, 4), 37); + let expected_result = qm31(1394542587, 260510989, 997191897, 2127074080); + assert_eq!(expected_result, result) + } }