Skip to content

Instantly share code, notes, and snippets.

@josselin-c
Created November 9, 2017 07:44
Show Gist options
  • Save josselin-c/9c66bac044e1489c290c37c43250ff28 to your computer and use it in GitHub Desktop.
Save josselin-c/9c66bac044e1489c290c37c43250ff28 to your computer and use it in GitHub Desktop.
diff --git a/crypto/secp256k1/curve.go b/crypto/secp256k1/curve.go
index ec6d266c..b9aa5765 100644
--- a/crypto/secp256k1/curve.go
+++ b/crypto/secp256k1/curve.go
@@ -35,17 +35,11 @@ import (
"crypto/elliptic"
"math/big"
"sync"
- "unsafe"
+ //"unsafe"
"github.com/ethereum/go-ethereum/common/math"
)
-/*
-#include "libsecp256k1/include/secp256k1.h"
-extern int secp256k1_pubkey_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar);
-*/
-import "C"
-
// This code is from https://github.com/ThePiachu/GoBit and implements
// several Koblitz elliptic curves over prime fields.
//
@@ -220,6 +214,8 @@ func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int,
}
func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
+ panic("ScalarMult")
+
// Ensure scalar is exactly 32 bytes. We pad always, even if
// scalar is 32 bytes long, to avoid a timing side channel.
if len(scalar) > 32 {
@@ -234,9 +230,10 @@ func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int,
point := make([]byte, 64)
math.ReadBits(Bx, point[:32])
math.ReadBits(By, point[32:])
- pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
- scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
- res := C.secp256k1_pubkey_scalar_mul(context, pointPtr, scalarPtr)
+ res := 1
+ //STUBED pointPtr := (*C.uchar)(unsafe.Pointer(&point[0]))
+ //STUBED scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0]))
+ //STUBED res := C.secp256k1_pubkey_scalar_mul(context, pointPtr, scalarPtr)
// Unpack the result and clear temporaries.
x := new(big.Int).SetBytes(point[:32])
diff --git a/crypto/secp256k1/panic_cb.go b/crypto/secp256k1/panic_cb.go
deleted file mode 100644
index e0e9034e..00000000
--- a/crypto/secp256k1/panic_cb.go
+++ /dev/null
@@ -1,33 +0,0 @@
-// Copyright 2015 The go-ethereum Authors
-// This file is part of the go-ethereum library.
-//
-// The go-ethereum library is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// The go-ethereum library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public License
-// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
-
-package secp256k1
-
-import "C"
-import "unsafe"
-
-// Callbacks for converting libsecp256k1 internal faults into
-// recoverable Go panics.
-
-//export secp256k1GoPanicIllegal
-func secp256k1GoPanicIllegal(msg *C.char, data unsafe.Pointer) {
- panic("illegal argument: " + C.GoString(msg))
-}
-
-//export secp256k1GoPanicError
-func secp256k1GoPanicError(msg *C.char, data unsafe.Pointer) {
- panic("internal error: " + C.GoString(msg))
-}
diff --git a/crypto/secp256k1/secp256.go b/crypto/secp256k1/secp256.go
index 0ffa04fe..f3f753f9 100644
--- a/crypto/secp256k1/secp256.go
+++ b/crypto/secp256k1/secp256.go
@@ -17,39 +17,11 @@
// Package secp256k1 wraps the bitcoin secp256k1 C library.
package secp256k1
-/*
-#cgo CFLAGS: -I./libsecp256k1
-#cgo CFLAGS: -I./libsecp256k1/src/
-#define USE_NUM_NONE
-#define USE_FIELD_10X26
-#define USE_FIELD_INV_BUILTIN
-#define USE_SCALAR_8X32
-#define USE_SCALAR_INV_BUILTIN
-#define NDEBUG
-#include "./libsecp256k1/src/secp256k1.c"
-#include "./libsecp256k1/src/modules/recovery/main_impl.h"
-#include "ext.h"
-
-typedef void (*callbackFunc) (const char* msg, void* data);
-extern void secp256k1GoPanicIllegal(const char* msg, void* data);
-extern void secp256k1GoPanicError(const char* msg, void* data);
-*/
-import "C"
-
import (
"errors"
- "unsafe"
+ //"unsafe"
)
-var context *C.secp256k1_context
-
-func init() {
- // around 20 ms on a modern CPU.
- context = C.secp256k1_context_create_sign_verify()
- C.secp256k1_context_set_illegal_callback(context, C.callbackFunc(C.secp256k1GoPanicIllegal), nil)
- C.secp256k1_context_set_error_callback(context, C.callbackFunc(C.secp256k1GoPanicError), nil)
-}
-
var (
ErrInvalidMsgLen = errors.New("invalid message length, need 32 bytes")
ErrInvalidSignatureLen = errors.New("invalid signature length")
@@ -66,33 +38,36 @@ var (
// directly by an attacker. It is usually preferable to use a cryptographic
// hash function on any input before handing it to this function.
func Sign(msg []byte, seckey []byte) ([]byte, error) {
+ panic("Sign")
+
if len(msg) != 32 {
return nil, ErrInvalidMsgLen
}
if len(seckey) != 32 {
return nil, ErrInvalidKey
}
- seckeydata := (*C.uchar)(unsafe.Pointer(&seckey[0]))
- if C.secp256k1_ec_seckey_verify(context, seckeydata) != 1 {
- return nil, ErrInvalidKey
- }
+ sig := make([]byte, 65)
+ //STUBED seckeydata := (*C.uchar)(unsafe.Pointer(&seckey[0]))
+ //STUBED if C.secp256k1_ec_seckey_verify(context, seckeydata) != 1 {
+ //STUBED return nil, ErrInvalidKey
+ //STUBED }
- var (
- msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
- noncefunc = C.secp256k1_nonce_function_rfc6979
- sigstruct C.secp256k1_ecdsa_recoverable_signature
- )
- if C.secp256k1_ecdsa_sign_recoverable(context, &sigstruct, msgdata, seckeydata, noncefunc, nil) == 0 {
- return nil, ErrSignFailed
- }
+ //STUBED var (
+ //STUBED msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
+ //STUBED noncefunc = C.secp256k1_nonce_function_rfc6979
+ //STUBED sigstruct C.secp256k1_ecdsa_recoverable_signature
+ //STUBED )
+ //STUBED if C.secp256k1_ecdsa_sign_recoverable(context, &sigstruct, msgdata, seckeydata, noncefunc, nil) == 0 {
+ //STUBED return nil, ErrSignFailed
+ //STUBED }
- var (
- sig = make([]byte, 65)
- sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
- recid C.int
- )
- C.secp256k1_ecdsa_recoverable_signature_serialize_compact(context, sigdata, &recid, &sigstruct)
- sig[64] = byte(recid) // add back recid to get 65 bytes sig
+ //STUBED var (
+ //STUBED sig = make([]byte, 65)
+ //STUBED sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
+ //STUBED recid C.int
+ //STUBED )
+ //STUBED C.secp256k1_ecdsa_recoverable_signature_serialize_compact(context, sigdata, &recid, &sigstruct)
+ //STUBED sig[64] = byte(recid) // add back recid to get 65 bytes sig
return sig, nil
}
@@ -101,21 +76,24 @@ func Sign(msg []byte, seckey []byte) ([]byte, error) {
// sig must be a 65-byte compact ECDSA signature containing the
// recovery id as the last element.
func RecoverPubkey(msg []byte, sig []byte) ([]byte, error) {
+ panic("RecoverPubkey")
+
if len(msg) != 32 {
return nil, ErrInvalidMsgLen
}
if err := checkSignature(sig); err != nil {
return nil, err
}
+ pubkey := make([]byte, 65)
- var (
- pubkey = make([]byte, 65)
- sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
- msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
- )
- if C.secp256k1_ecdsa_recover_pubkey(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 {
- return nil, ErrRecoverFailed
- }
+ // STUBED var (
+ // STUBED pubkey = make([]byte, 65)
+ // STUBED sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
+ // STUBED msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
+ // STUBED )
+ // STUBED if C.secp256k1_ecdsa_recover_pubkey(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 {
+ // STUBED return nil, ErrRecoverFailed
+ // STUBED }
return pubkey, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment