elliptic-curve 0.14.0

General purpose Elliptic Curve Cryptography (ECC) support, including traits and generic types for representing various elliptic curve forms, scalars, points, and public/secret keys composed thereof.
Documentation
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
//! [`MockCurve`]: incomplete simulation of a curve implementation for testing.
//!
//! This module contains a family of mock types for use with the [`Curve`] and [`CurveArithmetic`]
//! traits which can be used to a limited degree for writing tests of algorithms implemented
//! generically over curves without having to pull in a complete curve implementation.

use crate::{
    BatchNormalize, Curve, CurveAffine, CurveArithmetic, CurveGroup, Generate, PrimeCurve,
    array::typenum::U32,
    bigint::{Limb, Odd, U256, modular::Retrieve},
    ctutils,
    error::{Error, Result},
    ops::{
        Add, AddAssign, Invert, LinearCombination, Mul, MulAssign, MulByGeneratorVartime,
        MulVartime, Neg, Reduce, ShrAssign, Sub, SubAssign,
    },
    point::{AffineCoordinates, NonIdentity},
    rand_core::{TryCryptoRng, TryRng},
    scalar::{FromUintUnchecked, IsHigh},
    sec1::{CompressedPoint, FromSec1Point, ToSec1Point},
    subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption},
    zeroize::DefaultIsZeroes,
};
use core::{
    array,
    iter::{Product, Sum},
};
use ff::{Field, PrimeField};
use hex_literal::hex;
use pkcs8::AssociatedOid;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// Pseudo-coordinate for fixed-based scalar mult output
pub const PSEUDO_COORDINATE_FIXED_BASE_MUL: [u8; 32] =
    hex!("deadbeef00000000000000000000000000000000000000000000000000000001");

/// SEC1 encoded point.
pub type Sec1Point = crate::sec1::Sec1Point<MockCurve>;

/// Field element bytes.
pub type FieldBytes = crate::FieldBytes<MockCurve>;

/// Non-zero scalar value.
pub type NonZeroScalar = crate::NonZeroScalar<MockCurve>;

/// Public key.
pub type PublicKey = crate::PublicKey<MockCurve>;

/// Secret key.
pub type SecretKey = crate::SecretKey<MockCurve>;

/// Scalar value type.
pub type ScalarValue = crate::ScalarValue<MockCurve>;

/// Mock elliptic curve type useful for writing tests which require a concrete
/// curve type.
///
/// Note: this type is roughly modeled off of NIST P-256, but does not provide
/// an actual cure arithmetic implementation.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct MockCurve;

impl Curve for MockCurve {
    type FieldBytesSize = U32;
    type Uint = U256;

    const ORDER: Odd<U256> = Odd::<U256>::from_be_hex(
        "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
    );
}

impl PrimeCurve for MockCurve {}

impl CurveArithmetic for MockCurve {
    type AffinePoint = AffinePoint;
    type ProjectivePoint = ProjectivePoint;
    type Scalar = Scalar;
}

impl AssociatedOid for MockCurve {
    /// OID for NIST P-256
    const OID: pkcs8::ObjectIdentifier = pkcs8::ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7");
}

/// Example scalar type
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct Scalar(ScalarValue);

impl Field for Scalar {
    const ZERO: Self = Self(ScalarValue::ZERO);
    const ONE: Self = Self(ScalarValue::ONE);

    fn try_random<R: TryRng + ?Sized>(rng: &mut R) -> core::result::Result<Self, R::Error> {
        let mut bytes = FieldBytes::default();

        loop {
            rng.try_fill_bytes(&mut bytes)?;
            if let Some(scalar) = Self::from_repr(bytes).into() {
                return Ok(scalar);
            }
        }
    }

    fn is_zero(&self) -> Choice {
        self.0.is_zero()
    }

    fn square(&self) -> Self {
        unimplemented!();
    }

    fn double(&self) -> Self {
        self.add(self)
    }

    fn invert(&self) -> CtOption<Self> {
        unimplemented!();
    }

    fn sqrt(&self) -> CtOption<Self> {
        unimplemented!();
    }

