Skip to content

Commit

Permalink
Add unit tests to modulo builtin operations
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianGCalderon committed Sep 20, 2024
1 parent 7346f80 commit 399c14c
Showing 1 changed file with 89 additions and 3 deletions.
92 changes: 89 additions & 3 deletions vm/src/vm/runners/builtin_runner/modulo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ impl ModBuiltinRunner {
// Calculates the result of `lhs OP rhs`
//
// The builtin type (add or mul) determines the OP
fn apply_operation(
pub(crate) fn apply_operation(
&self,
lhs: &BigUint,
rhs: &BigUint,
Expand All @@ -674,7 +674,7 @@ impl ModBuiltinRunner {
// Given `known OP unknown = result (mod p)`, it deduces `unknown`
//
// The builtin type (add or mul) determines the OP
fn deduce_operand(
pub(crate) fn deduce_operand(
&self,
known: &BigUint,
result: &BigUint,
Expand All @@ -696,10 +696,96 @@ impl ModBuiltinRunner {

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

#[test]
fn apply_operation_add() {
let builtin = ModBuiltinRunner::new_add_mod(&ModInstanceDef::new(Some(8), 8, 8), true);

assert_eq!(
builtin
.apply_operation(
&BigUint::from(2u32),
&BigUint::from(3u32),
&BigUint::from(7u32)
)
.unwrap(),
BigUint::from(5u32)
);

assert_eq!(
builtin
.apply_operation(
&BigUint::from(5u32),
&BigUint::from(5u32),
&BigUint::from(5u32)
)
.unwrap(),
BigUint::from(5u32)
);
}

#[test]
fn apply_operation_mul() {
let builtin = ModBuiltinRunner::new_mul_mod(&ModInstanceDef::new(Some(8), 8, 8), true);

assert_eq!(
builtin
.apply_operation(
&BigUint::from(2u32),
&BigUint::from(3u32),
&BigUint::from(7u32)
)
.unwrap(),
BigUint::from(6u32)
);
}

#[test]
fn deduce_operand_add() {
let builtin = ModBuiltinRunner::new_add_mod(&ModInstanceDef::new(Some(8), 8, 8), true);

assert_eq!(
builtin
.deduce_operand(
&BigUint::from(2u32),
&BigUint::from(5u32),
&BigUint::from(7u32)
)
.unwrap(),
BigUint::from(3u32)
);
assert_eq!(
builtin
.deduce_operand(
&BigUint::from(5u32),
&BigUint::from(2u32),
&BigUint::from(7u32)
)
.unwrap(),
BigUint::from(4u32)
);
}

#[test]
fn deduce_operand_mul() {
let builtin = ModBuiltinRunner::new_mul_mod(&ModInstanceDef::new(Some(8), 8, 8), true);

assert_eq!(
builtin
.deduce_operand(
&BigUint::from(2u32),
&BigUint::from(1u32),
&BigUint::from(7u32)
)
.unwrap(),
BigUint::from(4u32)
);
}

#[test]
#[cfg(feature = "mod_builtin")]
fn test_air_private_input_all_cairo() {
use super::*;
use crate::{
air_private_input::{ModInput, ModInputInstance, ModInputMemoryVars, PrivateInput},
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,
Expand Down

0 comments on commit 399c14c

Please sign in to comment.