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
use ark_ec::models::ModelParameters;
use ark_ec::short_weierstrass_jacobian::GroupAffine;
use mina_crypto::{hex::skip_0x_prefix_when_needed, prelude::*};
use mina_serialization_types_macros::AutoFrom;
use num::Integer;
use smart_default::SmartDefault;
use crate::numbers::BigInt256;
pub type FieldElement = BigInt256;
#[derive(Clone, Default, Eq, PartialEq, Debug, AutoFrom)]
#[auto_from(mina_serialization_types::field_and_curve_elements::FieldElementVec)]
pub struct FieldElementVec(pub Vec<FieldElement>);
impl HexEncodable for FieldElementVec {
type Error = hex::FromHexError;
fn to_hex_string(&self) -> String {
let mut s = String::with_capacity(64 * self.0.len());
for i in &self.0 {
s.push_str(&i.to_hex_string());
}
s
}
fn try_from_hex(s: impl AsRef<[u8]>) -> Result<Self, Self::Error> {
let s = skip_0x_prefix_when_needed(s.as_ref());
let (q, r) = s.len().div_rem(&64);
let mut vec = Vec::with_capacity(match r > 0 {
true => q + 1,
_ => q,
});
for chunk in s.chunks(64) {
vec.push(BigInt256::try_from_hex(chunk)?);
}
Ok(Self(vec))
}
}
impl<Fs> From<&FieldElementVec> for Vec<Fs>
where
Fs: From<ark_ff::BigInteger256>,
{
fn from(t: &FieldElementVec) -> Self {
t.0.iter()
.map(|i| ark_ff::BigInteger256::from(i).into())
.collect()
}
}
#[derive(Clone, Default, Eq, PartialEq, Debug, AutoFrom)]
#[auto_from(mina_serialization_types::field_and_curve_elements::FiniteECPoint)]
pub struct FiniteECPoint(pub FieldElement, pub FieldElement);
impl<P> From<&FiniteECPoint> for GroupAffine<P>
where
P: ark_ec::SWModelParameters,
<P as ModelParameters>::BaseField: From<ark_ff::BigInteger256>,
{
fn from(p: &FiniteECPoint) -> Self {
Self::new(
ark_ff::BigInteger256::from(&p.0).into(),
ark_ff::BigInteger256::from(&p.1).into(),
false,
)
}
}
#[macro_export]
macro_rules! finite_ec_point {
($e1:expr, $e2:expr) => {
(|s1, s2| {
Ok::<_, hex::FromHexError>(FiniteECPoint(
FieldElement::try_from_hex(s1)?,
FieldElement::try_from_hex(s2)?,
))
})($e1, $e2)
};
}
#[derive(Clone, Default, Eq, PartialEq, Debug, AutoFrom)]
#[auto_from(mina_serialization_types::field_and_curve_elements::FiniteECPointVec)]
pub struct FiniteECPointVec(pub Vec<FiniteECPoint>);
impl<P> From<&FiniteECPointVec> for Vec<GroupAffine<P>>
where
P: ark_ec::SWModelParameters,
<P as ModelParameters>::BaseField: From<ark_ff::BigInteger256>,
{
fn from(v: &FiniteECPointVec) -> Self {
v.0.iter().map(Into::into).collect()
}
}
#[derive(Clone, Default, Eq, PartialEq, Debug, AutoFrom)]
#[auto_from(mina_serialization_types::field_and_curve_elements::FiniteECPointPair)]
pub struct FiniteECPointPair(pub FiniteECPoint, pub FiniteECPoint);
#[macro_export]
macro_rules! finite_ec_point_pair {
($e1:expr, $e2:expr, $e3:expr, $e4:expr) => {
(|s1, s2, s3, s4| {
use mina_rs_base::finite_ec_point;
use mina_rs_base::protocol_state_proof::*;
Ok::<_, hex::FromHexError>(FiniteECPointPair(
finite_ec_point!(s1, s2)?,
finite_ec_point!(s3, s4)?,
))
})($e1, $e2, $e3, $e4)
};
}
#[derive(Clone, Default, Eq, PartialEq, Debug, AutoFrom)]
#[auto_from(mina_serialization_types::field_and_curve_elements::FiniteECPointPairVec)]
pub struct FiniteECPointPairVec(pub Vec<FiniteECPointPair>);
impl<P> From<&FiniteECPointPairVec> for Vec<(GroupAffine<P>, GroupAffine<P>)>
where
P: ark_ec::SWModelParameters,
<P as ModelParameters>::BaseField: From<ark_ff::BigInteger256>,
{
fn from(v: &FiniteECPointPairVec) -> Self {
v.0.iter()
.map(|FiniteECPointPair(x, y)| (x.into(), y.into()))
.collect()
}
}
#[derive(Clone, Eq, PartialEq, Debug, SmartDefault, AutoFrom)]
#[auto_from(mina_serialization_types::field_and_curve_elements::ECPoint)]
pub enum ECPoint {
#[default]
Infinite,
Finite(FiniteECPoint),
}
impl<P> From<&ECPoint> for GroupAffine<P>
where
P: ark_ec::SWModelParameters,
<P as ModelParameters>::BaseField: From<ark_ff::BigInteger256>,
{
fn from(p: &ECPoint) -> Self {
match p {
ECPoint::Infinite => Self::new(Default::default(), Default::default(), true),
ECPoint::Finite(FiniteECPoint(x, y)) => Self::new(
ark_ff::BigInteger256::from(x).into(),
ark_ff::BigInteger256::from(y).into(),
false,
),
}
}
}
#[derive(Clone, Default, Eq, PartialEq, Debug, AutoFrom)]
#[auto_from(mina_serialization_types::field_and_curve_elements::ECPointVec)]
pub struct ECPointVec(pub Vec<ECPoint>);
impl<P> From<&ECPointVec> for Vec<GroupAffine<P>>
where
P: ark_ec::SWModelParameters,
<P as ModelParameters>::BaseField: From<ark_ff::BigInteger256>,
{
fn from(v: &ECPointVec) -> Self {
v.0.iter().map(Into::into).collect()
}
}