negentropy: fuzz testing, move accumulator to vector package.

This commit is contained in:
fiatjaf
2024-09-20 10:56:15 -03:00
parent e9e96be95e
commit 9813d1776f
13 changed files with 205 additions and 68 deletions

View File

@@ -0,0 +1,49 @@
package vector
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"github.com/nbd-wtf/go-nostr/nip77/negentropy"
)
type Accumulator struct {
Buf [32 + 8]byte // leave 8 bytes at the end as a slack for use in GetFingerprint append()
}
func (acc *Accumulator) Reset() {
for i := 0; i < 32; i++ {
acc.Buf[i] = 0
}
}
func (acc *Accumulator) AddAccumulator(other Accumulator) {
acc.AddBytes(other.Buf[:32])
}
func (acc *Accumulator) AddBytes(other []byte) {
var currCarry, nextCarry uint32
for i := 0; i < 8; i++ {
offset := i * 4
orig := binary.LittleEndian.Uint32(acc.Buf[offset:])
otherV := binary.LittleEndian.Uint32(other[offset:])
next := orig + currCarry + otherV
if next < orig || next < otherV {
nextCarry = 1
}
binary.LittleEndian.PutUint32(acc.Buf[offset:32], next&0xFFFFFFFF)
currCarry = nextCarry
nextCarry = 0
}
}
func (acc *Accumulator) GetFingerprint(n int) string {
input := acc.Buf[:32]
input = append(input, negentropy.EncodeVarInt(n)...)
hash := sha256.Sum256(input)
return hex.EncodeToString(hash[:negentropy.FingerprintSize])
}

View File

@@ -13,6 +13,8 @@ import (
type Vector struct {
items []negentropy.Item
sealed bool
acc Accumulator
}
func New() *Vector {
@@ -21,14 +23,13 @@ func New() *Vector {
}
}
func (v *Vector) Insert(createdAt nostr.Timestamp, id string) error {
func (v *Vector) Insert(createdAt nostr.Timestamp, id string) {
if len(id) != 64 {
return fmt.Errorf("bad id size for added item: expected %d bytes, got %d", 32, len(id)/2)
panic(fmt.Errorf("bad id size for added item: expected %d bytes, got %d", 32, len(id)/2))
}
item := negentropy.Item{Timestamp: createdAt, ID: id}
v.items = append(v.items, item)
return nil
}
func (v *Vector) Size() int { return len(v.items) }
@@ -63,15 +64,14 @@ func (v *Vector) FindLowerBound(begin, end int, bound negentropy.Bound) int {
return begin + idx
}
func (v *Vector) Fingerprint(begin, end int) [negentropy.FingerprintSize]byte {
var out negentropy.Accumulator
out.SetToZero()
func (v *Vector) Fingerprint(begin, end int) string {
v.acc.Reset()
tmp := make([]byte, 32)
for _, item := range v.Range(begin, end) {
hex.Decode(tmp, []byte(item.ID))
out.AddBytes(tmp)
v.acc.AddBytes(tmp)
}
return out.GetFingerprint(end - begin)
return v.acc.GetFingerprint(end - begin)
}