Beispiel #1
0
//generates deterministic keypair with weak SHA256 hash of seed
//internal use only
//be extremely careful with golang slice semantics
func generateDeterministicKeyPair(seed []byte) ([]byte, []byte) {
	if seed == nil {
		log.Panic()
	}

	pubkey_len := C.int(33)
	const seckey_len = 32

	var pubkey []byte = make([]byte, pubkey_len)
	var seckey []byte = make([]byte, seckey_len)

	var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
	var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))

new_seckey:
	seed = SumSHA256(seed[0:32])
	copy(seckey[0:32], seed[0:32])
	if C.secp256k1_ecdsa_seckey_verify(seckey_ptr) != 1 {
		goto new_seckey //rehash seckey until it succeeds, almost never happens
	}

	ret := C.secp256k1_ecdsa_pubkey_create(
		pubkey_ptr, &pubkey_len,
		seckey_ptr, 1)

	if ret != 1 {
		log.Panic("secp256k1-g0, generateDeterministicKeyPair, pubkey generation failing for valid seckey")
	}

	return pubkey, seckey
}
Beispiel #2
0
func GenerateKeyPair() ([]byte, []byte) {

	pubkey_len := C.int(33)
	const seckey_len = 32

	var pubkey []byte = make([]byte, pubkey_len)
	var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))

	var ret C.int

new_seckey:
	var seckey []byte = RandByte(seckey_len)
	var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))

	ret = C.secp256k1_ecdsa_seckey_verify(seckey_ptr)

	if ret != 1 {
		goto new_seckey
	}

	//for C.secp256k1_ecdsa_seckey_verify(seckey_ptr) != 1 {
	//	seckey = RandByte(seckey_len)
	//	seckey_ptr = (*C.uchar)(unsafe.Pointer(&seckey[0]))
	//}

	ret = C.secp256k1_ecdsa_pubkey_create(
		pubkey_ptr, &pubkey_len,
		seckey_ptr, 1)

	if ret != 1 {
		goto new_seckey
	}

	return pubkey, seckey
}
Beispiel #3
0
//generates deterministic keypair with weak SHA256 hash of seed
//internal use only
func generateDeterministicKeyPair(seed []byte) ([]byte, []byte) {
	if seed == nil {
		log.Panic()
	}
	seed_hash := SumSHA256(seed) //hash the seed

	pubkey_len := C.int(33)
	const seckey_len = 32

	var pubkey []byte = make([]byte, pubkey_len)
	var seckey []byte = make([]byte, seckey_len)
	copy(seckey[0:32], seed_hash[0:32])

	var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
	var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))

	ret := C.secp256k1_ecdsa_pubkey_create(
		pubkey_ptr, &pubkey_len,
		seckey_ptr, 1)

	if ret != 1 {
		//is this even possible?
		//invalid secret, try different
		seed_hash = SumSHA256(seed_hash[0:32])
		return GenerateDeterministicKeyPair(seed_hash)
	}

	return pubkey, seckey
}
Beispiel #4
0
//returns nil on error
//Does not return same output as generator does
func PubkeyFromSeckey(SecKey []byte) []byte {
	if len(SecKey) != 32 {
		log.Panic("PubkeyFromSeckey: invalid length")
	}

	pubkey_len := C.int(33)
	const seckey_len = 32

	var pubkey []byte = make([]byte, pubkey_len)
	var seckey []byte = make([]byte, seckey_len)
	copy(seckey, SecKey)

	var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
	var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))

	//ensure that private key is valid
	ret := C.secp256k1_ecdsa_seckey_verify(seckey_ptr)
	if ret != 1 {
		log.Panic("PubkeyFromSeckey: invalid seckey")
	}
	//create public key from seckey
	ret = C.secp256k1_ecdsa_pubkey_create(
		pubkey_ptr, &pubkey_len,
		seckey_ptr, 1)

	if ret != 1 {
		return nil
	}
	return pubkey
}
Beispiel #5
0
//returns nil on error
func PubkeyFromSeckey(SecKey []byte, compressed bool) []byte {
	if len(SecKey) != 32 {
		log.Panic("PubkeyFromSeckey: invalid length")
	}

	pubkey_len := C.int(65)
	compressflag := C.int(0)
	if compressed {
		pubkey_len = C.int(33)
		compressflag = C.int(1)
	}
	const seckey_len = 32

	var pubkey []byte = make([]byte, pubkey_len)
	var seckey []byte = make([]byte, seckey_len)
	copy(seckey, SecKey)

	var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
	var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))

	ret := C.secp256k1_ecdsa_pubkey_create(
		pubkey_ptr, &pubkey_len,
		seckey_ptr, compressflag)

	if ret != 1 {
		return nil
	}
	return pubkey
}
Beispiel #6
0
func GenerateKeyPair() ([]byte, []byte) {

	pubkey_len := C.int(33)
	const seckey_len = 32

	var pubkey []byte = make([]byte, pubkey_len)
	var seckey []byte = RandByte(seckey_len)

	var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
	var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))

	ret := C.secp256k1_ecdsa_pubkey_create(
		pubkey_ptr, &pubkey_len,
		seckey_ptr, 1)

	if ret != 1 {
		return GenerateKeyPair() //invalid secret, try again
	}
	return pubkey, seckey
}
Beispiel #7
0
// Returns nil on error
func PubkeyFromSeckey(secIn []byte) []byte {
	if VerifySeckey(secIn) != 1 {
		log.Panic("PubkeyFromSeckey: invalid secret key")
	}

	pubkey_len := C.int(33)
	const seckey_len = 32

	var pubkey []byte = make([]byte, pubkey_len)
	var seckey []byte = make([]byte, seckey_len)
	copy(seckey, secIn)

	var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
	var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))

	ret := C.secp256k1_ecdsa_pubkey_create(
		pubkey_ptr, &pubkey_len,
		seckey_ptr, 1)

	if ret != 1 {
		return nil
	}
	return pubkey
}