Skip to content

Commit

Permalink
change hash function (#2732)
Browse files Browse the repository at this point in the history
  • Loading branch information
nisdas authored May 31, 2019
1 parent 988b1fb commit 085962a
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 122 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/attestation/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestUpdateLatestAttestation_UpdatesLatest(t *testing.T) {
if err := service.UpdateLatestAttestation(ctx, attestation); err != nil {
t.Fatalf("could not update latest attestation: %v", err)
}
pubkey := bytesutil.ToBytes48(beaconState.ValidatorRegistry[12].Pubkey)
pubkey := bytesutil.ToBytes48(beaconState.ValidatorRegistry[10].Pubkey)
if service.store.m[pubkey].Data.Crosslink.Shard !=
attestation.Data.Crosslink.Shard {
t.Errorf("Incorrect shard stored, wanted: %d, got: %d",
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/blocks/block_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestProcessBlockHeader_OK(t *testing.T) {
LatestActiveIndexRoots: make([][]byte, params.BeaconConfig().LatestActiveIndexRootsLength),
}

validators[5297].Slashed = false
validators[5593].Slashed = false

latestBlockSignedRoot, err := ssz.SigningRoot(state.LatestBlockHeader)
if err != nil {
Expand Down Expand Up @@ -1325,19 +1325,19 @@ func TestConvertToIndexed_OK(t *testing.T) {
aggregationBitfield: []byte{0x03},
custodyBitfield: []byte{0x01},
wantedCustodyBit0Indices: []uint64{},
wantedCustodyBit1Indices: []uint64{82, 84},
wantedCustodyBit1Indices: []uint64{71, 127},
},
{
aggregationBitfield: []byte{0x03},
custodyBitfield: []byte{0x02},
wantedCustodyBit0Indices: []uint64{},
wantedCustodyBit1Indices: []uint64{82, 84},
wantedCustodyBit1Indices: []uint64{71, 127},
},
{
aggregationBitfield: []byte{0x03},
custodyBitfield: []byte{0x03},
wantedCustodyBit0Indices: []uint64{},
wantedCustodyBit1Indices: []uint64{82, 84},
wantedCustodyBit1Indices: []uint64{71, 127},
},
}

Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/core/epoch/epoch_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func TestEarliestAttestation_CanGetEarliest(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wantedInclusion := uint64(100)
wantedInclusion := uint64(18446744073709551615)
if att.InclusionDelay != wantedInclusion {
t.Errorf("wanted inclusion slot: %d, got: %d", wantedInclusion, att.InclusionDelay)

Expand Down Expand Up @@ -990,7 +990,7 @@ func TestCrosslinkDelta_SomeAttested(t *testing.T) {
}
committeeBalance := helpers.TotalBalance(state, committee)
attestingBalance := helpers.TotalBalance(state, winningIndices)
attestedIndices := []uint64{350, 361, 498, 533, 537, 629, 646}
attestedIndices := []uint64{79, 127, 232, 473, 569, 754, 774}
for _, i := range attestedIndices {
// Since all these validators attested, they should get the same rewards.
want := baseReward(state, i) * attestingBalance / committeeBalance
Expand Down Expand Up @@ -1133,7 +1133,7 @@ func TestAttestationDelta_SomeAttested(t *testing.T) {
t.Fatal(err)
}

attestedIndices := []uint64{350, 361, 498, 533, 537, 629, 646}
attestedIndices := []uint64{79, 127, 232, 473, 569, 754, 774}

attestedBalance, err := AttestingBalance(state, atts)
totalBalance := totalActiveBalance(state)
Expand Down
32 changes: 16 additions & 16 deletions beacon-chain/core/helpers/committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,19 @@ func TestAttestationParticipants_NoCommitteeCache(t *testing.T) {
attestationSlot: 3,
stateSlot: 5,
bitfield: []byte{0x03},
wanted: []uint64{82, 84},
wanted: []uint64{71, 127},
},
{
attestationSlot: 2,
stateSlot: 10,
bitfield: []byte{0x01},
wanted: []uint64{32, 63},
wanted: []uint64{85, 103},
},
{
attestationSlot: 11,
stateSlot: 10,
bitfield: []byte{0x03},
wanted: []uint64{37, 104},
wanted: []uint64{68, 102},
},
}
//startShard := uint64(960)
Expand Down Expand Up @@ -343,30 +343,30 @@ func TestCommitteeAssignment_CanRetrieve(t *testing.T) {
}{
{
index: 0,
slot: 186,
committee: []uint64{0, 45},
shard: 122,
slot: 146,
committee: []uint64{0, 3},
shard: 82,
isProposer: false,
},
{
index: 105,
slot: 135,
committee: []uint64{111, 105},
shard: 71,
slot: 160,
committee: []uint64{105, 20},
shard: 96,
isProposer: false,
},
{
index: 64,
slot: 170,
committee: []uint64{64, 80},
shard: 106,
isProposer: false,
slot: 183,
committee: []uint64{64, 33},
shard: 119,
isProposer: true,
},
{
index: 11,
slot: 191,
committee: []uint64{102, 11},
shard: 127,
slot: 135,
committee: []uint64{119, 11},
shard: 71,
isProposer: false,
},
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/helpers/randao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func TestGenerateSeed_OK(t *testing.T) {

got := GenerateSeed(state, 10)

wanted := [32]byte{184, 125, 45, 85, 9, 149, 28, 150, 244, 26, 107, 190, 20,
226, 23, 62, 239, 72, 184, 214, 219, 91, 33, 42, 123, 110, 161, 17, 6, 206, 182, 195}
wanted := [32]byte{239, 112, 63, 86, 124, 180, 155, 181, 91, 67, 231, 178,
94, 149, 243, 101, 176, 169, 153, 35, 37, 19, 115, 154, 6, 102, 125, 91, 81, 153, 186, 84}
if got != wanted {
t.Errorf("Incorrect generated seeds. Got: %v, wanted: %v",
got, wanted)
Expand Down
10 changes: 5 additions & 5 deletions beacon-chain/core/helpers/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ func TestBeaconProposerIndex_OK(t *testing.T) {
}{
{
slot: 1,
index: 537,
index: 1114,
},
{
slot: 5,
index: 1313,
index: 1207,
},
{
slot: 19,
index: 1321,
index: 264,
},
{
slot: 30,
index: 340,
index: 1421,
},
{
slot: 43,
index: 612,
index: 318,
},
}

Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/powchain/log_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func (w *Web3Service) ETH2GenesisTime() (uint64, error) {
// logs from the deposit contract on the ETH1.0 chain.
func (w *Web3Service) ProcessLog(depositLog gethTypes.Log) {
// Process logs according to their event signature.
if depositLog.Topics[0] == hashutil.Hash(depositEventSignature) {
if depositLog.Topics[0] == hashutil.HashKeccak256(depositEventSignature) {
w.ProcessDepositLog(depositLog)
return
}
if depositLog.Topics[0] == hashutil.Hash(chainStartEventSignature) && !w.chainStarted {
if depositLog.Topics[0] == hashutil.HashKeccak256(chainStartEventSignature) && !w.chainStarted {
w.ProcessChainStartLog(depositLog)
return
}
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/utils/shuffle.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func innerShuffledIndex(index uint64, indexCount uint64, seed [32]byte, shuffle
copy(buf[:32], seed[:])
for {
buf[seedSize] = round
hash := hashutil.HashSha256(buf[:pivotViewSize])
hash := hashutil.Hash(buf[:pivotViewSize])
hash8 := hash[:8]
hash8Int := bytesutil.FromBytes8(hash8)
pivot := hash8Int % indexCount
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 := bytesutil.ToBytes(position>>8, 4)
copy(buf[pivotViewSize:], position4bytes[:])
source := hashutil.HashSha256(buf)
source := hashutil.Hash(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.HashSha256(buf[:pivotViewSize])
ph := hashutil.Hash(buf[:pivotViewSize])
pivot := bytesutil.FromBytes8(ph[:8]) % listSize
mirror := (pivot + 1) >> 1
binary.LittleEndian.PutUint32(buf[pivotViewSize:], uint32(pivot>>8))
source := hashutil.HashSha256(buf)
source := hashutil.Hash(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.HashSha256(buf)
source = hashutil.Hash(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.HashSha256(buf)
source = hashutil.Hash(buf)
}
if j&0x7 == 0x7 {
byteV = source[(j&0xff)>>3]
Expand Down
2 changes: 1 addition & 1 deletion contracts/deposit-contract/depositContract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestETH2Genesis_OK(t *testing.T) {
t.Fatalf("Unable to get logs %v", err)
}

if logs[8].Topics[0] != hashutil.Hash([]byte("Eth2Genesis(bytes32,bytes,bytes)")) {
if logs[8].Topics[0] != hashutil.HashKeccak256([]byte("Eth2Genesis(bytes32,bytes,bytes)")) {
t.Error("Chain start did not even get emitted")
}
}
Expand Down
14 changes: 7 additions & 7 deletions shared/hashutil/deposit_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func DepositHash(dep *pb.DepositData) ([32]byte, error) {

var zeroBytes [32]byte

pubkeyRoot := HashSha256(append(dep.Pubkey, zeroBytes[:16]...))
sigHash := HashSha256(dep.Signature[:64])
sigZeroBytesHash := HashSha256(append(dep.Signature[64:96], zeroBytes[:]...))
sigRoot := HashSha256(append(sigHash[:], sigZeroBytesHash[:]...))
pubkeyRoot := Hash(append(dep.Pubkey, zeroBytes[:16]...))
sigHash := Hash(dep.Signature[:64])
sigZeroBytesHash := Hash(append(dep.Signature[64:96], zeroBytes[:]...))
sigRoot := Hash(append(sigHash[:], sigZeroBytesHash[:]...))

pubRootWCredsHash := HashSha256(append(pubkeyRoot[:], dep.WithdrawalCredentials...))
amountSigHash := HashSha256(append(append(bytesutil.Bytes8(dep.Amount), zeroBytes[:24]...), sigRoot[:]...))
pubRootWCredsHash := Hash(append(pubkeyRoot[:], dep.WithdrawalCredentials...))
amountSigHash := Hash(append(append(bytesutil.Bytes8(dep.Amount), zeroBytes[:24]...), sigRoot[:]...))

return HashSha256(append(pubRootWCredsHash[:], amountSigHash[:]...)), nil
return Hash(append(pubRootWCredsHash[:], amountSigHash[:]...)), nil
}
19 changes: 9 additions & 10 deletions shared/hashutil/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import (
// or has nil objects within lists.
var ErrNilProto = errors.New("cannot hash a nil protobuf message")

// Hash defines a function that returns the
// Keccak-256/SHA3 hash of the data passed in.
// Hash defines a function that returns the sha256 checksum of the data passed in.
// https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#appendix
func Hash(data []byte) [32]byte {
var hash [32]byte

h := sha3.NewLegacyKeccak256()

h := sha256.New()
// 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
Expand All @@ -32,12 +30,13 @@ func Hash(data []byte) [32]byte {
return hash
}

// HashSha256 defines a function which returns the sha256 checksum
// of the data passed in.
func HashSha256(data []byte) [32]byte {
// HashKeccak256 defines a function which returns the Keccak-256/SHA3
// hash of the data passed in.
func HashKeccak256(data []byte) [32]byte {
var hash [32]byte

h := sha256.New()
h := sha3.NewLegacyKeccak256()

// 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
Expand All @@ -49,7 +48,7 @@ func HashSha256(data []byte) [32]byte {
return hash
}

// RepeatHash applies the Keccak-256/SHA3 hash function repeatedly
// RepeatHash applies the sha256 hash function repeatedly
// numTimes on a [32]byte array.
func RepeatHash(data [32]byte, numTimes uint64) [32]byte {
if numTimes == 0 {
Expand All @@ -58,7 +57,7 @@ func RepeatHash(data [32]byte, numTimes uint64) [32]byte {
return RepeatHash(Hash(data[:]), numTimes-1)
}

// HashProto hashes a protocol buffer message using Keccak-256/SHA3.
// HashProto hashes a protocol buffer message using sha256.
func HashProto(msg proto.Message) (result [32]byte, err error) {
// Hashing a proto with nil pointers will cause a panic in the unsafe
// proto.Marshal library.
Expand Down
34 changes: 17 additions & 17 deletions shared/hashutil/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,49 @@ import (
)

func TestHash(t *testing.T) {
hashOf0 := [32]byte{188, 54, 120, 158, 122, 30, 40, 20, 54, 70, 66, 41, 130, 143, 129, 125, 102, 18, 247, 180, 119, 214, 101, 145, 255, 150, 169, 224, 100, 188, 201, 138}
hashOf0 := [32]byte{110, 52, 11, 156, 255, 179, 122, 152, 156, 165, 68, 230, 187, 120, 10, 44, 120, 144, 29, 63, 179, 55, 56, 118, 133, 17, 163, 6, 23, 175, 160, 29}
hash := hashutil.Hash([]byte{0})
if hash != hashOf0 {
t.Fatalf("expected hash and computed hash are not equal %d, %d", hash, hashOf0)
}

hashOf1 := [32]byte{95, 231, 249, 119, 231, 29, 186, 46, 161, 166, 142, 33, 5, 123, 238, 187, 155, 226, 172, 48, 198, 65, 10, 163, 141, 79, 63, 190, 65, 220, 255, 210}
hashOf1 := [32]byte{75, 245, 18, 47, 52, 69, 84, 197, 59, 222, 46, 187, 140, 210, 183, 227, 209, 96, 10, 214, 49, 195, 133, 165, 215, 204, 226, 60, 119, 133, 69, 154}
hash = hashutil.Hash([]byte{1})
if hash != hashOf1 {
t.Fatalf("expected hash and computed hash are not equal %d, %d", hash, hashOf1)
}

// Same hashing test from go-ethereum for keccak256
hashOfabc, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
hash = hashutil.Hash([]byte("abc"))

h := bytesutil.ToBytes32(hashOfabc)

if hash != h {
t.Fatalf("expected hash and computed hash are not equal %d, %d", hash, h)
}

if hashOf0 == hashOf1 {
t.Fatalf("expected hash and computed hash are equal %d, %d", hash, hashOf1)
}
}

func TestHashSha256(t *testing.T) {
hashOf0 := [32]byte{110, 52, 11, 156, 255, 179, 122, 152, 156, 165, 68, 230, 187, 120, 10, 44, 120, 144, 29, 63, 179, 55, 56, 118, 133, 17, 163, 6, 23, 175, 160, 29}
hash := hashutil.HashSha256([]byte{0})
func TestHashKeccak256(t *testing.T) {
hashOf0 := [32]byte{188, 54, 120, 158, 122, 30, 40, 20, 54, 70, 66, 41, 130, 143, 129, 125, 102, 18, 247, 180, 119, 214, 101, 145, 255, 150, 169, 224, 100, 188, 201, 138}
hash := hashutil.HashKeccak256([]byte{0})
if hash != hashOf0 {
t.Fatalf("expected hash and computed hash are not equal %d, %d", hash, hashOf0)
}

hashOf1 := [32]byte{75, 245, 18, 47, 52, 69, 84, 197, 59, 222, 46, 187, 140, 210, 183, 227, 209, 96, 10, 214, 49, 195, 133, 165, 215, 204, 226, 60, 119, 133, 69, 154}
hash = hashutil.HashSha256([]byte{1})
hashOf1 := [32]byte{95, 231, 249, 119, 231, 29, 186, 46, 161, 166, 142, 33, 5, 123, 238, 187, 155, 226, 172, 48, 198, 65, 10, 163, 141, 79, 63, 190, 65, 220, 255, 210}
hash = hashutil.HashKeccak256([]byte{1})
if hash != hashOf1 {
t.Fatalf("expected hash and computed hash are not equal %d, %d", hash, hashOf1)
}

if hashOf0 == hashOf1 {
t.Fatalf("expected hash and computed hash are equal %d, %d", hash, hashOf1)
}

// Same hashing test from go-ethereum for keccak256
hashOfabc, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
hash = hashutil.HashKeccak256([]byte("abc"))

h := bytesutil.ToBytes32(hashOfabc)

if hash != h {
t.Fatalf("expected hash and computed hash are not equal %d, %d", hash, h)
}
}

func TestHashProto(t *testing.T) {
Expand Down
Loading

0 comments on commit 085962a

Please sign in to comment.