Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #2241: Use Keccak-256 from golang.org/x/crypto/sha3 and mention explicitly #2242

Merged
merged 12 commits into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 0 additions & 18 deletions common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,6 @@ func FromHex(s string) []byte {
return nil
}

type Bytes []byte

func (self Bytes) String() string {
return string(self)
}

func DeleteFromByteSlice(s [][]byte, hash []byte) [][]byte {
for i, h := range s {
if bytes.Compare(h, hash) == 0 {
return append(s[:i:i], s[i+1:]...)
}
}

return s
}

// Number to bytes
//
// Returns the number in bytes with the specified base
Expand Down Expand Up @@ -154,7 +138,6 @@ func Hex2Bytes(str string) []byte {
}

func Hex2BytesFixed(str string, flen int) []byte {

h, _ := hex.DecodeString(str)
if len(h) == flen {
return h
Expand All @@ -167,7 +150,6 @@ func Hex2BytesFixed(str string, flen int) []byte {
return hh
}
}

}

func StringToByteFunc(str string, cb func(str string) []byte) (ret []byte) {
Expand Down
20 changes: 0 additions & 20 deletions common/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,6 @@ type BytesSuite struct{}

var _ = checker.Suite(&BytesSuite{})

func (s *BytesSuite) TestByteString(c *checker.C) {
var data Bytes
data = []byte{102, 111, 111}
exp := "foo"
res := data.String()

c.Assert(res, checker.Equals, exp)
}

/*
func (s *BytesSuite) TestDeleteFromByteSlice(c *checker.C) {
data := []byte{1, 2, 3, 4}
slice := []byte{1, 2, 3, 4}
exp := []byte{1, 4}
res := DeleteFromByteSlice(data, slice)

c.Assert(res, checker.DeepEquals, exp)
}

*/
func (s *BytesSuite) TestNumberToBytes(c *checker.C) {
// data1 := int(1)
// res1 := NumberToBytes(data1, 16)
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
Loading