diff --git a/c/openssl/bn.go b/c/openssl/bn.go index 4a765b1c..225dd324 100644 --- a/c/openssl/bn.go +++ b/c/openssl/bn.go @@ -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() {} // ----------------------------------------------------------------------------- diff --git a/internal/lib/math/big/int.go b/internal/lib/math/big/int.go index 02341907..f3ce8724 100644 --- a/internal/lib/math/big/int.go +++ b/internal/lib/math/big/int.go @@ -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 }