arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/src/uint/test_utils.rs
src/uint/test_utils.rsRaw
  1use ark_relations::r1cs::{ConstraintSystem, SynthesisError};
  2use std::ops::RangeInclusive;
  3
  4use crate::test_utils::{self, modes};
  5
  6use super::*;
  7
  8pub(crate) fn test_unary_op<T: PrimUInt, const N: usize, F: PrimeField>(
  9    a: T,
 10    mode: AllocationMode,
 11    test: impl FnOnce(UInt<N, T, F>) -> Result<(), SynthesisError>,
 12) -> Result<(), SynthesisError> {
 13    let cs = ConstraintSystem::<F>::new_ref();
 14    let a = UInt::<N, T, F>::new_variable(cs.clone(), || Ok(a), mode)?;
 15    test(a)
 16}
 17
 18pub(crate) fn test_binary_op<T: PrimUInt, const N: usize, F: PrimeField>(
 19    a: T,
 20    b: T,
 21    mode_a: AllocationMode,
 22    mode_b: AllocationMode,
 23    test: impl FnOnce(UInt<N, T, F>, UInt<N, T, F>) -> Result<(), SynthesisError>,
 24) -> Result<(), SynthesisError> {
 25    let cs = ConstraintSystem::<F>::new_ref();
 26    let a = UInt::<N, T, F>::new_variable(cs.clone(), || Ok(a), mode_a)?;
 27    let b = UInt::<N, T, F>::new_variable(cs.clone(), || Ok(b), mode_b)?;
 28    test(a, b)
 29}
 30
 31pub(crate) fn test_binary_op_with_native<T: PrimUInt, const N: usize, F: PrimeField>(
 32    a: T,
 33    b: T,
 34    mode_a: AllocationMode,
 35    test: impl FnOnce(UInt<N, T, F>, T) -> Result<(), SynthesisError>,
 36) -> Result<(), SynthesisError> {
 37    let cs = ConstraintSystem::<F>::new_ref();
 38    let a = UInt::<N, T, F>::new_variable(cs.clone(), || Ok(a), mode_a)?;
 39    test(a, b)
 40}
 41
 42pub(crate) fn run_binary_random<const ITERATIONS: usize, const N: usize, T, F>(
 43    test: impl Fn(UInt<N, T, F>, UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
 44) -> Result<(), SynthesisError>
 45where
 46    T: PrimUInt,
 47    F: PrimeField,
 48{
 49    let mut rng = ark_std::test_rng();
 50
 51    for _ in 0..ITERATIONS {
 52        for mode_a in modes() {
 53            let a = T::rand(&mut rng);
 54            for mode_b in modes() {
 55                let b = T::rand(&mut rng);
 56                test_binary_op(a, b, mode_a, mode_b, test)?;
 57            }
 58        }
 59    }
 60    Ok(())
 61}
 62
 63pub(crate) fn run_binary_exhaustive<const N: usize, T, F>(
 64    test: impl Fn(UInt<N, T, F>, UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
 65) -> Result<(), SynthesisError>
 66where
 67    T: PrimUInt,
 68    F: PrimeField,
 69    RangeInclusive<T>: Iterator<Item = T>,
 70{
 71    for (mode_a, a) in test_utils::combination(T::min_value()..=T::max_value()) {
 72        for (mode_b, b) in test_utils::combination(T::min_value()..=T::max_value()) {
 73            test_binary_op(a, b, mode_a, mode_b, test)?;
 74        }
 75    }
 76    Ok(())
 77}
 78
 79pub(crate) fn run_binary_random_with_native<const ITERATIONS: usize, const N: usize, T, F>(
 80    test: impl Fn(UInt<N, T, F>, T) -> Result<(), SynthesisError> + Copy,
 81) -> Result<(), SynthesisError>
 82where
 83    T: PrimUInt,
 84    F: PrimeField,
 85{
 86    let mut rng = ark_std::test_rng();
 87
 88    for _ in 0..ITERATIONS {
 89        for mode_a in modes() {
 90            let a = T::rand(&mut rng);
 91            let b = T::rand(&mut rng);
 92            test_binary_op_with_native(a, b, mode_a, test)?;
 93        }
 94    }
 95    Ok(())
 96}
 97
 98pub(crate) fn run_binary_exhaustive_with_native<const N: usize, T, F>(
 99    test: impl Fn(UInt<N, T, F>, T) -> Result<(), SynthesisError> + Copy,
100) -> Result<(), SynthesisError>
101where
102    T: PrimUInt,
103    F: PrimeField,
104    RangeInclusive<T>: Iterator<Item = T>,
105{
106    for (mode_a, a) in test_utils::combination(T::min_value()..=T::max_value()) {
107        for b in T::min_value()..=T::max_value() {
108            test_binary_op_with_native(a, b, mode_a, test)?;
109        }
110    }
111    Ok(())
112}
113
114pub(crate) fn run_unary_random<const ITERATIONS: usize, const N: usize, T, F>(
115    test: impl Fn(UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
116) -> Result<(), SynthesisError>
117where
118    T: PrimUInt,
119    F: PrimeField,
120{
121    let mut rng = ark_std::test_rng();
122
123    for _ in 0..ITERATIONS {
124        for mode_a in modes() {
125            let a = T::rand(&mut rng);
126            test_unary_op(a, mode_a, test)?;
127        }
128    }
129    Ok(())
130}
131
132pub(crate) fn run_unary_exhaustive<const N: usize, T, F>(
133    test: impl Fn(UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
134) -> Result<(), SynthesisError>
135where
136    T: PrimUInt,
137    F: PrimeField,
138    RangeInclusive<T>: Iterator<Item = T>,
139{
140    for (mode, a) in test_utils::combination(T::min_value()..=T::max_value()) {
141        test_unary_op(a, mode, test)?;
142    }
143    Ok(())
144}