-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
138 lines (126 loc) · 3.6 KB
/
lib.rs
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
use std::str::FromStr;
use ark_ec::{
short_weierstrass::{Affine, SWCurveConfig},
AffineRepr,
};
use ark_ff::{BigInteger, Fp2, Fp2Config, PrimeField};
use ethers::{
abi::AbiDecode,
prelude::{AbiError, EthAbiCodec, EthAbiType},
types::U256,
};
// TODO: (alex) maybe move these commonly shared util to a crate
/// convert a field element to U256, panic if field size is larger than 256 bit
pub fn field_to_u256<F: PrimeField>(f: F) -> U256 {
if F::MODULUS_BIT_SIZE > 256 {
panic!("Shouldn't convert a >256-bit field to U256");
}
U256::from_little_endian(&f.into_bigint().to_bytes_le())
}
/// convert U256 to a field (mod order)
pub fn u256_to_field<F: PrimeField>(x: U256) -> F {
let mut bytes = [0u8; 32];
x.to_little_endian(&mut bytes);
F::from_le_bytes_mod_order(&bytes)
}
/// an intermediate representation of `BN254.G1Point` in solidity.
#[derive(Clone, PartialEq, Eq, Debug, EthAbiType, EthAbiCodec)]
pub struct ParsedG1Point {
/// x coordinate of affine repr
pub x: U256,
/// y coordinate of affine repr
pub y: U256,
}
// this is convention from BN256 precompile
impl Default for ParsedG1Point {
fn default() -> Self {
Self {
x: U256::from(0),
y: U256::from(0),
}
}
}
impl FromStr for ParsedG1Point {
type Err = AbiError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parsed: (Self,) = AbiDecode::decode_hex(s)?;
Ok(parsed.0)
}
}
impl<P: SWCurveConfig> From<Affine<P>> for ParsedG1Point
where
P::BaseField: PrimeField,
{
fn from(p: Affine<P>) -> Self {
if p.is_zero() {
// this convention is from the BN precompile
Self {
x: U256::from(0),
y: U256::from(0),
}
} else {
Self {
x: field_to_u256::<P::BaseField>(*p.x().unwrap()),
y: field_to_u256::<P::BaseField>(*p.y().unwrap()),
}
}
}
}
impl<P: SWCurveConfig> From<ParsedG1Point> for Affine<P>
where
P::BaseField: PrimeField,
{
fn from(p: ParsedG1Point) -> Self {
if p == ParsedG1Point::default() {
Self::default()
} else {
Self::new_unchecked(
u256_to_field::<P::BaseField>(p.x),
u256_to_field::<P::BaseField>(p.y),
)
}
}
}
/// Intermediate representation of `G2Point` in Solidity
#[derive(Clone, PartialEq, Eq, Debug, EthAbiType, EthAbiCodec)]
pub struct ParsedG2Point {
/// x0 of x = x0 + u * x1 coordinate
pub x0: U256,
/// x1 of x = x0 + u * x1 coordinate
pub x1: U256,
/// y0 of y = y0 + u * y1 coordinate
pub y0: U256,
/// y1 of y = y0 + u * y1 coordinate
pub y1: U256,
}
impl FromStr for ParsedG2Point {
type Err = AbiError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let parsed: (Self,) = AbiDecode::decode_hex(s)?;
Ok(parsed.0)
}
}
impl<P: SWCurveConfig<BaseField = Fp2<C>>, C> From<ParsedG2Point> for Affine<P>
where
C: Fp2Config,
{
fn from(p: ParsedG2Point) -> Self {
Self::new_unchecked(
Fp2::new(u256_to_field(p.x0), u256_to_field(p.x1)),
Fp2::new(u256_to_field(p.y0), u256_to_field(p.y1)),
)
}
}
impl<P: SWCurveConfig<BaseField = Fp2<C>>, C> From<Affine<P>> for ParsedG2Point
where
C: Fp2Config,
{
fn from(p: Affine<P>) -> Self {
Self {
x0: field_to_u256(p.x().unwrap().c0),
x1: field_to_u256(p.x().unwrap().c1),
y0: field_to_u256(p.y().unwrap().c0),
y1: field_to_u256(p.y().unwrap().c1),
}
}
}