package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"math/big"
mathrand "math/rand"
"hash"
"fmt"
"io"
"os"
)
import "github.com/ethereum/go-ethereum/crypto/secp256k1"
type deterministicRandom struct {
stream cipher.Stream
}
func newDeterministicRand() io.Reader {
block, err := aes.NewCipher(make([]byte, 128/8))
if err != nil {
panic(err)
}
stream := cipher.NewCTR(block, make([]byte, block.BlockSize()))
return &deterministicRandom{stream}
}
func (r *deterministicRandom) Read(b []byte) (n int, err error) {
for i := range b {
b[i] = 0
}
r.stream.XORKeyStream(b, b)
return len(b), nil
}
var deterministicRand io.Reader
func printPadded(key string, n, max *big.Int) {
padded := make([]byte, len(max.Bytes()))
b := n.Bytes()
copy(padded[len(padded)-len(b):], b)
fmt.Printf("%s = %x\n", key, padded)
}
func randNonZeroInt(max *big.Int) *big.Int {
for {
r, err := rand.Int(deterministicRand, max)
if err != nil {
panic(err)
}
if r.Sign() != 0 {
return r
}
}
}
func randPoint(curve elliptic.Curve) (x, y *big.Int) {
k := randNonZeroInt(curve.Params().N)
return curve.ScalarBaseMult(k.Bytes())
}
func hashToInt(hash []byte, c elliptic.Curve) *big.Int {
orderBits := c.Params().N.BitLen()
orderBytes := (orderBits + 7) / 8
if len(hash) > orderBytes {
hash = hash[:orderBytes]
}
ret := new(big.Int).SetBytes(hash)
excess := len(hash)*8 - orderBits
if excess > 0 {
ret.Rsh(ret, uint(excess))
}
return ret
}
func signWithGivenK(priv *ecdsa.PrivateKey, dgst []byte, k *big.Int) (r, s *big.Int) {
N := priv.Curve.Params().N
r, _ = priv.Curve.ScalarBaseMult(k.Bytes())
r.Mod(r, N)
kInv := new(big.Int).ModInverse(k, N)
e := hashToInt(dgst, priv.Curve)
s = new(big.Int).Mul(priv.D, r)
s.Add(s, e)
s.Mul(s, kInv)
s.Mod(s, N)
return
}
const numTestsPerHash = 15
func printVerifyTestVectors(curve elliptic.Curve, curveName string, hash hash.Hash) {
for i := 0; i < numTestsPerHash; i++ {
priv, _ := ecdsa.GenerateKey(curve, deterministicRand)
k := randNonZeroInt(curve.Params().N)
msg := make([]byte, 128)
deterministicRand.Read(msg)
hash.Reset()
hash.Write(msg)
dgst := hash.Sum(nil)
r, s := signWithGivenK(priv, dgst, k)
event := mathrand.Intn(5)
one := new(big.Int).SetUint64(1)
xOut, yOut := priv.PublicKey.X, priv.PublicKey.Y
switch event {
case 1:
dgst[0] ^= 1 case 2:
r.Xor(r, one) case 3:
s.Xor(s, one) case 4:
xOut, yOut = randPoint(curve) }
fmt.Printf("\nCurve = %s\n", curveName)
printPadded("X", xOut, curve.Params().P)
printPadded("Y", yOut, curve.Params().P)
fmt.Printf("Digest = %x\n", dgst)
printPadded("R", r, curve.Params().P)
printPadded("S", s, curve.Params().P)
if event != 0 {
fmt.Printf("Invalid =\n")
}
}
}
func printSignTestVectors(curve elliptic.Curve, curveName string, hash hash.Hash) {
for i := 0; i < numTestsPerHash; i++ {
priv, _ := ecdsa.GenerateKey(curve, deterministicRand)
k := randNonZeroInt(curve.Params().N)
msg := make([]byte, 128)
deterministicRand.Read(msg)
hash.Reset()
hash.Write(msg)
dgst := hash.Sum(nil)
r, s := signWithGivenK(priv, dgst, k)
fmt.Printf("\nCurve = %s\n", curveName)
printPadded("Private", priv.D, curve.Params().P)
printPadded("X", priv.PublicKey.X, curve.Params().P)
printPadded("Y", priv.PublicKey.Y, curve.Params().P)
fmt.Printf("Digest = %x\n", dgst)
printPadded("K", k, curve.Params().P)
printPadded("R", r, curve.Params().P)
printPadded("S", s, curve.Params().P)
}
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Missing command line argument: specify 0 for Verify; anything else for Sign test vectors.")
return
}
deterministicRand = newDeterministicRand()
S256 := secp256k1.S256()
hashFuncs := []hash.Hash {sha1.New(), sha256.New224(), sha256.New(), sha512.New384(), sha512.New()}
fmt.Printf("# Test vectors for secp256k1 were generated by make_ecdsa_secp256k1_tests.go script.\n")
if os.Args[1] == "0" {
for i := 0; i < len(hashFuncs); i++ {
printVerifyTestVectors(S256, "secp256k1", hashFuncs[i])
}
} else {
for i := 0; i < len(hashFuncs); i++ {
printSignTestVectors(S256, "secp256k1", hashFuncs[i])
}
}
}