From 3cb10d14b7671ceee374d90ae0d4c3d024838f8a Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 27 Apr 2022 09:02:53 -0400 Subject: [dev.boringcrypto] crypto/internal/boring: avoid allocation in big.Int conversion The conversion via byte slices is inefficient; we can convert via word slices and avoid the copy entirely. For #51940. Change-Id: I06f747e0acffffae427d9706d43bdacf146c027d Reviewed-on: https://go-review.googlesource.com/c/go/+/395875 Reviewed-by: Roland Shoemaker Run-TryBot: Russ Cox TryBot-Result: Gopher Robot --- src/crypto/internal/boring/boring.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'src/crypto/internal/boring/boring.go') diff --git a/src/crypto/internal/boring/boring.go b/src/crypto/internal/boring/boring.go index 29e0baa131..7c6a730e0b 100644 --- a/src/crypto/internal/boring/boring.go +++ b/src/crypto/internal/boring/boring.go @@ -18,6 +18,8 @@ import ( "crypto/internal/boring/sig" _ "crypto/internal/boring/syso" "math/big" + "math/bits" + "unsafe" ) const available = true @@ -58,15 +60,26 @@ type fail string func (e fail) Error() string { return "boringcrypto: " + string(e) + " failed" } +func wbase(b []big.Word) *C.uint8_t { + if len(b) == 0 { + return nil + } + return (*C.uint8_t)(unsafe.Pointer(&b[0])) +} + +const wordBytes = bits.UintSize / 8 + func bigToBN(x *big.Int) *C.GO_BIGNUM { - raw := x.Bytes() - return C._goboringcrypto_BN_bin2bn(base(raw), C.size_t(len(raw)), nil) + raw := x.Bits() + return C._goboringcrypto_BN_le2bn(wbase(raw), C.size_t(len(raw)*wordBytes), nil) } func bnToBig(bn *C.GO_BIGNUM) *big.Int { - raw := make([]byte, C._goboringcrypto_BN_num_bytes(bn)) - n := C._goboringcrypto_BN_bn2bin(bn, base(raw)) - return new(big.Int).SetBytes(raw[:n]) + raw := make([]big.Word, (C._goboringcrypto_BN_num_bytes(bn)+wordBytes-1)/wordBytes) + if C._goboringcrypto_BN_bn2le_padded(wbase(raw), C.size_t(len(raw)*wordBytes), bn) == 0 { + panic("boringcrypto: bignum conversion failed") + } + return new(big.Int).SetBits(raw) } func bigToBn(bnp **C.GO_BIGNUM, b *big.Int) bool { @@ -77,8 +90,7 @@ func bigToBn(bnp **C.GO_BIGNUM, b *big.Int) bool { if b == nil { return true } - raw := b.Bytes() - bn := C._goboringcrypto_BN_bin2bn(base(raw), C.size_t(len(raw)), nil) + bn := bigToBN(b) if bn == nil { return false } -- cgit v1.2.3-54-g00ecf