Skip to content

Commit

Permalink
add support for new instructions (#155)
Browse files Browse the repository at this point in the history
* add ror instruction and update zkmips example

* fix errors

* fix constraint

* update example

* fix clippy and update README
  • Loading branch information
weilzkm authored Aug 7, 2024
1 parent 6b0b736 commit 0659078
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 217 deletions.
2 changes: 1 addition & 1 deletion emulator/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl Memory {

pub fn byte(&mut self, addr: u32) -> u8 {
let word = self.get_memory(addr & 0xFFFFFFFC);
word.to_le_bytes()[(addr & 3) as usize]
word.to_be_bytes()[(addr & 3) as usize]
}

fn alloc_page(&mut self, page_index: u32) -> Rc<RefCell<CachedPage>> {
Expand Down
31 changes: 30 additions & 1 deletion emulator/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,14 @@ impl InstrumentedState {
fn handle_hilo(&mut self, fun: u32, rs: u32, rt: u32, store_reg: u32) {
let mut val = 0u32;
match fun {
0x01 => {
// maddu
let mut acc = (rs as u64).wrapping_mul(rt as u64);
let hilo = ((self.state.hi as u64) << 32).wrapping_add(self.state.lo as u64);
acc = acc.wrapping_add(hilo);
self.state.hi = (acc >> 32) as u32;
self.state.lo = acc as u32;
}
0x10 => {
// mfhi
val = self.state.hi;
Expand Down Expand Up @@ -937,6 +945,12 @@ impl InstrumentedState {
}
}

if opcode == 0x1C && fun == 0x1 {
// maddu
self.handle_hilo(fun, rs, rt, rd_reg);
return;
}

if opcode == 0 && fun == 0x34 && val == 1 {
self.handle_trap();
}
Expand Down Expand Up @@ -1007,7 +1021,11 @@ impl InstrumentedState {
} else if fun == 0x00 {
return rt << shamt; // sll
} else if fun == 0x02 {
return rt >> shamt; // srl
if (insn >> 21) & 0x1F == 1 {
return rt >> shamt | rt << (32 - shamt); // ror
} else if (insn >> 21) & 0x1F == 0 {
return rt >> shamt; // srl
}
} else if fun == 0x03 {
return sign_extension(rt >> shamt, 32 - shamt); // sra
} else if fun == 0x04 {
Expand Down Expand Up @@ -1067,6 +1085,10 @@ impl InstrumentedState {
return rt << 16; // lui
} else if opcode == 0x1c {
// SPECIAL2
if fun == 1 {
//maddu: do nothing here
return rs;
}
if fun == 2 {
// mul
return rs.wrapping_mul(rt);
Expand All @@ -1092,6 +1114,13 @@ impl InstrumentedState {
let mask = (1 << (msbd + 1)) - 1;
let i = (rs >> lsb) & mask;
return i;
} else if fun == 4 {
// ins
let msb = (insn >> 11) & 0x1F;
let lsb = (insn >> 6) & 0x1F;
let size = msb - lsb + 1;
let mask = (1u32 << size) - 1;
return (rt & !(mask << lsb)) | ((rs & mask) << lsb);
} else if fun == 0b111011 {
//rdhwr
let rd = (insn >> 11) & 0x1F;
Expand Down
10 changes: 7 additions & 3 deletions prover/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ cd prover/examples/sha2
cargo build --target=mips-unknown-linux-musl
```

* Run the host program
* Run the host program

// echo -n 'world!' | sha256sum
// 711e9609339e92b03ddc0a211827dba421f38f9ed8b9d806e1ffdd8c15ffa03d

```
cd ../..
RUST_LOG=info ELF_PATH=examples/sha2/target/mips-unknown-linux-musl/debug/sha2-bench SEG_OUTPUT=/tmp/output cargo run --release --example zkmips bench
ARGS="711e9609339e92b03ddc0a211827dba421f38f9ed8b9d806e1ffdd8c15ffa03d world!" RUST_LOG=info ELF_PATH=examples/sha2/target/mips-unknown-linux-musl/debug/sha2-bench SEG_OUTPUT=/tmp/output cargo run --release --example zkmips bench
Or
RUST_LOG=info ELF_PATH=examples/revme/target/mips-unknown-linux-musl/debug/evm JSON_PATH=../emulator/test-vectors/test.json SEG_OUTPUT=/tmp/output cargo run --release --example zkmips revm
RUST_LOG=info ELF_PATH=examples/revme/target/mips-unknown-linux-musl/debug/evm JSON_PATH=../emulator/test-vectors/test.json SEG_OUTPUT=/tmp/output SEG_SIZE=262144 cargo run --release --example zkmips revm
```
6 changes: 5 additions & 1 deletion prover/examples/sha2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ use alloc::vec::Vec;
zkm_runtime::entrypoint!(main);

pub fn main() {
let public_input: Vec<u8> = zkm_runtime::io::read();
let input: Vec<u8> = zkm_runtime::io::read();

let mut hasher = Sha256::new();
hasher.update(input);
let result = hasher.finalize();

zkm_runtime::io::commit::<[u8; 32]>(&result.into());
let output: [u8; 32] = result.into();
assert_eq!(output.to_vec(), public_input);

zkm_runtime::io::commit::<[u8; 32]>(&output);
}
Loading

0 comments on commit 0659078

Please sign in to comment.