1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use anyhow::{anyhow, Error};
use num_bigint::BigInt;
use std::fmt;
use std::str::FromStr;

#[derive(Clone)]
pub enum Curve {
    Bn128,
    Bls12_381,
    Goldilocks,
}

// Used for testing.
impl Default for Curve {
    fn default() -> Self {
        Curve::Bn128
    }
}

impl fmt::Display for Curve {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Curve::*;
        match self {
            Bn128 => write!(f, "BN128"),
            Bls12_381 => write!(f, "BLS12_381"),
            Goldilocks => write!(f, "Goldilocks"),
        }
    }
}

impl fmt::Debug for Curve {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl Curve {
    fn prime(&self) -> BigInt {
        use Curve::*;
        let prime = match self {
            Bn128 => {
                "21888242871839275222246405745257275088548364400416034343698204186575808495617"
            }
            Bls12_381 => {
                "52435875175126190479447740508185965837690552500527637822603658699938581184513"
            }
            Goldilocks => "18446744069414584321",
        };
        BigInt::parse_bytes(prime.as_bytes(), 10).expect("failed to parse prime")
    }
}

impl FromStr for Curve {
    type Err = Error;

    fn from_str(prime: &str) -> Result<Self, Self::Err> {
        match &prime.to_uppercase()[..] {
            "BN128" => Ok(Curve::Bn128),
            "BLS12_381" => Ok(Curve::Bls12_381),
            "GOLDILOCKS" => Ok(Curve::Goldilocks),
            _ => Err(anyhow!("failed to parse prime `{prime}`")),
        }
    }
}

#[derive(Clone)]
pub struct UsefulConstants {
    prime: BigInt,
}

impl UsefulConstants {
    pub fn new(curve: &Curve) -> UsefulConstants {
        UsefulConstants { prime: curve.prime() }
    }

    /// Returns the used prime.
    pub fn prime(&self) -> &BigInt {
        &self.prime
    }

    /// Returns the size in bits of the used prime.
    pub fn prime_size(&self) -> usize {
        self.prime.bits()
    }
}