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}, variant::MinSig, sharing::Mode},
17//! dkg,
18//! };
19//! use commonware_utils::NZU32;
20//! use rand::rngs::OsRng;
21//!
22//! // Configure number of players
23//! let n = NZU32!(5);
24//!
25//! // Generate commitment and shares
26//! let (sharing, shares) = dkg::deal_anonymous::<MinSig>(&mut OsRng, Mode::default(), n);
27//!
28//! // Generate partial signatures from shares
29//! let namespace = Some(&b"demo"[..]);
30//! let message = b"hello world";
31//! let partials: Vec<_> = shares.iter().map(|s| partial_sign_message::<MinSig>(s, namespace, message)).collect();
32//!
33//! // Verify partial signatures
34//! for p in &partials {
35//! partial_verify_message::<MinSig>(&sharing, namespace, message, p).expect("signature should be valid");
36//! }
37//!
38//! // Aggregate partial signatures
39//! let threshold_sig = threshold_signature_recover::<MinSig, _>(&sharing, &partials).unwrap();
40//!
41//! // Verify threshold signature
42//! let threshold_pub = sharing.public();
43//! verify_message::<MinSig>(threshold_pub, namespace, message, &threshold_sig).expect("signature should be valid");
44//! ```
45
46pub mod group;
47pub mod ops;
48pub mod sharing;
49pub mod variant;
50
51use thiserror::Error;
52
53/// Errors that can occur when working with BLS12-381 primitives.
54#[derive(Error, Debug)]
55pub enum Error {
56 #[error("not enough partial signatures: {0}/{1}")]
57 NotEnoughPartialSignatures(usize, usize),
58 #[error("invalid signature")]
59 InvalidSignature,
60 #[error("invalid recovery")]
61 InvalidRecovery,
62 #[error("no inverse")]
63 NoInverse,
64 #[error("duplicate polynomial evaluation point")]
65 DuplicateEval,
66 #[error("evaluation index is invalid")]
67 InvalidIndex,
68}