    fn sqrt_ratio(_num: &Self, _div: &Self) -> (Choice, Self) {
        unimplemented!();
    }
}

impl PrimeField for Scalar {
    type Repr = FieldBytes;

    const MODULUS: &'static str =
        "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff";
    const NUM_BITS: u32 = 256;
    const CAPACITY: u32 = 255;
    const TWO_INV: Self = Self::ZERO; // BOGUS!
    const MULTIPLICATIVE_GENERATOR: Self = Self::ZERO; // BOGUS! Should be 7
    const S: u32 = 4;
    const ROOT_OF_UNITY: Self = Self::ZERO; // BOGUS! Should be 0xffc97f062a770992ba807ace842a3dfc1546cad004378daf0592d7fbb41e6602
    const ROOT_OF_UNITY_INV: Self = Self::ZERO; // BOGUS!
    const DELTA: Self = Self::ZERO; // BOGUS!

    fn from_repr(bytes: FieldBytes) -> CtOption<Self> {
        ScalarValue::from_bytes(&bytes).map(Self)
    }

    fn to_repr(&self) -> FieldBytes {
        self.0.to_bytes()
    }

    fn is_odd(&self) -> Choice {
        self.0.is_odd()
    }
}

impl Generate for Scalar {
    fn try_generate_from_rng<R: TryCryptoRng + ?Sized>(
        rng: &mut R,
    ) -> core::result::Result<Self, R::Error> {
        ScalarValue::try_generate_from_rng(rng).map(Self)
    }
}

impl AsRef<Scalar> for Scalar {
    fn as_ref(&self) -> &Scalar {
        self
    }
}

impl ConditionallySelectable for Scalar {
    fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
        Self(ScalarValue::conditional_select(&a.0, &b.0, choice))
    }
}

impl ConstantTimeEq for Scalar {
    fn ct_eq(&self, other: &Self) -> Choice {
        self.0.ct_eq(&other.0)
    }
}

impl ctutils::CtEq for Scalar {
    fn ct_eq(&self, other: &Self) -> ctutils::Choice {
        ctutils::CtEq::ct_eq(&self.0, &other.0)
    }
}

impl ctutils::CtSelect for Scalar {
    fn ct_select(&self, other: &Self, choice: ctutils::Choice) -> Self {
        Self(self.0.ct_select(&other.0, choice))
    }
}

impl DefaultIsZeroes for Scalar {}

impl Add<Scalar> for Scalar {
    type Output = Scalar;

    fn add(self, other: Scalar) -> Scalar {
        self.add(&other)
    }
}

impl Add<&Scalar> for Scalar {
    type Output = Scalar;

    fn add(self, other: &Scalar) -> Scalar {
        Self(self.0.add(&other.0))
    }
}

impl AddAssign<Scalar> for Scalar {
    fn add_assign(&mut self, other: Scalar) {
        *self = *self + other;
    }
}

impl AddAssign<&Scalar> for Scalar {
    fn add_assign(&mut self, other: &Scalar) {
        *self = *self + other;
    }
}

impl Sub<Scalar> for Scalar {
    type Output = Scalar;

    fn sub(self, other: Scalar) -> Scalar {
        self.sub(&other)
    }
}

impl Sub<&Scalar> for Scalar {
    type Output = Scalar;

    fn sub(self, other: &Scalar) -> Scalar {
        Self(self.0.sub(&other.0))
    }
}

impl SubAssign<Scalar> for Scalar {
    fn sub_assign(&mut self, other: Scalar) {
        *self = *self - other;
    }
}

impl SubAssign<&Scalar> for Scalar {
    fn sub_assign(&mut self, other: &Scalar) {
        *self = *self - other;
    }
}

crate::scalar_mul_impls!(MockCurve, Scalar);

impl Mul<Scalar> for Scalar {
    type Output = Scalar;
    fn mul(self, _other: Scalar) -> Scalar {
        unimplemented!();
    }
}

impl Mul<&Scalar> for Scalar {
    type Output = Scalar;

    fn mul(self, _other: &Scalar) -> Scalar {
        unimplemented!();
    }
}

