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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(warnings, unused)]

use hacspec_lib::*;

#[derive(Copy, Clone)]
pub struct WeiestrassCurve<T> {
    a1: T,
    a2: T,
    a3: T,
    a4: T,
    a5: T,
    a6: T,
    bit_size_of_scalar_field: usize,
}

impl<T> WeiestrassCurve<T> {
    /// Create a weierstrass curve
    ///
    /// The form of the weierstrass curve is:
    /// Y^2+a_1XY+a_3Y = X^3+a_2X^2+a_4X+a_6
    ///
    /// # Arguments
    ///
    /// * `a1`
    /// * `a2`
    /// * `a3`
    /// * `a4`
    /// * `a5`
    /// * `a6`
    /// * `bit_size_of_scalar_field` - the size of the scalar field used for scalar multiplication
    pub fn new(
        a1: T,
        a2: T,
        a3: T,
        a4: T,
        a5: T,
        a6: T,
        bit_size_of_scaler_field: usize,
    ) -> WeiestrassCurve<T> {
        (WeiestrassCurve {
            a1,
            a2,
            a3,
            a4,
            a5,
            a6,
            bit_size_of_scalar_field: bit_size_of_scaler_field,
        })
    }
}

/// Create a default instance of 'WeiestrassCurve<T>' with all values set to 0
impl<T: Integer> Default for WeiestrassCurve<T> {
    fn default() -> Self {
        WeiestrassCurve {
            a1: T::default(),
            a2: T::default(),
            a3: T::default(),
            a4: T::default(),
            a5: T::default(),
            a6: T::default(),
            bit_size_of_scalar_field: 0,
        }
    }
}
#[derive(Copy, Clone)]
pub struct EllipticCurvePoint<T> {
    x: T,
    y: T,
    isPointAtInfinity: bool,
    curve: WeiestrassCurve<T>,
}

impl<T: Numeric + NumericCopy + PartialEq + Integer + hacspec_lib::Div<Output = T>>
    EllipticCurvePoint<T>
{
    /// Create point on a weierstrass curve.
    /// If given point is not on the curve, the point at infinity will be created instead.
    ///
    /// # Arguments
    ///
    /// * `x` - the x-coordinate
    /// * `y` - the y-coordinate
    /// * `isPointAtInfinity` - the size of the scalar field used for scalar multiplication
    /// * 'curve' - The 'WeiestrassCurve<T>' object defining the curve
    pub fn new(
        x: T,
        y: T,
        isPointAtInfinity: bool,
        curve: WeiestrassCurve<T>,
    ) -> EllipticCurvePoint<T> {
        let point = (EllipticCurvePoint {
            x,
            y,
            isPointAtInfinity,
            curve,
        });
        if point.is_on_curve() {
            return point;
        }
        (EllipticCurvePoint {
            x: T::default(),
            y: T::default(),
            isPointAtInfinity: true,
            curve,
        })
    }
    ///Checks if point is identity/point at infinity
    pub fn is_identity(self) -> bool {
        self.isPointAtInfinity
    }
    ///Doubles point
    pub fn double(self) -> EllipticCurvePoint<T> {
        let curve = self.curve;
        if self.isPointAtInfinity {
            self
        } else {
            let x12 = self.x.exp(2u32);
            let lambda = (T::from_literal(3u128) * x12 + T::TWO() * curve.a2 * self.x
                - curve.a1 * self.y
                + curve.a4)
                / (T::TWO() * self.y + curve.a1 * self.x + curve.a3);
            let x3 = lambda.exp(2u32) + lambda * curve.a1 - curve.a2 - T::TWO() * self.x;
            let y3 = lambda * (self.x - x3) - self.y - curve.a1 * self.x - curve.a3;
            EllipticCurvePoint {
                x: x3,
                y: y3,
                isPointAtInfinity: false,
                curve: self.curve,
            }
        }
    }
    /// negates point as (x,-y)
    pub fn neg(self) -> EllipticCurvePoint<T> {
        EllipticCurvePoint {
            x: self.x,
            y: T::ZERO() - self.y,
            isPointAtInfinity: self.isPointAtInfinity,
            curve: self.curve,
        }
    }
    /// Checks if point is on the curve
    /// point is on the curve IFF
    /// the point satisfies the Weiestrass equation:
    /// Yˆ2 + a_1XY + a_3Y = xˆ3 + a_2Xˆ2 + a_4X + a_6
    /// or it is the point at infinity
    pub fn is_on_curve(self) -> bool {
        let curve = self.curve;
        let x = self.x;
        let y = self.y;
        let on_curve = y * y + curve.a1 * x * y + curve.a3 * y
            == x * x * x + curve.a2 * x * x + curve.a4 * x + curve.a6;

        (on_curve) || self.isPointAtInfinity
    }
}

