-
Notifications
You must be signed in to change notification settings - Fork 8
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
add: CPC extension for SelfCall #1585
base: main
Are you sure you want to change the base?
Changes from all commits
9a34820
b0ea4ab
d94a097
8d3855b
09cc75e
217cac3
4a31863
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ members = [ | |
"token", | ||
"wallet", | ||
"inputtape", | ||
"selfcaller", | ||
] | ||
resolver = "2" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[package] | ||
edition = "2021" | ||
name = "selfcaller" | ||
version = "0.1.0" | ||
|
||
[features] | ||
native = [] | ||
|
||
[dependencies] | ||
mozak-sdk = { path = "../../sdk" } | ||
rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } | ||
rkyv_derive = "=0.8.0-alpha.1" | ||
|
||
[target.'cfg(not(target_os="mozakvm"))'.dependencies] | ||
hex = "0.4" | ||
rand = "0.8" | ||
rand_chacha = "0.3" | ||
|
||
[[bin]] | ||
name = "selfcallerbin" | ||
path = "main_mozak.rs" | ||
|
||
[[bin]] | ||
name = "selfcaller-native" | ||
path = "main_native.rs" | ||
required-features = ["native"] | ||
|
||
[lib] | ||
name = "selfcaller" | ||
path = "core_logic.rs" | ||
|
||
# The following is read by `run_examples.py` | ||
[package.metadata.mozak] | ||
example_program_id = "MZK-5b7b6135be198533f7c7ec46651216b762e6d47e69b408d1bc79d641f9ae06de" |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||
# To run | ||||||
|
||||||
## Native | ||||||
|
||||||
To run on your system, use the following command (kindly change [target triple](https://doc.rust-lang.org/cargo/appendix/glossary.html#target) as per your machine's architecture): | ||||||
|
||||||
```sh | ||||||
# from project root | ||||||
cargo run --release --features="native" --bin selfcaller-native --target aarch64-apple-darwin | ||||||
``` | ||||||
|
||||||
This produces the `SystemTape` in both binary and debug formats. | ||||||
|
||||||
## Mozak-VM | ||||||
|
||||||
First, build the mozakvm binary: | ||||||
|
||||||
```sh | ||||||
# inside examples directory | ||||||
cargo build --release --bin selfcallerbin | ||||||
``` | ||||||
|
||||||
Test the ELF in mozak-vm using the below command. Note that you must run | ||||||
the native execution above to produce the system tape prior to running this. | ||||||
|
||||||
```sh | ||||||
# from project root | ||||||
MOZAK_STARK_DEBUG=true \ | ||||||
cargo run --bin mozak-cli -- prove-and-verify -vvv \ | ||||||
examples/target/riscv32im-mozak-mozakvm-elf/release/selfcallerbin \ | ||||||
--system-tape examples/selfcaller.tape.json \ | ||||||
--self-prog-id \ | ||||||
MZK-5b7b6135be198533f7c7ec46651216b762e6d47e69b408d1bc79d641f9ae06de; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#![feature(restricted_std)] | ||
#![allow(unused_attributes)] | ||
extern crate alloc; | ||
|
||
use mozak_sdk::common::types::ProgramIdentifier; | ||
use rkyv::{Archive, Deserialize, Serialize}; | ||
|
||
#[derive(Archive, Deserialize, Serialize, PartialEq, Eq, Clone)] | ||
#[cfg_attr(not(target_os = "mozakvm"), derive(Debug))] | ||
pub enum MethodArgs { | ||
SelfCall(ProgramIdentifier, u8), | ||
} | ||
|
||
#[derive(Archive, Debug, Deserialize, Serialize, PartialEq, Eq, Clone)] | ||
pub enum MethodReturns { | ||
SelfCall(u8), | ||
} | ||
|
||
// TODO: Remove later | ||
impl Default for MethodReturns { | ||
fn default() -> Self { Self::SelfCall(0) } | ||
} | ||
|
||
#[allow(clippy::unit_arg)] | ||
pub fn dispatch(args: MethodArgs) -> MethodReturns { | ||
match args { | ||
MethodArgs::SelfCall(self_id, counter) => self_call(self_id, counter), | ||
} | ||
} | ||
|
||
pub fn self_call(self_id: ProgramIdentifier, counter: u8) -> MethodReturns { | ||
if counter == 0 { | ||
return MethodReturns::SelfCall(0); | ||
} | ||
mozak_sdk::call_send( | ||
self_id, | ||
MethodArgs::SelfCall(self_id, counter - 1), | ||
dispatch, | ||
); | ||
|
||
MethodReturns::SelfCall(counter) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![no_main] | ||
#![allow(unused_attributes)] | ||
#![feature(restricted_std)] | ||
|
||
mod core_logic; | ||
|
||
use core_logic::{dispatch, MethodArgs, MethodReturns}; | ||
use mozak_sdk::call_receive; | ||
|
||
pub fn main() { | ||
while let Some((_caller, argument, return_)) = call_receive::<MethodArgs, MethodReturns>() { | ||
assert!(dispatch(argument) == return_); | ||
} | ||
} | ||
|
||
// We define `main()` to be the program's entry point. | ||
mozak_sdk::entry!(main); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![feature(restricted_std)] | ||
#![allow(unused_attributes)] | ||
|
||
mod core_logic; | ||
|
||
use mozak_sdk::common::types::ProgramIdentifier; | ||
|
||
use crate::core_logic::{dispatch, MethodArgs}; | ||
|
||
fn main() { | ||
let self_caller_program = ProgramIdentifier::new_from_rand_seed(5); | ||
|
||
mozak_sdk::call_send( | ||
self_caller_program, | ||
MethodArgs::SelfCall(self_caller_program, 5), | ||
dispatch, | ||
); | ||
|
||
mozak_sdk::native::dump_proving_files("selfcaller", self_caller_program); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ use super::types::{ | |
CallTapeType, EventTapeType, PrivateInputTapeType, PublicInputTapeType, SystemTape, | ||
}; | ||
#[cfg(target_os = "mozakvm")] | ||
use super::types::{SelfCallExtendedProgramIdentifier, SelfCallExtensionFlag}; | ||
#[cfg(target_os = "mozakvm")] | ||
use crate::common::types::{CanonicalOrderedTemporalHints, CrossProgramCall, ProgramIdentifier}; | ||
#[cfg(target_os = "mozakvm")] | ||
use crate::mozakvm::helpers::{ | ||
|
@@ -67,7 +69,10 @@ pub(crate) static mut SYSTEM_TAPE: Lazy<SystemTape> = Lazy::new(|| { | |
private_input_tape: PrivateInputTapeType::default(), | ||
public_input_tape: PublicInputTapeType::default(), | ||
call_tape: CallTapeType { | ||
self_prog_id: get_self_prog_id(), | ||
extended_self_prog_id: SelfCallExtendedProgramIdentifier( | ||
get_self_prog_id(), | ||
SelfCallExtensionFlag::default(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to extend this? |
||
), | ||
cast_list: get_rkyv_deserialized!(Vec<ProgramIdentifier>, _mozak_cast_list), | ||
reader: Some(get_rkyv_archived!(Vec<CrossProgramCall>, _mozak_call_tape)), | ||
index: 0, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,56 @@ | ||
use super::ProgramIdentifier; | ||
|
||
#[derive( | ||
Default, | ||
Copy, | ||
Clone, | ||
Hash, | ||
Eq, | ||
PartialEq, | ||
Ord, | ||
PartialOrd, | ||
rkyv::Archive, | ||
rkyv::Serialize, | ||
rkyv::Deserialize, | ||
)] | ||
#[cfg_attr( | ||
not(target_os = "mozakvm"), | ||
derive(Debug, serde::Serialize, serde::Deserialize) | ||
)] | ||
/// `SelfCallExtension` is a boolean value that differentiates the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sounds very weird. |
||
/// two program instances when a program calls itself under CPC. For | ||
/// example, if a program `P` in some function `f` wants to call another | ||
/// function `f'` in `P` under CPC regime, the caller and callee may form | ||
/// tuple as: `((P, 0), (P, 1))` or `((P, 1), (P, 0))`. The added digits | ||
/// help separate the two instances of call and hold no numeric significance | ||
/// apart from mere differentiators. | ||
pub struct SelfCallExtensionFlag(pub u8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you only need something that flips back and forth, use a bool, and 'not' to flip. But I'm not convinced yet this whole design is a good idea. |
||
|
||
impl SelfCallExtensionFlag { | ||
/// Provides a flag different from given flag. In essence, if `0` is | ||
/// provided, `1` is returned and vice versa | ||
#[must_use] | ||
pub fn differentiate_from(flag: Self) -> Self { Self(1 - flag.0) } | ||
} | ||
|
||
#[derive( | ||
Default, | ||
Clone, | ||
Hash, | ||
Eq, | ||
PartialEq, | ||
Ord, | ||
PartialOrd, | ||
rkyv::Archive, | ||
rkyv::Serialize, | ||
rkyv::Deserialize, | ||
)] | ||
#[cfg_attr( | ||
not(target_os = "mozakvm"), | ||
derive(Debug, serde::Serialize, serde::Deserialize) | ||
)] | ||
pub struct SelfCallExtendedProgramIdentifier(pub ProgramIdentifier, pub SelfCallExtensionFlag); | ||
|
||
#[derive( | ||
Default, Clone, Hash, PartialEq, PartialOrd, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, | ||
)] | ||
|
@@ -7,8 +60,8 @@ | |
)] | ||
#[allow(clippy::pub_underscore_fields)] | ||
pub struct CrossProgramCall { | ||
pub caller: super::ProgramIdentifier, | ||
pub callee: super::ProgramIdentifier, | ||
pub caller: SelfCallExtendedProgramIdentifier, | ||
pub callee: SelfCallExtendedProgramIdentifier, | ||
pub argument: super::RawMessage, | ||
pub return_: super::RawMessage, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like copy and paste?