commonware_cryptography/
lib.rs1#![doc(
8 html_logo_url = "https://commonware.xyz/imgs/rustdoc_logo.svg",
9 html_favicon_url = "https://commonware.xyz/favicon.ico"
10)]
11#![cfg_attr(not(any(feature = "std", test)), no_std)]
12
13#[cfg(not(feature = "std"))]
14extern crate alloc;
15
16#[cfg(not(any(
19 commonware_stability_GAMMA,
20 commonware_stability_DELTA,
21 commonware_stability_EPSILON,
22 commonware_stability_RESERVED
23)))] pub mod bls12381;
25#[cfg(not(any(
26 commonware_stability_GAMMA,
27 commonware_stability_DELTA,
28 commonware_stability_EPSILON,
29 commonware_stability_RESERVED
30)))] pub mod ed25519;
32#[cfg(not(any(
33 commonware_stability_BETA,
34 commonware_stability_GAMMA,
35 commonware_stability_DELTA,
36 commonware_stability_EPSILON,
37 commonware_stability_RESERVED
38)))] pub mod secp256r1;
40
41commonware_macros::stability_scope!(ALPHA {
42 pub mod bloomfilter;
43 pub use crate::bloomfilter::BloomFilter;
44
45 pub mod lthash;
46 pub use crate::lthash::LtHash;
47});
48commonware_macros::stability_scope!(BETA {
49 use commonware_codec::{Encode, ReadExt};
50 use commonware_math::algebra::Random;
51 use commonware_utils::Array;
52 use rand::SeedableRng as _;
53 use rand_chacha::ChaCha20Rng;
54 use rand_core::CryptoRngCore;
55
56 pub mod secret;
57 pub use crate::secret::Secret;
58
59 pub mod certificate;
60 pub mod transcript;
61
62 pub mod sha256;
63 pub use crate::sha256::{CoreSha256, Sha256};
64 pub mod blake3;
65 pub use crate::blake3::{Blake3, CoreBlake3};
66 #[cfg(feature = "std")]
67 pub mod crc32;
68 #[cfg(feature = "std")]
69 pub use crate::crc32::Crc32;
70
71 #[cfg(feature = "std")]
72 pub mod handshake;
73
74 pub trait Signer: Random + Send + Sync + Clone + 'static {
76 type Signature: Signature;
78
79 type PublicKey: PublicKey<Signature = Self::Signature>;
81
82 fn public_key(&self) -> Self::PublicKey;
84
85 fn sign(&self, namespace: &[u8], msg: &[u8]) -> Self::Signature;
96
97 fn from_seed(seed: u64) -> Self {
104 Self::random(&mut ChaCha20Rng::seed_from_u64(seed))
105 }
106 }
107
108 pub trait PrivateKey: Signer + Sized + ReadExt + Encode {}
110
111 pub trait Verifier {
113 type Signature: Signature;
115
116 fn verify(&self, namespace: &[u8], msg: &[u8], sig: &Self::Signature) -> bool;
124 }
125
126 pub trait PublicKey: Verifier + Sized + ReadExt + Encode + PartialEq + Array {}
128
129 pub trait Signature: Sized + Clone + ReadExt + Encode + PartialEq + Array {}
131
132 pub trait Recoverable: Signature {
134 type PublicKey: PublicKey<Signature = Self>;
136
137 fn recover_signer(&self, namespace: &[u8], msg: &[u8]) -> Option<Self::PublicKey>;
144 }
145
146 pub trait BatchVerifier {
148 type PublicKey: PublicKey;
150
151 fn new() -> Self;
153
154 fn add(
164 &mut self,
165 namespace: &[u8],
166 message: &[u8],
167 public_key: &Self::PublicKey,
168 signature: &<Self::PublicKey as Verifier>::Signature,
169 ) -> bool;
170
171 fn verify<R: CryptoRngCore>(self, rng: &mut R) -> bool;
185 }
186
187 pub trait Digest: Array + Copy + Random {
195 const EMPTY: Self;
197 }
198
199 pub trait Digestible: Clone + Sized + Send + Sync + 'static {
201 type Digest: Digest;
203
204 fn digest(&self) -> Self::Digest;
209 }
210
211 pub trait Committable: Clone + Sized + Send + Sync + 'static {
213 type Commitment: Digest;
215
216 fn commitment(&self) -> Self::Commitment;
229 }
230
231 pub type DigestOf<H> = <H as Hasher>::Digest;
232
233 pub trait Hasher: Default + Clone + Send + Sync + 'static {
246 type Digest: Digest;
248
249 fn new() -> Self {
251 Self::default()
252 }
253
254 fn update(&mut self, message: &[u8]) -> &mut Self;
256
257 fn finalize(&mut self) -> Self::Digest;
260
261 fn reset(&mut self) -> &mut Self;
265
266 fn hash(message: &[u8]) -> Self::Digest {
268 Self::new().update(message).finalize()
269 }
270 }
271});
272
273#[cfg(test)]
274mod tests {
275 use super::*;
276 use commonware_codec::{DecodeExt, FixedSize};
277 use commonware_utils::test_rng;
278
279 fn test_validate<C: PrivateKey>() {
280 let private_key = C::random(&mut test_rng());
281 let public_key = private_key.public_key();
282 assert!(C::PublicKey::decode(public_key.as_ref()).is_ok());
283 }
284
285 fn test_validate_invalid_public_key<C: Signer>() {
286 let result = C::PublicKey::decode(vec![0; 1024].as_ref());
287 assert!(result.is_err());
288 }
289
290 fn test_sign_and_verify<C: PrivateKey>() {
291 let private_key = C::from_seed(0);
292 let namespace = b"test_namespace";
293 let message = b"test_message";
294 let signature = private_key.sign(namespace, message);
295 let public_key = private_key.public_key();
296 assert!(public_key.verify(namespace, message, &signature));
297 }
298
299 fn test_sign_and_verify_wrong_message<C: PrivateKey>() {
300 let private_key = C::from_seed(0);
301 let namespace = b"test_namespace";
302 let message = b"test_message";
303 let wrong_message = b"wrong_message";
304 let signature = private_key.sign(namespace, message);
305 let public_key = private_key.public_key();
306 assert!(!public_key.verify(namespace, wrong_message, &signature));
307 }
308
309 fn test_sign_and_verify_wrong_namespace<C: PrivateKey>() {
310 let private_key = C::from_seed(0);
311 let namespace = b"test_namespace";
312 let wrong_namespace = b"wrong_namespace";
313 let message = b"test_message";
314 let signature = private_key.sign(namespace, message);
315 let public_key = private_key.public_key();
316 assert!(!public_key.verify(wrong_namespace, message, &signature));
317 }
318
319 fn test_empty_namespace<C: PrivateKey>() {
320 let private_key = C::from_seed(0);
321 let empty_namespace = b"";
322 let message = b"test_message";
323 let signature = private_key.sign(empty_namespace, message);
324 let public_key = private_key.public_key();
325 assert!(public_key.verify(empty_namespace, message, &signature));
326 }
327
328 fn test_signature_determinism<C: PrivateKey>() {
329 let private_key_1 = C::from_seed(0);
330 let private_key_2 = C::from_seed(0);
331 let namespace = b"test_namespace";
332 let message = b"test_message";
333 let signature_1 = private_key_1.sign(namespace, message);
334 let signature_2 = private_key_2.sign(namespace, message);
335 assert_eq!(private_key_1.public_key(), private_key_2.public_key());
336 assert_eq!(signature_1, signature_2);
337 }
338
339 fn test_invalid_signature_publickey_pair<C: PrivateKey>() {
340 let private_key = C::from_seed(0);
341 let private_key_2 = C::from_seed(1);
342 let namespace = b"test_namespace";
343 let message = b"test_message";
344 let signature = private_key.sign(namespace, message);
345 let public_key = private_key_2.public_key();
346 assert!(!public_key.verify(namespace, message, &signature));
347 }
348
349 #[test]
350 fn test_ed25519_validate() {
351 test_validate::<ed25519::PrivateKey>();
352 }
353
354 #[test]
355 fn test_ed25519_validate_invalid_public_key() {
356 test_validate_invalid_public_key::<ed25519::PrivateKey>();
357 }
358
359 #[test]
360 fn test_ed25519_sign_and_verify() {
361 test_sign_and_verify::<ed25519::PrivateKey>();
362 }
363
364 #[test]
365 fn test_ed25519_sign_and_verify_wrong_message() {
366 test_sign_and_verify_wrong_message::<ed25519::PrivateKey>();
367 }
368
369 #[test]
370 fn test_ed25519_sign_and_verify_wrong_namespace() {
371 test_sign_and_verify_wrong_namespace::<ed25519::PrivateKey>();
372 }
373
374 #[test]
375 fn test_ed25519_empty_namespace() {
376 test_empty_namespace::<ed25519::PrivateKey>();
377 }
378
379 #[test]
380 fn test_ed25519_signature_determinism() {
381 test_signature_determinism::<ed25519::PrivateKey>();
382 }
383
384 #[test]
385 fn test_ed25519_invalid_signature_publickey_pair() {
386 test_invalid_signature_publickey_pair::<ed25519::PrivateKey>();
387 }
388
389 #[test]
390 fn test_ed25519_len() {
391 assert_eq!(ed25519::PublicKey::SIZE, 32);
392 assert_eq!(ed25519::Signature::SIZE, 64);
393 }
394
395 #[test]
396 fn test_bls12381_validate() {
397 test_validate::<bls12381::PrivateKey>();
398 }
399
400 #[test]
401 fn test_bls12381_validate_invalid_public_key() {
402 test_validate_invalid_public_key::<bls12381::PrivateKey>();
403 }
404
405 #[test]
406 fn test_bls12381_sign_and_verify() {
407 test_sign_and_verify::<bls12381::PrivateKey>();
408 }
409
410 #[test]
411 fn test_bls12381_sign_and_verify_wrong_message() {
412 test_sign_and_verify_wrong_message::<bls12381::PrivateKey>();
413 }
414
415 #[test]
416 fn test_bls12381_sign_and_verify_wrong_namespace() {
417 test_sign_and_verify_wrong_namespace::<bls12381::PrivateKey>();
418 }
419
420 #[test]
421 fn test_bls12381_empty_namespace() {
422 test_empty_namespace::<bls12381::PrivateKey>();
423 }
424
425 #[test]
426 fn test_bls12381_signature_determinism() {
427 test_signature_determinism::<bls12381::PrivateKey>();
428 }
429
430 #[test]
431 fn test_bls12381_invalid_signature_publickey_pair() {
432 test_invalid_signature_publickey_pair::<bls12381::PrivateKey>();
433 }
434
435 #[test]
436 fn test_bls12381_len() {
437 assert_eq!(bls12381::PublicKey::SIZE, 48);
438 assert_eq!(bls12381::Signature::SIZE, 96);
439 }
440
441 #[test]
442 fn test_secp256r1_standard_validate() {
443 test_validate::<secp256r1::standard::PrivateKey>();
444 }
445
446 #[test]
447 fn test_secp256r1_standard_validate_invalid_public_key() {
448 test_validate_invalid_public_key::<secp256r1::standard::PrivateKey>();
449 }
450
451 #[test]
452 fn test_secp256r1_standard_sign_and_verify() {
453 test_sign_and_verify::<secp256r1::standard::PrivateKey>();
454 }
455
456 #[test]
457 fn test_secp256r1_standard_sign_and_verify_wrong_message() {
458 test_sign_and_verify_wrong_message::<secp256r1::standard::PrivateKey>();
459 }
460
461 #[test]
462 fn test_secp256r1_standard_sign_and_verify_wrong_namespace() {
463 test_sign_and_verify_wrong_namespace::<secp256r1::standard::PrivateKey>();
464 }
465
466 #[test]
467 fn test_secp256r1_standard_empty_namespace() {
468 test_empty_namespace::<secp256r1::standard::PrivateKey>();
469 }
470
471 #[test]
472 fn test_secp256r1_standard_signature_determinism() {
473 test_signature_determinism::<secp256r1::standard::PrivateKey>();
474 }
475
476 #[test]
477 fn test_secp256r1_standard_invalid_signature_publickey_pair() {
478 test_invalid_signature_publickey_pair::<secp256r1::standard::PrivateKey>();
479 }
480
481 #[test]
482 fn test_secp256r1_standard_len() {
483 assert_eq!(secp256r1::standard::PublicKey::SIZE, 33);
484 assert_eq!(secp256r1::standard::Signature::SIZE, 64);
485 }
486
487 #[test]
488 fn test_secp256r1_recoverable_validate() {
489 test_validate::<secp256r1::recoverable::PrivateKey>();
490 }
491
492 #[test]
493 fn test_secp256r1_recoverable_validate_invalid_public_key() {
494 test_validate_invalid_public_key::<secp256r1::recoverable::PrivateKey>();
495 }
496
497 #[test]
498 fn test_secp256r1_recoverable_sign_and_verify() {
499 test_sign_and_verify::<secp256r1::recoverable::PrivateKey>();
500 }
501
502 #[test]
503 fn test_secp256r1_recoverable_sign_and_verify_wrong_message() {
504 test_sign_and_verify_wrong_message::<secp256r1::recoverable::PrivateKey>();
505 }
506
507 #[test]
508 fn test_secp256r1_recoverable_sign_and_verify_wrong_namespace() {
509 test_sign_and_verify_wrong_namespace::<secp256r1::recoverable::PrivateKey>();
510 }
511
512 #[test]
513 fn test_secp256r1_recoverable_empty_namespace() {
514 test_empty_namespace::<secp256r1::recoverable::PrivateKey>();
515 }
516
517 #[test]
518 fn test_secp256r1_recoverable_signature_determinism() {
519 test_signature_determinism::<secp256r1::recoverable::PrivateKey>();
520 }
521
522 #[test]
523 fn test_secp256r1_recoverable_invalid_signature_publickey_pair() {
524 test_invalid_signature_publickey_pair::<secp256r1::recoverable::PrivateKey>();
525 }
526
527 #[test]
528 fn test_secp256r1_recoverable_len() {
529 assert_eq!(secp256r1::recoverable::PublicKey::SIZE, 33);
530 assert_eq!(secp256r1::recoverable::Signature::SIZE, 65);
531 }
532
533 fn test_hasher_multiple_runs<H: Hasher>() {
534 let mut hasher = H::new();
536 hasher.update(b"hello world");
537 let digest = hasher.finalize();
538 assert!(H::Digest::decode(digest.as_ref()).is_ok());
539 assert_eq!(digest.as_ref().len(), H::Digest::SIZE);
540
541 hasher.update(b"hello world");
543 let digest_again = hasher.finalize();
544 assert!(H::Digest::decode(digest_again.as_ref()).is_ok());
545 assert_eq!(digest, digest_again);
546
547 hasher.update(b"hello mars");
549 hasher.reset();
550 hasher.update(b"hello world");
551 let digest_reset = hasher.finalize();
552 assert!(H::Digest::decode(digest_reset.as_ref()).is_ok());
553 assert_eq!(digest, digest_reset);
554
555 hasher.update(b"hello mars");
557 let digest_mars = hasher.finalize();
558 assert!(H::Digest::decode(digest_mars.as_ref()).is_ok());
559 assert_ne!(digest, digest_mars);
560 }
561
562 fn test_hasher_multiple_updates<H: Hasher>() {
563 let mut hasher = H::new();
565 hasher.update(b"hello");
566 hasher.update(b" world");
567 let digest = hasher.finalize();
568 assert!(H::Digest::decode(digest.as_ref()).is_ok());
569
570 let mut hasher = H::new();
572 hasher.update(b"hello world");
573 let digest_oneshot = hasher.finalize();
574 assert!(H::Digest::decode(digest_oneshot.as_ref()).is_ok());
575 assert_eq!(digest, digest_oneshot);
576 }
577
578 fn test_hasher_empty_input<H: Hasher>() {
579 let mut hasher = H::new();
580 let digest = hasher.finalize();
581 assert!(H::Digest::decode(digest.as_ref()).is_ok());
582 }
583
584 fn test_hasher_large_input<H: Hasher>() {
585 let mut hasher = H::new();
586 let data = vec![1; 1024];
587 hasher.update(&data);
588 let digest = hasher.finalize();
589 assert!(H::Digest::decode(digest.as_ref()).is_ok());
590 }
591
592 #[test]
593 fn test_sha256_hasher_multiple_runs() {
594 test_hasher_multiple_runs::<Sha256>();
595 }
596
597 #[test]
598 fn test_sha256_hasher_multiple_updates() {
599 test_hasher_multiple_updates::<Sha256>();
600 }
601
602 #[test]
603 fn test_sha256_hasher_empty_input() {
604 test_hasher_empty_input::<Sha256>();
605 }
606
607 #[test]
608 fn test_sha256_hasher_large_input() {
609 test_hasher_large_input::<Sha256>();
610 }
611}