oblast_demo/
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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! High-level wrapper for BLS12-381 arithmetic using `blst`.

mod constants;
#[cfg(test)]
mod tests;

pub use constants::curve_order;

use blst::{blst_fp12, blst_fr, blst_scalar};
use paste::paste;

/// Field sub-group element.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub struct Fr {
    element: blst_fr,
}

impl Fr {
    pub fn from_u64(value: u64) -> Self {
        let mut point = Self::default();
        let input = vec![value, 0, 0, 0];
        unsafe {
            blst::blst_fr_from_uint64(&mut point.element, input.as_ptr());
        }
        point
    }

    pub fn from_raw(element: blst_fr) -> Self {
        Self { element }
    }

    pub fn as_u64(&self) -> u64 {
        let mut buffer = [0u64; 4];
        unsafe {
            blst::blst_uint64_from_fr(buffer.as_mut_ptr(), &self.element);
        }
        buffer[0]
    }
}

impl std::ops::Add for Fr {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        let mut sum = blst::blst_fr::default();
        unsafe {
            blst::blst_fr_add(&mut sum, &self.element, &other.element);
        }
        Self { element: sum }
    }
}

impl std::ops::AddAssign for Fr {
    fn add_assign(&mut self, other: Self) {
        *self = *self + other
    }
}

impl std::ops::Neg for Fr {
    type Output = Self;

    fn neg(self) -> Self {
        let mut result = blst::blst_fr::default();
        unsafe {
            // NOTE: boolean is conditional operation, always set `true` here.
            blst::blst_fr_cneg(&mut result, &self.element, true);
        }
        Self { element: result }
    }
}

impl std::ops::Sub for Fr {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        self + -other
    }
}

impl std::ops::Mul for Fr {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        let mut result = blst::blst_fr::default();
        unsafe {
            blst::blst_fr_mul(&mut result, &self.element, &other.element);
        }
        Self { element: result }
    }
}

impl std::ops::MulAssign for Fr {
    fn mul_assign(&mut self, other: Self) {
        *self = *self * other
    }
}

impl std::ops::Div for Fr {
    type Output = Self;

    fn div(self, other: Self) -> Self {
        let mut result = blst::blst_fr::default();
        let mut other_inverse = blst::blst_fr::default();

        unsafe {
            blst::blst_fr_eucl_inverse(&mut other_inverse, &other.element);
            blst::blst_fr_mul(&mut result, &self.element, &other_inverse);
        }
        Self { element: result }
    }
}

/// Element of the degree-12 field extension.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub struct Fp12 {
    element: blst_fp12,
}

impl std::ops::Mul for Fp12 {
    type Output = Fp12;

    fn mul(mut self, rhs: Fp12) -> Fp12 {
        unsafe {
            blst::blst_fp12_mul(&mut self.element, &self.element, &rhs.element);
        }
        self
    }
}

impl Fp12 {
    pub fn from_raw(element: blst_fp12) -> Self {
        Self { element }
    }

    pub fn final_exp(mut self) -> Self {
        unsafe {
            blst::blst_final_exp(&mut self.element, &self.element);
        }
        self
    }

    pub fn is_one(&self) -> bool {
        unsafe { blst::blst_fp12_is_one(&self.element) }
    }
}

/// Scalar for multiplying curve points.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Scalar {
    value: blst_scalar,
}

impl Scalar {
    /// Construct a `blst_scalar` instance of a `Fr` value from bytes in big-endian order.
    /// Panics if the value is not in `Fr`.
    pub fn from_fr_bytes(value: &[u8]) -> Self {
        assert!(value.len() == 32);
        let mut scalar = blst::blst_scalar::default();
        unsafe {
            blst::blst_scalar_from_bendian(&mut scalar, value.as_ptr());
            assert!(blst::blst_scalar_fr_check(&scalar));
        }
        Self { value: scalar }
    }
}

impl From<Fr> for Scalar {
    fn from(x: Fr) -> Self {
        let mut scalar = Self::default();
        unsafe {
            blst::blst_scalar_from_fr(&mut scalar.value, &x.element);
        }
        scalar
    }
}

