Skip to content

Commit

Permalink
Reapply "add risc0 receipt verifier host function"
Browse files Browse the repository at this point in the history
This reverts commit 2aa35fa.
  • Loading branch information
jonas089 committed Apr 29, 2024
1 parent 2aa35fa commit 1f38455
Show file tree
Hide file tree
Showing 97 changed files with 5,807 additions and 2,592 deletions.
1,921 changes: 882 additions & 1,039 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ members = [
"utils/global-state-update-gen",
"utils/validation",
"utils/highway-rewards-analysis",
"utils/highway-state-grapher",
]

default-members = [
Expand All @@ -28,7 +27,6 @@ default-members = [
"utils/global-state-update-gen",
"utils/validation",
"utils/highway-rewards-analysis",
"utils/highway-state-grapher",
]

exclude = ["utils/nctl/remotes/casper-client-rs"]
Expand Down
11 changes: 5 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ test: test-rs-no-default-features test-rs test-as

.PHONY: test-contracts-rs
test-contracts-rs: build-contracts-rs
$(DISABLE_LOGGING) $(CARGO) test $(CARGO_FLAGS) -p casper-engine-tests -- --ignored
$(DISABLE_LOGGING) $(CARGO) test $(CARGO_FLAGS) -p casper-engine-tests -- --ignored --skip repeated_ffi_call_should_gas_out_quickly

.PHONY: test-contracts-timings
test-contracts-timings: build-contracts-rs
$(DISABLE_LOGGING) $(CARGO) test --release $(filter-out --release, $(CARGO_FLAGS)) -p casper-engine-tests -- --ignored --test-threads=1 repeated_ffi_call_should_gas_out_quickly

.PHONY: test-contracts-as
test-contracts-as: build-contracts-rs build-contracts-as
Expand All @@ -101,10 +105,6 @@ test-contracts-as: build-contracts-rs build-contracts-as
.PHONY: test-contracts
test-contracts: test-contracts-rs

.PHONY: check-no-default-features
check-no-default-features:
cd types && $(CARGO) check --all-targets --no-default-features

.PHONY: check-std-features
check-std-features:
cd types && $(CARGO) check --all-targets --no-default-features --features=std
Expand Down Expand Up @@ -161,7 +161,6 @@ check-rs: \
doc \
lint \
audit \
check-no-default-features \
check-std-features \
test-rs \
test-rs-no-default-features \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# casper-node

Reference node for the Casper Blockchain Protocol.
This is the core application for the Casper blockchain.

## Casper Blockchain

Expand Down
15 changes: 15 additions & 0 deletions execution_engine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ All notable changes to this project will be documented in this file. The format
[comment]: <> (Fixed: any bug fixes)
[comment]: <> (Security: in case of vulnerabilities)

## 7.0.1

### Changed
* Change the cost of `wasm.storage_costs.gas_per_byte` and `shared::storage_costs::DEFAULT_GAS_PER_BYTE_COST` from `630_000` to `1_117_587`.
* Change the cost of the host function `casper_add_associated_key` from `9_000` to `1_200_000`.
* Change the cost of the argument `entry_points_size` of host function `casper_add_contract_version` from `0` to `120_000`.
* Change the cost of the host function `casper_blake2b`and its argument `in_size` from `200` and `0` respectively to `1_200_000` to `120_000`.
* Change the cost of the host function `casper_call_contract` and its arguments `entry_point_name_size` and `runtime_args_size` from `4_500`, `0` and `420` respectively to `300_000_000`, `120_000` and `120_000`.
* Change the cost of the host function `casper_call_versioned_contract` and the arguments `entry_point_name_size` and `runtime_args_size` from `4_500`, `0` and `420` respectively to `300_000_000`, `120_000` and `120_000`.
* Change the cost of the host function `casper_get_balance` from `3_800` to `3_000_000`.
* Change the cost of arguments `name_size` and `dest_size` of host function `casper_get_named_arg` from `0` to `120_000`.
* Change the cost of the host function `casper_put_key` and its arguments `name_size` and `key_size` from `38_000`, `1_100` and `0` respectively to `100_000_000`, `120_000` and `120_000`.
* Change the cost of the host function `casper_read_value` and its argument `key_size` from `6_000` and `0` respectively to `60_000` and `120_000`.
* Change the cost of the argument `urefs_size` of host function `casper_remove_contract_user_group_urefs` from `0` to `120_000`.
* Change the cost of the host function `casper_transfer_from_purse_to_purse` from `82_000` to `82_000_000`.



Expand Down
7 changes: 5 additions & 2 deletions execution_engine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casper-execution-engine"
version = "7.0.0" # when updating, also update 'html_root_url' in lib.rs
version = "7.0.1" # when updating, also update 'html_root_url' in lib.rs
authors = ["Henry Till <[email protected]>", "Ed Hastings <[email protected]>"]
edition = "2021"
description = "Casper execution engine crates."
Expand All @@ -13,7 +13,7 @@ license = "Apache-2.0"
[dependencies]
anyhow = "1.0.33"
base16 = "0.2.1"
bincode = "1.3.1"
#bincode = "1.3.1"
casper-hashing = { version = "3.0.0", path = "../hashing" }
casper-types = { version = "4.0.1", path = "../types", default-features = false, features = ["datasize", "gens", "json-schema"] }
casper-wasm = { version = "0.46.0", default-features = false }
Expand Down Expand Up @@ -48,6 +48,9 @@ thiserror = "1.0.18"
tracing = "0.1.18"
uint = "0.9.0"
uuid = { version = "0.8.1", features = ["serde", "v4"] }
risc0-zkvm = { version="0.21.0" }
kairos-risc0-types = { path="../kairos-risc0-types" }
bincode = "1.3.3"

[dev-dependencies]
assert_matches = "1.3.0"
Expand Down
6 changes: 6 additions & 0 deletions execution_engine/src/core/engine_state/engine_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ impl EngineConfig {
pub fn fee_handling(&self) -> FeeHandling {
self.fee_handling
}

/// Sets the `wasm_config.max_memory` to `new_value`.
#[cfg(feature = "test-support")]
pub fn set_max_memory(&mut self, new_value: u32) {
self.wasm_config.max_memory = new_value;
}
}

/// A builder for an [`EngineConfig`].
Expand Down
1 change: 1 addition & 0 deletions execution_engine/src/core/resolvers/v1_function_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub(crate) enum FunctionIndex {
RandomBytes,
DictionaryReadFuncIndex,
EnableContractVersion,
RiscZeroVerifier
}

impl From<FunctionIndex> for usize {
Expand Down
4 changes: 4 additions & 0 deletions execution_engine/src/core/resolvers/v1_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ impl ModuleImportResolver for RuntimeModuleImportResolver {
Signature::new(&[ValueType::I32; 4][..], Some(ValueType::I32)),
FunctionIndex::EnableContractVersion.into(),
),
"casper_risc_zero_verifier" => FuncInstance::alloc_host(
Signature::new(&[ValueType::I32; 4][..], Some(ValueType::I32)),
FunctionIndex::RiscZeroVerifier.into(),
),
_ => {
return Err(InterpreterError::Function(format!(
"host module doesn't export function with name {}",
Expand Down
41 changes: 35 additions & 6 deletions execution_engine/src/core/runtime/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use casper_types::{

use super::{args::Args, Error, Runtime};
use crate::{
core::resolvers::v1_function_index::FunctionIndex,
shared::host_function_costs::{Cost, HostFunction, DEFAULT_HOST_FUNCTION_NEW_DICTIONARY},
core::{resolvers::v1_function_index::FunctionIndex, runtime::risc0_verifier},
shared::host_function_costs::{Cost, HostFunction},
storage::global_state::StateReader,
};

Expand Down Expand Up @@ -562,6 +562,9 @@ where
existing_urefs_size,
output_size_ptr,
) = Args::parse(args)?;

// TODO - use `num_new_urefs` * costs for unit uref, assuming these aren't
// already charged.
self.charge_host_function_call(
&host_function_costs.create_contract_user_group,
[
Expand Down Expand Up @@ -837,6 +840,7 @@ where
// args(4) = output of size value of host bytes data
let (package_ptr, package_size, label_ptr, label_size, value_size_ptr) =
Args::parse(args)?;
// TODO - add cost for 1x unit uref, assuming this isn't already charged
self.charge_host_function_call(
&host_function_costs.provision_contract_user_group_uref,
[
Expand Down Expand Up @@ -954,10 +958,9 @@ where
// args(0) = pointer to output size (output param)
let (output_size_ptr,): (u32,) = Args::parse(args)?;

self.charge_host_function_call(
&DEFAULT_HOST_FUNCTION_NEW_DICTIONARY,
[output_size_ptr],
)?;
// TODO - dynamically calculate the size of the new data. Currently using
// hard-coded 33 which is correct as of now.
self.charge_host_function_call(&host_function_costs.new_uref, [0, 0, 33])?;
let ret = self.new_dictionary(output_size_ptr)?;
Ok(Some(RuntimeValue::I32(api_error::i32_from(ret))))
}
Expand Down Expand Up @@ -1097,6 +1100,32 @@ where

Ok(Some(RuntimeValue::I32(api_error::i32_from(result))))
}
FunctionIndex::RiscZeroVerifier => {
let (
proof_ptr,
proof_size,
out_ptr,
out_size
) =
Args::parse(args)?;
self.charge_host_function_call(
&host_function_costs.risc0_verifier,
[
proof_ptr,
proof_size,
out_ptr,
out_size
]
)?;
let result: [u8; 1] =
self.checked_memory_slice(proof_ptr as usize, proof_size as usize, |proof| {
risc0_verifier::verify(proof)
})?;
self.try_get_memory().unwrap()
.set(out_ptr, &result)
.map_err(|error| Error::Interpreter(error.into()))?;
Ok(Some(RuntimeValue::I32(0)))
}
}
}
}
1 change: 1 addition & 0 deletions execution_engine/src/core/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod handle_payment_internal;
mod host_function_flag;
mod mint_internal;
pub mod stack;
pub mod risc0_verifier;
mod standard_payment_internal;
mod utils;

Expand Down
17 changes: 17 additions & 0 deletions execution_engine/src/core/runtime/risc0_verifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use risc0_zkvm::{default_prover, ExecutorEnv, Receipt};
use kairos_risc0_types::RiscZeroProof;
use serde_json;
use bincode;

#[doc(hidden)]
pub fn verify<T: AsRef<[u8]>>(
proof_serialized: T
) -> [u8;1]{
let risc0_proof: RiscZeroProof = bincode::deserialize(&proof_serialized.as_ref()).unwrap();
let receipt: Receipt = bincode::deserialize(&risc0_proof.receipt_serialized).unwrap();
let program_id: [u32;8] = risc0_proof.program_id.try_into().unwrap();
match receipt.verify(program_id){
Ok(_) => [1],
Err(_) => [0]
}
}
2 changes: 1 addition & 1 deletion execution_engine/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The engine which executes smart contracts on the Casper network.
#![doc(html_root_url = "https://docs.rs/casper-execution-engine/7.0.0")]
#![doc(html_root_url = "https://docs.rs/casper-execution-engine/7.0.1")]
#![doc(
html_favicon_url = "https://raw.githubusercontent.com/casper-network/casper-node/blob/dev/images/Casper_Logo_Favicon_48.png",
html_logo_url = "https://raw.githubusercontent.com/casper-network/casper-node/blob/dev/images/Casper_Logo_Favicon.png",
Expand Down
Loading

0 comments on commit 1f38455

Please sign in to comment.