1- package bip0340
1+ package s256
22
33import (
44 "math/big"
5+ "crypto/subtle"
56 "crypto/elliptic"
67)
78
8- // CurveParams contains the parameters of an elliptic curve and also provides
9+ var (
10+ one = big .NewInt (1 )
11+ )
12+
13+ // S256Curve contains the parameters of an elliptic curve and also provides
914// a generic, non-constant time implementation of Curve.
10- type CurveParams struct {
15+ type S256Curve struct {
1116 P * big.Int // the order of the underlying field
1217 N * big.Int // the order of the base point
1318 B * big.Int // the constant of the curve equation
@@ -16,7 +21,7 @@ type CurveParams struct {
1621 Name string // the canonical name of the curve
1722}
1823
19- func (curve * CurveParams ) Params () * elliptic.CurveParams {
24+ func (curve * S256Curve ) Params () * elliptic.CurveParams {
2025 return & elliptic.CurveParams {
2126 P : curve .P ,
2227 N : curve .N ,
@@ -29,7 +34,7 @@ func (curve *CurveParams) Params() *elliptic.CurveParams {
2934}
3035
3136// polynomial returns x^3 + 7.
32- func (curve * CurveParams ) polynomial (x * big.Int ) * big.Int {
37+ func (curve * S256Curve ) polynomial (x * big.Int ) * big.Int {
3338 x3 := new (big.Int ).Mul (x , x )
3439 x3 .Mul (x3 , x )
3540
@@ -42,7 +47,7 @@ func (curve *CurveParams) polynomial(x *big.Int) *big.Int {
4247}
4348
4449// IsOnCurve implements Curve.IsOnCurve.
45- func (curve * CurveParams ) IsOnCurve (x , y * big.Int ) bool {
50+ func (curve * S256Curve ) IsOnCurve (x , y * big.Int ) bool {
4651 if x .Sign () < 0 || x .Cmp (curve .P ) >= 0 ||
4752 y .Sign () < 0 || y .Cmp (curve .P ) >= 0 {
4853 return false
@@ -56,7 +61,7 @@ func (curve *CurveParams) IsOnCurve(x, y *big.Int) bool {
5661}
5762
5863// Add implements Curve.Add.
59- func (curve * CurveParams ) Add (x1 , y1 , x2 , y2 * big.Int ) (* big.Int , * big.Int ) {
64+ func (curve * S256Curve ) Add (x1 , y1 , x2 , y2 * big.Int ) (* big.Int , * big.Int ) {
6065 if x1 .Sign () == 0 || y1 .Sign () == 0 {
6166 return x2 , y2
6267 }
@@ -121,7 +126,7 @@ func (curve *CurveParams) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
121126}
122127
123128// Double implements Curve.Double.
124- func (curve * CurveParams ) Double (x1 , y1 * big.Int ) (* big.Int , * big.Int ) {
129+ func (curve * S256Curve ) Double (x1 , y1 * big.Int ) (* big.Int , * big.Int ) {
125130 panicIfNotOnCurve (curve , x1 , y1 )
126131
127132 x2 := new (big.Int ).Set (x1 )
@@ -131,7 +136,7 @@ func (curve *CurveParams) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
131136}
132137
133138// ScalarMult implements Curve.ScalarMult.
134- func (curve * CurveParams ) ScalarMult (Bx , By * big.Int , k []byte ) (* big.Int , * big.Int ) {
139+ func (curve * S256Curve ) ScalarMult (Bx , By * big.Int , k []byte ) (* big.Int , * big.Int ) {
135140 panicIfNotOnCurve (curve , Bx , By )
136141
137142 x , y := new (big.Int ), new (big.Int )
@@ -156,12 +161,12 @@ func (curve *CurveParams) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.
156161}
157162
158163// ScalarBaseMult implements Curve.ScalarBaseMult.
159- func (curve * CurveParams ) ScalarBaseMult (k []byte ) (* big.Int , * big.Int ) {
164+ func (curve * S256Curve ) ScalarBaseMult (k []byte ) (* big.Int , * big.Int ) {
160165 return curve .ScalarMult (curve .Gx , curve .Gy , k )
161166}
162167
163168// Unmarshal implements elliptic.Unmarshal.
164- func (curve * CurveParams ) Unmarshal (data []byte ) (x , y * big.Int ) {
169+ func (curve * S256Curve ) Unmarshal (data []byte ) (x , y * big.Int ) {
165170 byteLen := (curve .Params ().BitSize + 7 ) / 8
166171
167172 if len (data ) != 1 + 2 * byteLen {
@@ -187,7 +192,7 @@ func (curve *CurveParams) Unmarshal(data []byte) (x, y *big.Int) {
187192}
188193
189194// UnmarshalCompressed implements elliptic.UnmarshalCompressed.
190- func (curve * CurveParams ) UnmarshalCompressed (data []byte ) (x , y * big.Int ) {
195+ func (curve * S256Curve ) UnmarshalCompressed (data []byte ) (x , y * big.Int ) {
191196 byteLen := (curve .Params ().BitSize + 7 ) / 8
192197
193198 if len (data ) != 1 + byteLen {
@@ -232,3 +237,9 @@ func panicIfNotOnCurve(curve elliptic.Curve, x, y *big.Int) {
232237 panic ("crypto/elliptic: attempted operation on invalid point" )
233238 }
234239}
240+
241+ // bigIntEqual reports whether a and b are equal leaking only their bit length
242+ // through timing side-channels.
243+ func bigIntEqual (a , b * big.Int ) bool {
244+ return subtle .ConstantTimeCompare (a .Bytes (), b .Bytes ()) == 1
245+ }
0 commit comments