impl MulAssign<Scalar> for Scalar {
    fn mul_assign(&mut self, _rhs: Scalar) {
        unimplemented!();
    }
}

impl MulAssign<&Scalar> for Scalar {
    fn mul_assign(&mut self, _rhs: &Scalar) {
        unimplemented!();
    }
}

impl Neg for Scalar {
    type Output = Scalar;

    fn neg(self) -> Scalar {
        Self(self.0.neg())
    }
}

impl ShrAssign<usize> for Scalar {
    fn shr_assign(&mut self, rhs: usize) {
        self.0 >>= rhs;
    }
}

impl Sum for Scalar {
    fn sum<I: Iterator<Item = Self>>(_iter: I) -> Self {
        unimplemented!();
    }
}

impl<'a> Sum<&'a Scalar> for Scalar {
    fn sum<I: Iterator<Item = &'a Scalar>>(_iter: I) -> Self {
        unimplemented!();
    }
}

impl Product for Scalar {
    fn product<I: Iterator<Item = Self>>(_iter: I) -> Self {
        unimplemented!();
    }
}

impl<'a> Product<&'a Scalar> for Scalar {
    fn product<I: Iterator<Item = &'a Scalar>>(_iter: I) -> Self {
        unimplemented!();
    }
}

impl Invert for Scalar {
    type Output = CtOption<Scalar>;

    fn invert(&self) -> CtOption<Scalar> {
        unimplemented!();
    }
}

impl Reduce<U256> for Scalar {
    fn reduce(w: &U256) -> Self {
        let (r, underflow) = w.borrowing_sub(&MockCurve::ORDER, Limb::ZERO);
        let underflow = Choice::from((underflow.0 >> (Limb::BITS - 1)) as u8);
        let reduced = U256::conditional_select(w, &r, !underflow);
        Self(ScalarValue::new(reduced).unwrap())
    }
}

impl Reduce<FieldBytes> for Scalar {
    fn reduce(_w: &FieldBytes) -> Self {
        todo!()
    }
}

impl Retrieve for Scalar {
    type Output = U256;

    fn retrieve(&self) -> U256 {
        self.0.to_uint()
    }
}

impl From<u64> for Scalar {
    fn from(n: u64) -> Scalar {
        Self(n.into())
    }
}

impl From<NonZeroScalar> for Scalar {
    fn from(scalar: NonZeroScalar) -> Self {
        scalar.0.into()
    }
}

impl From<ScalarValue> for Scalar {
    fn from(scalar: ScalarValue) -> Scalar {
        Self(scalar)
    }
}

impl From<Scalar> for ScalarValue {
    fn from(scalar: Scalar) -> ScalarValue {
        scalar.0
    }
}

impl From<Scalar> for U256 {
    fn from(scalar: Scalar) -> U256 {
        scalar.0.to_uint()
    }
}

impl TryFrom<Scalar> for NonZeroScalar {
    type Error = Error;

    fn try_from(scalar: Scalar) -> Result<Self> {
        NonZeroScalar::new(scalar).into_option().ok_or(Error)
    }
}

impl TryFrom<U256> for Scalar {
    type Error = Error;

    fn try_from(w: U256) -> Result<Self> {
        ScalarValue::new(w).into_option().map(Self).ok_or(Error)
    }
}

impl FromUintUnchecked for Scalar {
    type Uint = U256;

    fn from_uint_unchecked(uint: U256) -> Self {
        Self(ScalarValue::from_uint_unchecked(uint))
    }
}

impl From<Scalar> for FieldBytes {
    fn from(scalar: Scalar) -> Self {
        Self::from(&scalar)
    }
}

impl From<&Scalar> for FieldBytes {
    fn from(scalar: &Scalar) -> Self {
        scalar.to_repr()
    }
}

impl IsHigh for Scalar {
    fn is_high(&self) -> Choice {
        self.0.is_high()
    }
}

/// Example affine point type
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
pub enum AffinePoint {
    /// Result of fixed-based scalar multiplication.
    FixedBaseOutput(Scalar),

    /// Identity.
    #[default]
    Identity,

    /// Base point.
    Generator,

