Skip to content

Commit

Permalink
all: Rename crypto.Sha3{,Hash}() to crypto.Keccak256{,Hash}()
Browse files Browse the repository at this point in the history
As we aren't really using the standarized SHA-3
  • Loading branch information
jimenezrick committed Feb 21, 2016
1 parent c20d6e5 commit 436fc8d
Show file tree
Hide file tree
Showing 48 changed files with 92 additions and 92 deletions.
10 changes: 5 additions & 5 deletions accounts/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestMethodSignature(t *testing.T) {
t.Error("signature mismatch", exp, "!=", m.Sig())
}

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

sig := crypto.Sha3([]byte("foo(uint32)"))[:4]
sig := crypto.Keccak256([]byte("foo(uint32)"))[:4]
sig = append(sig, make([]byte, 32)...)
sig[35] = 10

Expand All @@ -286,7 +286,7 @@ func TestMultiPack(t *testing.T) {
t.FailNow()
}

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

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

sig := crypto.Sha3([]byte("slice256(uint256[2])"))[:4]
sig := crypto.Keccak256([]byte("slice256(uint256[2])"))[:4]
sig = append(sig, make([]byte, 64)...)
sig[35] = 1
sig[67] = 2
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ func (e Event) Id() common.Hash {
types[i] = input.Type.String()
i++
}
return common.BytesToHash(crypto.Sha3([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ",")))))
return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ",")))))
}
4 changes: 2 additions & 2 deletions accounts/abi/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestEventId(t *testing.T) {
{ "type" : "event", "name" : "check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }
]`,
expectations: map[string]common.Hash{
"balance": crypto.Sha3Hash([]byte("balance(uint256)")),
"check": crypto.Sha3Hash([]byte("check(address,uint256)")),
"balance": crypto.Keccak256Hash([]byte("balance(uint256)")),
"check": crypto.Keccak256Hash([]byte("check(address,uint256)")),
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion accounts/abi/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ func (m Method) String() string {
}

func (m Method) Id() []byte {
return crypto.Sha3([]byte(m.Sig()))[:4]
return crypto.Keccak256([]byte(m.Sig()))[:4]
}
2 changes: 1 addition & 1 deletion cmd/geth/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ multiply7 = Multiply7.at(contractaddress);
if sol != nil && solcVersion != sol.Version() {
modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"`+sol.Version()+`"`))
fmt.Printf("modified contractinfo:\n%s\n", modContractInfo)
contentHash = `"` + common.ToHex(crypto.Sha3([]byte(modContractInfo))) + `"`
contentHash = `"` + common.ToHex(crypto.Keccak256([]byte(modContractInfo))) + `"`
}
if checkEvalJSON(t, repl, `filename = "/tmp/info.json"`, `"/tmp/info.json"`) != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion common/compiler/solidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func SaveInfo(info *ContractInfo, filename string) (contenthash common.Hash, err
if err != nil {
return
}
contenthash = common.BytesToHash(crypto.Sha3(infojson))
contenthash = common.BytesToHash(crypto.Keccak256(infojson))
err = ioutil.WriteFile(filename, infojson, 0600)
return
}
2 changes: 1 addition & 1 deletion common/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (self *HTTPClient) GetAuthContent(uri string, hash common.Hash) ([]byte, er
}

// check hash to authenticate content
chash := crypto.Sha3Hash(content)
chash := crypto.Keccak256Hash(content)
if chash != hash {
return nil, fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:])
}
Expand Down
2 changes: 1 addition & 1 deletion common/httpclient/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestGetAuthContent(t *testing.T) {
client := New(dir)

text := "test"
hash := crypto.Sha3Hash([]byte(text))
hash := crypto.Keccak256Hash([]byte(text))
if err := ioutil.WriteFile(path.Join(dir, "test.content"), []byte(text), os.ModePerm); err != nil {
t.Fatal("could not write test file", err)
}
Expand Down
4 changes: 2 additions & 2 deletions common/natspec/natspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, client *httpc
err = fmt.Errorf("contract (%v) not found", contractAddress)
return
}
codehash := common.BytesToHash(crypto.Sha3(codeb))
codehash := common.BytesToHash(crypto.Keccak256(codeb))
// set up nameresolver with natspecreg + urlhint contract addresses
reg := registrar.New(xeth)

