Skip to content

Commit 19638da

Browse files
committed
fixed
1 parent 06fa286 commit 19638da

8 files changed

Lines changed: 157 additions & 94 deletions

File tree

elliptic/e521/params.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func (curve *E521Curve) polynomial(y *big.Int) *big.Int {
5454
return x2
5555
}
5656

57+
// IsOnCurve reports whether the given (x,y) lies on the curve.
5758
// check equation: x² + y² ≡ 1 + d*x²*y² (mod p),
5859
// so we can check equation: x² = (1 - y²) / (1 - d*y²).
5960
func (curve *E521Curve) IsOnCurve(x, y *big.Int) bool {
@@ -67,6 +68,7 @@ func (curve *E521Curve) IsOnCurve(x, y *big.Int) bool {
6768
return curve.polynomial(y).Cmp(x2) == 0
6869
}
6970

71+
// Add returns the sum of (x1,y1) and (x2,y2)
7072
func (curve *E521Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
7173
if x1.Sign() == 0 && y1.Sign() == 0 {
7274
return x2, y2
@@ -106,7 +108,7 @@ func (curve *E521Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
106108
return x3, y3
107109
}
108110

109-
// Double dobra um ponto na curva Edwards
111+
// Double returns 2*(x,y)
110112
func (curve *E521Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
111113
return curve.Add(x1, y1, x1, y1)
112114
}
Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
package bip0340
1+
package s256
22

33
import (
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+
}

elliptic/s256/s256.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Package secp256k1 implements the standard secp256k1 elliptic curve over prime fields.
2+
package s256
3+
4+
import (
5+
"sync"
6+
"encoding/asn1"
7+
)
8+
9+
var (
10+
OIDS256 = asn1.ObjectIdentifier{1, 3, 132, 0, 10}
11+
)
12+
13+
var once sync.Once
14+
15+
// The following conventions are used, with constants as defined for secp256k1.
16+
// We note that adapting this specification to other elliptic curves is not straightforward
17+
// and can result in an insecure scheme
18+
func S256() *S256Curve {
19+
once.Do(initAll)
20+
return s256
21+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
package bip0340
1+
package s256
22

3-
var s256 *CurveParams
3+
import (
4+
"math/big"
5+
)
46

5-
func init() {
6-
s256 = &CurveParams{
7+
var s256 *S256Curve
8+
9+
func initAll() {
10+
initS256()
11+
}
12+
13+
func initS256() {
14+
s256 = &S256Curve{
715
Name: "secp256k1",
816
BitSize: 256,
917
P: bigFromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"),
@@ -14,9 +22,9 @@ func init() {
1422
}
1523
}
1624

17-
// The following conventions are used, with constants as defined for secp256k1.
18-
// We note that adapting this specification to other elliptic curves is not straightforward
19-
// and can result in an insecure scheme
20-
func S256() *CurveParams {
21-
return s256
25+
func bigFromHex(s string) (i *big.Int) {
26+
i = new(big.Int)
27+
i.SetString(s, 16)
28+
29+
return
2230
}

elliptic/s256/s256_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package s256
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"math/big"
7+
"crypto/elliptic"
8+
)
9+
10+
func Test_Interface(t *testing.T) {
11+
var _ elliptic.Curve = (*S256Curve)(nil)
12+
}
13+
14+
func Test_S256_Curve_Add(t *testing.T) {
15+
{
16+
a1 := toBigint("dff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659")
17+
b1 := toBigint("2ce19b946c4ee58546f5251d441a065ea50735606985e5b228788bec4e582898")
18+
a2 := toBigint("dd308afec5777e13121fa72b9cc1b7cc0139715309b086c960e18fd969774eb8")
19+
b2 := toBigint("f594bb5f72b37faae396a4259ea64ed5e6fdeb2a51c6467582b275925fab1394")
20+
21+
xx, yy := S256().Add(a1, b1, a2, b2)
22+
23+
xx2 := fmt.Sprintf("%x", xx.Bytes())
24+
yy2 := fmt.Sprintf("%x", yy.Bytes())
25+
26+
xxcheck := "0b4b8b19e1666914c37647bf3eac2acc4348b02ef8b1f2940c8bf10a381df22c"
27+
yycheck := "1fbbb6a4be23f0019261e05f4d26114059b001649b998160020c0c4c31000ce5"
28+
29+
if xx2 != xxcheck {
30+
t.Errorf("xx fail, got %s, want %s", xx2, xxcheck)
31+
}
32+
if yy2 != yycheck {
33+
t.Errorf("yy fail, got %s, want %s", yy2, yycheck)
34+
}
35+
}
36+
37+
{
38+
a1 := toBigint("dff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659")
39+
b1 := toBigint("2ce19b946c4ee58546f5251d441a065ea50735606985e5b228788bec4e582898")
40+
41+
xx, yy := S256().Add(a1, b1, a1, b1)
42+
43+
xx2 := fmt.Sprintf("%x", xx.Bytes())
44+
yy2 := fmt.Sprintf("%x", yy.Bytes())
45+
46+
xxcheck := "768c61d8c3acc2bbbf37dec4e62b9c481802fd817a4dbc7d5542f02375412945"
47+
yycheck := "e227f7076346296e92364b75508102d997f66170764bcffb2bce80ff0e77be0a"
48+
49+
if xx2 != xxcheck {
50+
t.Errorf("xx fail, got %s, want %s", xx2, xxcheck)
51+
}
52+
if yy2 != yycheck {
53+
t.Errorf("yy fail, got %s, want %s", yy2, yycheck)
54+
}
55+
}
56+
57+
{
58+
a1 := toBigint("dff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659")
59+
b1 := toBigint("2ce19b946c4ee58546f5251d441a065ea50735606985e5b228788bec4e582898")
60+
61+
xx, yy := S256().Double(a1, b1)
62+
63+
xx2 := fmt.Sprintf("%x", xx.Bytes())
64+
yy2 := fmt.Sprintf("%x", yy.Bytes())
65+
66+
xxcheck := "768c61d8c3acc2bbbf37dec4e62b9c481802fd817a4dbc7d5542f02375412945"
67+
yycheck := "e227f7076346296e92364b75508102d997f66170764bcffb2bce80ff0e77be0a"
68+
69+
if xx2 != xxcheck {
70+
t.Errorf("xx fail, got %s, want %s", xx2, xxcheck)
71+
}
72+
if yy2 != yycheck {
73+
t.Errorf("yy fail, got %s, want %s", yy2, yycheck)
74+
}
75+
}
76+
77+
}
78+
79+
func toBigint(s string) *big.Int {
80+
result, _ := new(big.Int).SetString(s, 16)
81+
82+
return result
83+
}

pubkey/bip0340/batch_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"math/big"
77
"crypto/sha256"
88
"crypto/elliptic"
9+
10+
"github.com/deatil/go-cryptobin/elliptic/secp256k1"
911
)
1012

1113
func Test_Batch_Check(t *testing.T) {
@@ -16,13 +18,13 @@ func Test_Batch_Check(t *testing.T) {
1618
t.Run(fmt.Sprintf("index %d", i), func(t *testing.T) {
1719
pubBytes := append([]byte{byte(3)}, td.publicKey...)
1820

19-
x, y := elliptic.UnmarshalCompressed(S256(), pubBytes)
21+
x, y := elliptic.UnmarshalCompressed(secp256k1.S256(), pubBytes)
2022
if x == nil || y == nil {
2123
t.Fatal("publicKey error")
2224
}
2325

2426
pubkey := &PublicKey{
25-
Curve: S256(),
27+
Curve: secp256k1.S256(),
2628
X: x,
2729
Y: y,
2830
}

pubkey/bip0340/bip0340.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,12 @@ func NewPrivateKey(curve elliptic.Curve, k []byte) (*PrivateKey, error) {
139139
return priv, nil
140140
}
141141

142-
// 输出私钥明文
143142
// output PrivateKey data
144143
func PrivateKeyTo(key *PrivateKey) []byte {
145144
privateKey := make([]byte, (key.Curve.Params().N.BitLen()+7)/8)
146145
return key.D.FillBytes(privateKey)
147146
}
148147

149-
// 根据公钥明文初始化公钥
150148
// New a PublicKey from publicKey data
151149
func NewPublicKey(curve elliptic.Curve, k []byte) (*PublicKey, error) {
152150
x, y := elliptic.Unmarshal(curve, k)
@@ -163,7 +161,6 @@ func NewPublicKey(curve elliptic.Curve, k []byte) (*PublicKey, error) {
163161
return pub, nil
164162
}
165163

166-
// 输出公钥明文
167164
// output PublicKey data
168165
func PublicKeyTo(key *PublicKey) []byte {
169166
return elliptic.Marshal(key.Curve, key.X, key.Y)

0 commit comments

Comments
 (0)