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},
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(&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(s, namespace, message)).collect();
31//!
32//! // Verify partial signatures
33//! for p in &partials {
34//!     partial_verify_message(&commitment, namespace, message, p).expect("signature should be valid");
35//! }
36//!
37//! // Aggregate partial signatures
38//! let threshold_sig = threshold_signature_recover(t, &partials).unwrap();
39//!
40//! // Verify threshold signature
41//! let threshold_pub = public(&commitment);
42//! verify_message(&threshold_pub, namespace, message, &threshold_sig).expect("signature should be valid");
43//! ```
44
45pub mod group;
46pub mod ops;
47pub mod poly;
48
49use thiserror::Error;
50
51/// Errors that can occur when working with BLS12-381 primitives.
52#[derive(Error, Debug)]
53pub enum Error {
54    #[error("not enough partial signatures: {0}/{1}")]
55    NotEnoughPartialSignatures(usize, usize),
56    #[error("invalid signature")]
57    InvalidSignature,
58    #[error("invalid recovery")]
59    InvalidRecovery,
60    #[error("no inverse")]
61    NoInverse,
62    #[error("duplicate polynomial evaluation point")]
63    DuplicateEval,
64}