44//! making key exchange indistinguishable from random data.
55
66use crate :: CryptoError ;
7+ use rand_core:: OsRng ;
78
89/// Elligator2 representative (encoded public key)
910pub struct Representative ( [ u8 ; 32 ] ) ;
@@ -28,7 +29,7 @@ pub fn generate_encodable_keypair()
2829-> Result < ( x25519_dalek:: StaticSecret , Representative ) , CryptoError > {
2930 // TODO: Implement proper Elligator2 encoding
3031 // For now, return a placeholder
31- let secret = x25519_dalek:: StaticSecret :: random_from_rng ( rand :: thread_rng ( ) ) ;
32+ let secret = x25519_dalek:: StaticSecret :: random_from_rng ( OsRng ) ;
3233 let public = x25519_dalek:: PublicKey :: from ( & secret) ;
3334
3435 // Placeholder: just use the public key bytes
@@ -42,3 +43,46 @@ pub fn decode_representative(repr: &Representative) -> x25519_dalek::PublicKey {
4243 // For now, treat representative as raw public key bytes
4344 x25519_dalek:: PublicKey :: from ( repr. 0 )
4445}
46+
47+ #[ cfg( test) ]
48+ mod tests {
49+ use super :: * ;
50+
51+ #[ test]
52+ fn test_generate_encodable_keypair ( ) {
53+ // Test that keypair generation succeeds
54+ let result = generate_encodable_keypair ( ) ;
55+ assert ! ( result. is_ok( ) ) ;
56+
57+ let ( secret, representative) = result. unwrap ( ) ;
58+
59+ // Verify representative has valid bytes
60+ let repr_bytes = representative. as_bytes ( ) ;
61+ assert_eq ! ( repr_bytes. len( ) , 32 ) ;
62+
63+ // Verify the representative can be decoded back to a public key
64+ let decoded_public = decode_representative ( & representative) ;
65+
66+ // The decoded public key should match the one derived from the secret
67+ let expected_public = x25519_dalek:: PublicKey :: from ( & secret) ;
68+ assert_eq ! ( decoded_public. as_bytes( ) , expected_public. as_bytes( ) ) ;
69+ }
70+
71+ #[ test]
72+ fn test_representative_roundtrip ( ) {
73+ // Test Representative from_bytes and as_bytes
74+ let original_bytes = [ 0x42u8 ; 32 ] ;
75+ let repr = Representative :: from_bytes ( original_bytes) ;
76+ assert_eq ! ( repr. as_bytes( ) , & original_bytes) ;
77+ }
78+
79+ #[ test]
80+ fn test_multiple_keypairs_are_unique ( ) {
81+ // Generate multiple keypairs and verify they are different
82+ let ( _, repr1) = generate_encodable_keypair ( ) . unwrap ( ) ;
83+ let ( _, repr2) = generate_encodable_keypair ( ) . unwrap ( ) ;
84+
85+ // Representatives should be different (with overwhelming probability)
86+ assert_ne ! ( repr1. as_bytes( ) , repr2. as_bytes( ) ) ;
87+ }
88+ }
0 commit comments