github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/client/speedups/sipadll.go (about)

     1  package main
     2  
     3  /*
     4    This is a EC_Verify speedup that works only with Windows
     5  
     6    Use secp256k1.dll from gocoin/tools/sipa_dll
     7    or build one yourself.
     8  
     9  */
    10  
    11  import (
    12  	"encoding/hex"
    13  	"github.com/piotrnar/gocoin/client/common"
    14  	"github.com/piotrnar/gocoin/lib/btc"
    15  	"os"
    16  	"syscall"
    17  	"unsafe"
    18  )
    19  
    20  var (
    21  	dll           = syscall.NewLazyDLL("secp256k1.dll")
    22  	DLL_EC_Verify = dll.NewProc("EC_Verify")
    23  	DLL_Schnorr_Verify = dll.NewProc("Schnorr_Verify")
    24  	DLL_CheckPayToContract = dll.NewProc("CheckPayToContract")
    25  )
    26  
    27  func EC_Verify(pkey, sign, hash []byte) bool {
    28  	r1, _, _ := syscall.Syscall6(DLL_EC_Verify.Addr(), 6,
    29  		uintptr(unsafe.Pointer(&hash[0])), uintptr(32),
    30  		uintptr(unsafe.Pointer(&sign[0])), uintptr(len(sign)),
    31  		uintptr(unsafe.Pointer(&pkey[0])), uintptr(len(pkey)))
    32  	return r1 == 1
    33  }
    34  
    35  func Schnorr_Verify(pkey, sign, msg []byte) bool {
    36  	r1, _, _ := syscall.Syscall(DLL_Schnorr_Verify.Addr(), 3,
    37  		uintptr(unsafe.Pointer(&msg[0])),
    38  		uintptr(unsafe.Pointer(&sign[0])),
    39  		uintptr(unsafe.Pointer(&pkey[0])))
    40  	return r1 == 1
    41  }
    42  
    43  func CheckPayToContract(kd, base, hash []byte, parity bool) bool {
    44  	var par uintptr
    45  	if parity {
    46  		par = 1
    47  	}
    48  	r1, _, _ := syscall.Syscall6(DLL_CheckPayToContract.Addr(), 4,
    49  		uintptr(unsafe.Pointer(&kd[0])),
    50  		uintptr(unsafe.Pointer(&base[0])),
    51  		uintptr(unsafe.Pointer(&hash[0])), par, 0, 0)
    52  	return r1 == 1
    53  }
    54  
    55  func ec_verify() bool {
    56  	key, _ := hex.DecodeString("020eaebcd1df2df853d66ce0e1b0fda07f67d1cabefde98514aad795b86a6ea66d")
    57  	sig, _ := hex.DecodeString("3045022100fe00e013c244062847045ae7eb73b03fca583e9aa5dbd030a8fd1c6dfcf11b1002207d0d04fed8fa1e93007468d5a9e134b0a7023b6d31db4e50942d43a250f4d07c01")
    58  	has, _ := hex.DecodeString("3382219555ddbb5b00e0090f469e590ba1eae03c7f28ab937de330aa60294ed6")
    59  	return EC_Verify(key, sig, has)
    60  }
    61  
    62  func schnorr_verify() bool {
    63  	key, _ := hex.DecodeString("DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659")
    64  	sig, _ := hex.DecodeString("6896BD60EEAE296DB48A229FF71DFE071BDE413E6D43F917DC8DCF8C78DE33418906D11AC976ABCCB20B091292BFF4EA897EFCB639EA871CFA95F6DE339E4B0A")
    65  	msg, _ := hex.DecodeString("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
    66  	return Schnorr_Verify(key, sig, msg)
    67  }
    68  
    69  func p2scr_verify() bool {
    70  	kd, _ := hex.DecodeString("afaf8a67be00186668f74740e34ffce748139c2b73c9fbd2c1f33e48a612a75d")
    71  	base, _ := hex.DecodeString("f1cbd3f2430910916144d5d2bf63d48a6281e5b8e6ade31413adccff3d8839d4")
    72  	hash, _ := hex.DecodeString("93a760e87123883022cbd462ac40571176cf09d9d2c6168759fee6c2b079fdd8")
    73  	return CheckPayToContract(kd, base, hash, true)
    74  }
    75  
    76  func init() {
    77  	if !ec_verify() {
    78  		println("ERROR: Could not initiate secp256k1.dll (EC_Verify failed)")
    79  		os.Exit(1)
    80  	}
    81  	if !schnorr_verify() {
    82  		println("ERROR: Could not initiate secp256k1.dll (Schnorr_Verify failed)")
    83  		os.Exit(1)
    84  	}
    85  	if !p2scr_verify() {
    86  		println("ERROR: Could not initiate secp256k1.dll (CheckPayToContract failed)")
    87  		os.Exit(1)
    88  	}
    89  
    90  	common.Log.Println("Using secp256k1.dll of Bitcoin Core for EC_Verify, SchnorrVerify & CheckPayToContact")
    91  	btc.EC_Verify = EC_Verify
    92  	btc.Schnorr_Verify = Schnorr_Verify
    93  	btc.Check_PayToContract = CheckPayToContract
    94  }