Skip to content

Commit

Permalink
revert: 1f84ef4 ph4 bmt (#4338)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Sep 23, 2023
1 parent 25eb233 commit d337770
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 78 deletions.
13 changes: 0 additions & 13 deletions pkg/bmt/bmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ type Hasher struct {
span []byte // The span of the data subsumed under the chunk
}

// facade
func NewHasher(hasherFact func() hash.Hash) *Hasher {
conf := NewConf(hasherFact, swarm.BmtBranches, 32)

return &Hasher{
Conf: conf,
result: make(chan []byte),
errc: make(chan error, 1),
span: make([]byte, SpanSize),
bmt: newTree(conf.segmentSize, conf.maxSize, conf.depth, conf.hasher),
}
}

// Capacity returns the maximum amount of bytes that will be processed by this hasher implementation.
// since BMT assumes a balanced binary tree, capacity it is always a power of 2
func (h *Hasher) Capacity() int {
Expand Down
39 changes: 6 additions & 33 deletions pkg/bmt/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ type Proof struct {
Index int
}

// Override base hash function of Hasher to fill buffer with zeros until chunk length
func (p Prover) Hash(b []byte) ([]byte, error) {
for i := p.size; i < p.maxSize; i += len(zerosection) {
_, err := p.Write(zerosection)
if err != nil {
return []byte{}, err
}
}
return p.Hasher.Hash(b)
}

// Proof returns the inclusion proof of the i-th data segment
func (p Prover) Proof(i int) Proof {
index := i
Expand All @@ -47,50 +36,34 @@ func (p Prover) Proof(i int) Proof {
secsize := 2 * p.segmentSize
offset := i * secsize
section := p.bmt.buffer[offset : offset+secsize]
left := section[:p.segmentSize]
right := section[p.segmentSize:]
var segment, firstSegmentSister []byte
if index%2 == 0 {
segment, firstSegmentSister = left, right
} else {
segment, firstSegmentSister = right, left
}
sisters = append([][]byte{firstSegmentSister}, sisters...)
return Proof{segment, sisters, p.span, index}
return Proof{section, sisters, p.span, index}
}

// Verify returns the bmt hash obtained from the proof which can then be checked against
// the BMT hash of the chunk
func (p Prover) Verify(i int, proof Proof) (root []byte, err error) {
var section []byte
if i%2 == 0 {
section = append(append(section, proof.ProveSegment...), proof.ProofSegments[0]...)
} else {
section = append(append(section, proof.ProofSegments[0]...), proof.ProveSegment...)
}
i = i / 2
n := p.bmt.leaves[i]
hasher := p.hasher()
isLeft := n.isLeft
root, err = doHash(hasher, section)
root, err = doHash(n.hasher, proof.ProveSegment)
if err != nil {
return nil, err
}
n = n.parent

for _, sister := range proof.ProofSegments[1:] {
for _, sister := range proof.ProofSegments {
if isLeft {
root, err = doHash(hasher, root, sister)
root, err = doHash(n.hasher, root, sister)
} else {
root, err = doHash(hasher, sister, root)
root, err = doHash(n.hasher, sister, root)
}
if err != nil {
return nil, err
}
isLeft = n.isLeft
n = n.parent
}
return doHash(hasher, proof.Span, root)
return sha3hash(proof.Span, root)
}

func (n *node) getSister(isLeft bool) []byte {
Expand Down
30 changes: 12 additions & 18 deletions pkg/bmt/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func TestProofCorrectness(t *testing.T) {
t.Parallel()

testData := []byte("hello world")
testDataPadded := make([]byte, swarm.ChunkSize)
copy(testDataPadded, testData)
testData = append(testData, make([]byte, 4096-len(testData))...)

verifySegments := func(t *testing.T, exp []string, found [][]byte) {
t.Helper()
Expand Down Expand Up @@ -58,19 +57,18 @@ func TestProofCorrectness(t *testing.T) {
if err != nil {
t.Fatal(err)
}
pr := bmt.Prover{hh}
rh, err := pr.Hash(nil)

rh, err := hh.Hash(nil)
if err != nil {
t.Fatal(err)
}

t.Run("proof for left most", func(t *testing.T) {
t.Parallel()

proof := pr.Proof(0)
proof := bmt.Prover{hh}.Proof(0)

expSegmentStrings := []string{
"0000000000000000000000000000000000000000000000000000000000000000",
"ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5",
"b4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30",
"21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85",
Expand All @@ -81,7 +79,7 @@ func TestProofCorrectness(t *testing.T) {

verifySegments(t, expSegmentStrings, proof.ProofSegments)

if !bytes.Equal(proof.ProveSegment, testDataPadded[:hh.Size()]) {
if !bytes.Equal(proof.ProveSegment, testData[:2*hh.Size()]) {
t.Fatal("section incorrect")
}

Expand All @@ -93,10 +91,9 @@ func TestProofCorrectness(t *testing.T) {
t.Run("proof for right most", func(t *testing.T) {
t.Parallel()

proof := pr.Proof(127)
proof := bmt.Prover{hh}.Proof(127)

expSegmentStrings := []string{
"0000000000000000000000000000000000000000000000000000000000000000",
"ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5",
"b4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30",
"21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85",
Expand All @@ -107,7 +104,7 @@ func TestProofCorrectness(t *testing.T) {

verifySegments(t, expSegmentStrings, proof.ProofSegments)

if !bytes.Equal(proof.ProveSegment, testDataPadded[127*hh.Size():]) {
if !bytes.Equal(proof.ProveSegment, testData[126*hh.Size():]) {
t.Fatal("section incorrect")
}

Expand All @@ -119,10 +116,9 @@ func TestProofCorrectness(t *testing.T) {
t.Run("proof for middle", func(t *testing.T) {
t.Parallel()

proof := pr.Proof(64)
proof := bmt.Prover{hh}.Proof(64)

expSegmentStrings := []string{
"0000000000000000000000000000000000000000000000000000000000000000",
"ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5",
"b4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30",
"21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85",
Expand All @@ -133,7 +129,7 @@ func TestProofCorrectness(t *testing.T) {

verifySegments(t, expSegmentStrings, proof.ProofSegments)

if !bytes.Equal(proof.ProveSegment, testDataPadded[64*hh.Size():65*hh.Size()]) {
if !bytes.Equal(proof.ProveSegment, testData[64*hh.Size():66*hh.Size()]) {
t.Fatal("section incorrect")
}

Expand All @@ -146,7 +142,6 @@ func TestProofCorrectness(t *testing.T) {
t.Parallel()

segmentStrings := []string{
"0000000000000000000000000000000000000000000000000000000000000000",
"ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5",
"b4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30",
"21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85",
Expand All @@ -164,9 +159,9 @@ func TestProofCorrectness(t *testing.T) {
segments = append(segments, decoded)
}

segment := testDataPadded[64*hh.Size() : 65*hh.Size()]
segment := testData[64*hh.Size() : 66*hh.Size()]

rootHash, err := pr.Verify(64, bmt.Proof{
rootHash, err := bmt.Prover{hh}.Verify(64, bmt.Proof{
ProveSegment: segment,
ProofSegments: segments,
Span: bmt.LengthToSpan(4096),
Expand Down Expand Up @@ -205,7 +200,6 @@ func TestProof(t *testing.T) {
}

rh, err := hh.Hash(nil)
pr := bmt.Prover{hh}
if err != nil {
t.Fatal(err)
}
Expand All @@ -215,7 +209,7 @@ func TestProof(t *testing.T) {
t.Run(fmt.Sprintf("segmentIndex %d", i), func(t *testing.T) {
t.Parallel()

proof := pr.Proof(i)
proof := bmt.Prover{hh}.Proof(i)

h := pool.Get()
defer pool.Put(h)
Expand Down
25 changes: 25 additions & 0 deletions pkg/bmt/trhasher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2023 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package bmt

import (
"hash"

"github.com/ethersphere/bee/pkg/swarm"
)

func NewTrHasher(prefix []byte) *Hasher {
capacity := 32
hasherFact := func() hash.Hash { return swarm.NewTrHasher(prefix) }
conf := NewConf(hasherFact, swarm.BmtBranches, capacity)

return &Hasher{
Conf: conf,
result: make(chan []byte),
errc: make(chan error, 1),
span: make([]byte, SpanSize),
bmt: newTree(conf.segmentSize, conf.maxSize, conf.depth, conf.hasher),
}
}
9 changes: 1 addition & 8 deletions pkg/storer/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"crypto/hmac"
"encoding/binary"
"fmt"
"hash"
"math/big"
"sort"
"sync"
Expand Down Expand Up @@ -44,13 +43,7 @@ type Sample struct {
func RandSample(t *testing.T, anchor []byte) Sample {
t.Helper()

prefixHasherFactory := func() hash.Hash {
return swarm.NewPrefixHasher(anchor)
}
pool := bmt.NewPool(bmt.NewConf(prefixHasherFactory, swarm.BmtBranches, 8))

hasher := pool.Get()
defer pool.Put(hasher)
hasher := bmt.NewTrHasher(anchor)

items := make([]SampleItem, SampleSize)
for i := 0; i < SampleSize; i++ {
Expand Down
10 changes: 5 additions & 5 deletions pkg/swarm/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ func NewHasher() hash.Hash {
return sha3.NewLegacyKeccak256()
}

type PrefixHasher struct {
type trHasher struct {
hash.Hash
prefix []byte
}

// NewPrefixHasher returns new hasher which is Keccak-256 hasher
// NewTrHasher returns new hasher which is Keccak-256 hasher
// with prefix value added as initial data.
func NewPrefixHasher(prefix []byte) hash.Hash {
h := &PrefixHasher{
func NewTrHasher(prefix []byte) hash.Hash {
h := &trHasher{
Hash: NewHasher(),
prefix: prefix,
}
Expand All @@ -32,7 +32,7 @@ func NewPrefixHasher(prefix []byte) hash.Hash {
return h
}

func (h *PrefixHasher) Reset() {
func (h *trHasher) Reset() {
h.Hash.Reset()
_, _ = h.Write(h.prefix)
}
2 changes: 1 addition & 1 deletion pkg/swarm/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestNewTrHasher(t *testing.T) {

// Run tests cases against TrHasher
for _, tc := range tests {
h := swarm.NewPrefixHasher(tc.prefix)
h := swarm.NewTrHasher(tc.prefix)

_, err := h.Write(tc.plaintext)
if err != nil {
Expand Down

0 comments on commit d337770

Please sign in to comment.