-
Notifications
You must be signed in to change notification settings - Fork 35
/
fp.rs
102 lines (90 loc) · 2.48 KB
/
fp.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
use constantine_sys as constantine;
use constantine_sys::bls12_381_fp;
use core::fmt::{Debug, Formatter};
use kzg::G1Fp;
#[repr(C)]
#[derive(Default, Clone, Copy)]
pub struct CtFp(pub bls12_381_fp);
impl PartialEq for CtFp {
fn eq(&self, other: &Self) -> bool {
unsafe { constantine::ctt_bls12_381_fp_is_eq(&self.0, &other.0) != 0 }
}
}
impl Debug for CtFp {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "CtFp({:?})", self.0.limbs)
}
}
impl G1Fp for CtFp {
const ONE: Self = Self(bls12_381_fp {
limbs: [
8505329371266088957,
17002214543764226050,
6865905132761471162,
8632934651105793861,
6631298214892334189,
1582556514881692819,
],
});
const ZERO: Self = Self(bls12_381_fp {
limbs: [0, 0, 0, 0, 0, 0],
});
const BLS12_381_RX_P: Self = Self(bls12_381_fp {
limbs: [
8505329371266088957,
17002214543764226050,
6865905132761471162,
8632934651105793861,
6631298214892334189,
1582556514881692819,
],
});
fn inverse(&self) -> Option<Self> {
let mut out: Self = *self;
unsafe {
constantine::ctt_bls12_381_fp_inv(&mut out.0, &self.0);
}
Some(out)
}
fn square(&self) -> Self {
let mut out: Self = Default::default();
unsafe {
constantine::ctt_bls12_381_fp_square(&mut out.0, &self.0);
}
out
}
fn double(&self) -> Self {
let mut out: Self = Default::default();
unsafe {
constantine::ctt_bls12_381_fp_double(&mut out.0, &self.0);
}
out
}
fn from_underlying_arr(arr: &[u64; 6]) -> Self {
unsafe {
Self(bls12_381_fp {
limbs: core::mem::transmute(*arr),
})
}
}
fn neg_assign(&mut self) {
unsafe {
constantine::ctt_bls12_381_fp_neg_in_place(&mut self.0);
}
}
fn mul_assign_fp(&mut self, b: &Self) {
unsafe {
constantine::ctt_bls12_381_fp_mul_in_place(&mut self.0, &b.0);
}
}
fn sub_assign_fp(&mut self, b: &Self) {
unsafe {
constantine::ctt_bls12_381_fp_sub_in_place(&mut self.0, &b.0);
}
}
fn add_assign_fp(&mut self, b: &Self) {
unsafe {
constantine::ctt_bls12_381_fp_add_in_place(&mut self.0, &b.0);
}
}
}