@@ -26,7 +26,7 @@ const (
2626 // ContextMaxSize is the maximum length (in bytes) allowed for context.
2727 ContextMaxSize = 255
2828 // PublicKeySize is the size, in bytes, of public keys as used in this package.
29- PublicKeySize = 133
29+ PublicKeySize = 66
3030 // PrivateKeySize is the size, in bytes, of private keys as used in this package.
3131 PrivateKeySize = 66
3232 // SignatureSize is the size, in bytes, of signatures generated and verified by this package.
@@ -107,6 +107,11 @@ func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool {
107107 bigIntEqual (priv .D , xx .D )
108108}
109109
110+ func (priv * PrivateKey ) Seed () []byte {
111+ seed := make ([]byte , SeedSize )
112+ return priv .D .FillBytes (seed )
113+ }
114+
110115// Sign creates a signature for message
111116func (priv * PrivateKey ) Sign (rand io.Reader , message []byte , opts crypto.SignerOpts ) ([]byte , error ) {
112117 var context string
@@ -149,34 +154,37 @@ func GenerateKey(rand io.Reader) (*PrivateKey, error) {
149154 return nil , err
150155 }
151156
152- return newKeyFromSeed (k .Bytes ())
157+ bytes := make ([]byte , SeedSize )
158+ return newKeyFromSeed (k .FillBytes (bytes ))
153159}
154160
155161func newKeyFromSeed (seed []byte ) (* PrivateKey , error ) {
156- if l := len (seed ); l > SeedSize {
162+ if l := len (seed ); l != SeedSize {
157163 panic ("go-cryptobin/ed521: bad seed length: " + strconv .Itoa (l ))
158164 }
159165
160166 curve := ed521 .ED521 ()
161167
162- k := new (big.Int ).SetBytes (seed )
168+ h := make ([]byte , 132 )
169+ sha3 .ShakeSum256 (h , seed )
163170
164- n := new (big.Int ).Sub (curve .Params ().N , one )
165- if k .Cmp (n ) >= 0 {
166- return nil , errors .New ("go-cryptobin/ed521: privateKey's seed is overflow" )
167- }
171+ k := new (big.Int ).SetBytes (seed )
168172
169- h := make ([]byte , 132 )
170- sha3 .ShakeSum256 (h , k .Bytes ())
173+ scalar := ed521 .GetPrivateScalar (h [:66 ])
171174
172175 priv := new (PrivateKey )
173176 priv .PublicKey .Curve = curve
174177 priv .D = k
175- priv .PublicKey .X , priv .PublicKey .Y = curve .ScalarBaseMult (h [: 66 ] )
178+ priv .PublicKey .X , priv .PublicKey .Y = curve .ScalarBaseMult (scalar )
176179
177180 return priv , nil
178181}
179182
183+ // New a private key from seed bytes
184+ func NewKeyFromSeed (seed []byte ) (* PrivateKey , error ) {
185+ return newKeyFromSeed (seed )
186+ }
187+
180188// New a private key from key data bytes
181189func NewPrivateKey (d []byte ) (* PrivateKey , error ) {
182190 return newKeyFromSeed (d )
@@ -192,7 +200,7 @@ func PrivateKeyTo(key *PrivateKey) []byte {
192200func NewPublicKey (data []byte ) (* PublicKey , error ) {
193201 curve := ed521 .ED521 ()
194202
195- x , y := elliptic . Unmarshal (curve , data )
203+ x , y := ed521 . UnmarshalPoint (curve , data )
196204 if x == nil || y == nil {
197205 return nil , errors .New ("go-cryptobin/ed521: incorrect public key" )
198206 }
@@ -208,7 +216,7 @@ func NewPublicKey(data []byte) (*PublicKey, error) {
208216
209217// return PublicKey data
210218func PublicKeyTo (key * PublicKey ) []byte {
211- return ed521 .Marshal (key .Curve , key .X , key .Y )
219+ return ed521 .MarshalPoint (key .Curve , key .X , key .Y )
212220}
213221
214222// sign data and return marshal plain data
@@ -253,15 +261,14 @@ func sign(privateKey *PrivateKey, message []byte, domPre, context string) ([]byt
253261 n := params .N
254262 byteLen := (params .BitSize + 7 ) / 8
255263
256- var tmpBuf []byte
257-
258- seed := privateKey .D .Bytes ()
264+ seed := privateKey .Seed ()
259265 publicKeyBytes := ed521 .MarshalPoint (privateKey .Curve , privateKey .X , privateKey .Y )
260266
261267 h := make ([]byte , 132 )
262268 sha3 .ShakeSum256 (h , seed )
263269
264- s := new (big.Int ).SetBytes (h [:66 ])
270+ scalar := ed521 .GetPrivateScalar (h [:66 ])
271+ s := new (big.Int ).SetBytes (scalar )
265272 s .Mod (s , n )
266273
267274 prefix := h [66 :]
@@ -280,19 +287,14 @@ func sign(privateKey *PrivateKey, message []byte, domPre, context string) ([]byt
280287 r := new (big.Int ).SetBytes (messageDigest )
281288 r .Mod (r , n )
282289
283- _ , R := privateKey .Curve .ScalarBaseMult (r .Bytes ())
284-
285- buf := make ([]byte , 2 * byteLen )
286- R .FillBytes (buf [:byteLen ])
287-
288- tmpBuf = ed521 .Reverse (buf [:byteLen ])
289- copy (buf [:byteLen ], tmpBuf )
290+ Rx , Ry := privateKey .Curve .ScalarBaseMult (r .Bytes ())
291+ R := ed521 .MarshalPoint (privateKey .Curve , Rx , Ry )
290292
291293 kh := sha3 .NewShake256 ()
292294 kh .Write ([]byte (domPre ))
293295 kh .Write ([]byte {byte (len (context ))})
294296 kh .Write ([]byte (context ))
295- kh .Write (buf [: byteLen ] )
297+ kh .Write (R )
296298 kh .Write (publicKeyBytes )
297299 kh .Write (PHM )
298300 hramDigest := make ([]byte , 132 )
@@ -308,11 +310,13 @@ func sign(privateKey *PrivateKey, message []byte, domPre, context string) ([]byt
308310 S .Add (S , r )
309311 S .Mod (S , n )
310312
311- S .FillBytes (buf [byteLen :])
312- tmpBuf = ed521 .Reverse (buf [byteLen :])
313- copy (buf [byteLen :], tmpBuf )
313+ SBytes := ed521 .Reverse (S .FillBytes (make ([]byte , byteLen )))
314314
315- return buf , nil
315+ sig := make ([]byte , 2 * byteLen )
316+ copy (sig [:byteLen ], R )
317+ copy (sig [byteLen :], SBytes )
318+
319+ return sig , nil
316320}
317321
318322// VerifyWithOptions reports whether sig is a valid signature of message by
@@ -374,10 +378,14 @@ func verify(publicKey *PublicKey, message, sig []byte, domPre, context string) b
374378 return false
375379 }
376380
377- RBytes := ed521 .Reverse (sig [:byteLen ])
378- SBytes := ed521 .Reverse (sig [byteLen :])
381+ R := sig [:byteLen ]
379382
380- R := new (big.Int ).SetBytes (RBytes )
383+ Rx , Ry := ed521 .UnmarshalPoint (curve , R )
384+ if Rx == nil && Ry == nil {
385+ return false
386+ }
387+
388+ SBytes := ed521 .Reverse (sig [byteLen :])
381389 S := new (big.Int ).SetBytes (SBytes )
382390
383391 publicKeyBytes := ed521 .MarshalPoint (publicKey .Curve , publicKey .X , publicKey .Y )
@@ -386,7 +394,7 @@ func verify(publicKey *PublicKey, message, sig []byte, domPre, context string) b
386394 kh .Write ([]byte (domPre ))
387395 kh .Write ([]byte {byte (len (context ))})
388396 kh .Write ([]byte (context ))
389- kh .Write (sig [: byteLen ] )
397+ kh .Write (R )
390398 kh .Write (publicKeyBytes )
391399 kh .Write (PHM )
392400 hramDigest := make ([]byte , 132 )
@@ -401,26 +409,26 @@ func verify(publicKey *PublicKey, message, sig []byte, domPre, context string) b
401409 // r = S - k * pub
402410 x21 , y21 := curve .ScalarMult (publicKey .X , publicKey .Y , k .Bytes ())
403411 x22 , y22 := curve .ScalarBaseMult (S .Bytes ())
404- _ , y2 := curve .Add (x21 , y21 , x22 , y22 )
412+ y1 , y2 := curve .Add (x21 , y21 , x22 , y22 )
405413
406- return bigIntEqual (R , y2 )
414+ return bigIntEqual (Rx , y1 ) &&
415+ bigIntEqual (Ry , y2 )
407416}
408417
409418func randFieldElement (rand io.Reader , curve elliptic.Curve ) (* big.Int , error ) {
410419 N := curve .Params ().N
411420
412- byteLen := (N .BitLen () + 7 ) / 8
413- bytes := make ([]byte , byteLen )
421+ bytes := make ([]byte , SeedSize )
414422
415423 for {
416424 _ , err := io .ReadFull (rand , bytes )
417425 if err != nil {
418426 return nil , err
419427 }
420428
421- num := new (big.Int ).SetBytes (bytes )
422- if num .Cmp (N ) < 0 {
423- return num , nil
429+ k := new (big.Int ).SetBytes (bytes )
430+ if k .Cmp (N ) < 0 {
431+ return k , nil
424432 }
425433 }
426434}
0 commit comments