impl<T: Numeric + NumericCopy + PartialEq + Integer + hacspec_lib::Div<Output = T>>
    Add<EllipticCurvePoint<T>> for EllipticCurvePoint<T>
{
    type Output = Self;
    /// Adds two curve points
    fn add(self, other: EllipticCurvePoint<T>) -> Self {
        let curve = self.curve;
        if self.isPointAtInfinity {
            return other;
        }
        if other.isPointAtInfinity {
            return self;
        }
        if self == other {
            return self.double();
        }
        if self == other.neg() {
            return EllipticCurvePoint {
                x: T::default(),
                y: T::default(),
                isPointAtInfinity: true,
                curve: self.curve,
            };
        }
        let x_diff = other.x - self.x;
        let y_diff = other.y - self.y;
        let lambda = y_diff / x_diff;

        let x3 = lambda.exp(2u32) + curve.a1 * lambda - curve.a2 - self.x - other.x;
        let y3 = lambda * self.x - curve.a1 * x3 - curve.a3 - lambda * x3 - self.y;

        return EllipticCurvePoint {
            x: x3,
            y: y3,
            isPointAtInfinity: false,
            curve: self.curve,
        };
    }
}

impl<
        U: Numeric + NumericCopy + PartialEq + Integer + hacspec_lib::Div<Output = U>,
        T: Numeric + NumericCopy + PartialEq + Integer + hacspec_lib::Div<Output = T>,
    > Mul<U> for EllipticCurvePoint<T>
{
    type Output = Self;
    /// Scalar multiplication as G * m
    /// G is curve point and m is scalar
    fn mul(self, rhs: U) -> Self::Output {
        if self.isPointAtInfinity {
            return self;
        }
        let mut t = self;
        t.x = T::default();
        t.y = T::default();
        t.isPointAtInfinity = true;
        let bit_size_of_field = self.curve.bit_size_of_scalar_field;
        for i in 0..self.curve.bit_size_of_scalar_field {
            t = t.double();
            //starting from second most significant bit
            if rhs.get_bit(bit_size_of_field - 1 - i) == U::ONE() {
                t = t.add(self);
            }
        }
        t
    }
}

impl<T: Numeric + NumericCopy + PartialEq + Integer> PartialEq for EllipticCurvePoint<T> {
    /// Checks if two curve points are identical, regardless of underlying curve.
    fn eq(&self, other: &Self) -> bool {
        if self.x != other.x {
            return false;
        }
        if self.y != other.y {
            return false;
        }
        if self.isPointAtInfinity != other.isPointAtInfinity {
            return false;
        }
        return true;
    }
}

impl<T: Numeric + NumericCopy + Integer> Default for EllipticCurvePoint<T> {
    /// Creates default instance of 'EllipticCurvePoint<T>' as point at infinity
    /// The assosiated curve is set to be the default, which is all 0.
    ///
    fn default() -> Self {
        EllipticCurvePoint {
            x: T::default(),
            y: T::default(),
            isPointAtInfinity: true,
            curve: WeiestrassCurve::default(),
        }
    }
}

#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
#[macro_use(quickcheck)]
extern crate quickcheck_macros;
#[cfg(test)]
use quickcheck::*;
#[cfg(test)]
public_nat_mod!(
    type_name: FpPallas,
    type_of_canvas: PallasCanvas,
    bit_size_of_field: 255,
    modulo_value: "40000000000000000000000000000000224698FC094CF91B992D30ED00000001"
);
#[cfg(test)]
public_nat_mod!(
    type_name: FpVesta,
    type_of_canvas: VestaCanvas,
    bit_size_of_field: 255,
    modulo_value: "40000000000000000000000000000000224698FC0994A8DD8C46EB2100000001"
);

// Only needed for test/Arbitrary (and mut refs are not supported)
#[cfg(test)]
impl<T: Numeric + NumericCopy + PartialEq + Integer + hacspec_lib::Div<Output = T>> Debug
    for EllipticCurvePoint<T>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", (self.x, self.y, self.isPointAtInfinity))
    }
}

#[cfg(test)]
impl Arbitrary for EllipticCurvePoint<FpPallas> {
    fn arbitrary(g: &mut Gen) -> EllipticCurvePoint<FpPallas> {
        let a = FpPallas::from_literal(u128::arbitrary(g));
        let generator = g1_generator_pallas();
        let point = generator * a;
        point
    }
}

#[cfg(test)]
impl Arbitrary for FpVesta {
    fn arbitrary(g: &mut Gen) -> FpVesta {
        let mut a: [u64; 4] = [0; 4];
        for i in 0..4 {
            a[i] = u64::arbitrary(g);
        }
        let mut b: [u8; 32] = [0; 32];
        for i in 0..4 {
            let val: u64 = a[i];
            b[(i * 8)..((i + 1) * 8)].copy_from_slice(&(val.to_le_bytes()));
        }
        b[31] = b[31] & 127;
        FpVesta::from_byte_seq_le(Seq::<U8>::from_public_slice(&b))
    }
}

