1234567891011121314151617181920212223242526272829303132333435363738 |
- #[cfg(test)]
- mod tests {
- use apet_ex1::{circuit::Circuit, scalar_to_bits::scalar_to_bits};
- use curve25519_dalek::scalar::Scalar;
- use num_bigint::BigUint;
- use rand::rngs::OsRng;
- #[test]
- fn test_scalar_comparison_via_circuit() {
- for _ in 0..100 {
- let a = Scalar::random(&mut OsRng);
- let b = Scalar::random(&mut OsRng);
-
- // Convert to bit representation
- let a_bits = scalar_to_bits(&a);
- let b_bits = scalar_to_bits(&b);
-
- // Combine A and B bits into one input vector
- let mut input_bits = Vec::with_capacity(512);
- input_bits.extend_from_slice(&a_bits);
- input_bits.extend_from_slice(&b_bits);
-
- // Evaluate the comparison circuit
- let circuit = Circuit::compare_n_bit_numbers(256, input_bits);
- let circuit_result = circuit.clone().eval();
-
- // Compare expected result using BigUint
- let a_int = BigUint::from_bytes_le(&a.to_bytes());
- let b_int = BigUint::from_bytes_le(&b.to_bytes());
- let expected = a_int > b_int;
-
- assert_eq!(
- circuit_result, expected,
- "Mismatch: A = {:?}, B = {:?}, A > B = {}, but circuit says {}",
- a_int, b_int, expected, circuit_result
- );
- }
- }
- }
|