@@ -6,7 +6,11 @@ use elliptic_curve::{
6
6
CurveGroup , Error , Group ,
7
7
array:: Array ,
8
8
consts:: U56 ,
9
- group:: { GroupEncoding , cofactor:: CofactorGroup , prime:: PrimeGroup } ,
9
+ group:: {
10
+ GroupEncoding ,
11
+ cofactor:: CofactorGroup ,
12
+ prime:: { PrimeCurve , PrimeGroup } ,
13
+ } ,
10
14
ops:: LinearCombination ,
11
15
point:: NonIdentity ,
12
16
} ;
@@ -239,6 +243,10 @@ impl CofactorGroup for DecafPoint {
239
243
240
244
impl PrimeGroup for DecafPoint { }
241
245
246
+ impl PrimeCurve for DecafPoint {
247
+ type Affine = DecafAffinePoint ;
248
+ }
249
+
242
250
impl < const N : usize > LinearCombination < [ ( DecafPoint , DecafScalar ) ; N ] > for DecafPoint { }
243
251
244
252
impl LinearCombination < [ ( DecafPoint , DecafScalar ) ] > for DecafPoint { }
@@ -535,43 +543,7 @@ impl CompressedDecaf {
535
543
536
544
/// Decompress a point if it is valid
537
545
pub fn decompress ( & self ) -> CtOption < DecafPoint > {
538
- let s = FieldElement :: from_bytes ( & self . 0 ) ;
539
- //XX: Check for canonical encoding and sign,
540
- // Copied this check from Dalek: The From_bytes function does not throw an error, if the bytes exceed the prime.
541
- // However, to_bytes reduces the Field element before serialising
542
- // So we can use to_bytes -> from_bytes and if the representations are the same, then the element was already in reduced form
543
- let s_bytes_check = s. to_bytes ( ) ;
544
- let s_encoding_is_canonical = s_bytes_check[ ..] . ct_eq ( & self . 0 ) ;
545
- let s_is_negative = s. is_negative ( ) ;
546
- // if s_encoding_is_canonical.unwrap_u8() == 0u8 || s.is_negative().unwrap_u8() == 1u8 {
547
- // return None;
548
- // }
549
-
550
- let ss = s. square ( ) ;
551
- let u1 = FieldElement :: ONE - ss;
552
- let u2 = FieldElement :: ONE + ss;
553
- let u1_sqr = u1. square ( ) ;
554
-
555
- let v = ss * ( FieldElement :: NEG_FOUR_TIMES_TWISTED_D ) + u1_sqr; // XXX: constantify please
556
-
557
- let ( I , ok) = ( v * u1_sqr) . inverse_square_root ( ) ;
558
-
559
- let Dx = I * u1;
560
- let Dxs = s. double ( ) * Dx ;
561
-
562
- let mut X = ( Dxs * I ) * v;
563
- let k = Dxs * FieldElement :: DECAF_FACTOR ;
564
- X . conditional_negate ( k. is_negative ( ) ) ;
565
-
566
- let Y = Dx * u2;
567
- let Z = FieldElement :: ONE ;
568
- let T = X * Y ;
569
- let pt = ExtendedPoint { X , Y , Z , T } ;
570
-
571
- CtOption :: new (
572
- DecafPoint ( pt) ,
573
- ok & pt. is_on_curve ( ) & s_encoding_is_canonical & !s_is_negative,
574
- )
546
+ DecafAffinePoint :: from_bytes ( ( & self . 0 ) . into ( ) ) . map ( |pt| pt. to_decaf ( ) )
575
547
}
576
548
577
549
/// Get the bytes of this compressed point
@@ -633,109 +605,118 @@ mod test {
633
605
#[ test]
634
606
fn test_vectors_lib_decaf ( ) {
635
607
// Testing small multiples of basepoint. Taken from reference implementation.
636
- let compressed = [
608
+ const COMPRESSED : [ DecafPointBytes ; 16 ] = [
637
609
// Taken from libdecaf, where they were computed using SAGE script
638
- CompressedDecaf ( [
610
+ [
639
611
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
640
612
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
641
- ] ) ,
642
- CompressedDecaf ( [
613
+ ] ,
614
+ [
643
615
102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 ,
644
616
102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 51 , 51 , 51 , 51 , 51 , 51 ,
645
617
51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 , 51 ,
646
618
51 ,
647
- ] ) ,
648
- CompressedDecaf ( [
619
+ ] ,
620
+ [
649
621
200 , 152 , 235 , 79 , 135 , 249 , 124 , 86 , 76 , 111 , 214 , 31 , 199 , 228 , 150 , 137 , 49 , 74 ,
650
622
31 , 129 , 142 , 200 , 94 , 235 , 59 , 213 , 81 , 74 , 200 , 22 , 211 , 135 , 120 , 246 , 158 , 243 ,
651
623
71 , 168 , 159 , 202 , 129 , 126 , 102 , 222 , 253 , 237 , 206 , 23 , 140 , 124 , 199 , 9 , 178 ,
652
624
17 , 110 , 117 ,
653
- ] ) ,
654
- CompressedDecaf ( [
625
+ ] ,
626
+ [
655
627
160 , 192 , 155 , 242 , 186 , 114 , 8 , 253 , 160 , 244 , 191 , 227 , 208 , 245 , 178 , 154 , 84 ,
656
628
48 , 18 , 48 , 109 , 67 , 131 , 27 , 90 , 220 , 111 , 231 , 248 , 89 , 111 , 163 , 8 , 118 , 61 ,
657
629
177 , 84 , 104 , 50 , 59 , 17 , 207 , 110 , 74 , 235 , 140 , 24 , 254 , 68 , 103 , 143 , 68 , 84 ,
658
630
90 , 105 , 188 ,
659
- ] ) ,
660
- CompressedDecaf ( [
631
+ ] ,
632
+ [
661
633
180 , 111 , 24 , 54 , 170 , 40 , 124 , 10 , 90 , 86 , 83 , 240 , 236 , 94 , 249 , 233 , 3 , 244 , 54 ,
662
634
226 , 28 , 21 , 112 , 194 , 154 , 217 , 229 , 245 , 150 , 218 , 151 , 238 , 175 , 23 , 21 , 10 ,
663
635
227 , 11 , 203 , 49 , 116 , 208 , 75 , 194 , 215 , 18 , 200 , 199 , 120 , 157 , 124 , 180 , 253 ,
664
636
161 , 56 , 244 ,
665
- ] ) ,
666
- CompressedDecaf ( [
637
+ ] ,
638
+ [
667
639
28 , 91 , 190 , 207 , 71 , 65 , 223 , 170 , 231 , 157 , 183 , 45 , 250 , 206 , 0 , 234 , 170 , 197 ,
668
640
2 , 194 , 6 , 9 , 52 , 182 , 234 , 174 , 202 , 106 , 32 , 189 , 61 , 169 , 224 , 190 , 135 , 119 ,
669
641
247 , 208 , 32 , 51 , 209 , 177 , 88 , 132 , 35 , 34 , 129 , 164 , 31 , 199 , 248 , 14 , 237 , 4 ,
670
642
175 , 94 ,
671
- ] ) ,
672
- CompressedDecaf ( [
643
+ ] ,
644
+ [
673
645
134 , 255 , 1 , 130 , 212 , 15 , 127 , 158 , 219 , 120 , 98 , 81 , 88 , 33 , 189 , 103 , 191 , 214 ,
674
646
22 , 90 , 60 , 68 , 222 , 149 , 215 , 223 , 121 , 184 , 119 , 156 , 207 , 100 , 96 , 227 , 198 ,
675
647
139 , 112 , 193 , 106 , 170 , 40 , 15 , 45 , 123 , 63 , 34 , 215 , 69 , 185 , 122 , 137 , 144 , 108 ,
676
648
252 , 71 , 108 ,
677
- ] ) ,
678
- CompressedDecaf ( [
649
+ ] ,
650
+ [
679
651
80 , 43 , 203 , 104 , 66 , 235 , 6 , 240 , 228 , 144 , 50 , 186 , 232 , 124 , 85 , 76 , 3 , 29 , 109 ,
680
652
77 , 45 , 118 , 148 , 239 , 191 , 156 , 70 , 141 , 72 , 34 , 12 , 80 , 248 , 202 , 40 , 132 , 51 ,
681
653
100 , 215 , 12 , 238 , 146 , 214 , 254 , 36 , 110 , 97 , 68 , 143 , 157 , 185 , 128 , 139 , 59 , 36 ,
682
654
8 ,
683
- ] ) ,
684
- CompressedDecaf ( [
655
+ ] ,
656
+ [
685
657
12 , 152 , 16 , 241 , 226 , 235 , 211 , 137 , 202 , 167 , 137 , 55 , 77 , 120 , 0 , 121 , 116 , 239 ,
686
658
77 , 23 , 34 , 115 , 22 , 244 , 14 , 87 , 139 , 51 , 104 , 39 , 218 , 63 , 107 , 72 , 42 , 71 , 148 ,
687
659
235 , 106 , 57 , 117 , 185 , 113 , 181 , 225 , 56 , 143 , 82 , 233 , 30 , 162 , 241 , 188 , 176 ,
688
660
249 , 18 ,
689
- ] ) ,
690
- CompressedDecaf ( [
661
+ ] ,
662
+ [
691
663
32 , 212 , 29 , 133 , 161 , 141 , 86 , 87 , 162 , 150 , 64 , 50 , 21 , 99 , 187 , 208 , 76 , 47 ,
692
664
251 , 208 , 163 , 122 , 123 , 164 , 58 , 79 , 125 , 38 , 60 , 226 , 111 , 175 , 78 , 31 , 116 , 249 ,
693
665
244 , 181 , 144 , 198 , 146 , 41 , 174 , 87 , 31 , 227 , 127 , 166 , 57 , 181 , 184 , 235 , 72 ,
694
666
189 , 154 , 85 ,
695
- ] ) ,
696
- CompressedDecaf ( [
667
+ ] ,
668
+ [
697
669
230 , 180 , 184 , 244 , 8 , 199 , 1 , 13 , 6 , 1 , 231 , 237 , 160 , 195 , 9 , 161 , 164 , 39 , 32 ,
698
670
214 , 208 , 107 , 87 , 89 , 253 , 196 , 225 , 239 , 226 , 45 , 7 , 109 , 108 , 68 , 212 , 47 , 80 ,
699
671
141 , 103 , 190 , 70 , 41 , 20 , 210 , 139 , 142 , 220 , 227 , 46 , 112 , 148 , 48 , 81 , 100 , 175 ,
700
672
23 ,
701
- ] ) ,
702
- CompressedDecaf ( [
673
+ ] ,
674
+ [
703
675
190 , 136 , 187 , 184 , 108 , 89 , 193 , 61 , 142 , 157 , 9 , 171 , 152 , 16 , 95 , 105 , 194 , 209 ,
704
676
221 , 19 , 77 , 188 , 211 , 176 , 134 , 54 , 88 , 245 , 49 , 89 , 219 , 100 , 192 , 225 , 57 , 209 ,
705
677
128 , 243 , 200 , 155 , 130 , 150 , 208 , 174 , 50 , 68 , 25 , 192 , 111 , 168 , 127 , 199 , 218 ,
706
678
175 , 52 , 193 ,
707
- ] ) ,
708
- CompressedDecaf ( [
679
+ ] ,
680
+ [
709
681
164 , 86 , 249 , 54 , 151 , 105 , 232 , 240 , 137 , 2 , 18 , 74 , 3 , 20 , 199 , 160 , 101 , 55 ,
710
682
160 , 110 , 50 , 65 , 31 , 79 , 147 , 65 , 89 , 80 , 161 , 123 , 173 , 250 , 116 , 66 , 182 , 33 ,
711
683
116 , 52 , 163 , 160 , 94 , 244 , 91 , 229 , 241 , 11 , 215 , 178 , 239 , 142 , 160 , 12 , 67 , 30 ,
712
684
222 , 197 ,
713
- ] ) ,
714
- CompressedDecaf ( [
685
+ ] ,
686
+ [
715
687
24 , 110 , 69 , 44 , 68 , 102 , 170 , 67 , 131 , 180 , 192 , 2 , 16 , 213 , 46 , 121 , 34 , 219 ,
716
688
249 , 119 , 30 , 139 , 71 , 226 , 41 , 169 , 183 , 183 , 60 , 141 , 16 , 253 , 126 , 240 , 182 ,
717
689
228 , 21 , 48 , 249 , 31 , 36 , 163 , 237 , 154 , 183 , 31 , 163 , 139 , 152 , 178 , 254 , 71 , 70 ,
718
690
213 , 29 , 104 ,
719
- ] ) ,
720
- CompressedDecaf ( [
691
+ ] ,
692
+ [
721
693
74 , 231 , 253 , 202 , 233 , 69 , 63 , 25 , 90 , 142 , 173 , 92 , 190 , 26 , 123 , 150 , 153 , 103 ,
722
694
59 , 82 , 196 , 10 , 178 , 121 , 39 , 70 , 72 , 135 , 190 , 83 , 35 , 127 , 127 , 58 , 33 , 185 , 56 ,
723
695
212 , 13 , 14 , 201 , 225 , 91 , 29 , 81 , 48 , 177 , 63 , 254 , 216 , 19 , 115 , 165 , 62 , 43 , 67 ,
724
- ] ) ,
725
- CompressedDecaf ( [
696
+ ] ,
697
+ [
726
698
132 , 25 , 129 , 195 , 191 , 238 , 195 , 246 , 12 , 254 , 202 , 117 , 217 , 216 , 220 , 23 , 244 ,
727
699
108 , 240 , 16 , 111 , 36 , 34 , 181 , 154 , 236 , 88 , 10 , 88 , 243 , 66 , 39 , 46 , 58 , 94 , 87 ,
728
700
90 , 5 , 93 , 219 , 5 , 19 , 144 , 197 , 76 , 36 , 198 , 236 , 177 , 224 , 172 , 235 , 7 , 95 , 96 ,
729
701
86 ,
730
- ] ) ,
702
+ ] ,
731
703
] ;
732
704
let mut point = DecafPoint :: IDENTITY ;
733
705
let generator = DecafPoint :: GENERATOR ;
734
- for compressed_point in compressed. iter ( ) {
735
- assert_eq ! ( & point. compress( ) , compressed_point) ;
706
+ for compressed_point in COMPRESSED . iter ( ) {
707
+ assert_eq ! ( & point. to_bytes( ) . 0 , compressed_point) ;
708
+ assert_eq ! ( & point. to_affine( ) . to_bytes( ) . 0 , compressed_point) ;
736
709
point += generator;
737
- let decompressed_point = compressed_point. decompress ( ) ;
738
- assert_eq ! ( decompressed_point. is_some( ) . unwrap_u8( ) , 1u8 ) ;
710
+ assert ! (
711
+ DecafPoint :: from_bytes( compressed_point. into( ) )
712
+ . into_option( )
713
+ . is_some( )
714
+ ) ;
715
+ assert ! (
716
+ DecafAffinePoint :: from_bytes( compressed_point. into( ) )
717
+ . into_option( )
718
+ . is_some( )
719
+ ) ;
739
720
}
740
721
}
741
722
0 commit comments