Skip to content

Commit

Permalink
secp256k1: Optimize precomp value storage.
Browse files Browse the repository at this point in the history
This optimizes the storage of the pre-computed byte points used to
accelerate scalar base multiplication to store the data as 32-byte
big-endian integers instead of as a series of little-endian uint32s
which reduces the amount of uncompressed bytes that need to stored by
128KiB.

Further, since the compressed table is stored in the string table of the
binary, it also reduces the size the of final binary by ~92.5KiB.
  • Loading branch information
davecgh committed Feb 23, 2022
1 parent 8f4537c commit 167ec68
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dcrec/secp256k1/compressedbytepoints.go

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions dcrec/secp256k1/genstatics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package secp256k1
// [GECC]: Guide to Elliptic Curve Cryptography (Hankerson, Menezes, Vanstone)

import (
"encoding/binary"
"math/big"
)

Expand All @@ -35,7 +34,7 @@ func SerializedBytePoints() []byte {

// Separate the bits into byte-sized windows.
curveByteSize := curveParams.BitSize / 8
serialized := make([]byte, curveByteSize*256*2*10*4)
serialized := make([]byte, curveByteSize*256*2*32)
offset := 0
for byteNum := 0; byteNum < curveByteSize; byteNum++ {
// Grab the 8 bits that make up this byte from doubling points.
Expand All @@ -53,14 +52,10 @@ func SerializedBytePoints() []byte {
}
point.ToAffine()

for i := 0; i < len(point.X.n); i++ {
binary.LittleEndian.PutUint32(serialized[offset:], point.X.n[i])
offset += 4
}
for i := 0; i < len(point.Y.n); i++ {
binary.LittleEndian.PutUint32(serialized[offset:], point.Y.n[i])
offset += 4
}
point.X.PutBytesUnchecked(serialized[offset:])
offset += 32
point.Y.PutBytesUnchecked(serialized[offset:])
offset += 32
}
}

Expand Down
13 changes: 4 additions & 9 deletions dcrec/secp256k1/loadprecomputed.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package secp256k1
import (
"compress/zlib"
"encoding/base64"
"encoding/binary"
"io"
"strings"
"sync"
Expand Down Expand Up @@ -69,14 +68,10 @@ var s256BytePoints = func() func() *bytePointTable {
for i := 0; i < len(bytePoints[byteNum]); i++ {
px := &bytePoints[byteNum][i][0]
py := &bytePoints[byteNum][i][1]
for i := 0; i < len(px.n); i++ {
px.n[i] = binary.LittleEndian.Uint32(serialized[offset:])
offset += 4
}
for i := 0; i < len(py.n); i++ {
py.n[i] = binary.LittleEndian.Uint32(serialized[offset:])
offset += 4
}
px.SetByteSlice(serialized[offset:])
offset += 32
py.SetByteSlice(serialized[offset:])
offset += 32
}
}
data = &bytePoints
Expand Down

0 comments on commit 167ec68

Please sign in to comment.