Commit 4a02eb4d authored by inphi's avatar inphi

cannon: Fix oracle tests for mipsevm

parent 62fba98a
.section .test, "x" .section .test, "x"
.balign 4 .balign 4
.set noreorder .set noreorder
.global test .global test
.ent test .ent test
# load hash at 0x30001000 # load hash at 0x30001000
# 0x47173285 a8d7341e 5e972fc6 77286384 f802f8ef 42a5ec5f 03bbfa25 4cb01fad = "hello world" # 0x47173285 a8d7341e 5e972fc6 77286384 f802f8ef 42a5ec5f 03bbfa25 4cb01fad = keccak("hello world")
# 0x02173285 a8d7341e 5e972fc6 77286384 f802f8ef 42a5ec5f 03bbfa25 4cb01fad = keccak("hello world").key
test: test:
lui $s0, 0x3000 lui $s0, 0x3000
ori $s0, 0x1000 ori $s0, 0x1000
lui $t0, 0x4717 lui $t0, 0x0217
ori $t0, 0x3285 ori $t0, 0x3285
sw $t0, 0($s0) sw $t0, 0($s0)
lui $t0, 0xa8d7 lui $t0, 0xa8d7
...@@ -35,20 +36,53 @@ test: ...@@ -35,20 +36,53 @@ test:
ori $t0, 0x1fad ori $t0, 0x1fad
sw $t0, 0x1c($s0) sw $t0, 0x1c($s0)
# syscall 4020 to trigger # preimage request - write(fdPreimageWrite, preimageData, 32)
li $v0, 4020 li $a0, 6
li $a1, 0x30001000
li $t0, 8
li $a2, 4
$writeloop:
li $v0, 4004
syscall syscall
addiu $a1, $a1, 4
addiu $t0, $t0, -1
bnez $t0, $writeloop
nop
# preimage response to 0x30002000 - read(fdPreimageRead, addr, count)
# read preimage length
li $a0, 5
li $a1, 0x31000000
li $a2, 4
li $v0, 4003
syscall
li $a1, 0x31000004
li $v0, 4003
syscall
# read the preimage data
li $a1, 0x31000008
li $t0, 3
$readloop:
li $v0, 4003
syscall
addiu $a1, $a1, 4
addiu $t0, $t0, -1
bnez $t0, $readloop
nop
# length at 0x31000000 # length at 0x31000000. We also check that the lower 32 bits are zero
lui $s1, 0x3100 lui $s1, 0x3100
lw $t0, 0($s1) lw $t0, 0($s1)
sltiu $t6, $t0, 1
li $s1, 0x31000004
lw $t0, 0($s1)
# should be len("hello world") == 11 # should be len("hello world") == 11
li $t4, 11 li $t4, 11
subu $t5, $t0, $t4 subu $t5, $t0, $t4
sltiu $v0, $t5, 1 sltiu $v0, $t5, 1
and $v0, $v0, $t6
# data at 0x31000004 # data at 0x31000008
lw $t0, 4($s1) lw $t0, 4($s1)
lui $t4, 0x6865 lui $t4, 0x6865
ori $t4, 0x6c6c ori $t4, 0x6c6c
......
...@@ -31,8 +31,9 @@ func TestState(t *testing.T) { ...@@ -31,8 +31,9 @@ func TestState(t *testing.T) {
for _, f := range testFiles { for _, f := range testFiles {
t.Run(f.Name(), func(t *testing.T) { t.Run(f.Name(), func(t *testing.T) {
var oracle PreimageOracle
if f.Name() == "oracle.bin" { if f.Name() == "oracle.bin" {
t.Skip("oracle test needs to be updated to use syscall pre-image oracle") oracle = staticOracle(t, []byte("hello world"))
} }
// TODO: currently tests are compiled as flat binary objects // TODO: currently tests are compiled as flat binary objects
// We can use more standard tooling to compile them to ELF files and get remove maketests.py // We can use more standard tooling to compile them to ELF files and get remove maketests.py
...@@ -50,7 +51,7 @@ func TestState(t *testing.T) { ...@@ -50,7 +51,7 @@ func TestState(t *testing.T) {
// set the return address ($ra) to jump into when test completes // set the return address ($ra) to jump into when test completes
state.Registers[31] = endAddr state.Registers[31] = endAddr
us := NewInstrumentedState(state, nil, os.Stdout, os.Stderr) us := NewInstrumentedState(state, oracle, os.Stdout, os.Stderr)
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
if us.state.PC == endAddr { if us.state.PC == endAddr {
...@@ -196,3 +197,15 @@ func TestClaim(t *testing.T) { ...@@ -196,3 +197,15 @@ func TestClaim(t *testing.T) {
require.Equal(t, expectedStdOut, stdOutBuf.String(), "stdout") require.Equal(t, expectedStdOut, stdOutBuf.String(), "stdout")
require.Equal(t, expectedStdErr, stdErrBuf.String(), "stderr") require.Equal(t, expectedStdErr, stdErrBuf.String(), "stderr")
} }
func staticOracle(t *testing.T, preimageData []byte) *testOracle {
return &testOracle{
hint: func(v []byte) {},
getPreimage: func(k [32]byte) []byte {
if k != preimage.Keccak256Key(crypto.Keccak256Hash(preimageData)).PreimageKey() {
t.Fatalf("invalid preimage request for %x", k)
}
return preimageData
},
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment