3
1

threshold.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #![allow(non_snake_case)]
  2. use curve25519_dalek::ristretto::RistrettoPoint as G;
  3. use group::Group;
  4. use sha2::Sha512;
  5. use sigma_compiler::*;
  6. #[test]
  7. fn threshold_test() -> sigma_proofs::errors::Result<()> {
  8. sigma_compiler! { thresh3,
  9. (x1, x2, x3, x4, x5, rand r),
  10. (C, const cind G0, const cind G1, const cind G2, const cind G3,
  11. const cind G4, const cind G5),
  12. C = r*G0 + x1*G1 + x2*G2 + x3*G3 + x4*G4 + x5*G5,
  13. THRESH ( 3, x1 = 1, x2 = 2, x3 = 3, x4 = 4, x5 = 5 )
  14. }
  15. type Scalar = <G as Group>::Scalar;
  16. let mut rng = rand::thread_rng();
  17. let G0 = G::generator();
  18. let G1 = G::hash_from_bytes::<Sha512>(b"Generator G1");
  19. let G2 = G::hash_from_bytes::<Sha512>(b"Generator G2");
  20. let G3 = G::hash_from_bytes::<Sha512>(b"Generator G3");
  21. let G4 = G::hash_from_bytes::<Sha512>(b"Generator G4");
  22. let G5 = G::hash_from_bytes::<Sha512>(b"Generator G5");
  23. let r = Scalar::random(&mut rng);
  24. let y = Scalar::random(&mut rng);
  25. // Iterate over all combinations of 5 bits
  26. for true_pattern in 0u32..32 {
  27. let x1 = Scalar::from(if true_pattern & 1 == 0 { 2u32 } else { 1u32 });
  28. let x2 = Scalar::from(if true_pattern & 2 == 0 { 3u32 } else { 2u32 });
  29. let x3 = Scalar::from(if true_pattern & 4 == 0 { 4u32 } else { 3u32 });
  30. let x4 = Scalar::from(if true_pattern & 8 == 0 { 5u32 } else { 4u32 });
  31. let x5 = Scalar::from(if true_pattern & 16 == 0 { 6u32 } else { 5u32 });
  32. let C = r * G0 + x1 * G1 + x2 * G2 + x3 * G3 + x4 * G4 + x5 * G5;
  33. let num_true = true_pattern.count_ones();
  34. let instance = thresh3::Instance {
  35. C,
  36. G0,
  37. G1,
  38. G2,
  39. G3,
  40. G4,
  41. G5,
  42. };
  43. let witness = thresh3::Witness {
  44. x1,
  45. x2,
  46. x3,
  47. x4,
  48. x5,
  49. r,
  50. };
  51. match thresh3::prove(&instance, &witness, b"thresh_test", &mut rng) {
  52. Ok(_) if num_true < 3 => {
  53. panic!("THRESH passed when it should have failed (true_pattern = {true_pattern})")
  54. }
  55. Err(_) if num_true >= 3 => {
  56. panic!("THRESH failed when it should have passed (true_pattern = {true_pattern})")
  57. }
  58. Ok(proof) => {
  59. thresh3::verify(&instance, &proof, b"thresh_test")?;
  60. }
  61. Err(_) => {}
  62. }
  63. }
  64. Ok(())
  65. }