@@ -88,4 +88,170 @@ mod tests {
88
88
// Assert error handling for keypair derivation with invalid seed
89
89
assert ! ( derive( & invalid_seed) . is_err( ) ) ;
90
90
}
91
+
92
+ // Test for keypair generation using Keypair::generate
93
+ #[ test]
94
+ fn test_keypair_generate ( ) {
95
+ // Initialize a random number generator
96
+ let mut rng = OsRng ;
97
+ // Generate keypair using Keypair::generate
98
+ let keypair = Keypair :: generate ( & mut rng) . unwrap ( ) ;
99
+ // Assert the length of the public and secret keys
100
+ assert_eq ! ( keypair. public. len( ) , KYBER_PUBLIC_KEY_BYTES ) ;
101
+ assert_eq ! ( keypair. secret. len( ) , KYBER_SECRET_KEY_BYTES ) ;
102
+ }
103
+
104
+ // Test for Keypair::import method
105
+ #[ test]
106
+ fn test_keypair_import ( ) {
107
+ // Initialize a random number generator
108
+ let mut rng = OsRng ;
109
+ // Generate keypair
110
+ let keypair = keypair ( & mut rng) . unwrap ( ) ;
111
+ // Create mutable references to the public and secret keys
112
+ let mut public_key = keypair. public ;
113
+ let mut secret_key = keypair. secret ;
114
+ // Import keypair using Keypair::import
115
+ let imported_keypair = Keypair :: import ( & mut public_key, & mut secret_key, & mut rng) . unwrap ( ) ;
116
+ // Assert equality of the imported keypair and the original keypair
117
+ assert_eq ! ( imported_keypair. public, keypair. public) ;
118
+ assert_eq ! ( imported_keypair. secret, keypair. secret) ;
119
+ }
120
+
121
+ // Test for keypairfrom function
122
+ #[ test]
123
+ fn test_keypairfrom ( ) {
124
+ // Initialize a random number generator
125
+ let mut rng = OsRng ;
126
+ // Generate keypair
127
+ let keypair = keypair ( & mut rng) . unwrap ( ) ;
128
+ // Create mutable references to the public and secret keys
129
+ let mut public_key = keypair. public ;
130
+ let mut secret_key = keypair. secret ;
131
+ // Create keypair using keypairfrom
132
+ let new_keypair = keypairfrom ( & mut public_key, & mut secret_key, & mut rng) . unwrap ( ) ;
133
+ // Assert equality of the new keypair and the original keypair
134
+ assert_eq ! ( new_keypair. public, keypair. public) ;
135
+ assert_eq ! ( new_keypair. secret, keypair. secret) ;
136
+ }
137
+
138
+ // Test for handling of invalid inputs in keypairfrom
139
+ #[ test]
140
+ fn test_keypairfrom_invalid_input ( ) {
141
+ // Initialize a random number generator
142
+ let mut rng = OsRng ;
143
+ // Define invalid public key and secret key
144
+ let mut invalid_public_key = [ 0u8 ; KYBER_PUBLIC_KEY_BYTES ] ;
145
+ let mut invalid_secret_key = [ 0u8 ; KYBER_SECRET_KEY_BYTES ] ;
146
+ // Modify the public key and secret key to make them invalid
147
+ invalid_public_key[ 0 ] = 0xFF ;
148
+ invalid_secret_key[ 0 ] = 0xFF ;
149
+ // Assert error handling for keypairfrom with invalid public key and secret key
150
+ assert ! ( keypairfrom( & mut invalid_public_key, & mut invalid_secret_key, & mut rng) . is_err( ) ) ;
151
+ }
152
+
153
+ // Test for handling of invalid inputs in Keypair::import
154
+ #[ test]
155
+ fn test_keypair_import_invalid_input ( ) {
156
+ // Initialize a random number generator
157
+ let mut rng = OsRng ;
158
+ // Define invalid public key and secret key
159
+ let mut invalid_public_key = [ 0u8 ; KYBER_PUBLIC_KEY_BYTES ] ;
160
+ let mut invalid_secret_key = [ 0u8 ; KYBER_SECRET_KEY_BYTES ] ;
161
+ // Modify the public key and secret key to make them invalid
162
+ invalid_public_key[ 0 ] = 0xFF ;
163
+ invalid_secret_key[ 0 ] = 0xFF ;
164
+ // Assert error handling for Keypair::import with invalid public key and secret key
165
+ assert ! (
166
+ Keypair :: import( & mut invalid_public_key, & mut invalid_secret_key, & mut rng) . is_err( )
167
+ ) ;
168
+ }
169
+
170
+ // Test for handling of invalid inputs in Keypair::generate
171
+ #[ test]
172
+ fn test_keypair_generate_invalid_input ( ) {
173
+ // Initialize a random number generator
174
+ let mut rng = OsRng ;
175
+ // Define invalid public key and secret key
176
+ let mut invalid_public_key = [ 0u8 ; KYBER_PUBLIC_KEY_BYTES ] ;
177
+ let mut invalid_secret_key = [ 0u8 ; KYBER_SECRET_KEY_BYTES ] ;
178
+ // Modify the public key and secret key to make them invalid
179
+ invalid_public_key[ 0 ] = 0xFF ;
180
+ invalid_secret_key[ 0 ] = 0xFF ;
181
+ // Assert error handling for Keypair::generate with invalid public key and secret key
182
+ assert ! ( Keypair :: generate( & mut rng) . is_ok( ) ) ;
183
+ }
184
+
185
+ // Test for handling of invalid inputs in encapsulate
186
+ #[ test]
187
+ fn test_encapsulate_invalid_input ( ) {
188
+ // Initialize a random number generator
189
+ let mut rng = OsRng ;
190
+ // Define invalid public key
191
+ let invalid_public_key = [ 0u8 ; KYBER_PUBLIC_KEY_BYTES - 1 ] ;
192
+ // Assert error handling for encapsulate with invalid public key
193
+ assert ! ( encapsulate( & invalid_public_key, & mut rng) . is_err( ) ) ;
194
+ }
195
+
196
+ // Test for handling of invalid inputs in decapsulate
197
+ #[ test]
198
+ fn test_decapsulate_invalid_input ( ) {
199
+ // Initialize a random number generator
200
+ let mut rng = OsRng ;
201
+ // Generate keypair
202
+ let keypair = keypair ( & mut rng) . unwrap ( ) ;
203
+ // Define invalid ciphertext and secret key
204
+ let invalid_ciphertext = [ 0u8 ; KYBER_CIPHERTEXT_BYTES - 1 ] ;
205
+ let invalid_secret_key = [ 0u8 ; KYBER_SECRET_KEY_BYTES - 1 ] ;
206
+ // Assert error handling for decapsulate with invalid ciphertext and secret key
207
+ assert ! ( decapsulate( & invalid_ciphertext, & keypair. secret) . is_err( ) ) ;
208
+ assert ! ( decapsulate( & invalid_ciphertext, & invalid_secret_key) . is_err( ) ) ;
209
+ }
210
+
211
+ // Test for handling of invalid inputs in derive
212
+ #[ test]
213
+ fn test_derive_invalid_input ( ) {
214
+ // Define invalid secret key
215
+ let invalid_secret_key = [ 0u8 ; KYBER_SECRET_KEY_BYTES - 1 ] ;
216
+ // Assert error handling for derive with invalid secret key
217
+ assert ! ( derive( & invalid_secret_key) . is_err( ) ) ;
218
+ }
219
+
220
+ // Test for handling of invalid inputs in public
221
+ #[ test]
222
+ fn test_public_invalid_secret_key_length ( ) {
223
+ let invalid_secret_key = [ 0u8 ; KYBER_SECRET_KEY_BYTES - 1 ] ;
224
+ assert_eq ! ( public( & invalid_secret_key) . len( ) , KYBER_PUBLIC_KEY_BYTES ) ;
225
+ }
226
+
227
+ // Test for keypair equality
228
+ #[ test]
229
+ fn test_keypair_equality ( ) {
230
+ let mut rng = OsRng ;
231
+ let keypair1 = keypair ( & mut rng) . unwrap ( ) ;
232
+ let keypair2 = keypair ( & mut rng) . unwrap ( ) ;
233
+ assert_eq ! ( keypair1, keypair1) ;
234
+ assert_ne ! ( keypair1, keypair2) ;
235
+ }
236
+
237
+ // Test for valid seed for derivation
238
+ #[ test]
239
+ fn test_derive_valid_seed ( ) {
240
+ let seed = [ 0u8 ; 64 ] ;
241
+ let keypair = derive ( & seed) . unwrap ( ) ;
242
+ assert_eq ! ( keypair. public. len( ) , KYBER_PUBLIC_KEY_BYTES ) ;
243
+ assert_eq ! ( keypair. secret. len( ) , KYBER_SECRET_KEY_BYTES ) ;
244
+ }
245
+
246
+ // Test for valid input for encapsulation and decapsulation
247
+ #[ test]
248
+ fn test_encapsulate_decapsulate_valid_input ( ) {
249
+ let mut rng = OsRng ;
250
+ let keypair = keypair ( & mut rng) . unwrap ( ) ;
251
+ let ( ciphertext, shared_secret) = encapsulate ( & keypair. public , & mut rng) . unwrap ( ) ;
252
+ assert_eq ! ( ciphertext. len( ) , KYBER_CIPHERTEXT_BYTES ) ;
253
+ assert_eq ! ( shared_secret. len( ) , KYBER_SHARED_SECRET_BYTES ) ;
254
+ let decapsulated_secret = decapsulate ( & ciphertext, & keypair. secret ) . unwrap ( ) ;
255
+ assert_eq ! ( shared_secret, decapsulated_secret) ;
256
+ }
91
257
}
0 commit comments