Skip to content

Commit fe9a28c

Browse files
committed
secp256k1: No allocs in slow scalar base mult path.
This moves the conversion of the base point represented in Jacobian coordinates outside of the slow scalar base multiplication path used in resource constrained environments to avoid unnecessary allocations. name old time/op new time/op delta -------------------------------------------------------------------------------- ScalarBaseMultNonConstSlow 91.9µs ± 1% 91.5µs ± 1% ~ (p=0.280 n=10+10) name old alloc/op new alloc/op delta -------------------------------------------------------------------------------- ScalarBaseMultNonConstSlow 64.0B ± 0% 0.0B -100.00% (p=0.000 n=10+10) name old allocs/op new allocs/op delta -------------------------------------------------------------------------------- ScalarBaseMultNonConstSlow 2.00 ± 0% 0.00 -100.00% (p=0.000 n=10+10)
1 parent 2104419 commit fe9a28c

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

dcrec/secp256k1/curve.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,11 +1224,17 @@ func ScalarBaseMultNonConst(k *ModNScalar, result *JacobianPoint) {
12241224
scalarBaseMultNonConst(k, result)
12251225
}
12261226

1227-
// scalarBaseMultNonConstSlow computes k*G through ScalarMultNonConst.
1228-
func scalarBaseMultNonConstSlow(k *ModNScalar, result *JacobianPoint) {
1227+
// jacobianG is the secp256k1 base point converted to Jacobian coordinates and
1228+
// is defined here to avoid repeatedly converting it.
1229+
var jacobianG = func() JacobianPoint {
12291230
var G JacobianPoint
12301231
bigAffineToJacobian(curveParams.Gx, curveParams.Gy, &G)
1231-
ScalarMultNonConst(k, &G, result)
1232+
return G
1233+
}()
1234+
1235+
// scalarBaseMultNonConstSlow computes k*G through ScalarMultNonConst.
1236+
func scalarBaseMultNonConstSlow(k *ModNScalar, result *JacobianPoint) {
1237+
ScalarMultNonConst(k, &jacobianG, result)
12321238
}
12331239

12341240
// scalarBaseMultNonConstFast computes k*G through the precomputed lookup

0 commit comments

Comments
 (0)