Skip to main content

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//! * <https://github.com/supranational/blst/blob/v0.3.13/bindings/rust/src/pippenger.rs>: Parallel MSM using tile-based Pippenger.
12//!
13//! # Example
14//!
15//! ```rust
16//! use commonware_cryptography::bls12381::{
17//!     dkg::feldman_desmedt as dkg,
18//!     primitives::{ops::{self, threshold}, variant::MinSig, sharing::Mode},
19//! };
20//! use commonware_utils::{NZU32, N3f1};
21//! use commonware_utils::test_rng;
22//!
23//! let mut rng = test_rng();
24//!
25//! // Configure number of players
26//! let n = NZU32!(5);
27//!
28//! // Generate commitment and shares
29//! let (sharing, shares) =
30//!     dkg::deal_anonymous::<MinSig, N3f1>(&mut rng, Mode::NonZeroCounter, n);
31//!
32//! // Generate partial signatures from shares
33//! let namespace = b"demo";
34//! let message = b"hello world";
35//! let partials: Vec<_> = shares.iter().map(|s| threshold::sign_message::<MinSig>(s, namespace, message)).collect();
36//!
37//! // Verify partial signatures
38//! for p in &partials {
39//!     threshold::verify_message::<MinSig>(&sharing, namespace, message, p).expect("signature should be valid");
40//! }
41//!
42//! // Aggregate partial signatures
43//! let threshold_sig = threshold::recover(&sharing, &partials, &commonware_parallel::Sequential).unwrap();
44//!
45//! // Verify threshold signature
46//! let threshold_pub = sharing.public();
47//! ops::verify_message::<MinSig>(threshold_pub, namespace, message, &threshold_sig).expect("signature should be valid");
48//! ```
49
50pub mod group;
51pub mod ops;
52pub mod sharing;
53pub mod variant;
54
55use thiserror::Error;
56
57/// Errors that can occur when working with BLS12-381 primitives.
58#[derive(Error, Debug)]
59pub enum Error {
60    #[error("not enough partial signatures: {0}/{1}")]
61    NotEnoughPartialSignatures(usize, usize),
62    #[error("invalid signature")]
63    InvalidSignature,
64    #[error("invalid recovery")]
65    InvalidRecovery,
66    #[error("no inverse")]
67    NoInverse,
68    #[error("duplicate polynomial evaluation point")]
69    DuplicateEval,
70    #[error("evaluation index is invalid")]
71    InvalidIndex,
72}