kimchi_stubs/arkworks/
pasta_fp.rs1use crate::{arkworks::CamlBigInteger256, caml::caml_bytes_string::CamlBytesString};
2use ark_ff::{FftField, Field, One, PrimeField, UniformRand, Zero};
3use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as Domain};
4use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
5use core::{
6 cmp::Ordering::{Equal, Greater, Less},
7 convert::{TryFrom, TryInto},
8 ops::Deref,
9};
10use mina_curves::pasta::fields::{
11 fft::FpParameters,
12 fp::{Fp, FpParameters as Fp_params},
13};
14use num_bigint::BigUint;
15use rand::rngs::StdRng;
16
17#[derive(Clone, Copy, Debug, ocaml_gen::CustomType)]
18pub struct CamlFp(pub Fp);
19
20unsafe impl<'a> ocaml::FromValue<'a> for CamlFp {
21 fn from_value(value: ocaml::Value) -> Self {
22 let x: ocaml::Pointer<Self> = ocaml::FromValue::from_value(value);
23 *x.as_ref()
24 }
25}
26
27impl CamlFp {
28 unsafe extern "C" fn caml_pointer_finalize(v: ocaml::Raw) {
29 let ptr = v.as_pointer::<Self>();
30 ptr.drop_in_place()
31 }
32
33 unsafe extern "C" fn ocaml_compare(x: ocaml::Raw, y: ocaml::Raw) -> i32 {
34 let x = x.as_pointer::<Self>();
35 let y = y.as_pointer::<Self>();
36 match x.as_ref().0.into_bigint().cmp(&y.as_ref().0.into_bigint()) {
37 core::cmp::Ordering::Less => -1,
38 core::cmp::Ordering::Equal => 0,
39 core::cmp::Ordering::Greater => 1,
40 }
41 }
42}
43
44ocaml::custom!(CamlFp {
45 finalize: CamlFp::caml_pointer_finalize,
46 compare: CamlFp::ocaml_compare,
47});
48
49impl Deref for CamlFp {
50 type Target = Fp;
51
52 fn deref(&self) -> &Self::Target {
53 &self.0
54 }
55}
56
57impl From<Fp> for CamlFp {
62 fn from(fp: Fp) -> Self {
63 CamlFp(fp)
64 }
65}
66
67impl From<&Fp> for CamlFp {
68 fn from(fp: &Fp) -> Self {
69 CamlFp(*fp)
70 }
71}
72
73impl From<CamlFp> for Fp {
74 fn from(camlfp: CamlFp) -> Fp {
75 camlfp.0
76 }
77}
78
79impl From<&CamlFp> for Fp {
80 fn from(camlfp: &CamlFp) -> Fp {
81 camlfp.0
82 }
83}
84
85impl TryFrom<CamlBigInteger256> for CamlFp {
86 type Error = ocaml::Error;
87 fn try_from(x: CamlBigInteger256) -> Result<Self, Self::Error> {
88 Fp::from_bigint(x.0)
89 .map(Into::into)
90 .ok_or(ocaml::Error::Message(
91 "TryFrom<CamlBigInteger256>: integer is larger than order",
92 ))
93 }
94}
95
96#[ocaml_gen::func]
101#[ocaml::func]
102pub fn caml_pasta_fp_size_in_bits() -> ocaml::Int {
103 Fp_params::MODULUS_BITS as isize
104}
105
106#[ocaml_gen::func]
107#[ocaml::func]
108pub fn caml_pasta_fp_size() -> CamlBigInteger256 {
109 Fp_params::MODULUS.into()
110}
111
112#[ocaml_gen::func]
117#[ocaml::func]
118pub fn caml_pasta_fp_add(x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) -> CamlFp {
119 CamlFp(x.as_ref().0 + y.as_ref().0)
120}
121
122#[ocaml_gen::func]
123#[ocaml::func]
124pub fn caml_pasta_fp_sub(x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) -> CamlFp {
125 CamlFp(x.as_ref().0 - y.as_ref().0)
126}
127
128#[ocaml_gen::func]
129#[ocaml::func]
130pub fn caml_pasta_fp_negate(x: ocaml::Pointer<CamlFp>) -> CamlFp {
131 CamlFp(-x.as_ref().0)
132}
133
134#[ocaml_gen::func]
135#[ocaml::func]
136pub fn caml_pasta_fp_mul(x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) -> CamlFp {
137 CamlFp(x.as_ref().0 * y.as_ref().0)
138}
139
140#[ocaml_gen::func]
141#[ocaml::func]
142pub fn caml_pasta_fp_div(x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) -> CamlFp {
143 CamlFp(x.as_ref().0 / y.as_ref().0)
144}
145
146#[ocaml_gen::func]
147#[ocaml::func]
148pub fn caml_pasta_fp_inv(x: ocaml::Pointer<CamlFp>) -> Option<CamlFp> {
149 x.as_ref().0.inverse().map(CamlFp)
150}
151
152#[ocaml_gen::func]
153#[ocaml::func]
154pub fn caml_pasta_fp_square(x: ocaml::Pointer<CamlFp>) -> CamlFp {
155 CamlFp(x.as_ref().0.square())
156}
157
158#[ocaml_gen::func]
159#[ocaml::func]
160pub fn caml_pasta_fp_is_square(x: ocaml::Pointer<CamlFp>) -> bool {
161 let s = x.as_ref().0.pow(Fp_params::MODULUS_MINUS_ONE_DIV_TWO);
162 s.is_zero() || s.is_one()
163}
164
165#[ocaml_gen::func]
166#[ocaml::func]
167pub fn caml_pasta_fp_sqrt(x: ocaml::Pointer<CamlFp>) -> Option<CamlFp> {
168 x.as_ref().0.sqrt().map(CamlFp)
169}
170
171#[ocaml_gen::func]
172#[ocaml::func]
173pub fn caml_pasta_fp_of_int(i: ocaml::Int) -> CamlFp {
174 CamlFp(Fp::from(i as u64))
175}
176
177#[ocaml_gen::func]
182#[ocaml::func]
183pub fn caml_pasta_fp_to_string(x: ocaml::Pointer<CamlFp>) -> String {
184 CamlBigInteger256(x.as_ref().into_bigint()).to_string()
185}
186
187#[ocaml_gen::func]
188#[ocaml::func]
189pub fn caml_pasta_fp_of_string(s: CamlBytesString) -> Result<CamlFp, ocaml::Error> {
190 let biguint = BigUint::parse_bytes(s.0, 10).ok_or(ocaml::Error::Message(
191 "caml_pasta_fp_of_string: couldn't parse input",
192 ))?;
193 let camlbigint: CamlBigInteger256 = biguint
194 .try_into()
195 .map_err(|_| ocaml::Error::Message("caml_pasta_fp_of_string: Biguint is too large"))?;
196 CamlFp::try_from(camlbigint).map_err(|_| ocaml::Error::Message("caml_pasta_fp_of_string"))
197}
198
199#[ocaml_gen::func]
204#[ocaml::func]
205pub fn caml_pasta_fp_print(x: ocaml::Pointer<CamlFp>) {
206 println!("{}", CamlBigInteger256(x.as_ref().0.into_bigint()));
207}
208
209#[ocaml_gen::func]
210#[ocaml::func]
211pub fn caml_pasta_fp_print_rust(x: ocaml::Pointer<CamlFp>) {
212 println!("{}", x.as_ref().0);
213}
214
215#[ocaml_gen::func]
216#[ocaml::func]
217pub fn caml_pasta_fp_copy(mut x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) {
218 *x.as_mut() = *y.as_ref()
219}
220
221#[ocaml_gen::func]
222#[ocaml::func]
223pub fn caml_pasta_fp_mut_add(mut x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) {
224 x.as_mut().0 += y.as_ref().0;
225}
226
227#[ocaml_gen::func]
228#[ocaml::func]
229pub fn caml_pasta_fp_mut_sub(mut x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) {
230 x.as_mut().0 -= y.as_ref().0;
231}
232
233#[ocaml_gen::func]
234#[ocaml::func]
235pub fn caml_pasta_fp_mut_mul(mut x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) {
236 x.as_mut().0 *= y.as_ref().0;
237}
238
239#[ocaml_gen::func]
240#[ocaml::func]
241pub fn caml_pasta_fp_mut_square(mut x: ocaml::Pointer<CamlFp>) {
242 x.as_mut().0.square_in_place();
243}
244
245#[ocaml_gen::func]
246#[ocaml::func]
247pub fn caml_pasta_fp_compare(x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) -> ocaml::Int {
248 match x.as_ref().0.into_bigint().cmp(&y.as_ref().0.into_bigint()) {
249 Less => -1,
250 Equal => 0,
251 Greater => 1,
252 }
253}
254
255#[ocaml_gen::func]
256#[ocaml::func]
257pub fn caml_pasta_fp_equal(x: ocaml::Pointer<CamlFp>, y: ocaml::Pointer<CamlFp>) -> bool {
258 x.as_ref().0 == y.as_ref().0
259}
260
261#[ocaml_gen::func]
262#[ocaml::func]
263pub fn caml_pasta_fp_random() -> CamlFp {
264 let fp: Fp = UniformRand::rand(&mut rand::thread_rng());
265 CamlFp(fp)
266}
267
268#[ocaml_gen::func]
269#[ocaml::func]
270pub fn caml_pasta_fp_rng(i: ocaml::Int) -> CamlFp {
271 let i: u64 = (i as u32).into();
273 let mut rng: StdRng = rand::SeedableRng::seed_from_u64(i);
274 let fp: Fp = UniformRand::rand(&mut rng);
275 CamlFp(fp)
276}
277
278#[ocaml_gen::func]
279#[ocaml::func]
280pub fn caml_pasta_fp_to_bigint(x: ocaml::Pointer<CamlFp>) -> CamlBigInteger256 {
281 CamlBigInteger256(x.as_ref().0.into_bigint())
282}
283
284#[ocaml_gen::func]
285#[ocaml::func]
286pub fn caml_pasta_fp_of_bigint(x: CamlBigInteger256) -> Result<CamlFp, ocaml::Error> {
287 Fp::from_bigint(x.0).map(CamlFp).ok_or_else(|| {
288 let err = format!(
289 "caml_pasta_fp_of_bigint was given an invalid CamlBigInteger256: {}",
290 x.0
291 );
292 ocaml::Error::Error(err.into())
293 })
294}
295
296#[ocaml_gen::func]
297#[ocaml::func]
298pub fn caml_pasta_fp_two_adic_root_of_unity() -> CamlFp {
299 let res: Fp = FftField::TWO_ADIC_ROOT_OF_UNITY;
300 CamlFp(res)
301}
302
303#[ocaml_gen::func]
304#[ocaml::func]
305pub fn caml_pasta_fp_domain_generator(log2_size: ocaml::Int) -> Result<CamlFp, ocaml::Error> {
306 Domain::new(1 << log2_size)
307 .map(|x| CamlFp(x.group_gen))
308 .ok_or(ocaml::Error::Message("caml_pasta_fp_domain_generator"))
309}
310
311#[ocaml_gen::func]
312#[ocaml::func]
313pub fn caml_pasta_fp_to_bytes(x: ocaml::Pointer<CamlFp>) -> [u8; core::mem::size_of::<Fp>()] {
314 let mut res = [0u8; core::mem::size_of::<Fp>()];
315 x.as_ref().0.serialize_compressed(&mut res[..]).unwrap();
316 res
317}
318
319#[ocaml_gen::func]
320#[ocaml::func]
321pub fn caml_pasta_fp_of_bytes(x: &[u8]) -> Result<CamlFp, ocaml::Error> {
322 let x = Fp::deserialize_compressed(x)?;
323 Ok(CamlFp(x))
324}
325
326#[ocaml_gen::func]
327#[ocaml::func]
328pub fn caml_pasta_fp_deep_copy(x: CamlFp) -> CamlFp {
329 x
330}