add BN_CTX Start, Get, End and Add big.Int Mul and test it

This commit is contained in:
tsingbx
2024-08-08 09:10:31 +08:00
parent 0a8bad46b5
commit 289caa7cc2
2 changed files with 28 additions and 1 deletions

View File

@@ -49,8 +49,19 @@ func BN_CTXSecureNew() *BN_CTX
func (*BN_CTX) Free() {}
// void BN_CTX_start(BN_CTX *ctx);
//
// llgo:link (*BN_CTX).Start C.BN_CTX_start
func (*BN_CTX) Start() {}
// BIGNUM *BN_CTX_get(BN_CTX *ctx);
//
// llgo:link (*BN_CTX).Get C.BN_CTX_get
func (*BN_CTX) Get() *BIGNUM { return nil }
// void BN_CTX_end(BN_CTX *ctx);
//
// llgo:link (*BN_CTX).End C.BN_CTX_end
func (*BN_CTX) End() {}
// -----------------------------------------------------------------------------

View File

@@ -18,6 +18,8 @@ package big
// llgo:skipall
import (
"sync"
"github.com/goplus/llgo/c/openssl"
)
@@ -35,6 +37,18 @@ func ctxPut(ctx *openssl.BN_CTX) {
ctx.Free()
}
var g_lock = &sync.Mutex{}
var g_ctx *openssl.BN_CTX
func getCtxInstance() *openssl.BN_CTX {
if g_ctx == nil {
g_lock.Lock()
defer g_lock.Unlock()
g_ctx = ctxGet()
}
return g_ctx
}
// -----------------------------------------------------------------------------
type Int openssl.BIGNUM
@@ -147,7 +161,9 @@ func (z *Int) Mul(x, y *Int) *Int {
a := (*openssl.BIGNUM)(z)
xx := (*openssl.BIGNUM)(x)
yy := (*openssl.BIGNUM)(y)
a.Mul(a, xx, yy, ctxGet())
getCtxInstance().Start()
defer getCtxInstance().End()
a.Mul(a, xx, yy, getCtxInstance())
return z
}