Skip to content

Commit 8490456

Browse files
committed
fixed
1 parent a497cf3 commit 8490456

File tree

3 files changed

+44
-84
lines changed

3 files changed

+44
-84
lines changed

elliptic/ed521/params.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,15 @@ func (curve *Ed521Curve) UnmarshalPoint(data []byte) (x, y *big.Int) {
269269

270270
eP = Reverse(eP)
271271

272-
p := curve.Params().P
272+
p := curve.P
273273
y = new(big.Int).SetBytes(eP)
274274
if y.Cmp(p) >= 0 {
275275
return
276276
}
277277

278278
// x² = (y² - 1) / (dy² - 1)
279279
x = curve.polynomial(y)
280-
x = x.ModSqrt(x, curve.P)
280+
x = x.ModSqrt(x, p)
281281
if x == nil {
282282
return
283283
}

pubkey/ed521/ed521.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ func (priv *PrivateKey) Seed() []byte {
112112
return priv.D.FillBytes(seed)
113113
}
114114

115+
func (priv *PrivateKey) Bytes() []byte {
116+
seed := make([]byte, SeedSize)
117+
priv.D.FillBytes(seed)
118+
119+
pub := ed521.MarshalPoint(priv.Curve, priv.X, priv.Y)
120+
121+
buf := make([]byte, 132)
122+
copy(buf[:66], seed)
123+
copy(buf[66:], pub)
124+
125+
return buf
126+
}
127+
115128
// Sign creates a signature for message
116129
func (priv *PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) ([]byte, error) {
117130
var context string
@@ -160,7 +173,7 @@ func GenerateKey(rand io.Reader) (*PrivateKey, error) {
160173

161174
func newKeyFromSeed(seed []byte) (*PrivateKey, error) {
162175
if l := len(seed); l != SeedSize {
163-
panic("go-cryptobin/ed521: bad seed length: " + strconv.Itoa(l))
176+
return nil, errors.New("go-cryptobin/ed521: bad seed length: " + strconv.Itoa(l))
164177
}
165178

166179
curve := ed521.ED521()
@@ -185,6 +198,24 @@ func NewKeyFromSeed(seed []byte) (*PrivateKey, error) {
185198
return newKeyFromSeed(seed)
186199
}
187200

201+
// New a private key from bytes
202+
func NewKeyFromBytes(bytes []byte) (*PrivateKey, error) {
203+
if l := len(bytes); l != 132 {
204+
return nil, errors.New("go-cryptobin/ed521: bad bytes length: " + strconv.Itoa(l))
205+
}
206+
207+
curve := ed521.ED521()
208+
209+
k := new(big.Int).SetBytes(bytes[:66])
210+
211+
priv := new(PrivateKey)
212+
priv.PublicKey.Curve = curve
213+
priv.D = k
214+
priv.PublicKey.X, priv.PublicKey.Y = ed521.UnmarshalPoint(curve, bytes[66:])
215+
216+
return priv, nil
217+
}
218+
188219
// New a private key from key data bytes
189220
func NewPrivateKey(d []byte) (*PrivateKey, error) {
190221
return newKeyFromSeed(d)

pubkey/ed521/ed521_test.go

Lines changed: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ed521
22

33
import (
44
"fmt"
5-
"bytes"
65
"testing"
76
"crypto"
87
"crypto/rand"
@@ -71,6 +70,16 @@ func Test_NewPrivateKey(t *testing.T) {
7170

7271
// ======
7372

73+
privBytes2 := priv.Bytes()
74+
priv22, err := NewKeyFromBytes(privBytes2)
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
79+
cryptobin_test.Equal(t, priv22, priv, "NewKeyFromBytes Equal error")
80+
81+
// ======
82+
7483
pub := &priv.PublicKey
7584

7685
pubBytes := PublicKeyTo(pub)
@@ -338,86 +347,6 @@ func Test_Random_Private_Keys(t *testing.T) {
338347

339348
}
340349

341-
func Test_Vec_Check(t *testing.T) {
342-
for i, td := range testSigVec {
343-
t.Run(fmt.Sprintf("index %d", i), func(t *testing.T) {
344-
curve := ed521.ED521()
345-
346-
if len(td.secretKey) > 0 {
347-
priv, err := NewPrivateKey(td.secretKey)
348-
if err != nil {
349-
t.Fatal(err)
350-
}
351-
352-
pub := &priv.PublicKey
353-
354-
pubBytes := ed521.MarshalCompressed(pub.Curve, pub.X, pub.Y)
355-
356-
// check publicKey
357-
if !bytes.Equal(pubBytes, td.publicKey) {
358-
t.Errorf("PublicKey got: %x, want: %x", pubBytes, td.publicKey)
359-
}
360-
361-
// check sig
362-
sig, err := priv.Sign(rand.Reader, td.message, nil)
363-
if err != nil {
364-
t.Error("encode sig fail")
365-
}
366-
367-
if bytes.Equal(sig, td.signature) != td.verification {
368-
t.Errorf("sig fail, got: %x, want: %x", sig, td.signature)
369-
}
370-
371-
}
372-
373-
x, y := ed521.UnmarshalCompressed(curve, td.publicKey)
374-
if x == nil || y == nil {
375-
t.Fatal("publicKey error")
376-
}
377-
378-
pubkey := &PublicKey{
379-
Curve: curve,
380-
X: x,
381-
Y: y,
382-
}
383-
384-
veri := pubkey.Verify(td.message, td.signature)
385-
if veri != td.verification {
386-
t.Error("Verify fail")
387-
}
388-
389-
})
390-
}
391-
392-
}
393-
394-
type testVec struct {
395-
secretKey []byte
396-
publicKey []byte
397-
message []byte
398-
signature []byte
399-
verification bool
400-
}
401-
402-
var testSigVec = []testVec{
403-
{
404-
secretKey: fromHex("22713dc2f3d8a4611e9266d8a2a9e3d237505dc34c65d87d598b9a4e6c41b35e3d090458e66c8213a4af011e5614377960c99d9f84e379fdd1f1e168b163b5d93012"),
405-
publicKey: fromHex("020006be6a2ea17441c94e25799154b049ebae2fedcedfb27355ab03f9eb802d239677c340392fe113ffb18138b95dc8ba3efd766401cabfc4cc30d0ccdce7b178d954"),
406-
message: fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
407-
signature: fromHex("e6309d7d752de236179d694954f9345a73b1abcbe7ebde16e5e0fa1a4ed60003839a13f03fe1fccfecdac66614b8aff731e7956678be247b8f19795b20351a578301780dd7e6d41db086ef9bdc4658cbcc3d86d259b7e36ea399b075da86f2559489c64f59d196c5a439b3c5442609912dee28721d82dd08c71884b7f406961510110e00"),
408-
verification: true,
409-
},
410-
411-
// fail
412-
{
413-
secretKey: fromHex("22713dc2f3d8a4611e9266d8a2a9e3d237505dc34c65d87d598b9a4e6c41b35e3d090458e66c8213a4af011e5614377960c99d9f84e379fdd1f1e168b163b5d93012"),
414-
publicKey: fromHex("020006be6a2ea17441c94e25799154b049ebae2fedcedfb27355ab03f9eb802d239677c340392fe113ffb18138b95dc8ba3efd766401cabfc4cc30d0ccdce7b178d954"),
415-
message: fromHex("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
416-
signature: fromHex("3bd0454bd7a48f25fa40e0ebd76c1eb48e7fff63f606f35f9a8de9fc6d8fde0f75c10c0d97b7dc3001c5da6da681ff95d1a6ace65c19134ca727b99b1c349752ee01e8c9d1f325ab430511bcbf1e009d37418f56f132383ed55e43d97c04feff5a6f875fa64458ab73f9e4d1ad93cc978a7939a9afe532e8e6162b7bcb986f04a2282a00"),
417-
verification: false,
418-
},
419-
}
420-
421350
func Test_ED521_Vec_Check(t *testing.T) {
422351
var message []byte
423352
var privateKeyBytes []byte

0 commit comments

Comments
 (0)