    /// Point corresponding to a given [`Sec1Point`].
    Other(Sec1Point),
}

impl AffineCoordinates for AffinePoint {
    type FieldRepr = FieldBytes;

    fn from_coordinates(_: &FieldBytes, _: &FieldBytes) -> CtOption<Self> {
        unimplemented!();
    }

    fn x(&self) -> FieldBytes {
        unimplemented!();
    }

    fn y(&self) -> FieldBytes {
        unimplemented!();
    }

    fn x_is_odd(&self) -> Choice {
        unimplemented!();
    }

    fn y_is_odd(&self) -> Choice {
        unimplemented!();
    }
}

impl CurveAffine for AffinePoint {
    type Curve = ProjectivePoint;
    type Scalar = Scalar;

    fn identity() -> Self {
        Self::Identity
    }

    fn generator() -> Self {
        Self::Generator
    }

    fn is_identity(&self) -> Choice {
        unimplemented!();
    }

    fn to_curve(&self) -> ProjectivePoint {
        unimplemented!();
    }
}

impl ConstantTimeEq for AffinePoint {
    fn ct_eq(&self, other: &Self) -> Choice {
        match (self, other) {
            (Self::FixedBaseOutput(scalar), Self::FixedBaseOutput(other_scalar)) => {
                scalar.ct_eq(other_scalar)
            }
            (Self::Identity, Self::Identity) | (Self::Generator, Self::Generator) => 1.into(),
            (Self::Other(point), Self::Other(other_point)) => u8::from(point == other_point).into(),
            _ => 0.into(),
        }
    }
}

impl ConditionallySelectable for AffinePoint {
    fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
        ctutils::CtSelect::ct_select(a, b, choice.into())
    }
}

impl ctutils::CtEq for AffinePoint {
    fn ct_eq(&self, other: &Self) -> ctutils::Choice {
        ConstantTimeEq::ct_eq(self, other).into()
    }
}

impl ctutils::CtSelect for AffinePoint {
    fn ct_select(&self, other: &Self, choice: ctutils::Choice) -> Self {
        // Not really constant time, but this is dev code
        if choice.to_bool() { *other } else { *self }
    }
}

impl DefaultIsZeroes for AffinePoint {}

impl From<NonIdentity<AffinePoint>> for AffinePoint {
    fn from(affine: NonIdentity<AffinePoint>) -> Self {
        affine.to_point()
    }
}

impl Generate for AffinePoint {
    fn try_generate_from_rng<R: TryCryptoRng + ?Sized>(
        _rng: &mut R,
    ) -> core::result::Result<Self, R::Error> {
        unimplemented!()
    }
}

impl FromSec1Point<MockCurve> for AffinePoint {
    fn from_sec1_point(encoded_point: &Sec1Point) -> ctutils::CtOption<Self> {
        let point = if encoded_point.is_identity() {
            Self::Identity
        } else {
            Self::Other(*encoded_point)
        };

        ctutils::CtOption::new(point, ctutils::Choice::TRUE)
    }
}

impl ToSec1Point<MockCurve> for AffinePoint {
    fn to_sec1_point(&self, compress: bool) -> Sec1Point {
        match self {
            Self::FixedBaseOutput(scalar) => Sec1Point::from_affine_coordinates(
                &scalar.to_repr(),
                &PSEUDO_COORDINATE_FIXED_BASE_MUL.into(),
                false,
            ),
            Self::Other(point) => {
                if compress == point.is_compressed() {
                    *point
                } else {
                    unimplemented!();
                }
            }
            _ => unimplemented!(),
        }
    }
}

impl Mul<Scalar> for AffinePoint {
    type Output = ProjectivePoint;

    fn mul(self, _scalar: Scalar) -> ProjectivePoint {
        unimplemented!();
    }
}

impl Mul<&Scalar> for AffinePoint {
    type Output = ProjectivePoint;

    fn mul(self, _scalar: &Scalar) -> ProjectivePoint {
        unimplemented!();
    }
}

impl MulVartime<Scalar> for AffinePoint {
    fn mul_vartime(self, _scalar: Scalar) -> ProjectivePoint {
        unimplemented!()
    }
}

