Skip to content

Commit

Permalink
Merge pull request #363 from lightninglabs/btcec-v2-negation
Browse files Browse the repository at this point in the history
poolscript: make sure negation works with btcec v2
  • Loading branch information
guggero authored May 9, 2022
2 parents 0d92b40 + d63b815 commit 3e49f4c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
6 changes: 5 additions & 1 deletion poolscript/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,13 @@ func DecrementKey(pubKey *btcec.PublicKey) *btcec.PublicKey {
// Multiply G by 1 to get G.
secp.ScalarBaseMultNonConst(new(secp.ModNScalar).SetInt(1), &g)

// Get -G by negating the Y axis.
// Get -G by negating the Y axis. We normalize first, so we can negate
// with the magnitude of 1 and then again to make sure everything is
// normalized again after the negation.
g.ToAffine()
g.Y.Normalize()
g.Y.Negate(1)
g.Y.Normalize()

// priorKey = key - G
// priorKey = (key.x, key.y) + (G.x, -G.y)
Expand Down
50 changes: 29 additions & 21 deletions poolscript/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package poolscript

import (
"encoding/hex"
"math/rand"
"testing"
"testing/quick"
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/require"
)

const (
numOperations = 1000
numOperations = 10000
numOperationsQuickTest = 1000
)

var (
Expand All @@ -22,10 +26,10 @@ var (
)

// batchKeyIncremented1kTimesBytes is the initial batch keys incremented
// by G 1000 times.
batchKeyIncremented1kTimesBytes, _ = hex.DecodeString(
"0280488e115da2415389bbe07854133840de2741b31dabd60184c7f5d80c" +
"057d79",
// by G 10000 times.
batchKeyIncremented10kTimesBytes, _ = hex.DecodeString(
"03d9dfc4971c9cbabb1b9a4c991914211aa21286e007c15d7e9d828da0b8" +
"f07763",
)
)

Expand All @@ -34,24 +38,28 @@ var (
func TestIncrementDecrementKey(t *testing.T) {
t.Parallel()

privKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
rand.Seed(time.Now().Unix())

randomStartBatchKey := privKey.PubKey()
type byteInput [32]byte
mainScenario := func(b byteInput) bool {
_, randomStartBatchKey := btcec.PrivKeyFromBytes(b[:])

// Increment the key numOperations times.
currentKey := randomStartBatchKey
for i := 0; i < numOperations; i++ {
currentKey = IncrementKey(currentKey)
}
// Increment the key numOperations times.
currentKey := randomStartBatchKey
for i := 0; i < numOperationsQuickTest; i++ {
currentKey = IncrementKey(currentKey)
}

// Decrement the key again.
for i := 0; i < numOperations; i++ {
currentKey = DecrementKey(currentKey)
// Decrement the key again.
for i := 0; i < numOperationsQuickTest; i++ {
currentKey = DecrementKey(currentKey)
}

// We should arrive at the same start key again.
return randomStartBatchKey.IsEqual(currentKey)
}

// We should arrive at the same start key again.
require.Equal(t, randomStartBatchKey, currentKey)
require.NoError(t, quick.Check(mainScenario, nil))
}

// TestIncrementBatchKey tests that incrementing the static, hard-coded batch
Expand All @@ -63,8 +71,8 @@ func TestIncrementBatchKey(t *testing.T) {
startBatchKey, err := btcec.ParsePubKey(initialBatchKeyBytes)
require.NoError(t, err)

batchKeyIncremented1kTimes, err := btcec.ParsePubKey(
batchKeyIncremented1kTimesBytes,
batchKeyIncremented10kTimes, err := btcec.ParsePubKey(
batchKeyIncremented10kTimesBytes,
)
require.NoError(t, err)

Expand All @@ -73,7 +81,7 @@ func TestIncrementBatchKey(t *testing.T) {
currentKey = IncrementKey(currentKey)
}

require.Equal(t, batchKeyIncremented1kTimes, currentKey)
require.Equal(t, batchKeyIncremented10kTimes, currentKey)

for i := 0; i < numOperations; i++ {
currentKey = DecrementKey(currentKey)
Expand Down

0 comments on commit 3e49f4c

Please sign in to comment.