rsnark_core/metadata.rs
1use num::{BigInt, Num};
2
3/// Trait for identifying elliptic curves used in zero-knowledge proof systems.
4///
5/// This trait provides a way to statically identify different elliptic curves
6/// through unique numeric identifiers. Each curve type implements this trait
7/// to return its specific curve ID, enabling type-safe curve selection and
8/// backend specialization.
9///
10/// Curve types are typically used as type parameters to specify which elliptic
11/// curve a particular backend or proof system should use.
12///
13pub trait CurveId {
14 /// Returns the unique numeric identifier for this curve.
15 ///
16 /// Each curve implementation must return a unique ID that distinguishes
17 /// it from all other supported curves.
18 fn curve_id() -> u64;
19
20 fn curve_type() -> CurveType;
21
22 fn field() -> BigInt;
23}
24
25macro_rules! define_curve {
26 ($name:ident, $id:expr, $field:expr) => {
27 pub struct $name;
28
29 impl CurveId for $name {
30 fn curve_id() -> u64 {
31 $id
32 }
33
34 fn curve_type() -> CurveType {
35 CurveType::$name
36 }
37
38 fn field() -> BigInt {
39 BigInt::from_str_radix($field, 10).unwrap()
40 }
41 }
42 };
43}
44
45/// Predefined elliptic curve types for common ZK-SNARK applications.
46///
47/// This module contains type definitions for widely-used elliptic curves in
48/// zero-knowledge proof systems. Each curve is defined with a unique identifier
49/// and can be used as a type parameter to specify which curve a backend should use.
50///
51/// # Available Curves
52///
53/// - [`curve::BN254`]
54/// - [`curve::BLS12_381`]
55/// - [`curve::BLS12_377`]
56/// - [`curve::BLS24_317`]
57/// - [`curve::BLS24_315`]
58/// - [`curve::BW6_761`]
59/// - [`curve::BW6_633`]
60///
61pub mod curve {
62 use super::*;
63
64 define_curve!(
65 BN254,
66 1,
67 "21888242871839275222246405745257275088548364400416034343698204186575808495617"
68 );
69 define_curve!(
70 BLS12_381,
71 2,
72 "52435875175126190479447740508185965837690552500527637822603658699938581184513"
73 );
74 define_curve!(
75 BLS24_317,
76 3,
77 "30869589236456844204538189757527902584594726589286811523515204428962673459201"
78 );
79
80 define_curve!(
81 BLS12_377,
82 4,
83 "8444461749428370424248824938781546531375899335154063827935233455917409239041"
84 );
85 define_curve!(
86 BW6_761,
87 5,
88 "258664426012969094010652733694893533536393512754914660539884262666720468348340822774968888139573360124440321458177"
89 );
90 define_curve!(
91 BLS24_315,
92 6,
93 "11502027791375260645628074404575422495959608200132055716665986169834464870401"
94 );
95 define_curve!(
96 BW6_633,
97 7,
98 "39705142709513438335025689890408969744933502416914749335064285505637884093126342347073617133569"
99 );
100}
101
102/// Enumeration of supported elliptic curve types.
103///
104/// This enum represents all the elliptic curves that can be used with rSnark.
105/// Each curve type corresponds to a specific elliptic curve with unique mathematical
106/// properties and field characteristics.
107///
108/// # Curve Categories
109///
110/// ## Production Curves
111/// - [`BN254`](CurveType::BN254): Barreto-Naehrig curve with 254-bit prime, widely used in Ethereum
112/// - [`BLS12_381`](CurveType::BLS12_381): BLS12 curve with 381-bit prime, used in Ethereum 2.0
113/// - [`BLS12_377`](CurveType::BLS12_377): BLS12 curve with 377-bit prime
114/// - [`BLS24_317`](CurveType::BLS24_317): BLS24 curve with 317-bit prime
115/// - [`BLS24_315`](CurveType::BLS24_315): BLS24 curve with 315-bit prime
116/// - [`BW6_761`](CurveType::BW6_761): BW6 curve with 761-bit prime
117/// - [`BW6_633`](CurveType::BW6_633): BW6 curve with 633-bit prime
118///
119/// ## Testing Curves
120/// - [`Mock`](CurveType::Mock): Mock curve for testing purposes
121#[derive(Debug, Clone)]
122pub enum CurveType {
123 Mock,
124 BN254,
125 BLS12_381,
126 BLS24_317,
127 BLS12_377,
128 BW6_761,
129 BLS24_315,
130 BW6_633,
131}
132
133/// Enumeration of supported zero-knowledge proving systems.
134///
135/// This enum represents the different proving systems that can be used to generate
136/// and verify zero-knowledge proofs. Each proving system has different characteristics
137/// in terms of proof size, verification time, and trusted setup requirements.
138///
139/// # Proving Systems
140///
141/// ## Production Systems
142/// - [`Groth16`](ProvingSystem::Groth16): Efficient zk-SNARK with constant-size proofs and fast verification
143/// - [`Plonk`](ProvingSystem::Plonk): Universal zk-SNARK with universal trusted setup
144///
145/// ## Testing Systems
146/// - [`Mock`](ProvingSystem::Mock): Mock proving system for testing without cryptographic operations
147///
148#[derive(Debug, Clone)]
149pub enum ProvingSystem {
150 Mock,
151 Groth16,
152 Plonk,
153}
154
155#[doc(hidden)]
156#[derive(Debug, Clone)]
157pub struct MetadataInfo {
158 pub field: BigInt,
159 pub curve: CurveType,
160 pub proving_system: ProvingSystem,
161}
162
163/// Trait for accessing metadata information from proof system configurations.
164///
165/// This trait provides a standardized interface for retrieving metadata from
166/// various backend configurations. It enables generic code to inspect the
167/// properties of different proving systems without knowing their specific types.
168///
169/// # Methods
170///
171/// - [`field`](Metadata::field): Returns the prime field modulus
172/// - [`curve`](Metadata::curve): Returns the elliptic curve type
173/// - [`proving_system`](Metadata::proving_system): Returns the proving system type
174///
175/// # Usage
176///
177/// This trait is typically implemented by backend types to provide introspection
178/// capabilities and enable metadata-aware generic programming.
179///
180/// ```rust,ignore
181/// fn inspect_backend<T: Metadata>(backend: &T) {
182/// println!("Curve: {:?}", backend.curve());
183/// println!("Proving system: {:?}", backend.proving_system());
184/// println!("Field size: {} bits", backend.field().bits());
185/// }
186/// ```
187pub trait Metadata {
188 /// Returns a reference to the prime field modulus used by this configuration.
189 fn field(&self) -> &BigInt;
190
191 /// Returns a reference to the elliptic curve type used by this configuration.
192 fn curve(&self) -> &CurveType;
193
194 /// Returns a reference to the proving system type used by this configuration.
195 fn proving_system(&self) -> &ProvingSystem;
196}
197
198impl Metadata for MetadataInfo {
199 fn field(&self) -> &BigInt {
200 &self.field
201 }
202
203 fn curve(&self) -> &CurveType {
204 &self.curve
205 }
206
207 fn proving_system(&self) -> &ProvingSystem {
208 &self.proving_system
209 }
210}