func Sign(msg []byte, seckey []byte) ([]byte, error) { nonce := randentropy.GetEntropyCSPRNG(32) var sig []byte = make([]byte, 65) var recid C.int var msg_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&msg[0])) var sig_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&sig[0])) var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0])) var noncefp_ptr = &(*C.secp256k1_nonce_function_default) var ndata_ptr = unsafe.Pointer(&nonce[0]) if C.secp256k1_ec_seckey_verify(seckey_ptr) != C.int(1) { return nil, errors.New("Invalid secret key") } ret := C.secp256k1_ecdsa_sign_compact( msg_ptr, sig_ptr, seckey_ptr, noncefp_ptr, ndata_ptr, &recid) sig[64] = byte(int(recid)) if ret != C.int(1) { // nonce invalid, retry return Sign(msg, seckey) } return sig, nil }
func VerifySeckeyValidity(seckey []byte) error { if len(seckey) != 32 { return errors.New("priv key is not 32 bytes") } var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0])) ret := C.secp256k1_ec_seckey_verify(seckey_ptr) if int(ret) != 1 { return errors.New("invalid seckey") } return nil }
func Sign(msg []byte, seckey []byte) ([]byte, error) { msg_ptr := (*C.uchar)(unsafe.Pointer(&msg[0])) seckey_ptr := (*C.uchar)(unsafe.Pointer(&seckey[0])) sig := make([]byte, 65) sig_ptr := (*C.secp256k1_ecdsa_recoverable_signature)(unsafe.Pointer(&sig[0])) nonce := randentropy.GetEntropyCSPRNG(32) ndata_ptr := unsafe.Pointer(&nonce[0]) noncefp_ptr := &(*C.secp256k1_nonce_function_default) if C.secp256k1_ec_seckey_verify(context, seckey_ptr) != C.int(1) { return nil, errors.New("Invalid secret key") } ret := C.secp256k1_ecdsa_sign_recoverable( context, sig_ptr, msg_ptr, seckey_ptr, noncefp_ptr, ndata_ptr, ) if ret == C.int(0) { return Sign(msg, seckey) //invalid secret, try again } sig_serialized := make([]byte, 65) sig_serialized_ptr := (*C.uchar)(unsafe.Pointer(&sig_serialized[0])) var recid C.int C.secp256k1_ecdsa_recoverable_signature_serialize_compact( context, sig_serialized_ptr, // 64 byte compact signature &recid, sig_ptr, // 65 byte "recoverable" signature ) sig_serialized[64] = byte(int(recid)) // add back recid to get 65 bytes sig return sig_serialized, nil }
func Seckey_verify(seckey [32]byte) bool { success := C.secp256k1_ec_seckey_verify(cBuf(seckey[:])) return goBool(success) }