Skip to content

Commit

Permalink
Upgrade to 0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
flaub committed Nov 30, 2023
1 parent 5a804fa commit dcf776c
Show file tree
Hide file tree
Showing 15 changed files with 1,369 additions and 2,595 deletions.
1,935 changes: 1,034 additions & 901 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
[workspace]
members = [
"core",
"methods",
"web/client",
"web/server",
]
resolver = "2"
members = ["core", "methods", "web/client", "web/server"]

exclude = [
"contract",
]
exclude = ["contract"]
11 changes: 6 additions & 5 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ version = "0.1.0"
edition = "2021"

[dependencies]
risc0-zkvm-core = { version = "0.10", default-features = false, features = ["pure"] }
bytemuck = "1.14"
risc0-zkvm = { version = "0.19", default-features = false }
serde = { version = "1.0", default-features = false }

[dev-dependencies]
anyhow = "1.0"
battleship-methods = { path = "../methods" }
ctor = "0.1"
env_logger = "0.8"
env_logger = "0.10"
log = "0.4"
risc0-zkvm-host = "0.10"
risc0-zkvm-serde = "0.10"
risc0-zkvm = "0.19"
test-log = "0.2"
13 changes: 11 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@

#![cfg_attr(not(test), no_std)]

extern crate alloc;

use core::fmt::Display;

use risc0_zkvm::{
serde::to_vec,
sha::{Digest, Impl, Sha256},
};
use serde::{Deserialize, Serialize};

use risc0_zkvm_core::Digest;

pub const NUM_SHIPS: usize = 5;
pub const BOARD_SIZE: usize = 10;

Expand Down Expand Up @@ -152,6 +156,11 @@ impl GameState {
}
true
}

pub fn digest(&self) -> Digest {
let bytes = to_vec(&self).unwrap();
*Impl::hash_bytes(bytemuck::cast_slice(&bytes))
}
}

