Skip to content

Commit 436fc8d

Browse files
committed
all: Rename crypto.Sha3{,Hash}() to crypto.Keccak256{,Hash}()
As we aren't really using the standarized SHA-3
1 parent c20d6e5 commit 436fc8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+92
-92
lines changed

accounts/abi/abi_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func TestMethodSignature(t *testing.T) {
244244
t.Error("signature mismatch", exp, "!=", m.Sig())
245245
}
246246

247-
idexp := crypto.Sha3([]byte(exp))[:4]
247+
idexp := crypto.Keccak256([]byte(exp))[:4]
248248
if !bytes.Equal(m.Id(), idexp) {
249249
t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
250250
}
@@ -264,7 +264,7 @@ func TestPack(t *testing.T) {
264264
t.FailNow()
265265
}
266266

267-
sig := crypto.Sha3([]byte("foo(uint32)"))[:4]
267+
sig := crypto.Keccak256([]byte("foo(uint32)"))[:4]
268268
sig = append(sig, make([]byte, 32)...)
269269
sig[35] = 10
270270

@@ -286,7 +286,7 @@ func TestMultiPack(t *testing.T) {
286286
t.FailNow()
287287
}
288288

289-
sig := crypto.Sha3([]byte("bar(uint32,uint16)"))[:4]
289+
sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
290290
sig = append(sig, make([]byte, 64)...)
291291
sig[35] = 10
292292
sig[67] = 11
@@ -309,7 +309,7 @@ func TestPackSlice(t *testing.T) {
309309
t.FailNow()
310310
}
311311

312-
sig := crypto.Sha3([]byte("slice(uint32[2])"))[:4]
312+
sig := crypto.Keccak256([]byte("slice(uint32[2])"))[:4]
313313
sig = append(sig, make([]byte, 64)...)
314314
sig[35] = 1
315315
sig[67] = 2
@@ -332,7 +332,7 @@ func TestPackSliceBig(t *testing.T) {
332332
t.FailNow()
333333
}
334334

335-
sig := crypto.Sha3([]byte("slice256(uint256[2])"))[:4]
335+
sig := crypto.Keccak256([]byte("slice256(uint256[2])"))[:4]
336336
sig = append(sig, make([]byte, 64)...)
337337
sig[35] = 1
338338
sig[67] = 2

accounts/abi/event.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ func (e Event) Id() common.Hash {
4040
types[i] = input.Type.String()
4141
i++
4242
}
43-
return common.BytesToHash(crypto.Sha3([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ",")))))
43+
return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ",")))))
4444
}

accounts/abi/event_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func TestEventId(t *testing.T) {
1919
{ "type" : "event", "name" : "check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }
2020
]`,
2121
expectations: map[string]common.Hash{
22-
"balance": crypto.Sha3Hash([]byte("balance(uint256)")),
23-
"check": crypto.Sha3Hash([]byte("check(address,uint256)")),
22+
"balance": crypto.Keccak256Hash([]byte("balance(uint256)")),
23+
"check": crypto.Keccak256Hash([]byte("check(address,uint256)")),
2424
},
2525
},
2626
}

accounts/abi/method.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ func (m Method) String() string {
7272
}
7373

7474
func (m Method) Id() []byte {
75-
return crypto.Sha3([]byte(m.Sig()))[:4]
75+
return crypto.Keccak256([]byte(m.Sig()))[:4]
7676
}

cmd/geth/js_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ multiply7 = Multiply7.at(contractaddress);
396396
if sol != nil && solcVersion != sol.Version() {
397397
modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"`+sol.Version()+`"`))
398398
fmt.Printf("modified contractinfo:\n%s\n", modContractInfo)
399-
contentHash = `"` + common.ToHex(crypto.Sha3([]byte(modContractInfo))) + `"`
399+
contentHash = `"` + common.ToHex(crypto.Keccak256([]byte(modContractInfo))) + `"`
400400
}
401401
if checkEvalJSON(t, repl, `filename = "/tmp/info.json"`, `"/tmp/info.json"`) != nil {
402402
return

common/compiler/solidity.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func SaveInfo(info *ContractInfo, filename string) (contenthash common.Hash, err
220220
if err != nil {
221221
return
222222
}
223-
contenthash = common.BytesToHash(crypto.Sha3(infojson))
223+
contenthash = common.BytesToHash(crypto.Keccak256(infojson))
224224
err = ioutil.WriteFile(filename, infojson, 0600)
225225
return
226226
}

common/httpclient/httpclient.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (self *HTTPClient) GetAuthContent(uri string, hash common.Hash) ([]byte, er
7474
}
7575

7676
// check hash to authenticate content
77-
chash := crypto.Sha3Hash(content)
77+
chash := crypto.Keccak256Hash(content)
7878
if chash != hash {
7979
return nil, fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:])
8080
}

