Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

secp256k1: Reduce scalar base mult copies. #2898

Merged
merged 1 commit into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions dcrec/secp256k1/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,23 +1223,20 @@ func ScalarMultNonConst(k *ModNScalar, point, result *JacobianPoint) {
func ScalarBaseMultNonConst(k *ModNScalar, result *JacobianPoint) {
bytePoints := s256BytePoints()

// Point Q = ∞ (point at infinity).
var q JacobianPoint
// Start with the point at infinity.
result.X.Zero()
result.Y.Zero()
result.Z.Zero()

// bytePoints has all 256 byte points for each 8-bit window. The strategy
// is to add up the byte points. This is best understood by expressing k in
// base-256 which it already sort of is. Each "digit" in the 8-bit window
// can be looked up using bytePoints and added together.
var pt JacobianPoint
for i, byteVal := range k.Bytes() {
p := bytePoints[i][byteVal]
pt.X.Set(&p[0])
pt.Y.Set(&p[1])
pt.Z.SetInt(1)
AddNonConst(&q, &pt, &q)
kb := k.Bytes()
for i := 0; i < len(kb); i++ {
pt := &bytePoints[i][kb[i]]
AddNonConst(result, pt, result)
}

result.Set(&q)
}

// isOnCurve returns whether or not the affine point (x,y) is on the curve.
Expand Down
10 changes: 5 additions & 5 deletions dcrec/secp256k1/loadprecomputed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// bytePointTable describes a table used to house pre-computed values for
// accelerating scalar base multiplication.
type bytePointTable [32][256][2]FieldVal
type bytePointTable [32][256]JacobianPoint

// compressedBytePointsFn is set to a real function by the code generation to
// return the compressed pre-computed values for accelerating scalar base
Expand Down Expand Up @@ -66,12 +66,12 @@ var s256BytePoints = func() func() *bytePointTable {
for byteNum := 0; byteNum < len(bytePoints); byteNum++ {
// All points in this window.
for i := 0; i < len(bytePoints[byteNum]); i++ {
px := &bytePoints[byteNum][i][0]
py := &bytePoints[byteNum][i][1]
px.SetByteSlice(serialized[offset:])
p := &bytePoints[byteNum][i]
p.X.SetByteSlice(serialized[offset:])
offset += 32
py.SetByteSlice(serialized[offset:])
p.Y.SetByteSlice(serialized[offset:])
offset += 32
p.Z.SetInt(1)
}
}
data = &bytePoints
Expand Down