two_groups.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //! The basepoints map is keyed by group type, so two groups must be able
  2. //! to coexist in one process.
  3. //!
  4. //! Before the key was `G`, `StaticTypeMap::call_once` was handed
  5. //! `Box<dyn CMZbp>` — the same `TypeId` for every group — so the map held a
  6. //! single shared slot. The second group's `cmz_group_init` was silently
  7. //! ignored and the following `cmz_basepoints` read back the *first* group's
  8. //! basepoints, failing the downcast and panicking.
  9. use cmz::{cmz_basepoints, cmz_group_init};
  10. use curve25519_dalek::ristretto::RistrettoPoint;
  11. use group::Group;
  12. use sha2::Sha512;
  13. #[test]
  14. fn two_groups_coexist() {
  15. let ristretto_a = RistrettoPoint::hash_from_bytes::<Sha512>(b"CMZ Generator A");
  16. let p256_a = p256::ProjectivePoint::generator() * p256::Scalar::from(42u64);
  17. cmz_group_init::<RistrettoPoint>(ristretto_a);
  18. cmz_group_init::<p256::ProjectivePoint>(p256_a);
  19. // Each group must read back its own basepoints, not the other's.
  20. assert_eq!(cmz_basepoints::<RistrettoPoint>().A(), ristretto_a);
  21. assert_eq!(cmz_basepoints::<RistrettoPoint>().B(), RistrettoPoint::generator());
  22. assert_eq!(cmz_basepoints::<p256::ProjectivePoint>().A(), p256_a);
  23. assert_eq!(
  24. cmz_basepoints::<p256::ProjectivePoint>().B(),
  25. p256::ProjectivePoint::generator()
  26. );
  27. // Re-initializing keeps the first value, per cmz_group_init's contract.
  28. cmz_group_init::<RistrettoPoint>(RistrettoPoint::generator());
  29. assert_eq!(cmz_basepoints::<RistrettoPoint>().A(), ristretto_a);
  30. }