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