The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/crypto/pki/utils.go

91 lines
2.6 KiB

package pki
import (
"crypto/sha256"
6 years ago
"encoding/binary"
"github.com/dedis/kyber"
6 years ago
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/crypto"
)
func init() {
bls.Init(bls.BLS12_381)
}
6 years ago
// GetAddressFromPublicKey returns address given a public key.
6 years ago
func GetAddressFromPublicKey(pubKey *bls.PublicKey) [20]byte {
bytes := pubKey.Serialize()
address := [20]byte{}
hash := sha256.Sum256(bytes)
copy(address[:], hash[12:])
return address
}
6 years ago
// GetAddressFromPrivateKey returns address given a private key.
6 years ago
func GetAddressFromPrivateKey(priKey *bls.SecretKey) [20]byte {
return GetAddressFromPublicKey(priKey.GetPublicKey())
}
6 years ago
// GetAddressFromPrivateKeyBytes returns address from private key in bytes.
func GetAddressFromPrivateKeyBytes(priKey [32]byte) [20]byte {
6 years ago
var privateKey bls.SecretKey
privateKey.SetLittleEndian(priKey[:])
return GetAddressFromPublicKey(privateKey.GetPublicKey())
}
6 years ago
// GetAddressFromInt is the temporary helper function for benchmark use
func GetAddressFromInt(value int) [20]byte {
6 years ago
priKey := [32]byte{}
binary.LittleEndian.PutUint32(priKey[:], uint32(value))
return GetAddressFromPrivateKeyBytes(priKey)
}
6 years ago
// GetPrivateKeyScalarFromInt return private key scalar.
func GetPrivateKeyScalarFromInt(value int) kyber.Scalar {
return crypto.Ed25519Curve.Scalar().SetInt64(int64(value))
}
6 years ago
// GetBLSPrivateKeyFromInt returns bls private key
func GetBLSPrivateKeyFromInt(value int) *bls.SecretKey {
priKey := [32]byte{}
binary.LittleEndian.PutUint32(priKey[:], uint32(value))
var privateKey bls.SecretKey
privateKey.SetLittleEndian(priKey[:])
return &privateKey
}
6 years ago
// GetPrivateKeyFromInt returns private key in bytes given an interger.
func GetPrivateKeyFromInt(value int) [32]byte {
priKey, err := crypto.Ed25519Curve.Scalar().SetInt64(int64(value)).MarshalBinary()
priKeyBytes := [32]byte{}
if err == nil {
copy(priKeyBytes[:], priKey[:])
}
return priKeyBytes
}
6 years ago
// GetPublicKeyFromPrivateKey return public key from private key.
func GetPublicKeyFromPrivateKey(priKey [32]byte) kyber.Point {
suite := crypto.Ed25519Curve
scalar := suite.Scalar()
scalar.UnmarshalBinary(priKey[:])
return suite.Point().Mul(scalar, nil)
}
6 years ago
// GetPublicKeyFromScalar is the same as GetPublicKeyFromPrivateKey, but it directly works on kyber.Scalar object.
func GetPublicKeyFromScalar(priKey kyber.Scalar) kyber.Point {
return crypto.Ed25519Curve.Point().Mul(priKey, nil)
}
6 years ago
// GetBytesFromPublicKey converts public key point to bytes
func GetBytesFromPublicKey(pubKey kyber.Point) [32]byte {
bytes, err := pubKey.MarshalBinary()
result := [32]byte{}
if err == nil {
copy(result[:], bytes)
}
return result
}