macro_rules! define_curve_struct {
    ($struct_name:ident, $blst_name:ident, $group_name:ident, $compressed_bytes:expr) => {
        paste! {
            #[doc = "Point on the curve sub-group " $group_name "."]
            #[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
            pub struct $struct_name {
                point: blst::[<blst_ $blst_name>],
            }
        }

        paste! {
            #[doc = "Affine encoding of a point on " $group_name "."]
            #[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
            pub struct [<$struct_name Affine>] {
                point: blst::[<blst_ $blst_name _affine>],
            }

            impl From<&$struct_name> for [<$struct_name Affine>] {
                fn from(point: &$struct_name) -> Self {
                    let mut affine = Self::default();
                    unsafe {
                        blst::[<blst_ $blst_name _to_affine>](&mut affine.point, &point.point);
                    }
                    affine
                }
            }
        }

        impl $struct_name {
            /// Return the distinguished generator point.
            pub fn generator() -> Self {
                unsafe {
                    Self {
                        point: *paste! { blst::[<blst_ $blst_name _generator>]() },
                    }
                }
            }

            pub fn from_raw(point: paste! { blst::[<blst_ $blst_name>] }) -> Self {
                Self { point }
            }

            pub fn compress(&self) -> Vec<u8> {
                let mut compressed_point = vec![0; $compressed_bytes];
                let compress = paste! { blst::[<blst_ $blst_name _compress>] };
                unsafe {
                    compress(compressed_point.as_mut_ptr(), &self.point);
                }
                compressed_point
            }
        }

        impl std::fmt::Display for $struct_name {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "0x{}", hex::encode(self.compress()))
            }
        }

        impl $struct_name {
            /// Negate this point in-place.
            pub fn negate(&mut self) {
                paste! {
                    unsafe {
                        blst::[<blst_ $blst_name _cneg>](&mut self.point, true);
                    }
                }
            }
        }

        /// Unary negation.
        impl std::ops::Neg for $struct_name {
            type Output = Self;

            fn neg(mut self) -> Self {
                self.negate();
                self
            }
        }

        /// Point addition.
        impl std::ops::Add for $struct_name {
            type Output = Self;

            fn add(mut self, rhs: $struct_name) -> $struct_name {
                let add = paste! { blst::[<blst_ $blst_name _add>] };
                unsafe {
                    add(&mut self.point, &self.point, &rhs.point);
                }
                self
            }
        }

        /// Scalar multiplication.
        impl std::ops::Mul<$struct_name> for Scalar {
            type Output = $struct_name;

            fn mul(self, mut rhs: $struct_name) -> Self::Output {
                let mult = paste! { blst::[<blst_ $blst_name _mult>] };
                unsafe {
                    mult(
                        &mut rhs.point,
                        &rhs.point,
                        (&self.value.b).as_ptr(),
                        constants::MODULUS_BIT_SIZE,
                    );
                }
                rhs
            }
        }

        /// Scalar multiplication for [<$struct_name>].
        impl std::ops::Mul<$struct_name> for Fr {
            type Output = $struct_name;

            fn mul(self, rhs: $struct_name) -> Self::Output {
                Scalar::from(self) * rhs
            }
        }
    };
}

define_curve_struct!(P1, p1, G1, 48);
define_curve_struct!(P2, p2, G2, 96);

/// Check that `e(x1, x2) = e(y1, y2)`.
pub fn verify_pairings(mut x1: P1, x2: P2, y1: P1, y2: P2) -> bool {
    // Negate one of the inputs to avoid an exponentiation.
    x1.negate();

    let x1_affine = P1Affine::from(&x1);
    let x2_affine = P2Affine::from(&x2);
    let y1_affine = P1Affine::from(&y1);
    let y2_affine = P2Affine::from(&y2);

    let mut lhs = Fp12::default();
    let mut rhs = Fp12::default();

    unsafe {
        blst::blst_miller_loop(&mut lhs.element, &x2_affine.point, &x1_affine.point);
        blst::blst_miller_loop(&mut rhs.element, &y2_affine.point, &y1_affine.point);
    }
    (lhs * rhs).final_exp().is_one()
}