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_both<const ITERATIONS: usize, const N: usize, T, F>(
 43    test: impl Fn(UInt<N, T, F>, UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
 44    test_native: impl Fn(UInt<N, T, F>, T) -> Result<(), SynthesisError> + Copy,
 45) -> Result<(), SynthesisError>
 46where
 47    T: PrimUInt,
 48    F: PrimeField,
 49{
 50    let mut rng = ark_std::test_rng();
 51
 52    for _ in 0..ITERATIONS {
 53        for mode_a in modes() {
 54            let a = T::rand(&mut rng);
 55            for mode_b in modes() {
 56                let b = T::rand(&mut rng);
 57                test_binary_op(a, b, mode_a, mode_b, test)?;
 58                test_binary_op_with_native(a, b, mode_a, test_native)?;
 59            }
 60        }
 61    }
 62    Ok(())
 63}
 64
 65pub(crate) fn run_binary_random<const ITERATIONS: usize, const N: usize, T, F>(
 66    test: impl Fn(UInt<N, T, F>, UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
 67) -> Result<(), SynthesisError>
 68where
 69    T: PrimUInt,
 70    F: PrimeField,
 71{
 72    let mut rng = ark_std::test_rng();
 73
 74    for _ in 0..ITERATIONS {
 75        for mode_a in modes() {
 76            let a = T::rand(&mut rng);
 77            for mode_b in modes() {
 78                let b = T::rand(&mut rng);
 79                test_binary_op(a, b, mode_a, mode_b, test)?;
 80            }
 81        }
 82    }
 83    Ok(())
 84}
 85
 86pub(crate) fn run_binary_exhaustive<const N: usize, T, F>(
 87    test: impl Fn(UInt<N, T, F>, UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
 88) -> Result<(), SynthesisError>
 89where
 90    T: PrimUInt,
 91    F: PrimeField,
 92    RangeInclusive<T>: Iterator<Item = T>,
 93{
 94    for (mode_a, a) in test_utils::combination(T::min_value()..=T::max_value()) {
 95        for (mode_b, b) in test_utils::combination(T::min_value()..=T::max_value()) {
 96            test_binary_op(a, b, mode_a, mode_b, test)?;
 97        }
 98    }
 99    Ok(())
100}
101
102pub(crate) fn run_binary_exhaustive_both<const N: usize, T, F>(
103    test: impl Fn(UInt<N, T, F>, UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
104    test_native: impl Fn(UInt<N, T, F>, T) -> Result<(), SynthesisError> + Copy,
105) -> Result<(), SynthesisError>
106where
107    T: PrimUInt,
108    F: PrimeField,
109    RangeInclusive<T>: Iterator<Item = T>,
110{
111    for (mode_a, a) in test_utils::combination(T::min_value()..=T::max_value()) {
112        for (mode_b, b) in test_utils::combination(T::min_value()..=T::max_value()) {
113            test_binary_op(a, b, mode_a, mode_b, test)?;
114            test_binary_op_with_native(a, b, mode_a, test_native)?;
115        }
116    }
117    Ok(())
118}
119
120pub(crate) fn run_binary_random_native_only<const ITERATIONS: usize, const N: usize, T, F>(
121    test: impl Fn(UInt<N, T, F>, T) -> Result<(), SynthesisError> + Copy,
122) -> Result<(), SynthesisError>
123where
124    T: PrimUInt,
125    F: PrimeField,
126{
127    let mut rng = ark_std::test_rng();
128
129    for _ in 0..ITERATIONS {
130        for mode_a in modes() {
131            let a = T::rand(&mut rng);
132            let b = T::rand(&mut rng);
133            test_binary_op_with_native(a, b, mode_a, test)?;
134        }
135    }
136    Ok(())
137}
138
139pub(crate) fn run_binary_exhaustive_native_only<const N: usize, T, F>(
140    test: impl Fn(UInt<N, T, F>, T) -> Result<(), SynthesisError> + Copy,
141) -> Result<(), SynthesisError>
142where
143    T: PrimUInt,
144    F: PrimeField,
145    RangeInclusive<T>: Iterator<Item = T>,
146{
147    for (mode_a, a) in test_utils::combination(T::min_value()..=T::max_value()) {
148        for b in T::min_value()..=T::max_value() {
149            test_binary_op_with_native(a, b, mode_a, test)?;
150        }
151    }
152    Ok(())
153}
154
155pub(crate) fn run_unary_random<const ITERATIONS: usize, const N: usize, T, F>(
156    test: impl Fn(UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
157) -> Result<(), SynthesisError>
158where
159    T: PrimUInt,
160    F: PrimeField,
161{
162    let mut rng = ark_std::test_rng();
163
164    for _ in 0..ITERATIONS {
165        for mode_a in modes() {
166            let a = T::rand(&mut rng);
167            test_unary_op(a, mode_a, test)?;
168        }
169    }
170    Ok(())
171}
172
173pub(crate) fn run_unary_exhaustive<const N: usize, T, F>(
174    test: impl Fn(UInt<N, T, F>) -> Result<(), SynthesisError> + Copy,
175) -> Result<(), SynthesisError>
176where
177    T: PrimUInt,
178    F: PrimeField,
179    RangeInclusive<T>: Iterator<Item = T>,
180{
181    for (mode, a) in test_utils::combination(T::min_value()..=T::max_value()) {
182        test_unary_op(a, mode, test)?;
183    }
184    Ok(())
185}