You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.rs 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #[cfg(test)]
  2. mod tests {
  3. use apet_ex1::{circuit::Circuit, scalar_to_bits::scalar_to_bits};
  4. use curve25519_dalek::scalar::Scalar;
  5. use num_bigint::BigUint;
  6. use rand::rngs::OsRng;
  7. #[test]
  8. fn test_scalar_comparison_via_circuit() {
  9. let circuit = Circuit::compare_n_bit_numbers(256);
  10. for _ in 0..100 {
  11. let a = Scalar::random(&mut OsRng);
  12. let b = Scalar::random(&mut OsRng);
  13. // Convert to bit representation
  14. let a_bits = scalar_to_bits(&a);
  15. let b_bits = scalar_to_bits(&b);
  16. // Combine A and B bits into one input vector
  17. let mut input_bits = Vec::with_capacity(512);
  18. input_bits.extend_from_slice(&a_bits);
  19. input_bits.extend_from_slice(&b_bits);
  20. // Evaluate the comparison circuit
  21. let circuit_result = circuit.clone().eval(input_bits);
  22. // Compare expected result using BigUint
  23. let a_int = BigUint::from_bytes_le(&a.to_bytes());
  24. let b_int = BigUint::from_bytes_le(&b.to_bytes());
  25. let expected = a_int > b_int;
  26. assert_eq!(
  27. circuit_result, expected,
  28. "Mismatch: A = {:?}, B = {:?}, A > B = {}, but circuit says {}",
  29. a_int, b_int, expected, circuit_result
  30. );
  31. }
  32. }
  33. }