impl MulVartime<&Scalar> for AffinePoint {
    fn mul_vartime(self, _scalar: &Scalar) -> ProjectivePoint {
        unimplemented!()
    }
}

impl Mul<NonZeroScalar> for AffinePoint {
    type Output = AffinePoint;

    fn mul(self, _scalar: NonZeroScalar) -> Self {
        unimplemented!();
    }
}

impl Neg for AffinePoint {
    type Output = Self;

    fn neg(self) -> Self {
        unimplemented!();
    }
}

impl TryFrom<AffinePoint> for NonIdentity<AffinePoint> {
    type Error = Error;

    fn try_from(affine: AffinePoint) -> Result<Self> {
        NonIdentity::new(affine).into_option().ok_or(Error)
    }
}

/// Example projective point type
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
pub enum ProjectivePoint {
    /// Result of fixed-based scalar multiplication
    FixedBaseOutput(Scalar),

    /// Is this point the identity point?
    #[default]
    Identity,

    /// Is this point the generator point?
    Generator,

    /// Is this point a different point corresponding to a given [`AffinePoint`]
    Other(AffinePoint),
}

impl<const N: usize> BatchNormalize<[ProjectivePoint; N]> for ProjectivePoint {
    type Output = [AffinePoint; N];

    fn batch_normalize(points: &[ProjectivePoint; N]) -> [AffinePoint; N] {
        array::from_fn(|index| points[index].into())
    }
}

#[cfg(feature = "alloc")]
impl BatchNormalize<[ProjectivePoint]> for ProjectivePoint {
    type Output = Vec<AffinePoint>;

    fn batch_normalize(points: &[ProjectivePoint]) -> Vec<AffinePoint> {
        points.iter().copied().map(AffinePoint::from).collect()
    }
}

impl ConstantTimeEq for ProjectivePoint {
    fn ct_eq(&self, other: &Self) -> Choice {
        match (self, other) {
            (Self::FixedBaseOutput(scalar), Self::FixedBaseOutput(other_scalar)) => {
                scalar.ct_eq(other_scalar)
            }
            (Self::Identity, Self::Identity) | (Self::Generator, Self::Generator) => 1.into(),
            (Self::Other(point), Self::Other(other_point)) => point.ct_eq(other_point),
            _ => 0.into(),
        }
    }
}

impl ConditionallySelectable for ProjectivePoint {
    fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
        ctutils::CtSelect::ct_select(a, b, choice.into())
    }
}

impl ctutils::CtEq for ProjectivePoint {
    fn ct_eq(&self, other: &Self) -> ctutils::Choice {
        ConstantTimeEq::ct_eq(self, other).into()
    }
}

impl ctutils::CtSelect for ProjectivePoint {
    fn ct_select(&self, other: &Self, choice: ctutils::Choice) -> Self {
        if choice.to_bool() { *other } else { *self }
    }
}

impl DefaultIsZeroes for ProjectivePoint {}

impl From<AffinePoint> for ProjectivePoint {
    fn from(point: AffinePoint) -> ProjectivePoint {
        match point {
            AffinePoint::FixedBaseOutput(scalar) => ProjectivePoint::FixedBaseOutput(scalar),
            AffinePoint::Identity => ProjectivePoint::Identity,
            AffinePoint::Generator => ProjectivePoint::Generator,
            other => ProjectivePoint::Other(other),
        }
    }
}

impl From<NonIdentity<ProjectivePoint>> for ProjectivePoint {
    fn from(point: NonIdentity<ProjectivePoint>) -> Self {
        point.to_point()
    }
}

impl From<ProjectivePoint> for AffinePoint {
    fn from(point: ProjectivePoint) -> AffinePoint {
        CurveGroup::to_affine(&point)
    }
}

impl Generate for ProjectivePoint {
    fn try_generate_from_rng<R: TryCryptoRng + ?Sized>(
        _rng: &mut R,
    ) -> core::result::Result<Self, R::Error> {
        unimplemented!()
    }
}