Expand Down Expand Up @@ -197,7 +197,7 @@ type userDoc struct {
func (self *NatSpec) makeAbi2method(abiKey [8]byte) (meth *method) {
for signature, m := range self.userDoc.Methods {
name := strings.Split(signature, "(")[0]
hash := []byte(common.Bytes2Hex(crypto.Sha3([]byte(signature))))
hash := []byte(common.Bytes2Hex(crypto.Keccak256([]byte(signature))))
var key [8]byte
copy(key[:], hash[:8])
if bytes.Equal(key[:], abiKey[:]) {
Expand Down
4 changes: 2 additions & 2 deletions common/natspec/natspec_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ func TestNatspecE2E(t *testing.T) {
// create a contractInfo file (mock cloud-deployed contract metadocs)
// incidentally this is the info for the HashReg contract itself
ioutil.WriteFile("/tmp/"+testFileName, []byte(testContractInfo), os.ModePerm)
dochash := crypto.Sha3Hash([]byte(testContractInfo))
dochash := crypto.Keccak256Hash([]byte(testContractInfo))

// take the codehash for the contract we wanna test
codeb := tf.xeth.CodeAtBytes(registrar.HashRegAddr)
codehash := crypto.Sha3Hash(codeb)
codehash := crypto.Keccak256Hash(codeb)

reg := registrar.New(tf.xeth)
_, err := reg.SetHashToHash(addr, codehash, dochash)
Expand Down
2 changes: 1 addition & 1 deletion common/registrar/ethreg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (api *PrivateRegistarAPI) Register(sender common.Address, addr common.Addre
}

codeb := state.GetCode(addr)
codeHash := common.BytesToHash(crypto.Sha3(codeb))
codeHash := common.BytesToHash(crypto.Keccak256(codeb))
contentHash := common.HexToHash(contentHashHex)

_, err = registrar.New(api.be).SetHashToHash(sender, codeHash, contentHash)
Expand Down
4 changes: 2 additions & 2 deletions common/registrar/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const (
)

func abiSignature(s string) string {
return common.ToHex(crypto.Sha3([]byte(s))[:4])
return common.ToHex(crypto.Keccak256([]byte(s))[:4])
}

var (
Expand Down Expand Up @@ -401,7 +401,7 @@ func storageMapping(addr, key []byte) []byte {
data := make([]byte, 64)
copy(data[0:32], key[0:32])
copy(data[32:64], addr[0:32])
sha := crypto.Sha3(data)
sha := crypto.Keccak256(data)
return sha
}

Expand Down
2 changes: 1 addition & 1 deletion common/registrar/registrar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type testBackend struct {
var (
text = "test"
codehash = common.StringToHash("1234")
hash = common.BytesToHash(crypto.Sha3([]byte(text)))
hash = common.BytesToHash(crypto.Keccak256([]byte(text)))
url = "bzz://bzzhash/my/path/contr.act"
)

Expand Down
4 changes: 2 additions & 2 deletions compression/rle/read_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const (
tokenToken = 0xff
)

var empty = crypto.Sha3([]byte(""))
var emptyList = crypto.Sha3([]byte{0x80})
var empty = crypto.Keccak256([]byte(""))
var emptyList = crypto.Keccak256([]byte{0x80})

func Decompress(dat []byte) ([]byte, error) {
buf := new(bytes.Buffer)
Expand Down
16 changes: 8 additions & 8 deletions compression/rle/read_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (s *CompressionRleSuite) TestDecompressSimple(c *checker.C) {
// }

// var exp []byte
// exp = append(exp, crypto.Sha3([]byte(""))...)
// exp = append(exp, crypto.Sha3([]byte{0x80})...)
// exp = append(exp, crypto.Keccak256([]byte(""))...)
// exp = append(exp, crypto.Keccak256([]byte{0x80})...)
// exp = append(exp, make([]byte, 10)...)

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

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

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

// func TestCompressMulti(t *testing.T) {
// in := []byte{0, 0, 0, 0, 0}
// in = append(in, crypto.Sha3([]byte(""))...)
// in = append(in, crypto.Sha3([]byte{0x80})...)
// in = append(in, crypto.Keccak256([]byte(""))...)
// in = append(in, crypto.Keccak256([]byte{0x80})...)
// in = append(in, token)
// res := Compress(in)

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

// for i := 0; i < 20; i++ {
// in = append(in, []byte{0, 0, 0, 0, 0}...)
// in = append(in, crypto.Sha3([]byte(""))...)
// in = append(in, crypto.Sha3([]byte{0x80})...)
// in = append(in, crypto.Keccak256([]byte(""))...)
// in = append(in, crypto.Keccak256([]byte{0x80})...)
// in = append(in, []byte{123, 2, 19, 89, 245, 254, 255, token, 98, 233}...)
// in = append(in, token)
// }
Expand Down
4 changes: 2 additions & 2 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/ethereum/go-ethereum/trie"
)

var emptyCodeHash = crypto.Sha3(nil)
var emptyCodeHash = crypto.Keccak256(nil)

type Code []byte

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

func (self *StateObject) SetCode(code []byte) {
self.code = code
self.codeHash = crypto.Sha3(code)
self.codeHash = crypto.Keccak256(code)
self.dirty = true
}

Expand Down
2 changes: 1 addition & 1 deletion core/types/bloom9.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func LogsBloom(logs vm.Logs) *big.Int {
}

func bloom9(b []byte) *big.Int {
b = crypto.Sha3(b[:])
b = crypto.Keccak256(b[:])

r := new(big.Int)

Expand Down
2 changes: 1 addition & 1 deletion core/types/bloom9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestBloom9(t *testing.T) {
func TestAddress(t *testing.T) {
block := &Block{}
block.Coinbase = common.Hex2Bytes("22341ae42d6dd7384bc8584e50419ea3ac75b83f")
fmt.Printf("%x\n", crypto.Sha3(block.Coinbase))
fmt.Printf("%x\n", crypto.Keccak256(block.Coinbase))
bin := CreateBloom(block)
fmt.Printf("bin = %x\n", common.LeftPadBytes(bin, 64))
Expand Down
2 changes: 1 addition & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func doFrom(tx *Transaction, homestead bool) (common.Address, error) {
return common.Address{}, err
}
var addr common.Address
copy(addr[:], crypto.Sha3(pubkey[1:])[12:])
copy(addr[:], crypto.Keccak256(pubkey[1:])[12:])
tx.from.Store(addr)
return addr, nil
}
Expand Down
2 changes: 1 addition & 1 deletion core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func ecrecoverFunc(in []byte) []byte {
}

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

func memCpy(in []byte) []byte {
Expand Down
2 changes: 1 addition & 1 deletion core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func opMulmod(instr instruction, pc *uint64, env Environment, contract *Contract

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

stack.push(common.BytesToBig(hash))
}
Expand Down
2 changes: 1 addition & 1 deletion core/vm/jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type Program struct {
// NewProgram returns a new JIT program
func NewProgram(code []byte) *Program {
program := &Program{
Id: crypto.Sha3Hash(code),
Id: crypto.Keccak256Hash(code),
mapping: make(map[uint64]uint64),
destinations: make(map[uint64]struct{}),
code: code,
Expand Down
2 changes: 1 addition & 1 deletion core/vm/jit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (self *Env) Db() Database { return nil }
func (self *Env) GasLimit() *big.Int { return self.gasLimit }
func (self *Env) VmType() Type { return StdVmTy }
func (self *Env) GetHash(n uint64) common.Hash {
return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String())))
return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
}
func (self *Env) AddLog(log *Log) {
}
Expand Down
2 changes: 1 addition & 1 deletion core/vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func setDefaults(cfg *Config) {
}
if cfg.GetHashFn == nil {
cfg.GetHashFn = func(n uint64) common.Hash {
return common.BytesToHash(crypto.Sha3([]byte(new(big.Int).SetUint64(n).String())))
return common.BytesToHash(crypto.Keccak256([]byte(new(big.Int).SetUint64(n).String())))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (self *Vm) Run(contract *Contract, input []byte) (ret []byte, err error) {
}

var (
codehash = crypto.Sha3Hash(contract.Code) // codehash is used when doing jump dest caching
codehash = crypto.Keccak256Hash(contract.Code) // codehash is used when doing jump dest caching
program *Program
)
if EnableJit {
Expand Down
4 changes: 2 additions & 2 deletions core/vm/vm_jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (self *JitVm) Run(me, caller ContextRef, code []byte, value, gas, price *bi
self.data.timestamp = self.env.Time()
self.data.code = getDataPtr(code)
self.data.codeSize = uint64(len(code))
self.data.codeHash = hash2llvm(crypto.Sha3(code)) // TODO: Get already computed hash?
self.data.codeHash = hash2llvm(crypto.Keccak256(code)) // TODO: Get already computed hash?
jit := C.evmjit_create()
retCode := C.evmjit_run(jit, unsafe.Pointer(&self.data), unsafe.Pointer(self))
Expand Down Expand Up @@ -242,7 +242,7 @@ func (self *JitVm) Env() Environment {
//export env_sha3
func env_sha3(dataPtr *byte, length uint64, resultPtr unsafe.Pointer) {
data := llvm2bytesRef(dataPtr, length)
hash := crypto.Sha3(data)
hash := crypto.Keccak256(data)
result := (*i256)(resultPtr)
*result = hash2llvm(hash)
}
Expand Down
10 changes: 5 additions & 5 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ import (
"golang.org/x/crypto/ripemd160"
)

func Sha3(data ...[]byte) []byte {
func Keccak256(data ...[]byte) []byte {
d := sha3.NewKeccak256()
for _, b := range data {
d.Write(b)
}
return d.Sum(nil)
}

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

Expand Down Expand Up @@ -265,7 +265,7 @@ func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error
if err != nil {
return nil, err
}
ethPriv := Sha3(plainText)
ethPriv := Keccak256(plainText)
ecKey := ToECDSA(ethPriv)
key = &Key{
Id: nil,
Expand Down Expand Up @@ -330,7 +330,7 @@ func PKCS7Unpad(in []byte) []byte {

func PubkeyToAddress(p ecdsa.PublicKey) common.Address {
pubBytes := FromECDSAPub(&p)
return common.BytesToAddress(Sha3(pubBytes[1:])[12:])
return common.BytesToAddress(Keccak256(pubBytes[1:])[12:])
}

func zeroBytes(bytes []byte) {
Expand Down
Loading

0 comments on commit 436fc8d

Please sign in to comment.