submodule.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #![allow(non_snake_case)]
  2. use cmz::*;
  3. use curve25519_dalek::ristretto::RistrettoPoint;
  4. use curve25519_dalek::scalar::Scalar;
  5. use group::Group;
  6. use rand::{CryptoRng, RngCore};
  7. pub mod cred {
  8. use super::*;
  9. CMZ! { Basic<RistrettoPoint>: x, y, z }
  10. }
  11. pub mod submod {
  12. use super::cred::Basic;
  13. use super::*;
  14. CMZ14Protocol! { issue_proto,
  15. ,
  16. N: Basic { x: J, y: H, z: I },
  17. }
  18. CMZ14Protocol! { basic_proto,
  19. A: Basic { x: H, y: R, z: I },
  20. N: Basic { x: J, y: H, z: I },
  21. }
  22. #[test]
  23. fn test_submodule() -> Result<(), CMZError> {
  24. let mut rng = rand::thread_rng();
  25. let (privkey, pubkey) = Basic::cmz14_gen_keys(&mut rng);
  26. let mut basic_iss = Basic::using_pubkey(&pubkey);
  27. basic_iss.x = Some(Scalar::ZERO);
  28. basic_iss.y = Some(Scalar::ONE);
  29. basic_iss.z = Some(Scalar::ONE);
  30. let (ireq, istate) = issue_proto::prepare(&mut rng, b"issue_proto", basic_iss).unwrap();
  31. let (ireply, _) = issue_proto::handle(
  32. &mut rng,
  33. b"issue_proto",
  34. ireq,
  35. |N: &mut Basic| {
  36. N.set_privkey(&privkey);
  37. N.z = Some(Scalar::ONE);
  38. Ok(())
  39. },
  40. |_N: &Basic| Ok(()),
  41. )?;
  42. let basic_cred = match istate.finalize(ireply) {
  43. Ok(c) => c,
  44. Err((err, _)) => return Err(err),
  45. };
  46. let mut basic_new = Basic::using_pubkey(&pubkey);
  47. basic_new.y = Some(2u128.into());
  48. basic_new.z = Some(3u128.into());
  49. let (req, state) =
  50. basic_proto::prepare(&mut rng, b"basic_proto", &basic_cred, basic_new).unwrap();
  51. let (reply, _) = basic_proto::handle(
  52. &mut rng,
  53. b"basic_proto",
  54. req,
  55. |A: &mut Basic, N: &mut Basic| {
  56. A.set_privkey(&privkey);
  57. A.z = Some(Scalar::ONE);
  58. N.set_privkey(&privkey);
  59. N.z = Some(3u128.into());
  60. Ok(())
  61. },
  62. |_A: &Basic, _N: &Basic| Ok(()),
  63. )?;
  64. let res = state.finalize(reply);
  65. match res {
  66. Ok(_) => Ok(()),
  67. Err((err, _state)) => Err(err),
  68. }
  69. }
  70. }