Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ Both branches support Stwo prover opcodes (Blake2s, QM31) since v2.0.0.
---

#### Upcoming Changes
* feat: add math Cairo test suite under `vm/src/tests/cairo_test_suite` using the `test_utils` feature flag [#2379](https://github.com/starkware-libs/cairo-vm/pull/2379)
* fix: decrease spamming logs level to trace [#2385](https://github.com/starkware-libs/cairo-vm/pull/2385)

* feat: add `test_helpers` module (`error_utils`, `test_utils`) with `assert_mr_eq`, `load_cairo_program!` macro and `expect_*` error checkers, behind `test_utils` feature flag [#2381](https://github.com/starkware-libs/cairo-vm/pull/2381)

* feat(makefile,ci): add `cairo_test_suite_programs` Makefile target and CI integration to compile Cairo test suite programs before running tests [#2380](https://github.com/starkware-libs/cairo-vm/pull/2380)

* Add Stwo cairo runner API [#2351](https://github.com/starkware-libs/cairo-vm/pull/2351)

* feat: make max traceback entries configurable [#2370](https://github.com/starkware-libs/cairo-vm/pull/2370)

* feat: create and use VirtualMachineConfig [#2369](https://github.com/starkware-libs/cairo-vm/pull/2369)
Expand Down
31 changes: 31 additions & 0 deletions vm/src/tests/cairo_test_suite/common/math_test.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
%builtins range_check

from starkware.cairo.common.math import (
assert_not_zero,
assert_not_equal,
assert_nn,
assert_le,
assert_lt,
assert_nn_le,
assert_in_range,
assert_250_bit,
split_felt,
assert_le_felt,
assert_lt_felt,
abs_value,
sign,
unsigned_div_rem,
signed_div_rem,
safe_div,
safe_mult,
split_int,
sqrt,
horner_eval,
is_quad_residue,
assert_is_power_of_2,
)


func main{range_check_ptr}() {
return ();
}
37 changes: 37 additions & 0 deletions vm/src/tests/cairo_test_suite/common/math_test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::sync::LazyLock;

use crate::{math_utils::is_quad_residue, utils::CAIRO_PRIME};
use num_bigint::BigUint;
use num_integer::Integer;

/// RC_BOUND = 2^128
pub static RC_BOUND: LazyLock<BigUint> = LazyLock::new(|| BigUint::from(2u64).pow(128));

/// MAX_DIV = CAIRO_PRIME // RC_BOUND
pub static MAX_DIV: LazyLock<BigUint> = LazyLock::new(|| CAIRO_PRIME.div_floor(&RC_BOUND));

/// Returns 1 if `a` is a quadratic residue modulo CAIRO_PRIME, 0 if not, and -1 on error.
pub fn is_quad_residue_mod_prime(a: &BigUint) -> i64 {
match is_quad_residue(a, &CAIRO_PRIME) {
Ok(true) => 1,
Ok(false) => 0,
Err(_) => -1,
}
}

#[cfg(test)]
mod tests {
use super::*;

/// Returns 1 for a known quadratic residue: 4 = 2² mod CAIRO_PRIME.
#[test]
fn test_quad_residue_mod_prime_returns_1_for_residue() {
assert_eq!(is_quad_residue_mod_prime(&BigUint::from(4u32)), 1);
}

/// Returns 0 for a known non-residue: 3 is not a square mod CAIRO_PRIME.
#[test]
fn test_quad_residue_mod_prime_returns_0_for_non_residue() {
assert_eq!(is_quad_residue_mod_prime(&BigUint::from(3u32)), 0);
}
}
2 changes: 2 additions & 0 deletions vm/src/tests/cairo_test_suite/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod math_test_utils;
mod test_math_cairo;
Loading
Loading