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

Keccak256 host #11

Closed
wants to merge 10 commits into from
Closed
Changes from 2 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
2 changes: 1 addition & 1 deletion op-program/client/io_wasm.go
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ func (o wasmHostIO) Get(key preimage.Key) []byte {
// Integrity check
// TODO: can use customized circuit to optimize
if !_isPublic {
//hash := crypto.Keccak256Hash(buf)
// hash := crypto.Keccak256Hash(buf)
hash := Keccak256Hash(buf)
hash[0] = _key[0]
require_bool(hash == _key)
18 changes: 16 additions & 2 deletions op-program/client/keccak256_wasm.go
Original file line number Diff line number Diff line change
@@ -55,15 +55,15 @@ func (kh *KeccakHasher) UpdateByte(v byte) {
func (kh *KeccakHasher) Finalize() [4]uint64 {
bytesToPad := 136 - kh.byteIdx - kh.bufSize*8
if bytesToPad == 1 {
var result uint64 = 0x86 << 56
var result uint64 = 0x81 << 56
keccak_push(kh.data + result)
} else {
kh.UpdateByte(1)
for i := 0; i < int(bytesToPad)-2; i++ {
kh.UpdateByte(0)
}
var result uint64 = 0x80 << 56
keccak_push(kh.data + result)
keccak_push(kh.data ^ result)
}

return [4]uint64{
@@ -88,6 +88,20 @@ func Keccak256Hash(data ...[]byte) (output [32]byte) {
return output
}

func Keccak256HashSimple(data ...[]byte) (output [32]byte) {
hasher := NewKeccakHasher()
for _, value := range data {
for _, byteValue := range value {
hasher.UpdateByte(byteValue)
}
}
result := hasher.Finalize()
for i, val := range result {
binary.LittleEndian.PutUint64(output[i*8:], val)
}
return output
}

/*
// for test