forked from stellar/rs-soroban-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbn254.rs
More file actions
267 lines (211 loc) · 6.64 KB
/
Copy pathbn254.rs
File metadata and controls
267 lines (211 loc) · 6.64 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#[cfg(not(target_family = "wasm"))]
use crate::xdr::ScVal;
use crate::{
env::internal::{self, BytesObject, U256Val},
impl_bytesn_repr,
unwrap::{UnwrapInfallible, UnwrapOptimized},
Bytes, BytesN, ConversionError, Env, IntoVal, TryFromVal, Val, Vec, U256,
};
use core::{cmp::Ordering, fmt::Debug};
const FP_SERIALIZED_SIZE: usize = 32; // Size in bytes of a serialized Fp element in BN254.
pub const G1_SERIALIZED_SIZE: usize = FP_SERIALIZED_SIZE * 2; // Size in bytes of a serialized G1 element in BN254. Each coordinate (X, Y) is 32 bytes.
pub const G2_SERIALIZED_SIZE: usize = G1_SERIALIZED_SIZE * 2; // Size in bytes of a serialized G1 element in BN254. Each coordinate (X, Y) is 32 bytes.
/// Bn254 provides access to curve and pairing operations on the BN254
/// (also known as alt_bn128) curve.
pub struct Bn254 {
env: Env,
}
// TODO: Add comments
#[derive(Clone)]
#[repr(transparent)]
pub struct G1Affine(BytesN<G1_SERIALIZED_SIZE>);
#[derive(Clone)]
#[repr(transparent)]
pub struct G2Affine(BytesN<G2_SERIALIZED_SIZE>);
#[derive(Clone)]
#[repr(transparent)]
pub struct Fr(U256);
impl_bytesn_repr!(G1Affine, G1_SERIALIZED_SIZE);
impl_bytesn_repr!(G2Affine, G2_SERIALIZED_SIZE);
struct Fp(BytesN<FP_SERIALIZED_SIZE>);
impl G1Affine {
pub fn env(&self) -> &Env {
self.0.env()
}
}
impl G2Affine {
pub fn env(&self) -> &Env {
self.0.env()
}
}
impl Fr {
pub fn env(&self) -> &Env {
self.0.env()
}
pub fn from_u256(value: U256) -> Self {
value.into()
}
pub fn to_u256(&self) -> U256 {
self.0.clone()
}
pub fn as_u256(&self) -> &U256 {
&self.0
}
pub fn from_bytes(bytes: BytesN<32>) -> Self {
U256::from_be_bytes(bytes.env(), bytes.as_ref()).into()
}
pub fn to_bytes(&self) -> BytesN<32> {
self.as_u256().to_be_bytes().try_into().unwrap_optimized()
}
pub fn as_val(&self) -> &Val {
self.0.as_val()
}
pub fn to_val(&self) -> Val {
self.0.to_val()
}
}
impl From<U256> for Fr {
fn from(value: U256) -> Self {
Self(value)
}
}
impl From<&Fr> for U256Val {
fn from(value: &Fr) -> Self {
value.as_u256().into()
}
}
impl TryFromVal<Env, Val> for Fr {
type Error = ConversionError;
fn try_from_val(env: &Env, val: &Val) -> Result<Self, Self::Error> {
let u = U256::try_from_val(env, val)?;
Ok(Fr(u))
}
}
impl TryFromVal<Env, Fr> for Val {
type Error = ConversionError;
fn try_from_val(_env: &Env, fr: &Fr) -> Result<Self, Self::Error> {
Ok(fr.to_val())
}
}
impl TryFromVal<Env, &Fr> for Val {
type Error = ConversionError;
fn try_from_val(_env: &Env, fr: &&Fr) -> Result<Self, Self::Error> {
Ok(fr.to_val())
}
}
#[cfg(not(target_family = "wasm"))]
impl From<&Fr> for ScVal {
fn from(v: &Fr) -> Self {
Self::from(&v.0)
}
}
#[cfg(not(target_family = "wasm"))]
impl From<Fr> for ScVal {
fn from(v: Fr) -> Self {
(&v).into()
}
}
impl Eq for Fr {}
impl PartialEq for Fr {
fn eq(&self, other: &Self) -> bool {
self.as_u256().partial_cmp(other.as_u256()) == Some(core::cmp::Ordering::Equal)
}
}
impl Debug for Fr {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Fr({:?})", self.as_u256())
}
}
impl Bn254 {
pub(crate) fn new(env: &Env) -> Bn254 {
Bn254 { env: env.clone() }
}
pub fn env(&self) -> &Env {
&self.env
}
/// Adds two points `p0` and `p1` in G1.
pub fn g1_add(&self, p0: &G1Affine, p1: &G1Affine) -> G1Affine {
let env = self.env();
let bin =
internal::Env::bn254_g1_add(env, p0.to_object(), p1.to_object()).unwrap_infallible();
unsafe { G1Affine::from_bytes(BytesN::unchecked_new(env.clone(), bin)) }
}
/// Multiplies a point `p0` in G1 by a scalar.
pub fn g1_mul(&self, p0: &G1Affine, scalar: &Fr) -> G1Affine {
let env = self.env();
let bin =
internal::Env::bn254_g1_mul(env, p0.to_object(), scalar.into()).unwrap_infallible();
unsafe { G1Affine::from_bytes(BytesN::unchecked_new(env.clone(), bin)) }
}
// pairing
/// Performs a multi-pairing check between vectors of points in G1 and G2.
///
/// This function computes the pairing for each pair of points in the
/// provided vectors `vp1` (G1 points) and `vp2` (G2 points) and verifies if
/// the product of all pairings is equal to 1 in the target group Fq12.
///
/// # Returns:
/// - `true` if the pairing check holds (i.e., the product of pairings equals 1),
/// otherwise `false`.
///
/// # Panics:
/// - If the lengths of `vp1` and `vp2` are not equal or if they are empty.
pub fn pairing_check(&self, vp1: Vec<G1Affine>, vp2: Vec<G2Affine>) -> bool {
let env = self.env();
internal::Env::bn254_multi_pairing_check(env, vp1.into(), vp2.into())
.unwrap_infallible()
.into()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_g1affine_to_val() {
let env = Env::default();
let g1 = G1Affine::from_bytes(BytesN::from_array(&env, &[1; 64]));
let val: Val = g1.clone().into_val(&env);
let rt: G1Affine = val.into_val(&env);
assert_eq!(g1, rt);
}
#[test]
fn test_ref_g1affine_to_val() {
let env = Env::default();
let g1 = G1Affine::from_bytes(BytesN::from_array(&env, &[1; 64]));
let val: Val = (&g1).into_val(&env);
let rt: G1Affine = val.into_val(&env);
assert_eq!(g1, rt);
}
#[test]
fn test_double_ref_g1affine_to_val() {
let env = Env::default();
let g1 = G1Affine::from_bytes(BytesN::from_array(&env, &[1; 64]));
let val: Val = (&&g1).into_val(&env);
let rt: G1Affine = val.into_val(&env);
assert_eq!(g1, rt);
}
#[test]
fn test_fr_to_val() {
let env = Env::default();
let fr = Fr::from_bytes(BytesN::from_array(&env, &[1; 32]));
let val: Val = fr.clone().into_val(&env);
let rt: Fr = val.into_val(&env);
assert_eq!(fr, rt);
}
#[test]
fn test_ref_fr_to_val() {
let env = Env::default();
let fr = Fr::from_bytes(BytesN::from_array(&env, &[1; 32]));
let val: Val = (&fr).into_val(&env);
let rt: Fr = val.into_val(&env);
assert_eq!(fr, rt);
}
#[test]
fn test_double_ref_fr_to_val() {
let env = Env::default();
let fr = Fr::from_bytes(BytesN::from_array(&env, &[1; 32]));
let val: Val = (&&fr).into_val(&env);
let rt: Fr = val.into_val(&env);
assert_eq!(fr, rt);
}
}