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