Skip to content

Commit

Permalink
secp256k1: Support go generate w/o removing file.
Browse files Browse the repository at this point in the history
This modifies the logic that deals with generating, loading, and
decompressing the compressed byte points to allow go generate to run
correctly regardless of whether or not the generated file already
exists.

It accomplishes this by making use of a function to obtain the
compressed byte points string instead of directly referencing the
variable such that the function is nil by default and only set to the
real function via the generated file.
  • Loading branch information
davecgh committed Feb 23, 2022
1 parent 81255ee commit 8f4537c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
9 changes: 8 additions & 1 deletion dcrec/secp256k1/compressedbytepoints.go

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion dcrec/secp256k1/genprecomps.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
base64.StdEncoding.Encode(encoded, compressed.Bytes())

fmt.Fprintln(fi, "// Copyright (c) 2015 The btcsuite developers")
fmt.Fprintln(fi, "// Copyright (c) 2015-2021 The Decred developers")
fmt.Fprintln(fi, "// Copyright (c) 2015-2022 The Decred developers")
fmt.Fprintln(fi, "// Use of this source code is governed by an ISC")
fmt.Fprintln(fi, "// license that can be found in the LICENSE file.")
fmt.Fprintln(fi)
Expand All @@ -54,6 +54,13 @@ func main() {
fmt.Fprintln(fi, "// DO NOT EDIT")
fmt.Fprintln(fi)
fmt.Fprintf(fi, "var compressedBytePoints = %q\n", string(encoded))
fmt.Fprintln(fi)
fmt.Fprintln(fi, "// Set accessor to a real function.")
fmt.Fprintln(fi, "func init() {")
fmt.Fprintln(fi, " compressedBytePointsFn = func() string {")
fmt.Fprintln(fi, " return compressedBytePoints")
fmt.Fprintln(fi, " }")
fmt.Fprintln(fi, "}")

a1, b1, a2, b2 := secp256k1.EndomorphismVectors()
fmt.Println("The following values are the computed linearly " +
Expand Down
4 changes: 0 additions & 4 deletions dcrec/secp256k1/genstatics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (
"math/big"
)

// compressedBytePoints are dummy points used so the code which generates the
// real values can compile.
var compressedBytePoints = ""

// SerializedBytePoints returns a serialized byte slice which contains all of
// the possible points per 8-bit window. This is used to when generating
// compressedbytepoints.go.
Expand Down
9 changes: 7 additions & 2 deletions dcrec/secp256k1/loadprecomputed.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
// accelerating scalar base multiplication.
type bytePointTable [32][256][2]FieldVal

// compressedBytePointsFn is set to a real function by the code generation to
// return the compressed pre-computed values for accelerating scalar base
// multiplication.
var compressedBytePointsFn func() string

// s256BytePoints houses pre-computed values used to accelerate scalar base
// multiplication such that they are only loaded on first use.
var s256BytePoints = func() func() *bytePointTable {
Expand All @@ -38,10 +43,10 @@ var s256BytePoints = func() func() *bytePointTable {
var data *bytePointTable
mustLoadBytePoints := func() {
// There will be no byte points to load when generating them.
bp := compressedBytePoints
if len(bp) == 0 {
if compressedBytePointsFn == nil {
return
}
bp := compressedBytePointsFn()

// Decompress the pre-computed table used to accelerate scalar base
// multiplication.
Expand Down

0 comments on commit 8f4537c

Please sign in to comment.