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
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
46 changes: 45 additions & 1 deletion op-program/client/io_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewOracleClientAndHintWriter() (preimage.Oracle, preimage.Hinter) {
type wasmHostIO struct {
}

func (o wasmHostIO) Get(key preimage.Key) []byte {
func (o wasmHostIO) OldGet(key preimage.Key) []byte {
_key := key.PreimageKey()
_, _isPublic := key.(preimage.LocalIndexKey)

Expand All @@ -44,12 +44,52 @@ func (o wasmHostIO) Get(key preimage.Key) []byte {
// TODO: can use customized circuit to optimize
if !_isPublic {
hash := crypto.Keccak256Hash(buf)
//hash := Keccak256Hash(buf)
hash[0] = _key[0]
require_bool(hash == _key)
}
return buf
}

func (o wasmHostIO) Get(key preimage.Key) []byte {
_key := key.PreimageKey()
_, _isPublic := key.(preimage.LocalIndexKey)

size := wasm_input(0)
padding := size % 136
if padding != 0 {
padding = 136 - padding
} else {
padding = 136
}

buf := make([]byte, size+padding)

ssize := size / 8
for i := uint64(0); i < ssize; i++ {
data := wasm_input(0)
binary.BigEndian.PutUint64(buf[i*8:], data)
}

if ssize*8 < size {
data := wasm_input(0)
var sv uint64 = 56
for i := uint64(ssize * 8); i < size; i++ {
buf[i] = byte(data >> sv)
sv = sv - 8
}
}
// Integrity check
// TODO: can use customized circuit to optimize
if !_isPublic {
// hash := crypto.Keccak256Hash(buf)
hash := Keccak256Hash(buf, size, padding)
hash[0] = _key[0]
require_bool(hash == _key)
}
return buf[:size]
}

func (o wasmHostIO) Hint(v preimage.Hint) {
// do nothing
return
Expand All @@ -67,6 +107,10 @@ func wasm_output(value uint64)
//go:noescape
func require(uint32)

//go:wasmimport env wasm_dbg
//go:noescape
func wasm_dbg(uint64)

func require_bool(cond bool) {
if cond {
require(1)
Expand Down
89 changes: 89 additions & 0 deletions op-program/client/keccak256_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//go:build js || wasm || wasip1
// +build js wasm wasip1

package client

import "encoding/binary"

//go:wasmimport env keccak_new
//go:noescape
func keccak_new(uint64)

//go:wasmimport env keccak_push
//go:noescape
func keccak_push(uint64)

//go:wasmimport env keccak_finalize
//go:noescape
func keccak_finalize() uint64

var hash [32]byte

func Keccak256Hash(data []byte, size uint64, padding uint64) [32]byte {
total_len := len(data)
if padding == 1 {
data[total_len-1] = 0x81
} else {
data[size] = 0x01
data[total_len-1] = 0x80
}

var hash_0 uint64
var hash_1 uint64
var hash_2 uint64
var hash_3 uint64

var val uint64

round := total_len / 136
keccak_new(1)
for i := 0; i < round; i++ {
for j := 0; j < 17; j++ {
start := i*136 + j*8
val = binary.LittleEndian.Uint64(data[start : start+8])
keccak_push(val)
}
hash_0 = keccak_finalize()
hash_1 = keccak_finalize()
hash_2 = keccak_finalize()
hash_3 = keccak_finalize()
keccak_new(0)
}

binary.LittleEndian.PutUint64(hash[:], hash_0)
binary.LittleEndian.PutUint64(hash[8:], hash_1)
binary.LittleEndian.PutUint64(hash[16:], hash_2)
binary.LittleEndian.PutUint64(hash[24:], hash_3)

return hash
}

/*
// for test

func keccak256check(input []byte, output []byte) {
result := Keccak256Hash(input)
for i := 0; i < len(result); i++ {
if result[i] != output[i] {
require(1)
require(0)
}
}
}

func main() {
input := make([]byte, 0)
emtpy_output := []byte{
197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83,
202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112,
}
keccak256check(input, emtpy_output)

input = []byte{102, 111, 111, 98, 97, 114, 97, 97}
short_output := []byte{
172, 132, 33, 155, 248, 181, 178, 245, 199, 105, 157, 164, 188, 53, 193, 25, 7, 35, 159,
188, 30, 123, 91, 143, 30, 100, 188, 128, 172, 248, 137, 202,
}
keccak256check(input, short_output)
}
*/
3 changes: 2 additions & 1 deletion op-program/client/smoke_test_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
package client

// in smoke test we only run 10 steps
var maximumSteps = 10
//var maximumSteps = 10
var maximumSteps = 1