Skip to content

Commit df717ab

Browse files
gballetfjl
authored andcommitted
whisper/whisperv6: fix PoW calculations to match the spec (#19330)
This PR fixes two issues in the PoW calculation of a Whisper envelope, compared to the spec (see PoW Requirements): - The pow is supposed to take the leading number of zeroes (i.e. most significant zeroes) and what it did was to take the number of trailing zeroes (i.e. least significant zeroes). It has been fixed to match what the spec and Parity does. - The spec expects to use the size of the RLP encoded envelope, and it took something else, as described in #18070.
1 parent b8b4fb0 commit df717ab

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

‎whisper/whisperv6/envelope.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"time"
2828

2929
"github.com/ethereum/go-ethereum/common"
30-
"github.com/ethereum/go-ethereum/common/math"
3130
"github.com/ethereum/go-ethereum/crypto"
3231
"github.com/ethereum/go-ethereum/crypto/ecies"
3332
"github.com/ethereum/go-ethereum/rlp"
@@ -82,7 +81,7 @@ func (e *Envelope) Seal(options *MessageParams) error {
8281
return nil
8382
}
8483

85-
var target, bestBit int
84+
var target, bestLeadingZeros int
8685
if options.PoW < 0 {
8786
// target is not set - the function should run for a period
8887
// of time specified in WorkTime param. Since we can predict
@@ -101,18 +100,18 @@ func (e *Envelope) Seal(options *MessageParams) error {
101100
for i := 0; i < 1024; i++ {
102101
binary.BigEndian.PutUint64(buf[56:], nonce)
103102
d := new(big.Int).SetBytes(crypto.Keccak256(buf))
104-
firstBit := math.FirstBitSet(d)
105-
if firstBit > bestBit {
106-
e.Nonce, bestBit = nonce, firstBit
107-
if target > 0 && bestBit >= target {
103+
leadingZeros := 256 - d.BitLen()
104+
if leadingZeros > bestLeadingZeros {
105+
e.Nonce, bestLeadingZeros = nonce, leadingZeros
106+
if target > 0 && bestLeadingZeros >= target {
108107
return nil
109108
}
110109
}
111110
nonce++
112111
}
113112
}
114113

115-
if target > 0 && bestBit < target {
114+
if target > 0 && bestLeadingZeros < target {
116115
return fmt.Errorf("failed to reach the PoW target, specified pow time (%d seconds) was insufficient", options.WorkTime)
117116
}
118117

@@ -130,13 +129,14 @@ func (e *Envelope) PoW() float64 {
130129

131130
func (e *Envelope) calculatePoW(diff uint32) {
132131
buf := make([]byte, 64)
133-
h := crypto.Keccak256(e.rlpWithoutNonce())
132+
rlp := e.rlpWithoutNonce()
133+
h := crypto.Keccak256(rlp)
134134
copy(buf[:32], h)
135135
binary.BigEndian.PutUint64(buf[56:], e.Nonce)
136-
d := new(big.Int).SetBytes(crypto.Keccak256(buf))
137-
firstBit := math.FirstBitSet(d)
138-
x := gmath.Pow(2, float64(firstBit))
139-
x /= float64(e.size())
136+
powHash := new(big.Int).SetBytes(crypto.Keccak256(buf))
137+
leadingZeroes := 256 - powHash.BitLen()
138+
x := gmath.Pow(2, float64(leadingZeroes))
139+
x /= float64(len(rlp))
140140
x /= float64(e.TTL + diff)
141141
e.pow = x
142142
}

‎whisper/whisperv6/envelope_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ import (
2525
"github.com/ethereum/go-ethereum/crypto"
2626
)
2727

28+
func TestPoWCalculationsWithNoLeadingZeros(t *testing.T) {
29+
e := Envelope{
30+
TTL: 1,
31+
Data: []byte{0xde, 0xad, 0xbe, 0xef},
32+
Nonce: 100000,
33+
}
34+
35+
e.calculatePoW(0)
36+
37+
if e.pow != 0.07692307692307693 {
38+
t.Fatalf("invalid PoW calculation. Expected 0.07692307692307693, got %v", e.pow)
39+
}
40+
}
41+
42+
func TestPoWCalculationsWith8LeadingZeros(t *testing.T) {
43+
e := Envelope{
44+
TTL: 1,
45+
Data: []byte{0xde, 0xad, 0xbe, 0xef},
46+
Nonce: 48159,
47+
}
48+
e.calculatePoW(0)
49+
50+
if e.pow != 40329.846153846156 {
51+
t.Fatalf("invalid PoW calculation. Expected 0.07692307692307693, got %v", e.pow)
52+
}
53+
}
54+
2855
func TestEnvelopeOpenAcceptsOnlyOneKeyTypeInFilter(t *testing.T) {
2956
symKey := make([]byte, aesKeyLength)
3057
mrand.Read(symKey)

0 commit comments

Comments
 (0)