common/httpclient/httpclient_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestGetAuthContent(t *testing.T) {
3636
client := New(dir)
3737

3838
text := "test"
39-
hash := crypto.Sha3Hash([]byte(text))
39+
hash := crypto.Keccak256Hash([]byte(text))
4040
if err := ioutil.WriteFile(path.Join(dir, "test.content"), []byte(text), os.ModePerm); err != nil {
4141
t.Fatal("could not write test file", err)
4242
}

common/natspec/natspec.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, client *httpc
115115
err = fmt.Errorf("contract (%v) not found", contractAddress)
116116
return
117117
}
118-
codehash := common.BytesToHash(crypto.Sha3(codeb))
118+
codehash := common.BytesToHash(crypto.Keccak256(codeb))
119119
// set up nameresolver with natspecreg + urlhint contract addresses
120120
reg := registrar.New(xeth)
121121

@@ -197,7 +197,7 @@ type userDoc struct {
197197
func (self *NatSpec) makeAbi2method(abiKey [8]byte) (meth *method) {
198198
for signature, m := range self.userDoc.Methods {
199199
name := strings.Split(signature, "(")[0]
200-
hash := []byte(common.Bytes2Hex(crypto.Sha3([]byte(signature))))
200+
hash := []byte(common.Bytes2Hex(crypto.Keccak256([]byte(signature))))
201201
var key [8]byte
202202
copy(key[:], hash[:8])
203203
if bytes.Equal(key[:], abiKey[:]) {

common/natspec/natspec_e2e_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ func TestNatspecE2E(t *testing.T) {
238238
// create a contractInfo file (mock cloud-deployed contract metadocs)
239239
// incidentally this is the info for the HashReg contract itself
240240
ioutil.WriteFile("/tmp/"+testFileName, []byte(testContractInfo), os.ModePerm)
241-
dochash := crypto.Sha3Hash([]byte(testContractInfo))
241+
dochash := crypto.Keccak256Hash([]byte(testContractInfo))
242242

243243
// take the codehash for the contract we wanna test
244244
codeb := tf.xeth.CodeAtBytes(registrar.HashRegAddr)
245-
codehash := crypto.Sha3Hash(codeb)
245+
codehash := crypto.Keccak256Hash(codeb)
246246

247247
reg := registrar.New(tf.xeth)
248248
_, err := reg.SetHashToHash(addr, codehash, dochash)

common/registrar/ethreg/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (api *PrivateRegistarAPI) Register(sender common.Address, addr common.Addre
8686
}
8787

8888
codeb := state.GetCode(addr)
89-
codeHash := common.BytesToHash(crypto.Sha3(codeb))
89+
codeHash := common.BytesToHash(crypto.Keccak256(codeb))
9090
contentHash := common.HexToHash(contentHashHex)
9191

9292
_, err = registrar.New(api.be).SetHashToHash(sender, codeHash, contentHash)

common/registrar/registrar.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const (
6868
)
6969

7070
func abiSignature(s string) string {
71-
return common.ToHex(crypto.Sha3([]byte(s))[:4])
71+
return common.ToHex(crypto.Keccak256([]byte(s))[:4])
7272
}
7373

7474
var (
@@ -401,7 +401,7 @@ func storageMapping(addr, key []byte) []byte {
401401
data := make([]byte, 64)
402402
copy(data[0:32], key[0:32])
403403
copy(data[32:64], addr[0:32])
404-
sha := crypto.Sha3(data)
404+
sha := crypto.Keccak256(data)
405405
return sha
406406
}
407407

common/registrar/registrar_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type testBackend struct {
3131
var (
3232
text = "test"
3333
codehash = common.StringToHash("1234")
34-
hash = common.BytesToHash(crypto.Sha3([]byte(text)))
34+
hash = common.BytesToHash(crypto.Keccak256([]byte(text)))
3535
url = "bzz://bzzhash/my/path/contr.act"
3636
)
3737

compression/rle/read_write.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const (
3131
tokenToken = 0xff
3232
)
3333

34-
var empty = crypto.Sha3([]byte(""))
35-
var emptyList = crypto.Sha3([]byte{0x80})
34+
var empty = crypto.Keccak256([]byte(""))
35+
var emptyList = crypto.Keccak256([]byte{0x80})
3636

3737
func Decompress(dat []byte) ([]byte, error) {
3838
buf := new(bytes.Buffer)

compression/rle/read_write_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (s *CompressionRleSuite) TestDecompressSimple(c *checker.C) {
6767
// }
6868

6969
// var exp []byte
70-
// exp = append(exp, crypto.Sha3([]byte(""))...)
71-
// exp = append(exp, crypto.Sha3([]byte{0x80})...)
70+
// exp = append(exp, crypto.Keccak256([]byte(""))...)
71+
// exp = append(exp, crypto.Keccak256([]byte{0x80})...)
7272
// exp = append(exp, make([]byte, 10)...)
7373

7474
// if bytes.Compare(res, res) != 0 {
@@ -82,12 +82,12 @@ func (s *CompressionRleSuite) TestDecompressSimple(c *checker.C) {
8282
// t.Error("5 * zero", res)
8383
// }
8484

85-
// res = Compress(crypto.Sha3([]byte("")))
85+
// res = Compress(crypto.Keccak256([]byte("")))
8686
// if bytes.Compare(res, []byte{token, emptyShaToken}) != 0 {
8787
// t.Error("empty sha", res)
8888
// }
8989

90-
// res = Compress(crypto.Sha3([]byte{0x80}))
90+
// res = Compress(crypto.Keccak256([]byte{0x80}))
9191
// if bytes.Compare(res, []byte{token, emptyListShaToken}) != 0 {
9292
// t.Error("empty list sha", res)
9393
// }
@@ -100,8 +100,8 @@ func (s *CompressionRleSuite) TestDecompressSimple(c *checker.C) {
100100

101101
// func TestCompressMulti(t *testing.T) {
102102
// in := []byte{0, 0, 0, 0, 0}
103-
// in = append(in, crypto.Sha3([]byte(""))...)
104-
// in = append(in, crypto.Sha3([]byte{0x80})...)
103+
// in = append(in, crypto.Keccak256([]byte(""))...)
104+
// in = append(in, crypto.Keccak256([]byte{0x80})...)
105105
// in = append(in, token)
106106
// res := Compress(in)
107107

@@ -116,8 +116,8 @@ func (s *CompressionRleSuite) TestDecompressSimple(c *checker.C) {
116116

117117
// for i := 0; i < 20; i++ {
118118
// in = append(in, []byte{0, 0, 0, 0, 0}...)
119-
// in = append(in, crypto.Sha3([]byte(""))...)
120-
// in = append(in, crypto.Sha3([]byte{0x80})...)
119+
// in = append(in, crypto.Keccak256([]byte(""))...)
120+
// in = append(in, crypto.Keccak256([]byte{0x80})...)
121121
// in = append(in, []byte{123, 2, 19, 89, 245, 254, 255, token, 98, 233}...)
122122
// in = append(in, token)
123123
// }

core/state/state_object.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/trie"
3131
)
3232

33-
var emptyCodeHash = crypto.Sha3(nil)
33+
var emptyCodeHash = crypto.Keccak256(nil)
3434

3535
type Code []byte
3636

@@ -225,7 +225,7 @@ func (self *StateObject) Code() []byte {
225225

226226
func (self *StateObject) SetCode(code []byte) {
227227
self.code = code
228-
self.codeHash = crypto.Sha3(code)
228+
self.codeHash = crypto.Keccak256(code)
229229
self.dirty = true
230230
}
231231

core/types/bloom9.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func LogsBloom(logs vm.Logs) *big.Int {
101101
}
102102

103103
func bloom9(b []byte) *big.Int {
104-
b = crypto.Sha3(b[:])
104+
b = crypto.Keccak256(b[:])
105105

106106
r := new(big.Int)
107107

core/types/bloom9_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestBloom9(t *testing.T) {
7373
func TestAddress(t *testing.T) {
7474
block := &Block{}
7575
block.Coinbase = common.Hex2Bytes("22341ae42d6dd7384bc8584e50419ea3ac75b83f")
76-
fmt.Printf("%x\n", crypto.Sha3(block.Coinbase))
76+
fmt.Printf("%x\n", crypto.Keccak256(block.Coinbase))
7777
7878
bin := CreateBloom(block)
7979
fmt.Printf("bin = %x\n", common.LeftPadBytes(bin, 64))

core/types/transaction.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func doFrom(tx *Transaction, homestead bool) (common.Address, error) {
202202
return common.Address{}, err
203203
}
204204
var addr common.Address
205-
copy(addr[:], crypto.Sha3(pubkey[1:])[12:])
205+
copy(addr[:], crypto.Keccak256(pubkey[1:])[12:])
206206
tx.from.Store(addr)
207207
return addr, nil
208208
}

core/vm/contracts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func ecrecoverFunc(in []byte) []byte {
111111
}
112112

113113
// the first byte of pubkey is bitcoin heritage
114-
return common.LeftPadBytes(crypto.Sha3(pubKey[1:])[12:], 32)
114+
return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32)
115115
}
116116

117117
func memCpy(in []byte) []byte {

core/vm/instructions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func opMulmod(instr instruction, pc *uint64, env Environment, contract *Contract
316316

317317
func opSha3(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *stack) {
318318
offset, size := stack.pop(), stack.pop()
319-
hash := crypto.Sha3(memory.Get(offset.Int64(), size.Int64()))
319+
hash := crypto.Keccak256(memory.Get(offset.Int64(), size.Int64()))
320320

321321
stack.push(common.BytesToBig(hash))
322322
}

core/vm/jit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type Program struct {
9696
// NewProgram returns a new JIT program
9797
func NewProgram(code []byte) *Program {
9898
program := &Program{
99-
Id: crypto.Sha3Hash(code),
99+
Id: crypto.Keccak256Hash(code),
100100
mapping: make(map[uint64]uint64),
101101
destinations: make(map[uint64]struct{}),
102102
code: code,

core/vm/jit_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (self *Env) Db() Database { return nil }
189189
func (self *Env) GasLimit() *big.Int { return self.gasLimit }
190190
func (self *Env) VmType() Type { return StdVmTy }
191191
func (self *Env) GetHash(n uint64) common.Hash {
192-
return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String())))
192+
return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
193193
}
194194
func (self *Env) AddLog(log *Log) {
195195
}

core/vm/runtime/runtime.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func setDefaults(cfg *Config) {
6767
}
6868
if cfg.GetHashFn == nil {
6969
cfg.GetHashFn = func(n uint64) common.Hash {
70-
return common.BytesToHash(crypto.Sha3([]byte(new(big.Int).SetUint64(n).String())))
70+
return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
7171
}
7272
}
7373
}

core/vm/vm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
5858
}
5959

6060
var (
61-
codehash = crypto.Sha3Hash(contract.Code) // codehash is used when doing jump dest caching
61+
codehash = crypto.Keccak256Hash(contract.Code) // codehash is used when doing jump dest caching
6262
program *Program
6363
)
6464
if EnableJit {

core/vm/vm_jit.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (self *JitVm) Run(me, caller ContextRef, code []byte, value, gas, price *bi
200200
self.data.timestamp = self.env.Time()
201201
self.data.code = getDataPtr(code)
202202
self.data.codeSize = uint64(len(code))
203-
self.data.codeHash = hash2llvm(crypto.Sha3(code)) // TODO: Get already computed hash?
203+
self.data.codeHash = hash2llvm(crypto.Keccak256(code)) // TODO: Get already computed hash?
204204
205205
jit := C.evmjit_create()
206206
retCode := C.evmjit_run(jit, unsafe.Pointer(&self.data), unsafe.Pointer(self))
@@ -242,7 +242,7 @@ func (self *JitVm) Env() Environment {
242242
//export env_sha3
243243
func env_sha3(dataPtr *byte, length uint64, resultPtr unsafe.Pointer) {
244244
data := llvm2bytesRef(dataPtr, length)
245-
hash := crypto.Sha3(data)
245+
hash := crypto.Keccak256(data)
246246
result := (*i256)(resultPtr)
247247
*result = hash2llvm(hash)
248248
}

crypto/crypto.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ import (
4343
"golang.org/x/crypto/ripemd160"
4444
)
4545

46-
func Sha3(data ...[]byte) []byte {
46+
func Keccak256(data ...[]byte) []byte {
4747
d := sha3.NewKeccak256()
4848
for _, b := range data {
4949
d.Write(b)
5050
}
5151
return d.Sum(nil)
5252
}
5353

54-
func Sha3Hash(data ...[]byte) (h common.Hash) {
54+
func Keccak256Hash(data ...[]byte) (h common.Hash) {
5555
d := sha3.NewKeccak256()
5656
for _, b := range data {
5757
d.Write(b)
@@ -63,7 +63,7 @@ func Sha3Hash(data ...[]byte) (h common.Hash) {
6363
// Creates an ethereum address given the bytes and the nonce
6464
func CreateAddress(b common.Address, nonce uint64) common.Address {
6565
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
66-
return common.BytesToAddress(Sha3(data)[12:])
66+
return common.BytesToAddress(Keccak256(data)[12:])
6767
//return Sha3(common.NewValue([]interface{}{b, nonce}).Encode())[12:]
6868
}
6969

@@ -265,7 +265,7 @@ func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error
265265
if err != nil {
266266
return nil, err
267267
}
268-
ethPriv := Sha3(plainText)
268+
ethPriv := Keccak256(plainText)
269269
ecKey := ToECDSA(ethPriv)
270270
key = &Key{
271271
Id: nil,
@@ -330,7 +330,7 @@ func PKCS7Unpad(in []byte) []byte {
330330

331331
func PubkeyToAddress(p ecdsa.PublicKey) common.Address {
332332
pubBytes := FromECDSAPub(&p)
333-
return common.BytesToAddress(Sha3(pubBytes[1:])[12:])
333+
return common.BytesToAddress(Keccak256(pubBytes[1:])[12:])
334334
}
335335

336336
func zeroBytes(bytes []byte) {

0 commit comments

Comments
 (0)