threshold_bls/curve/
mod.rs

1use thiserror::Error;
2
3use crate::group::{Curve, PairingCurve};
4
5/// Wrappers around the BLS12-381 curve from the [paired](http://docs.rs/paired) crate
6#[cfg(feature = "bls12_381")]
7pub mod bls12381;
8
9#[cfg(feature = "bn254")]
10pub mod bn254;
11
12/// Error which unifies all curve specific errors from different libraries
13#[derive(Debug, Error)]
14pub enum CurveError {
15    #[cfg(feature = "bls12_381")]
16    #[error("Bellman Error: {0}")]
17    BLS12_381(bls12381::BLS12Error),
18
19    #[cfg(feature = "bn254")]
20    #[error("Bellman Error: {0}")]
21    BN254(bn254::BNError),
22}
23
24#[derive(Debug, Error)]
25/// Error type
26pub enum BLSError {
27    /// Error
28    #[error("signature verification failed")]
29    VerificationFailed,
30
31    /// An IO error
32    #[error("io error {0}")]
33    IoError(#[from] std::io::Error),
34
35    /// Error while hashing
36    #[error("error in hasher {0}")]
37    HashingError(#[from] Box<dyn std::error::Error>),
38
39    /// Personalization string cannot be larger than 8 bytes
40    #[error("domain length is too large: {0}")]
41    DomainTooLarge(usize),
42
43    /// Hashing to curve failed
44    #[error("Could not hash to curve")]
45    HashToCurveError,
46
47    /// There must be the same number of keys and messages
48    #[error("there must be the same number of keys and messages")]
49    UnevenNumKeysMessages,
50
51    /// Serialization error in the underlying library
52    #[error(transparent)]
53    SerializationError(#[from] ark_serialize::SerializationError),
54
55    /// Serialization error when transform to/from contract form
56    #[error("serialization error when transform to/from contract form")]
57    ContractSerializationError,
58
59    #[error("not a valid group element")]
60    NotValidPoint,
61}
62
63pub trait CurveType {
64    type G1Curve: Curve;
65    type G2Curve: Curve;
66    type PairingCurve: PairingCurve;
67}
68
69#[cfg(test)]
70mod tests {
71    use crate::curve::bn254::G2Curve;
72    use crate::group::Curve;
73    use crate::group::Element;
74    use ethers_core::utils::hex;
75    use rand::prelude::*;
76
77    fn keypair<C: Curve>() -> (C::Scalar, C::Point) {
78        let private = C::Scalar::rand(&mut thread_rng());
79        let mut public = C::Point::one();
80        public.mul(&private);
81        (private, public)
82    }
83
84    #[test]
85    fn keypairs() {
86        for _ in 0..12 {
87            let (private, public) = keypair::<G2Curve>();
88            println!("{:?}", hex::encode(bincode::serialize(&private).unwrap()));
89            println!("{:?}", hex::encode(bincode::serialize(&public).unwrap()));
90        }
91    }
92}