commonware_cryptography/bls12381/primitives/mod.rs
1//! Operations over the BLS12-381 scalar field.
2//!
3//! # Acknowledgements
4//!
5//! _The following crates were used as a reference when implementing this crate. If code is very similar
6//! to the reference, it is accompanied by a comment and link._
7//!
8//! * <https://github.com/celo-org/celo-threshold-bls-rs>: Operations over the BLS12-381 scalar field, GJKR99, and Desmedt97.
9//! * <https://github.com/filecoin-project/blstrs> + <https://github.com/MystenLabs/fastcrypto>: Implementing operations over
10//! the BLS12-381 scalar field with <https://github.com/supranational/blst>.
11//!
12//! # Example
13//!
14//! ```rust
15//! use commonware_cryptography::bls12381::{
16//! primitives::{ops::{partial_sign_message, partial_verify_message, threshold_signature_recover, verify_message}, poly::public, variant::MinSig},
17//! dkg::ops::{generate_shares},
18//! };
19//! use rand::rngs::OsRng;
20//!
21//! // Configure threshold
22//! let (n, t) = (5, 4);
23//!
24//! // Generate commitment and shares
25//! let (commitment, shares) = generate_shares::<_, MinSig>(&mut OsRng, None, n, t);
26//!
27//! // Generate partial signatures from shares
28//! let namespace = Some(&b"demo"[..]);
29//! let message = b"hello world";
30//! let partials: Vec<_> = shares.iter().map(|s| partial_sign_message::<MinSig>(s, namespace, message)).collect();
31//!
32//! // Verify partial signatures
33//! for p in &partials {
34//! partial_verify_message::<MinSig>(&commitment, namespace, message, p).expect("signature should be valid");
35//! }
36//!
37//! // Aggregate partial signatures
38//! let threshold_sig = threshold_signature_recover::<MinSig, _>(t, &partials).unwrap();
39//!
40//! // Verify threshold signature
41//! let threshold_pub = public::<MinSig>(&commitment);
42//! verify_message::<MinSig>(&threshold_pub, namespace, message, &threshold_sig).expect("signature should be valid");
43//! ```
44
45pub mod group;
46pub mod ops;
47pub mod poly;
48pub mod variant;
49
50use thiserror::Error;
51
52/// Errors that can occur when working with BLS12-381 primitives.
53#[derive(Error, Debug)]
54pub enum Error {
55 #[error("not enough partial signatures: {0}/{1}")]
56 NotEnoughPartialSignatures(usize, usize),
57 #[error("invalid signature")]
58 InvalidSignature,
59 #[error("invalid recovery")]
60 InvalidRecovery,
61 #[error("no inverse")]
62 NoInverse,
63 #[error("duplicate polynomial evaluation point")]
64 DuplicateEval,
65 #[error("evaluation index is invalid")]
66 InvalidIndex,
67}