//generate signature in repeatable way func SignDeterministic(msg []byte, seckey []byte, nonce_seed []byte) []byte { nonce := SumSHA256(nonce_seed) //deterministicly generate nonce var sig []byte = make([]byte, 65) var recid C.int var msg_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&msg[0])) var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0])) var nonce_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&nonce[0])) var sig_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&sig[0])) if C.secp256k1_ecdsa_seckey_verify(seckey_ptr) != C.int(1) { log.Panic("Invalid secret key") } ret := C.secp256k1_ecdsa_sign_compact( msg_ptr, C.int(len(msg)), sig_ptr, seckey_ptr, nonce_ptr, &recid) sig[64] = byte(int(recid)) if int(recid) > 4 { log.Panic() } if ret != 1 { return SignDeterministic(msg, seckey, nonce_seed) //nonce invalid,retry } return sig }
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 Sign(msg []byte, seckey []byte) []byte { var nonce []byte = RandByte(32) var sig []byte = make([]byte, 65) var recid C.int var msg_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&msg[0])) var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0])) var nonce_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&nonce[0])) var sig_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&sig[0])) if C.secp256k1_ecdsa_seckey_verify(seckey_ptr) != C.int(1) { log.Panic("Invalid secret key") } ret := C.secp256k1_ecdsa_sign_compact( msg_ptr, C.int(len(msg)), sig_ptr, seckey_ptr, nonce_ptr, &recid) sig[64] = byte(int(recid)) if int(recid) > 4 { log.Panic() } if ret != 1 { return Sign(msg, seckey) //nonce invalid,retry } return sig }
//Rename SignHash func Sign(msg []byte, seckey []byte) []byte { if len(seckey) != 32 { log.Panic("Sign, Invalid seckey length") } if msg == nil { log.Panic("Sign, message nil") } var nonce []byte = RandByte(32) //going to get bitcoins stolen! var sig []byte = make([]byte, 65) var recid C.int var msg_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&msg[0])) var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0])) var nonce_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&nonce[0])) var sig_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&sig[0])) if C.secp256k1_ecdsa_seckey_verify(seckey_ptr) != C.int(1) { log.Panic() //invalid seckey } ret := C.secp256k1_ecdsa_sign_compact( msg_ptr, C.int(len(msg)), sig_ptr, seckey_ptr, nonce_ptr, &recid) sig[64] = byte(int(recid)) if int(recid) > 4 { log.Panic() } if ret != 1 { return Sign(msg, seckey) //nonce invalid,retry } return sig }