Skip to content

Commit 399c14c

Browse files
Add unit tests to modulo builtin operations
1 parent 7346f80 commit 399c14c

File tree

1 file changed

+89
-3
lines changed

1 file changed

+89
-3
lines changed

vm/src/vm/runners/builtin_runner/modulo.rs

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl ModBuiltinRunner {
651651
// Calculates the result of `lhs OP rhs`
652652
//
653653
// The builtin type (add or mul) determines the OP
654-
fn apply_operation(
654+
pub(crate) fn apply_operation(
655655
&self,
656656
lhs: &BigUint,
657657
rhs: &BigUint,
@@ -674,7 +674,7 @@ impl ModBuiltinRunner {
674674
// Given `known OP unknown = result (mod p)`, it deduces `unknown`
675675
//
676676
// The builtin type (add or mul) determines the OP
677-
fn deduce_operand(
677+
pub(crate) fn deduce_operand(
678678
&self,
679679
known: &BigUint,
680680
result: &BigUint,
@@ -696,10 +696,96 @@ impl ModBuiltinRunner {
696696

697697
#[cfg(test)]
698698
mod tests {
699+
use super::*;
700+
701+
#[test]
702+
fn apply_operation_add() {
703+
let builtin = ModBuiltinRunner::new_add_mod(&ModInstanceDef::new(Some(8), 8, 8), true);
704+
705+
assert_eq!(
706+
builtin
707+
.apply_operation(
708+
&BigUint::from(2u32),
709+
&BigUint::from(3u32),
710+
&BigUint::from(7u32)
711+
)
712+
.unwrap(),
713+
BigUint::from(5u32)
714+
);
715+
716+
assert_eq!(
717+
builtin
718+
.apply_operation(
719+
&BigUint::from(5u32),
720+
&BigUint::from(5u32),
721+
&BigUint::from(5u32)
722+
)
723+
.unwrap(),
724+
BigUint::from(5u32)
725+
);
726+
}
727+
728+
#[test]
729+
fn apply_operation_mul() {
730+
let builtin = ModBuiltinRunner::new_mul_mod(&ModInstanceDef::new(Some(8), 8, 8), true);
731+
732+
assert_eq!(
733+
builtin
734+
.apply_operation(
735+
&BigUint::from(2u32),
736+
&BigUint::from(3u32),
737+
&BigUint::from(7u32)
738+
)
739+
.unwrap(),
740+
BigUint::from(6u32)
741+
);
742+
}
743+
744+
#[test]
745+
fn deduce_operand_add() {
746+
let builtin = ModBuiltinRunner::new_add_mod(&ModInstanceDef::new(Some(8), 8, 8), true);
747+
748+
assert_eq!(
749+
builtin
750+
.deduce_operand(
751+
&BigUint::from(2u32),
752+
&BigUint::from(5u32),
753+
&BigUint::from(7u32)
754+
)
755+
.unwrap(),
756+
BigUint::from(3u32)
757+
);
758+
assert_eq!(
759+
builtin
760+
.deduce_operand(
761+
&BigUint::from(5u32),
762+
&BigUint::from(2u32),
763+
&BigUint::from(7u32)
764+
)
765+
.unwrap(),
766+
BigUint::from(4u32)
767+
);
768+
}
769+
770+
#[test]
771+
fn deduce_operand_mul() {
772+
let builtin = ModBuiltinRunner::new_mul_mod(&ModInstanceDef::new(Some(8), 8, 8), true);
773+
774+
assert_eq!(
775+
builtin
776+
.deduce_operand(
777+
&BigUint::from(2u32),
778+
&BigUint::from(1u32),
779+
&BigUint::from(7u32)
780+
)
781+
.unwrap(),
782+
BigUint::from(4u32)
783+
);
784+
}
785+
699786
#[test]
700787
#[cfg(feature = "mod_builtin")]
701788
fn test_air_private_input_all_cairo() {
702-
use super::*;
703789
use crate::{
704790
air_private_input::{ModInput, ModInputInstance, ModInputMemoryVars, PrivateInput},
705791
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,

0 commit comments

Comments
 (0)