github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/lib/others/cgo/ec_bench/sipadll.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/hex"
     5  	"runtime"
     6  	"sync"
     7  	"syscall"
     8  	"time"
     9  	"unsafe"
    10  )
    11  
    12  var (
    13  	secp256k1     = syscall.NewLazyDLL("secp256k1.dll")
    14  	DLL_EC_Verify = secp256k1.NewProc("EC_Verify")
    15  )
    16  
    17  func EC_Verify(pkey, sign, hash []byte) int32 {
    18  	r1, _, _ := syscall.Syscall6(DLL_EC_Verify.Addr(), 6,
    19  		uintptr(unsafe.Pointer(&hash[0])), uintptr(32),
    20  		uintptr(unsafe.Pointer(&sign[0])), uintptr(len(sign)),
    21  		uintptr(unsafe.Pointer(&pkey[0])), uintptr(len(pkey)))
    22  	return int32(r1)
    23  }
    24  
    25  var CNT int = 100e3
    26  
    27  func main() {
    28  	key, _ := hex.DecodeString("040eaebcd1df2df853d66ce0e1b0fda07f67d1cabefde98514aad795b86a6ea66dbeb26b67d7a00e2447baeccc8a4cef7cd3cad67376ac1c5785aeebb4f6441c16")
    29  	sig, _ := hex.DecodeString("3045022100fe00e013c244062847045ae7eb73b03fca583e9aa5dbd030a8fd1c6dfcf11b1002207d0d04fed8fa1e93007468d5a9e134b0a7023b6d31db4e50942d43a250f4d07c01")
    30  	msg, _ := hex.DecodeString("3382219555ddbb5b00e0090f469e590ba1eae03c7f28ab937de330aa60294ed6")
    31  	var wg sync.WaitGroup
    32  	max_routines := make(chan bool, 2*runtime.NumCPU())
    33  	println("Number of threads:", cap(max_routines))
    34  	sta := time.Now()
    35  	for i := 0; i < CNT; i++ {
    36  		wg.Add(1)
    37  		max_routines <- true
    38  		go func() {
    39  			if EC_Verify(key, sig, msg) != 1 {
    40  				println("Verify error")
    41  			}
    42  			wg.Done()
    43  			<-max_routines
    44  		}()
    45  	}
    46  	wg.Wait()
    47  	sto := time.Now()
    48  	println((sto.UnixNano()-sta.UnixNano())/int64(CNT), "ns per ECDSA_Verify")
    49  }