1- use std:: ops:: Mul ;
1+ use std:: ops:: { Add , Mul } ;
22
33use crypto_bigint:: Uint ;
44use serde:: { Deserialize , Serialize } ;
@@ -24,6 +24,23 @@ pub struct Secp256k1(
2424#[ derive( Serialize , Deserialize , Debug , Clone , Copy ) ]
2525pub struct AffinePoint ( Secp256k1PrimeField , Secp256k1PrimeField ) ;
2626
27+ impl AffinePoint {
28+ pub fn x ( & self ) -> & Secp256k1PrimeField {
29+ & self . 0
30+ }
31+
32+ pub fn y ( & self ) -> & Secp256k1PrimeField {
33+ & self . 1
34+ }
35+
36+ pub fn is_valid ( & self ) -> bool {
37+ let b = Secp256k1PrimeField :: from ( 7 ) ;
38+ let lhs = self . y ( ) . mul ( self . y ( ) ) ;
39+ let rhs = self . x ( ) . mul ( self . x ( ) ) . mul ( self . x ( ) ) . add ( & b) ;
40+ lhs. eq ( & rhs)
41+ }
42+ }
43+
2744impl Secp256k1 {
2845 /// Point at infinity using affine coordinates.
2946 pub const POINT_AT_INFINITY : Self = Self (
@@ -52,7 +69,7 @@ impl Secp256k1 {
5269 if self . z ( ) . eq ( & Secp256k1PrimeField :: ONE ) {
5370 AffinePoint ( * self . x ( ) , * self . y ( ) )
5471 } else {
55- // TODO: Check the safety of this unwrap.
72+ assert ! ( ! ( self . z ( ) == & Secp256k1PrimeField :: ZERO ) ) ;
5673 let z = self . z ( ) . inverse ( ) . unwrap ( ) ;
5774 AffinePoint ( self . x ( ) . mul ( & z) , self . y ( ) . mul ( & z) )
5875 }
@@ -89,6 +106,7 @@ impl Secp256k1 {
89106 t1 = * self . x ( ) * self . y ( ) ;
90107 x3 = t0 * & t1;
91108 x3 = x3 + & x3;
109+
92110 Self ( x3, y3, z3)
93111 }
94112}
@@ -99,10 +117,10 @@ impl EllipticCurve<4> for Secp256k1 {
99117
100118 fn gen ( ) -> Self {
101119 Self (
102- Secp256k1PrimeField :: new ( Uint :: from_le_hex (
120+ Secp256k1PrimeField :: new ( Uint :: from_be_hex (
103121 "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798" ,
104122 ) ) ,
105- Secp256k1PrimeField :: new ( Uint :: from_le_hex (
123+ Secp256k1PrimeField :: new ( Uint :: from_be_hex (
106124 "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8" ,
107125 ) ) ,
108126 Secp256k1PrimeField :: ONE ,
@@ -180,7 +198,7 @@ impl EllipticCurve<4> for Secp256k1 {
180198 let mut result = Self :: POINT_AT_INFINITY ;
181199 let naf = scalar. to_naf ( ) ;
182200 for i in ( 0 ..naf. len ( ) ) . rev ( ) {
183- result = self . dbl ( ) ;
201+ result = result . dbl ( ) ;
184202 if naf. pos ( i) {
185203 result = result. add ( self ) ;
186204 } else if naf. neg ( i) {
0 commit comments