Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test.rs 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. for _ in 0..100 {
  10. let a = Scalar::random(&mut OsRng);
  11. let b = Scalar::random(&mut OsRng);
  12. // Convert to bit representation
  13. let a_bits = scalar_to_bits(&a);
  14. let b_bits = scalar_to_bits(&b);
  15. // Combine A and B bits into one input vector
  16. let mut input_bits = Vec::with_capacity(512);
  17. input_bits.extend_from_slice(&a_bits);
  18. input_bits.extend_from_slice(&b_bits);
  19. // Build and evaluate the comparison circuit
  20. let circuit = Circuit::compare_n_bit_numbers(input_bits, 256);
  21. let circuit_result = circuit.eval();
  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. }