impl RoundParams {
Expand Down
96 changes: 40 additions & 56 deletions core/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::bail;
use anyhow::Result;
use test_log::test;

use battleship_core::{
GameState, HitType, Position, RoundCommit, RoundParams, RoundResult, Ship, ShipDirection,
};
use battleship_methods::{INIT_ID, INIT_PATH, TURN_ID, TURN_PATH};
use log::LevelFilter;
use risc0_zkvm_core::Digest;
use risc0_zkvm_host::{Exception, Prover, Receipt, Result};
use risc0_zkvm_serde::{from_slice, to_slice, to_vec};
use battleship_methods::{INIT_ELF, INIT_ID, TURN_ELF, TURN_ID};
use risc0_zkvm::{
default_prover,
serde::{from_slice, to_vec},
sha::Digest,
ExecutorEnv, Receipt,
};

pub struct InitMessage {
receipt: Receipt,
Expand All @@ -43,15 +49,13 @@ pub struct Battleship {

impl InitMessage {
pub fn get_state(&self) -> Result<Digest> {
let msg = self.receipt.get_journal_vec()?;
Ok(from_slice(msg.as_slice()).unwrap())
Ok(self.receipt.journal.decode()?)
}
}

impl RoundMessage {
pub fn get_commit(&self) -> Result<RoundCommit> {
let msg = self.receipt.get_journal_vec()?;
Ok(from_slice(msg.as_slice()).unwrap())
Ok(self.receipt.journal.decode()?)
}
}

Expand All @@ -65,11 +69,9 @@ impl Battleship {
}

pub fn init(&self) -> Result<InitMessage> {
let elf_contents = std::fs::read(INIT_PATH).unwrap();
let mut prover = Prover::new(&elf_contents, INIT_ID)?;
let vec = to_vec(&self.state).unwrap();
prover.add_input(vec.as_slice())?;
let receipt = prover.run()?;
let prover = default_prover();
let env = ExecutorEnv::builder().write(&self.state)?.build()?;
let receipt = prover.prove_elf(env, INIT_ELF)?;
Ok(InitMessage { receipt })
}

Expand All @@ -83,22 +85,22 @@ impl Battleship {

pub fn turn(&mut self, x: u32, y: u32) -> TurnMessage {
let shot = Position::new(x, y);
log::info!("turn: {}", shot);
log::info!("turn: {shot}");
self.last_shot = shot.clone();
TurnMessage { shot: shot.clone() }
}

pub fn on_turn_msg(&mut self, msg: &TurnMessage) -> Result<RoundMessage> {
log::info!("on_turn_msg: {:?}", msg);
log::info!("on_turn_msg: {msg:?}");
let params = RoundParams::new(self.state.clone(), msg.shot.x, msg.shot.y);

let elf_contents = std::fs::read(TURN_PATH).unwrap();
let mut prover = Prover::new(&elf_contents, TURN_ID)?;
let vec = to_vec(&params).unwrap();
prover.add_input(vec.as_slice())?;
let receipt = prover.run()?;
let vec = prover.get_output_vec()?;
let result = from_slice::<RoundResult>(vec.as_slice()).unwrap();
let prover = default_prover();
let mut output = Vec::new();
let env = ExecutorEnv::builder()
.write(&params)?
.stdout(&mut output)
.build()?;
let receipt = prover.prove_elf(env, TURN_ELF)?;
let result: RoundResult = from_slice(&output)?;
self.state = result.state.clone();
Ok(RoundMessage { receipt })
}
Expand All @@ -107,26 +109,22 @@ impl Battleship {
log::info!("on_round_msg");
msg.receipt.verify(TURN_ID)?;
let commit = msg.get_commit()?;
log::info!(" commit: {:?}", commit);
log::info!(" commit: {commit:?}");

if commit.old_state != self.peer_state {
return Err(Exception::new(
format!(
"Cheater: state mismatch. old_state ({}) != peer_state ({})",
commit.old_state, self.peer_state
)
.as_str(),
));
bail!(
"Cheater: state mismatch. old_state ({}) != peer_state ({})",
commit.old_state,
self.peer_state
);
}

if commit.shot != self.last_shot {
return Err(Exception::new(
format!(
"Cheater: shot mismatch. cur_shot ({}) != last_shot ({})",
commit.shot, self.last_shot
)
.as_str(),
));
bail!(
"Cheater: shot mismatch. cur_shot ({}) != last_shot ({})",
commit.shot,
self.last_shot
);
}

self.peer_state = commit.new_state.clone();
Expand All @@ -142,31 +140,17 @@ fn round(player1: &mut Battleship, player2: &mut Battleship, x: u32, y: u32) ->
.unwrap()
}

#[ctor::ctor]
fn init() {
env_logger::builder().filter_level(LevelFilter::Info).init();
}

#[test]
fn serde() {
struct Logger;
impl risc0_zkvm_core::Log for Logger {
fn log(&self, msg: &str) {
log::info!("{}", msg);
}
}
static LOGGER: Logger = Logger;
risc0_zkvm_core::set_logger(&LOGGER);
let commit = RoundCommit {
old_state: Digest::new([0, 1, 2, 3, 4, 5, 6, 7]),
new_state: Digest::new([8, 7, 6, 5, 4, 3, 2, 1]),
shot: Position::new(1, 9),
hit: HitType::Hit,
};
let buf: &mut [u32] = &mut [0; 256];
let buf = to_slice(&commit, buf).unwrap();
log::info!("buf: {}, {:x?}", buf.len(), buf);
let result = from_slice(buf).unwrap();
let buf = to_vec(&commit).unwrap();
log::info!("buf: {}, {buf:x?}", buf.len());
let result = from_slice(&buf).unwrap();
assert_eq!(commit, result);
}

Expand Down
6 changes: 1 addition & 5 deletions methods/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ edition = "2021"
[package.metadata.risc0]
methods = ["guest"]

[profile.release]
lto = true
opt-level = "z"

[build-dependencies]
risc0-build = "0.10"
risc0-build = "0.19"
Loading

0 comments on commit dcf776c

Please sign in to comment.