Skip to content

Commit

Permalink
crypto: update from upstream (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Jan 28, 2019
1 parent c199183 commit 74d960d
Show file tree
Hide file tree
Showing 59 changed files with 265 additions and 2,010 deletions.
8 changes: 5 additions & 3 deletions accounts/abi/bind/topics.go
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/gochain-io/gochain/v3/accounts/abi"
"github.com/gochain-io/gochain/v3/common"
"github.com/gochain-io/gochain/v3/crypto/sha3"
"github.com/gochain-io/gochain/v3/crypto"
)

// makeTopics converts a filter query argument list into a filter topic set.
Expand Down Expand Up @@ -72,9 +72,11 @@ func makeTopics(query ...[]interface{}) ([][]common.Hash, error) {
blob := new(big.Int).SetUint64(rule).Bytes()
copy(topic[common.HashLength-len(blob):], blob)
case string:
sha3.Keccak256(topic[:32], []byte(rule))
hash := crypto.Keccak256Hash([]byte(rule))
copy(topic[:], hash[:])
case []byte:
sha3.Keccak256(topic[:32], rule)
hash := crypto.Keccak256Hash(rule)
copy(topic[:], hash[:])

default:
// Attempt to generate the topic from funky types
Expand Down
20 changes: 10 additions & 10 deletions bmt/bmt_test.go
Expand Up @@ -28,7 +28,7 @@ import (
"testing"
"time"

"github.com/gochain-io/gochain/v3/crypto/sha3"
"golang.org/x/crypto/sha3"
)

const (
Expand All @@ -38,7 +38,7 @@ const (
// TestRefHasher tests that the RefHasher computes the expected BMT hash for
// all data lengths between 0 and 256 bytes
func TestRefHasher(t *testing.T) {
hashFunc := sha3.NewKeccak256
hashFunc := sha3.NewLegacyKeccak256

sha3 := func(data ...[]byte) []byte {
h := hashFunc()
Expand Down Expand Up @@ -211,7 +211,7 @@ func testHasher(f func(BaseHasher, []byte, int, int) error) error {
tdata := testDataReader(4128)
data := make([]byte, 4128)
tdata.Read(data)
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
size := hasher().Size()
counts := []int{1, 2, 3, 4, 5, 8, 16, 32, 64, 128}

Expand All @@ -238,7 +238,7 @@ func TestHasherReuseWithRelease(t *testing.T) {
}

func testHasherReuse(i int, t *testing.T) {
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
pool := NewTreePool(hasher, 128, i)
defer pool.Drain(0)
bmt := New(pool)
Expand All @@ -257,7 +257,7 @@ func testHasherReuse(i int, t *testing.T) {
}

func TestHasherConcurrency(t *testing.T) {
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
pool := NewTreePool(hasher, 128, maxproccnt)
defer pool.Drain(0)
wg := sync.WaitGroup{}
Expand Down Expand Up @@ -372,7 +372,7 @@ func benchmarkBMTBaseline(n int, t *testing.B) {
tdata := testDataReader(64)
data := make([]byte, 64)
tdata.Read(data)
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256

t.ReportAllocs()
t.ResetTimer()
Expand Down Expand Up @@ -402,7 +402,7 @@ func benchmarkHasher(n int, t *testing.B) {
tdata.Read(data)

size := 1
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
segmentCount := 128
pool := NewTreePool(hasher, segmentCount, size)
bmt := New(pool)
Expand All @@ -421,7 +421,7 @@ func benchmarkHasherReuse(poolsize, n int, t *testing.B) {
data := make([]byte, n)
tdata.Read(data)

hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
segmentCount := 128
pool := NewTreePool(hasher, segmentCount, poolsize)
cycles := 200
Expand All @@ -448,7 +448,7 @@ func benchmarkSHA3(n int, t *testing.B) {
data := make([]byte, n)
tdata := testDataReader(n)
tdata.Read(data)
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
h := hasher()

t.ReportAllocs()
Expand All @@ -464,7 +464,7 @@ func benchmarkRefHasher(n int, t *testing.B) {
data := make([]byte, n)
tdata := testDataReader(n)
tdata.Read(data)
hasher := sha3.NewKeccak256
hasher := sha3.NewLegacyKeccak256
rbmt := NewRefHasher(hasher, 128)

t.ReportAllocs()
Expand Down
9 changes: 4 additions & 5 deletions cmd/wnode/main.go
Expand Up @@ -136,8 +136,8 @@ func processArgs() {
}

if *asymmetricMode && len(*argPub) > 0 {
pub = crypto.ToECDSAPub(common.FromHex(*argPub))
if !isKeyValid(pub) {
var err error
if pub, err = crypto.UnmarshalPubkey(common.FromHex(*argPub)); err != nil {
utils.Fatalf("invalid public key")
}
}
Expand Down Expand Up @@ -320,9 +320,8 @@ func configureNode() {
if b == nil {
utils.Fatalf("Error: can not convert hexadecimal string")
}
pub = crypto.ToECDSAPub(b)
if !isKeyValid(pub) {
utils.Fatalf("Error: invalid public key")
if pub, err = crypto.UnmarshalPubkey(b); err != nil {
utils.Fatalf("Error: invalid peer public key")
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions common/types.go
Expand Up @@ -26,8 +26,9 @@ import (
"reflect"
"strings"

"golang.org/x/crypto/sha3"

"github.com/gochain-io/gochain/v3/common/hexutil"
"github.com/gochain-io/gochain/v3/crypto/sha3"
)

// Lengths of hashes and addresses in bytes.
Expand Down Expand Up @@ -218,8 +219,9 @@ func (a Address) Hash() Hash { return BytesToHash(a[:]) }
// Hex returns an EIP55-compliant hex string representation of the address.
func (a Address) Hex() string {
unchecksummed := hex.EncodeToString(a[:])
var hash Hash
sha3.Keccak256(hash[:], []byte(unchecksummed))
sha := sha3.NewLegacyKeccak256()
sha.Write([]byte(unchecksummed))
hash := sha.Sum(nil)

result := []byte(unchecksummed)
for i := 0; i < len(result); i++ {
Expand Down
4 changes: 2 additions & 2 deletions consensus/clique/clique.go
Expand Up @@ -29,14 +29,14 @@ import (

"github.com/hashicorp/golang-lru"
"go.opencensus.io/trace"
"golang.org/x/crypto/sha3"

"github.com/gochain-io/gochain/v3/accounts"
"github.com/gochain-io/gochain/v3/common"
"github.com/gochain-io/gochain/v3/common/hexutil"
"github.com/gochain-io/gochain/v3/consensus"
"github.com/gochain-io/gochain/v3/core/types"
"github.com/gochain-io/gochain/v3/crypto"
"github.com/gochain-io/gochain/v3/crypto/sha3"
"github.com/gochain-io/gochain/v3/log"
"github.com/gochain-io/gochain/v3/params"
"github.com/gochain-io/gochain/v3/rlp"
Expand Down Expand Up @@ -143,7 +143,7 @@ var (
// panics. This is done to avoid accidentally using both forms (signature present
// or not), which could be abused to produce different hashes for the same header.
func sigHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewKeccak256SingleSum()
hasher := sha3.NewLegacyKeccak256()

rlp.Encode(hasher, []interface{}{
header.ParentHash,
Expand Down
9 changes: 5 additions & 4 deletions core/rawdb/accessors_chain_test.go
Expand Up @@ -21,9 +21,10 @@ import (
"math/big"
"testing"

"golang.org/x/crypto/sha3"

"github.com/gochain-io/gochain/v3/common"
"github.com/gochain-io/gochain/v3/core/types"
"github.com/gochain-io/gochain/v3/crypto/sha3"
"github.com/gochain-io/gochain/v3/ethdb"
"github.com/gochain-io/gochain/v3/rlp"
)
Expand All @@ -47,7 +48,7 @@ func TestHeaderStorage(t *testing.T) {
if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
t.Fatalf("Stored header RLP not found")
} else {
hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
hasher.Write(entry)

if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
Expand All @@ -68,7 +69,7 @@ func TestBodyStorage(t *testing.T) {
// Create a test body to move around the database and make sure it's really new
body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}

hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
rlp.Encode(hasher, body)
hash := common.BytesToHash(hasher.Sum(nil))

Expand All @@ -85,7 +86,7 @@ func TestBodyStorage(t *testing.T) {
if entry := ReadBodyRLP(db, hash, 0); entry == nil {
t.Fatalf("Stored body RLP not found")
} else {
hasher := sha3.NewKeccak256()
hasher := sha3.NewLegacyKeccak256()
hasher.Write(entry)

if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
Expand Down
5 changes: 3 additions & 2 deletions core/types/block.go
Expand Up @@ -26,9 +26,10 @@ import (
"time"
"unsafe"

"golang.org/x/crypto/sha3"

"github.com/gochain-io/gochain/v3/common"
"github.com/gochain-io/gochain/v3/common/hexutil"
"github.com/gochain-io/gochain/v3/crypto/sha3"
"github.com/gochain-io/gochain/v3/rlp"
)

Expand Down Expand Up @@ -113,7 +114,7 @@ func (h *Header) Size() common.StorageSize {
}

func rlpHash(x interface{}) (h common.Hash) {
hw := sha3.NewKeccak256SingleSum()
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, x)
hw.Sum(h[:0])
return h
Expand Down
5 changes: 3 additions & 2 deletions core/vm/instructions.go
Expand Up @@ -21,10 +21,11 @@ import (
"fmt"
"math/big"

"golang.org/x/crypto/sha3"

"github.com/gochain-io/gochain/v3/common"
"github.com/gochain-io/gochain/v3/common/math"
"github.com/gochain-io/gochain/v3/core/types"
"github.com/gochain-io/gochain/v3/crypto/sha3"
"github.com/gochain-io/gochain/v3/params"
)

Expand Down Expand Up @@ -387,7 +388,7 @@ func opSha3(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
data := memory.Get(offset.Int64(), size.Int64())

if interpreter.hasher == nil {
interpreter.hasher = sha3.NewKeccak256().(keccakState)
interpreter.hasher = sha3.NewLegacyKeccak256().(keccakState)
} else {
interpreter.hasher.Reset()
}
Expand Down
3 changes: 2 additions & 1 deletion crypto/sha3/LICENSE → crypto/bn256/LICENSE
@@ -1,4 +1,5 @@
Copyright (c) 2009 The Go Authors. All rights reserved.
Copyright (c) 2012 The Go Authors. All rights reserved.
Copyright (c) 2018 Péter Szilágyi. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
Expand Down
18 changes: 3 additions & 15 deletions crypto/bn256/bn256_fast.go
@@ -1,18 +1,6 @@
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Copyright 2018 Péter Szilágyi. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.

// +build amd64 arm64

Expand Down
18 changes: 3 additions & 15 deletions crypto/bn256/bn256_fuzz.go
@@ -1,18 +1,6 @@
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Copyright 2018 Péter Szilágyi. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.

// +build gofuzz

Expand Down
18 changes: 3 additions & 15 deletions crypto/bn256/bn256_slow.go
@@ -1,18 +1,6 @@
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Copyright 2018 Péter Szilágyi. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.

// +build !amd64,!arm64

Expand Down
10 changes: 9 additions & 1 deletion crypto/bn256/cloudflare/example_test.go
Expand Up @@ -6,9 +6,12 @@ package bn256

import (
"crypto/rand"
"testing"

"github.com/stretchr/testify/require"
)

func ExamplePair() {
func TestExamplePair(t *testing.T) {
// This implements the tripartite Diffie-Hellman algorithm from "A One
// Round Protocol for Tripartite Diffie-Hellman", A. Joux.
// http://www.springerlink.com/content/cddc57yyva0hburb/fulltext.pdf
Expand Down Expand Up @@ -40,4 +43,9 @@ func ExamplePair() {
k3.ScalarMult(k3, c)

// k1, k2 and k3 will all be equal.

require.Equal(t, k1, k2)
require.Equal(t, k1, k3)

require.Equal(t, len(np), 4) //Avoid gometalinter varcheck err on np
}

0 comments on commit 74d960d

Please sign in to comment.