Ejemplo n.º 1
0
func newPublicKeyCoords(x, y *big.Int) *PublicKey {
	// Serialize coordinates
	pk := new(PublicKey)
	util.PaddedCopy(pk[:CoordinateSize], x.Bytes(), CoordinateSize)
	util.PaddedCopy(pk[CoordinateSize:], y.Bytes(), CoordinateSize)

	return pk
}
Ejemplo n.º 2
0
// Sign creates a signature on the hash under the given secret key.
func Sign(sk *eckey.SecretKey, hash []byte) (*Signature, error) {
	// Generate random nonce
nonce:
	k, kG, err := eckey.GenerateKeyPair()
	if err != nil {
		return nil, err
	}
	// Try again if kG is nil (point at infinity)
	if kG == nil {
		goto nonce
	}
	// Clear nonce after completion
	defer k.Zero()

	// Compute non-interactive challenge
	e := util.Hash256d(append([]byte(hash), kG[:]...))

	kInt := new(big.Int).SetBytes(k[:])
	eInt := new(big.Int).SetBytes(e[:])
	rInt := new(big.Int).SetBytes(sk[:])

	// Compute s = k - er
	s := new(big.Int)
	s.Mul(eInt, rInt)
	s.Sub(kInt, s)
	s.Mod(s, eckey.S256.N)

	// Serialize signature
	sig := new(Signature)
	copy(sig[:SignatureSize/2], e[:])
	util.PaddedCopy(sig[SignatureSize/2:], s.Bytes(), SignatureSize/2)

	return sig, nil
}
Ejemplo n.º 3
0
// computeChildSecret helper method that derives a child secret key from the
// intermediary state.
func (k *HDKey) computeChildSecret(ilInt *big.Int) *eckey.SecretKey {
	keyInt := new(big.Int).SetBytes(k[childKeyOffset+1:])
	defer keyInt.SetUint64(0)

	ilInt.Add(ilInt, keyInt)
	ilInt.Mod(ilInt, eckey.S256.N)

	sk := new(eckey.SecretKey)
	util.PaddedCopy(sk[:], ilInt.Bytes(), eckey.SecretSize)

	return sk
}
Ejemplo n.º 4
0
func TestGenerateKeyPairDeterministic(t *testing.T) {
	for _, test := range ecTestCases {
		sk, pk := keyPairFromTestCase(test)

		// Copy sk as entropy source
		entropy := new(Entropy)
		util.PaddedCopy(entropy[:], sk[:], EntropySize)
		sk2, pk2 := GenerateKeyPairDeterministic(entropy)

		// Secret keys should be the same
		if bytes.Compare(sk[:], sk2[:]) != 0 {
			t.Error("Failed to generate deterministic secret key")
			continue
		}

		// Public keys should be the same
		if bytes.Compare(pk[:], pk2[:]) != 0 {
			t.Error("Failed to generate deterministic public key")
			continue
		}
	}
}
Ejemplo n.º 5
0
func newSecretKeyInt(s *big.Int) *SecretKey {
	sk := new(SecretKey)
	util.PaddedCopy(sk[:], s.Bytes(), SecretSize)

	return sk
}