From d5b54a1c9137ffce6852344fce8328ed5b56855f Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 7 Apr 2026 17:20:10 -0300 Subject: [PATCH] negentropy: fix varint encoding. --- nip77/negentropy/encoding.go | 24 ++++++++---------------- nip77/negentropy/negentropy.go | 4 ++-- nip77/negentropy/storage/accumulator.go | 7 ++++--- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/nip77/negentropy/encoding.go b/nip77/negentropy/encoding.go index c501af9..62b9059 100644 --- a/nip77/negentropy/encoding.go +++ b/nip77/negentropy/encoding.go @@ -66,13 +66,13 @@ func (bw *BoundWriter) WriteTimestamp(w *bytes.Buffer, timestamp nostr.Timestamp bw.lastTimestampOut = timestamp // add 1 to prevent zeroes from being read as infinites - WriteVarInt(w, int(delta+1)) + WriteVarInt(w, uint64(delta)+1) return } func (bw *BoundWriter) WriteBound(w *bytes.Buffer, bound Bound) { bw.WriteTimestamp(w, bound.Timestamp) - WriteVarInt(w, len(bound.IDPrefix)) + WriteVarInt(w, uint64(len(bound.IDPrefix))) w.Write(bound.IDPrefix) } @@ -111,33 +111,25 @@ func ReadVarInt(reader *bytes.Reader) (int, error) { return res, nil } -func WriteVarInt(w *bytes.Buffer, n int) { +func WriteVarInt(w *bytes.Buffer, n uint64) { if n == 0 { w.WriteByte(0) return } - w.Write(EncodeVarInt(n)) -} - -func EncodeVarInt(n int) []byte { - if n == 0 { - return []byte{0} - } - - result := make([]byte, 8) - idx := 7 + var buf [10]byte + idx := 9 for n != 0 { - result[idx] = byte(n & 0x7F) + buf[idx] = byte(n & 0x7F) n >>= 7 idx-- } - result = result[idx+1:] + result := buf[idx+1:] for i := 0; i < len(result)-1; i++ { result[i] |= 0x80 } - return result + w.Write(result) } diff --git a/nip77/negentropy/negentropy.go b/nip77/negentropy/negentropy.go index 83e07a5..58cb35e 100644 --- a/nip77/negentropy/negentropy.go +++ b/nip77/negentropy/negentropy.go @@ -230,7 +230,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader) ([]byte, error) { finishSkip() responseIds := make([]byte, 0, 32*100) - responses := 0 + var responses uint64 = 0 endBound := currBound @@ -284,7 +284,7 @@ func (n *Negentropy) SplitRange(lower, upper int, upperBound Bound, output *byte // we just send the full ids here n.WriteBound(output, upperBound) output.WriteByte(byte(IdListMode)) - WriteVarInt(output, numElems) + WriteVarInt(output, uint64(numElems)) for _, item := range n.storage.Range(lower, upper) { output.Write(item.ID[:]) diff --git a/nip77/negentropy/storage/accumulator.go b/nip77/negentropy/storage/accumulator.go index 9368df3..cefc207 100644 --- a/nip77/negentropy/storage/accumulator.go +++ b/nip77/negentropy/storage/accumulator.go @@ -1,6 +1,7 @@ package storage import ( + "bytes" "crypto/sha256" "encoding/binary" @@ -41,8 +42,8 @@ func (acc *Accumulator) AddBytes(other []byte) { } func (acc *Accumulator) GetFingerprint(n int) [negentropy.FingerprintSize]byte { - input := acc.Buf[:32] - input = append(input, negentropy.EncodeVarInt(n)...) - hash := sha256.Sum256(input) + input := bytes.NewBuffer(acc.Buf[:32]) + negentropy.WriteVarInt(input, uint64(n)) + hash := sha256.Sum256(input.Bytes()) return [negentropy.FingerprintSize]byte(hash[:negentropy.FingerprintSize]) }