Skip to content

Commit

Permalink
secp256k1: No allocs in slow scalar base mult path.
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
davecgh committed Mar 21, 2024
1 parent 2104419 commit fe9a28c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions dcrec/secp256k1/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,11 +1224,17 @@ func ScalarBaseMultNonConst(k *ModNScalar, result *JacobianPoint) {
scalarBaseMultNonConst(k, result)
}

// scalarBaseMultNonConstSlow computes k*G through ScalarMultNonConst.
func scalarBaseMultNonConstSlow(k *ModNScalar, result *JacobianPoint) {
// jacobianG is the secp256k1 base point converted to Jacobian coordinates and
// is defined here to avoid repeatedly converting it.
var jacobianG = func() JacobianPoint {
var G JacobianPoint
bigAffineToJacobian(curveParams.Gx, curveParams.Gy, &G)
ScalarMultNonConst(k, &G, result)
return G
}()

// scalarBaseMultNonConstSlow computes k*G through ScalarMultNonConst.
func scalarBaseMultNonConstSlow(k *ModNScalar, result *JacobianPoint) {
ScalarMultNonConst(k, &jacobianG, result)
}

// scalarBaseMultNonConstFast computes k*G through the precomputed lookup
Expand Down

0 comments on commit fe9a28c

Please sign in to comment.