impl FromSec1Point<MockCurve> for ProjectivePoint {
    fn from_sec1_point(_point: &Sec1Point) -> ctutils::CtOption<Self> {
        unimplemented!();
    }
}

impl ToSec1Point<MockCurve> for ProjectivePoint {
    fn to_sec1_point(&self, _compress: bool) -> Sec1Point {
        unimplemented!();
    }
}

impl TryFrom<ProjectivePoint> for NonIdentity<ProjectivePoint> {
    type Error = Error;

    fn try_from(point: ProjectivePoint) -> Result<Self> {
        NonIdentity::new(point).into_option().ok_or(Error)
    }
}

impl group::Group for ProjectivePoint {
    type Scalar = Scalar;

    fn try_random<R: TryRng + ?Sized>(_rng: &mut R) -> core::result::Result<Self, R::Error> {
        unimplemented!();
    }

    fn identity() -> Self {
        Self::Identity
    }

    fn generator() -> Self {
        Self::Generator
    }

    fn is_identity(&self) -> Choice {
        Choice::from(u8::from(self == &Self::Identity))
    }

    fn double(&self) -> Self {
        unimplemented!();
    }
}

impl group::GroupEncoding for AffinePoint {
    type Repr = CompressedPoint<MockCurve>;

    #[allow(clippy::map_unwrap_or)]
    fn from_bytes(bytes: &Self::Repr) -> CtOption<Self> {
        Sec1Point::from_bytes(bytes)
            .map(|point| ctutils::CtOption::new(point, ctutils::Choice::TRUE))
            .unwrap_or_else(|_| {
                let is_identity =
                    ctutils::CtEq::ct_eq(bytes.as_slice(), Self::Repr::default().as_slice());
                ctutils::CtOption::new(Sec1Point::identity(), is_identity)
            })
            .and_then(|point| Self::from_sec1_point(&point))
            .into()
    }

    fn from_bytes_unchecked(bytes: &Self::Repr) -> CtOption<Self> {
        Self::from_bytes(bytes)
    }

    fn to_bytes(&self) -> Self::Repr {
        let encoded = self.to_sec1_point(true);
        let mut result = CompressedPoint::<MockCurve>::default();
        result[..encoded.len()].copy_from_slice(encoded.as_bytes());
        result
    }
}

impl group::GroupEncoding for ProjectivePoint {
    type Repr = CompressedPoint<MockCurve>;

    fn from_bytes(bytes: &Self::Repr) -> CtOption<Self> {
        <AffinePoint as group::GroupEncoding>::from_bytes(bytes).map(Into::into)
    }

    fn from_bytes_unchecked(bytes: &Self::Repr) -> CtOption<Self> {
        Self::from_bytes(bytes)
    }

    fn to_bytes(&self) -> Self::Repr {
        CurveGroup::to_affine(self).to_bytes()
    }
}

impl CurveGroup for ProjectivePoint {
    type Affine = AffinePoint;

    fn to_affine(&self) -> AffinePoint {
        match self {
            Self::FixedBaseOutput(scalar) => AffinePoint::FixedBaseOutput(*scalar),
            Self::Other(affine) => *affine,
            _ => unimplemented!(),
        }
    }
}

impl LinearCombination<[(ProjectivePoint, Scalar)]> for ProjectivePoint {}
impl<const N: usize> LinearCombination<[(ProjectivePoint, Scalar); N]> for ProjectivePoint {}

impl Add<ProjectivePoint> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn add(self, _other: ProjectivePoint) -> ProjectivePoint {
        unimplemented!();
    }
}

impl Add<&ProjectivePoint> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn add(self, _other: &ProjectivePoint) -> ProjectivePoint {
        unimplemented!();
    }
}

impl AddAssign<ProjectivePoint> for ProjectivePoint {
    fn add_assign(&mut self, _rhs: ProjectivePoint) {
        unimplemented!();
    }
}

impl AddAssign<&ProjectivePoint> for ProjectivePoint {
    fn add_assign(&mut self, _rhs: &ProjectivePoint) {
        unimplemented!();
    }
}

impl Sub<ProjectivePoint> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn sub(self, _other: ProjectivePoint) -> ProjectivePoint {
        unimplemented!();
    }
}

