Skip to content

Commit

Permalink
Remove prysm dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Nov 1, 2021
1 parent 1cb8737 commit 969ed85
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 54 deletions.
57 changes: 46 additions & 11 deletions bor/shuffle.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package bor

import (
"crypto/sha256"
"encoding/binary"
"fmt"

"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"hash"
"sync"
)

const seedSize = int8(32)
Expand Down Expand Up @@ -86,9 +86,9 @@ func innerShuffledIndex(index uint64, indexCount uint64, seed [32]byte, shuffle
copy(buf[:32], seed[:])
for {
buf[seedSize] = round
hash := hashutil.Hash(buf[:pivotViewSize])
hash := sha256Hash(buf[:pivotViewSize])
hash8 := hash[:8]
hash8Int := bytesutil.FromBytes8(hash8)
hash8Int := FromBytes8(hash8)
pivot := hash8Int % indexCount
flip := (pivot + indexCount - index) % indexCount
// Consider every pair only once by picking the highest pair index to retrieve randomness.
Expand All @@ -100,7 +100,7 @@ func innerShuffledIndex(index uint64, indexCount uint64, seed [32]byte, shuffle
// it will be used later to select a bit from the resulting hash.
position4bytes := ToBytes(position>>8, 4)
copy(buf[pivotViewSize:], position4bytes[:])
source := hashutil.Hash(buf)
source := sha256Hash(buf)
// Effectively keep the first 5 bits of the byte value of the position,
// and use it to retrieve one of the 32 (= 2^5) bytes of the hash.
byteV := source[(position&0xff)>>3]
Expand Down Expand Up @@ -183,11 +183,11 @@ func innerShuffleList(input []uint64, seed [32]byte, shuffle bool) ([]uint64, er
copy(buf[:seedSize], seed[:])
for {
buf[seedSize] = r
ph := hashutil.Hash(buf[:pivotViewSize])
pivot := bytesutil.FromBytes8(ph[:8]) % listSize
ph := sha256Hash(buf[:pivotViewSize])
pivot := FromBytes8(ph[:8]) % listSize
mirror := (pivot + 1) >> 1
binary.LittleEndian.PutUint32(buf[pivotViewSize:], uint32(pivot>>8))
source := hashutil.Hash(buf)
source := sha256Hash(buf)
byteV := source[(pivot&0xff)>>3]
for i, j := uint64(0), pivot; i < mirror; i, j = i+1, j-1 {
byteV, source = swapOrNot(buf, byteV, i, input, j, source)
Expand All @@ -196,7 +196,7 @@ func innerShuffleList(input []uint64, seed [32]byte, shuffle bool) ([]uint64, er
mirror = (pivot + listSize + 1) >> 1
end := listSize - 1
binary.LittleEndian.PutUint32(buf[pivotViewSize:], uint32(end>>8))
source = hashutil.Hash(buf)
source = sha256Hash(buf)
byteV = source[(end&0xff)>>3]
for i, j := pivot+1, end; i < mirror; i, j = i+1, j-1 {
byteV, source = swapOrNot(buf, byteV, i, input, j, source)
Expand All @@ -222,7 +222,7 @@ func swapOrNot(buf []byte, byteV byte, i uint64, input []uint64, j uint64, sourc
if j&0xff == 0xff {
// just overwrite the last part of the buffer, reuse the start (seed, round)
binary.LittleEndian.PutUint32(buf[pivotViewSize:], uint32(j>>8))
source = hashutil.Hash(buf)
source = sha256Hash(buf)
}
if j&0x7 == 0x7 {
byteV = source[(j&0xff)>>3]
Expand All @@ -248,3 +248,38 @@ func ToBytes(x uint64, length int) []byte {
binary.LittleEndian.PutUint64(bytes, x)
return bytes[:length]
}

// FromBytes8 returns an integer which is stored in the little-endian format(8, 'little')
// from a byte array.
func FromBytes8(x []byte) uint64 {
if len(x) < 8 {
return 0
}
return binary.LittleEndian.Uint64(x)
}

var sha256Pool = sync.Pool{New: func() interface{} {
return sha256.New()
}}

// Hash defines a function that returns the sha256 checksum of the data passed in
func sha256Hash(data []byte) [32]byte {
h, ok := sha256Pool.Get().(hash.Hash)
if !ok {
h = sha256.New()
}
defer sha256Pool.Put(h)
h.Reset()

var b [32]byte

// The hash interface never returns an error, for that reason
// we are not handling the error below. For reference, it is
// stated here https://golang.org/pkg/hash/#Hash

// #nosec G104
h.Write(data)
h.Sum(b[:0])

return b
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ require (
github.com/peterh/liner v1.2.0 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/prysmaticlabs/prysm v0.0.0-20190507024903-1be950f90cad
github.com/rakyll/statik v0.1.6
github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962 // indirect
github.com/rjeczalik/notify v0.9.2 // indirect
Expand Down
Loading

0 comments on commit 969ed85

Please sign in to comment.