#[cfg(test)]
pub fn g1_generator_pallas() -> EllipticCurvePoint<FpPallas> {
    EllipticCurvePoint {
        x: FpPallas::from_hex("1"),
        y: FpPallas::from_hex("1B74B5A30A12937C53DFA9F06378EE548F655BD4333D477119CF7A23CAED2ABB"),
        isPointAtInfinity: false,
        curve: WeiestrassCurve {
            a1: FpPallas::ZERO(),
            a2: FpPallas::ZERO(),
            a3: FpPallas::ZERO(),
            a4: FpPallas::ZERO(),
            a5: FpPallas::ZERO(),
            a6: FpPallas::from_literal(5),
            bit_size_of_scalar_field: 255,
        },
    }
}

#[cfg(test)]
pub fn identity_pallas() -> EllipticCurvePoint<FpPallas> {
    EllipticCurvePoint {
        x: FpPallas::ZERO(),
        y: FpPallas::ZERO(),
        isPointAtInfinity: true,
        curve: WeiestrassCurve {
            a1: FpPallas::ZERO(),
            a2: FpPallas::ZERO(),
            a3: FpPallas::ZERO(),
            a4: FpPallas::ZERO(),
            a5: FpPallas::ZERO(),
            a6: FpPallas::from_literal(5),
            bit_size_of_scalar_field: 255,
        },
    }
}

#[cfg(test)]
#[test]
fn test_g1_arithmetic_vesta() {
    let g = g1_generator_pallas();
    let g2 = g.double();
    let g4 = g2.double();
    let g3 = g2 + g;
    let g4b = g3 + g;
    assert_eq!(g4b, g4);
}

#[cfg(test)]
#[quickcheck]
fn test_closure(a: EllipticCurvePoint<FpPallas>, b: EllipticCurvePoint<FpPallas>) {
    let sum = a + b;
    assert!(sum.is_on_curve());
}

#[cfg(test)]
#[quickcheck]
fn test_associativity(
    a: EllipticCurvePoint<FpPallas>,
    b: EllipticCurvePoint<FpPallas>,
    c: EllipticCurvePoint<FpPallas>,
) {
    let sum1 = (a + b) + c;
    let sum2 = (b + c) + a;
    assert_eq!(sum1, sum2);
}

#[cfg(test)]
#[quickcheck]
fn test_identity(a: EllipticCurvePoint<FpPallas>) {
    use std::convert::identity;

    let identity = EllipticCurvePoint::default();
    let sum = a + identity;
    println!("{:?}", sum);
    println!("{:?}", a);

    assert_eq!(sum, a);
}

#[cfg(test)]
#[quickcheck]
fn test_inverse(a: EllipticCurvePoint<FpPallas>) {
    let a_neg = a.neg();

    let sum = a + a_neg;

    assert!(sum.isPointAtInfinity);
}

#[cfg(test)]
#[test]
fn test_mul_standard() {
    let g = g1_generator_pallas();
    let m = FpVesta::ONE();
    assert_eq!(g, g * m);
    let m = FpVesta::from_literal(2u128);
    let g2 = g.double();
    assert_eq!(g2, g * m);
    let m = FpVesta::from_literal(3u128);
    let g3 = g + g2;
    assert_eq!(g3, g * m);
}

#[cfg(test)]
#[test]
fn test_mul_zero() {
    let g = g1_generator_pallas();
    let m = FpVesta::ZERO();
    let h = g * m;
    assert!(h.isPointAtInfinity);
}

#[cfg(test)]
#[test]
fn test_mul() {
    fn test_g1_mul_pallas(a: FpVesta) -> bool {
        let g = g1_generator_pallas() * a;
        let r = FpVesta::from_hex("0"); //r
        let h = g * r;
        h.isPointAtInfinity
    }
    //Only needing 5 successes, slow because affine
    QuickCheck::new()
        .tests(5)
        .quickcheck(test_g1_mul_pallas as fn(FpVesta) -> bool);
}

#[cfg(test)]
#[test]
fn test_g1_add_double_equiv_pallas() {
    fn test_g1_mul_pallas(a: FpVesta) -> bool {
        let g = g1_generator_pallas() * a;
        g + g == g.double()
    }
    //Only needing 5 successes, slow because affine
    QuickCheck::new()
        .tests(5)
        .quickcheck(test_g1_mul_pallas as fn(FpVesta) -> bool);
}

#[cfg(test)]
#[test]
fn test_add_double_special_case() {
    let g = EllipticCurvePoint {
        x: FpPallas::TWO(),
        y: FpPallas::ZERO(),
        isPointAtInfinity: true,
        curve: WeiestrassCurve {
            a1: FpPallas::ZERO(),
            a2: FpPallas::ZERO(),
            a3: FpPallas::ZERO(),
            a4: FpPallas::ZERO(),
            a5: FpPallas::ZERO(),
            a6: FpPallas::from_literal(5),
            bit_size_of_scalar_field: 255,
        },
    };
    assert_eq!(g + g, g.double());
}