impl Sub<&ProjectivePoint> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn sub(self, _other: &ProjectivePoint) -> ProjectivePoint {
        unimplemented!();
    }
}

impl SubAssign<ProjectivePoint> for ProjectivePoint {
    fn sub_assign(&mut self, _rhs: ProjectivePoint) {
        unimplemented!();
    }
}

impl SubAssign<&ProjectivePoint> for ProjectivePoint {
    fn sub_assign(&mut self, _rhs: &ProjectivePoint) {
        unimplemented!();
    }
}

impl Add<AffinePoint> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn add(self, _other: AffinePoint) -> ProjectivePoint {
        unimplemented!();
    }
}

impl Add<&AffinePoint> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn add(self, _other: &AffinePoint) -> ProjectivePoint {
        unimplemented!();
    }
}

impl AddAssign<AffinePoint> for ProjectivePoint {
    fn add_assign(&mut self, _rhs: AffinePoint) {
        unimplemented!();
    }
}

impl AddAssign<&AffinePoint> for ProjectivePoint {
    fn add_assign(&mut self, _rhs: &AffinePoint) {
        unimplemented!();
    }
}

impl Sum for ProjectivePoint {
    fn sum<I: Iterator<Item = Self>>(_iter: I) -> Self {
        unimplemented!();
    }
}

impl<'a> Sum<&'a ProjectivePoint> for ProjectivePoint {
    fn sum<I: Iterator<Item = &'a ProjectivePoint>>(_iter: I) -> Self {
        unimplemented!();
    }
}

impl Sub<AffinePoint> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn sub(self, _other: AffinePoint) -> ProjectivePoint {
        unimplemented!();
    }
}

impl Sub<&AffinePoint> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn sub(self, _other: &AffinePoint) -> ProjectivePoint {
        unimplemented!();
    }
}

impl SubAssign<AffinePoint> for ProjectivePoint {
    fn sub_assign(&mut self, _rhs: AffinePoint) {
        unimplemented!();
    }
}

impl SubAssign<&AffinePoint> for ProjectivePoint {
    fn sub_assign(&mut self, _rhs: &AffinePoint) {
        unimplemented!();
    }
}

impl Mul<Scalar> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn mul(self, scalar: Scalar) -> ProjectivePoint {
        match self {
            Self::Generator => Self::FixedBaseOutput(scalar),
            _ => unimplemented!(),
        }
    }
}

impl Mul<&Scalar> for ProjectivePoint {
    type Output = ProjectivePoint;

    fn mul(self, scalar: &Scalar) -> ProjectivePoint {
        self * *scalar
    }
}

impl Mul<&Scalar> for &ProjectivePoint {
    type Output = ProjectivePoint;

    fn mul(self, _scalar: &Scalar) -> ProjectivePoint {
        unimplemented!();
    }
}

impl MulByGeneratorVartime for ProjectivePoint {}

impl MulVartime<Scalar> for ProjectivePoint {
    fn mul_vartime(self, _scalar: Scalar) -> ProjectivePoint {
        unimplemented!()
    }
}

impl MulVartime<&Scalar> for ProjectivePoint {
    fn mul_vartime(self, _scalar: &Scalar) -> ProjectivePoint {
        unimplemented!()
    }
}

impl MulAssign<Scalar> for ProjectivePoint {
    fn mul_assign(&mut self, _rhs: Scalar) {
        unimplemented!();
    }
}

impl MulAssign<&Scalar> for ProjectivePoint {
    fn mul_assign(&mut self, _rhs: &Scalar) {
        unimplemented!();
    }
}

impl Neg for ProjectivePoint {
    type Output = ProjectivePoint;

    fn neg(self) -> ProjectivePoint {
        unimplemented!();
    }
}

#[cfg(test)]
mod tests {
    use super::Scalar;
    use ff::PrimeField;
    use hex_literal::hex;

    #[test]
    fn round_trip() {
        let bytes = hex!("c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721");
        let scalar = Scalar::from_repr(bytes.into()).unwrap();
        assert_eq!(&bytes, scalar.to_repr().as_slice());
    }
}