예제 #1
0
func TestSignAndVerify(t *testing.T) {
	testSignAndVerify(t, bitelliptic.S256(), "S256")
	if testing.Short() {
		return
	}
	testSignAndVerify(t, bitelliptic.S160(), "S160")
	testSignAndVerify(t, bitelliptic.S192(), "S192")
	testSignAndVerify(t, bitelliptic.S224(), "S224")
}
예제 #2
0
func TestKeyGeneration(t *testing.T) {
	testKeyGeneration(t, bitelliptic.S256(), "S256")
	if testing.Short() {
		return
	}
	testKeyGeneration(t, bitelliptic.S160(), "S160")
	testKeyGeneration(t, bitelliptic.S192(), "S192")
	testKeyGeneration(t, bitelliptic.S224(), "S224")
}
예제 #3
0
func BenchmarkSign(b *testing.B) {
	b.StopTimer()
	priv, _ := GenerateKey(bitelliptic.S256(), rand.Reader)
	hashed := []byte("testing")
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		Sign(rand.Reader, priv, hashed)
	}
}
예제 #4
0
func BenchmarkVerify(b *testing.B) {
	b.StopTimer()
	data := testVectors[0]
	pub := &PublicKey{
		BitCurve: bitelliptic.S256(),
		X:        fromHex(data.Qx),
		Y:        fromHex(data.Qy),
	}
	hashed, _ := base64.StdEncoding.DecodeString(data.hash)
	r := fromHex(data.r)
	s := fromHex(data.s)
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		Verify(pub, hashed, r, s)
	}
}
예제 #5
0
func TestVectors(t *testing.T) {
	for i, test := range testVectors {
		pub := PublicKey{
			BitCurve: bitelliptic.S256(),
			X:        fromHex(test.Qx),
			Y:        fromHex(test.Qy),
		}
		hashed, _ := base64.StdEncoding.DecodeString(test.hash)
		r := fromHex(test.r)
		s := fromHex(test.s)
		if Verify(&pub, hashed, r, s) != test.ok {
			t.Errorf("%d: bad result", i)
		}
		if testing.Short() {
			break
		}
	}
}
예제 #6
0
func Decode(wif string) (*bitecdsa.PrivateKey, error) {

	if len(wif) < 6 {
		return nil, errors.New("wif.Decode: wif is too short")
	}

	extended, err := base58.Decode(wif)
	if err != nil {
		return nil, err
	}

	decoded := extended[1 : len(extended)-4]
	keys := new(bitecdsa.PrivateKey)
	keys.D = new(big.Int).SetBytes(decoded)
	keys.PublicKey.BitCurve = bitelliptic.S256()
	for keys.PublicKey.X == nil {
		keys.PublicKey.X, keys.PublicKey.Y = keys.PublicKey.BitCurve.ScalarBaseMult(decoded)
	}

	if !keys.BitCurve.IsOnCurve(keys.PublicKey.X, keys.PublicKey.Y) {
		return nil, errors.New("wif.Decode: Point is not on curve")
	}
	return keys, nil
}
예제 #7
0
func New(addressVersion, stream uint64, eighteenByteRipe bool) (*address, error) {

	var err error
	addr := new(address)
	addr.SigningKey, err = bitecdsa.GenerateKey(bitelliptic.S256(), rand.Reader)
	if err != nil {
		return nil, errors.New("address.New: Error generating ecdsa signing keys")
	}

	var ripe []byte

	for {
		addr.EncryptionKey, err = bitecdsa.GenerateKey(bitelliptic.S256(), rand.Reader)
		if err != nil {
			return nil, errors.New("address.New: Error generating ecdsa encryption keys")
		}

		var keyMerge bytes.Buffer
		keyMerge.Write(addr.SigningKey.PublicKey.X.Bytes())
		keyMerge.Write(addr.SigningKey.PublicKey.Y.Bytes())
		keyMerge.Write(addr.EncryptionKey.PublicKey.X.Bytes())
		keyMerge.Write(addr.EncryptionKey.PublicKey.Y.Bytes())

		sha := sha512.New()
		sha.Write(keyMerge.Bytes())

		ripemd := ripemd160.New()
		ripemd.Write(sha.Sum(nil))
		ripe = ripemd.Sum(nil)

		if eighteenByteRipe {
			if ripe[0] == 0x00 && ripe[1] == 0x00 {
				ripe = ripe[2:]
				break
			}
		} else {
			if ripe[0] == 0x00 {
				ripe = ripe[1:]
				break
			}
		}
	}

	var bmAddr bytes.Buffer
	bmAddr.Write(varint.Encode(addressVersion))
	bmAddr.Write(varint.Encode(stream))
	bmAddr.Write(ripe)

	sha1, sha2 := sha512.New(), sha512.New()
	sha1.Write(bmAddr.Bytes())
	sha2.Write(sha1.Sum(nil))
	checksum := sha2.Sum(nil)[:4]
	bmAddr.Write(checksum)

	encoded, err := base58.Encode(bmAddr.Bytes())
	if err != nil {
		return nil, err
	}
	addr.Identifier = "BM-" + encoded

	return addr, nil
}
예제 #8
0
func TestWIF(t *testing.T) {

	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	keys1, err := bitecdsa.GenerateKey(bitelliptic.S256(), rand.Reader)
	if err != nil {
		t.Error(err.Error())
	}

	wif, err := Encode(keys1)
	if err != nil {
		t.Error(err.Error())
	}

	keys2, err := Decode(wif)
	if err != nil {
		t.Error(err.Error())
	}

	if bytes.Compare(keys1.D.Bytes(), keys2.D.Bytes()) != 0 {
		t.Error("Private keys are different. Expected %x, got %x\n", keys1.D.Bytes(), keys2.D.Bytes())
	}

	if bytes.Compare(keys1.PublicKey.X.Bytes(), keys2.PublicKey.X.Bytes()) != 0 {
		t.Error("Public point X are different. Expected %x, got %x\n", keys1.PublicKey.X.Bytes(), keys2.PublicKey.X.Bytes())
	}

	if bytes.Compare(keys1.PublicKey.Y.Bytes(), keys2.PublicKey.Y.Bytes()) != 0 {
		t.Error("Public point Y are different. Expected %x, got %x\n", keys1.PublicKey.Y.Bytes(), keys2.PublicKey.Y.Bytes())
	}

	if !keys2.PublicKey.BitCurve.IsOnCurve(keys2.PublicKey.X, keys2.PublicKey.Y) {
		t.Error("Public point is not on curve\n")
	}

	ok, err := ValidateChecksum(wif)
	if err != nil {
		t.Error(err.Error())
	}

	if !ok {
		t.Error("Invalid checksum")
	}

	// WIF: 5HtKNfWZH4QQZPUGRadud7wfyPGEKLhQJfnYPGvpiivgwfrHfpX
	// Priv. hex: 092715c60df8c561c832ab3c804be0a0f90b108072133df7d1e348e2570be801
	// Pub. uncompressed hex: 0437a3191fe90d9b483324c28ecd019479e708cfcff96800131c113ec30a0646ee95c31b4c5656b1e7122f071ae4471a97511f372179147277ea2a2087147f9486

	keys3, err := Decode("5HtKNfWZH4QQZPUGRadud7wfyPGEKLhQJfnYPGvpiivgwfrHfpX")
	privHex := hex.EncodeToString(keys3.D.Bytes())
	if privHex != "092715c60df8c561c832ab3c804be0a0f90b108072133df7d1e348e2570be801" {
		t.Error("Private key (keys3) is wrong. Expected 092715c60df8c561c832ab3c804be0a0f90b108072133df7d1e348e2570be801, got %s\n", privHex)
	}

	var pub bytes.Buffer
	pub.WriteByte(byte(0x04))
	pub.Write(keys3.PublicKey.X.Bytes())
	pub.Write(keys3.PublicKey.Y.Bytes())
	pubHex := hex.EncodeToString(pub.Bytes())
	if pubHex != "0437a3191fe90d9b483324c28ecd019479e708cfcff96800131c113ec30a0646ee95c31b4c5656b1e7122f071ae4471a97511f372179147277ea2a2087147f9486" {
		t.Error("Public key (keys3) is wrong. Expected 0437a3191fe90d9b483324c28ecd019479e708cfcff96800131c113ec30a0646ee95c31b4c5656b1e7122f071ae4471a97511f372179147277ea2a2087147f9486, got ", pubHex)
	}
}