mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-02 13:23:10 +00:00
feat: template added
This commit is contained in:
parent
6a5ec2d953
commit
e33819e787
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -4218,7 +4218,6 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"storage",
|
||||
"utxo",
|
||||
]
|
||||
|
||||
|
||||
@ -19,6 +19,11 @@ members = [
|
||||
"sc_core",
|
||||
]
|
||||
|
||||
exclude = [
|
||||
"template/sc_example",
|
||||
"template/zk_sc_test"
|
||||
]
|
||||
|
||||
[workspace.dependencies]
|
||||
anyhow = "1.0"
|
||||
num_cpus = "1.13.1"
|
||||
|
||||
@ -24,9 +24,6 @@ risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-2.0
|
||||
[dependencies.accounts]
|
||||
path = "../accounts"
|
||||
|
||||
[dependencies.storage]
|
||||
path = "../storage"
|
||||
|
||||
[dependencies.utxo]
|
||||
path = "../utxo"
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
pub mod cryptography;
|
||||
pub mod proofs_circuits;
|
||||
pub mod traits;
|
||||
pub mod transaction_payloads_tools;
|
||||
|
||||
44
sc_core/src/traits.rs
Normal file
44
sc_core/src/traits.rs
Normal file
@ -0,0 +1,44 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utxo::utxo_core::UTXO;
|
||||
|
||||
/// Marker trait for contract state object
|
||||
pub trait IContract<'a>: Serialize + Deserialize<'a> {}
|
||||
|
||||
/// Trait for input parameters
|
||||
///
|
||||
/// To be able to publish public part of input parameters,
|
||||
/// we need to be able to make them.
|
||||
pub trait IInputParameters<'a>: Deserialize<'a> {
|
||||
fn public_input_parameters_ser(&self) -> Vec<Vec<u8>>;
|
||||
}
|
||||
|
||||
/// Marker trait for public outputs
|
||||
pub trait IPublicOutput: Serialize {}
|
||||
|
||||
/// Trait for private output
|
||||
///
|
||||
/// Must produce the list of resulting UTXO for publication
|
||||
pub trait IPrivateOutput {
|
||||
fn make_utxo_list(&self) -> Vec<UTXO>;
|
||||
}
|
||||
|
||||
/// Trait for public execution type
|
||||
pub trait IPublicExecutor<'a, I: IInputParameters<'a>, PuO: IPublicOutput> {
|
||||
fn public_execution(&mut self, inputs: I) -> PuO;
|
||||
}
|
||||
|
||||
/// Trait for private execution type
|
||||
pub trait IPrivateExecutor<'a, I: IInputParameters<'a>, PrO: IPrivateOutput> {
|
||||
fn private_execution(&mut self, inputs: I) -> PrO;
|
||||
}
|
||||
|
||||
/// Trait for shielded execution type
|
||||
pub trait IShieldedExecutor<'a, I: IInputParameters<'a>, PuO: IPublicOutput, PrO: IPrivateOutput> {
|
||||
fn shielded_execution(&mut self, inputs: I) -> (PuO, PrO);
|
||||
}
|
||||
|
||||
/// Trait for deshielded execution type
|
||||
pub trait IDeshieldedExecutor<'a, I: IInputParameters<'a>, PuO: IPublicOutput, PrO: IPrivateOutput>
|
||||
{
|
||||
fn deshielded_execution(&mut self, inputs: I) -> (PuO, PrO);
|
||||
}
|
||||
17
template/sc_example/Cargo.toml
Normal file
17
template/sc_example/Cargo.toml
Normal file
@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "sc_example"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1.0.81"
|
||||
|
||||
[dependencies.sc_core]
|
||||
path = "../../sc_core"
|
||||
|
||||
[dependencies.utxo]
|
||||
path = "../../utxo"
|
||||
|
||||
[dependencies.serde]
|
||||
features = ["derive"]
|
||||
version = "1.0.60"
|
||||
70
template/sc_example/src/lib.rs
Normal file
70
template/sc_example/src/lib.rs
Normal file
@ -0,0 +1,70 @@
|
||||
use sc_core::traits::{
|
||||
IContract, IDeshieldedExecutor, IInputParameters, IPrivateOutput, IPublicOutput,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utxo::utxo_core::UTXO;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct SmartContract {}
|
||||
|
||||
impl<'a> IContract<'a> for SmartContract {}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct InputParameters {
|
||||
pub a: u64,
|
||||
pub b: u64,
|
||||
}
|
||||
|
||||
impl<'a> IInputParameters<'a> for InputParameters {
|
||||
fn public_input_parameters_ser(&self) -> Vec<Vec<u8>> {
|
||||
let param_vec = vec![self.a];
|
||||
|
||||
param_vec
|
||||
.into_iter()
|
||||
.map(|item| serde_json::to_vec(&item).unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct PublicOutputs {
|
||||
pub ab: u64,
|
||||
}
|
||||
|
||||
impl IPublicOutput for PublicOutputs {}
|
||||
|
||||
struct PrivateOutputs {
|
||||
pub a_plus_b: u64,
|
||||
}
|
||||
|
||||
impl IPrivateOutput for PrivateOutputs {
|
||||
fn make_utxo_list(&self) -> Vec<UTXO> {
|
||||
let mut utxo_list = vec![];
|
||||
|
||||
let res_utxo = UTXO {
|
||||
hash: [0; 32],
|
||||
owner: [1; 32],
|
||||
asset: vec![1, 2, 3],
|
||||
amount: self.a_plus_b as u128,
|
||||
privacy_flag: true,
|
||||
randomness: [2; 32],
|
||||
};
|
||||
|
||||
utxo_list.push(res_utxo);
|
||||
|
||||
utxo_list
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IDeshieldedExecutor<'a, InputParameters, PublicOutputs, PrivateOutputs> for SmartContract {
|
||||
fn deshielded_execution(&self, inputs: InputParameters) -> (PublicOutputs, PrivateOutputs) {
|
||||
(
|
||||
PublicOutputs {
|
||||
ab: inputs.a * inputs.b,
|
||||
},
|
||||
PrivateOutputs {
|
||||
a_plus_b: inputs.a + inputs.b,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
20
template/zk_test_template/Cargo.toml
Normal file
20
template/zk_test_template/Cargo.toml
Normal file
@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "zk_test_template"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
serde_json = "1.0.81"
|
||||
|
||||
risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-2.0" }
|
||||
test-methods = { path = "test_methods" }
|
||||
|
||||
[dependencies.serde]
|
||||
features = ["derive"]
|
||||
version = "1.0.60"
|
||||
|
||||
[features]
|
||||
cuda = ["risc0-zkvm/cuda"]
|
||||
default = []
|
||||
prove = ["risc0-zkvm/prove"]
|
||||
1
template/zk_test_template/src/lib.rs
Normal file
1
template/zk_test_template/src/lib.rs
Normal file
@ -0,0 +1 @@
|
||||
//ToDo: handle ABI of smart contract
|
||||
10
template/zk_test_template/test_methods/Cargo.toml
Normal file
10
template/zk_test_template/test_methods/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "test-methods"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[build-dependencies]
|
||||
risc0-build = { git = "https://github.com/risc0/risc0.git", branch = "release-2.0" }
|
||||
|
||||
[package.metadata.risc0]
|
||||
methods = ["guest"]
|
||||
3
template/zk_test_template/test_methods/build.rs
Normal file
3
template/zk_test_template/test_methods/build.rs
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
risc0_build::embed_methods();
|
||||
}
|
||||
22
template/zk_test_template/test_methods/guest/Cargo.toml
Normal file
22
template/zk_test_template/test_methods/guest/Cargo.toml
Normal file
@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "test"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1.0.81"
|
||||
risc0-zkvm = { git = "https://github.com/risc0/risc0.git", default-features = false, features = [
|
||||
"std",
|
||||
] }
|
||||
|
||||
[dependencies.serde]
|
||||
features = ["derive"]
|
||||
version = "1.0.60"
|
||||
|
||||
[dependencies.{sc_name}]
|
||||
path = "../../../{sc_name}"
|
||||
|
||||
[dependencies.sc_core]
|
||||
path = "../../../../sc_core"
|
||||
@ -0,0 +1,68 @@
|
||||
use risc0_zkvm::{
|
||||
guest::env,
|
||||
};
|
||||
|
||||
use {sc_name}::{SmartContract, InputParameters, PublicOutputs, PrivateOutputs};
|
||||
use sc_core::traits::{IContract, IInputParameters, IPublicOutput, IPrivateOutput};
|
||||
use sc_core::{{execution_type_trait}};
|
||||
|
||||
//Not sure, how path will look like, so
|
||||
//ToDo: Make it available from sc_core
|
||||
//Curently in node_core
|
||||
use sc_core::PublicSCContext;
|
||||
|
||||
//Not sure, how path will look like, so
|
||||
//ToDo: Make it available from sc_core
|
||||
//Curently in storage
|
||||
use sc_core::{produce_blob_list_from_sc_public_state, compare_blob_lists};
|
||||
|
||||
fn main() {
|
||||
let mut state: SmartContract = env::read();
|
||||
//Must fail if this step fails
|
||||
let old_state = produce_blob_list_from_sc_public_state(&state).unwrap();
|
||||
|
||||
let public_context: PublicSCContext = env::read();
|
||||
let inputs: InputParameters = env::read();
|
||||
|
||||
//In RISC0 all input parameters are private, so we need to commit to public ones
|
||||
env::commit(&(inputs.public_input_parameters_ser()))
|
||||
|
||||
//Next, push one of possible variants depending on execution type
|
||||
{
|
||||
public => let public_outputs = state.public_execution(inputs);,
|
||||
private => let private_outputs = state.private_execution(inputs);,
|
||||
shielded => let (public_outputs, private_outputs) = state.shielded_execution(inputs);,
|
||||
deshielded => let (public_outputs, private_outputs) = state.shielded_execution(inputs);,
|
||||
}
|
||||
|
||||
//Next, push one of possible variants depending on execution type
|
||||
//ToDo [Debatable]: Rework and update circuits to work with new trait system system
|
||||
{
|
||||
public => ,
|
||||
private => private_circuit(public_context, private_outputs);,
|
||||
shielded => shielded_circuit(public_context, public_outputs, private_outputs);,
|
||||
deshielded => deshielded_circuit(public_context, public_outputs, private_outputs);,
|
||||
}
|
||||
|
||||
//Must fail if this step fails
|
||||
let new_state = produce_blob_list_from_sc_public_state(&state).unwrap();
|
||||
|
||||
//Commiting public state changes
|
||||
let state_changes = compare_blob_lists(old_state, new_state);
|
||||
env::commit(&state_changes);
|
||||
|
||||
//Next, push one of possible variants depending on execution type
|
||||
//ToDo: Make UTXO encoding for their owners available from PublicSCContext
|
||||
{
|
||||
public => {
|
||||
env::commit(&public_outputs);
|
||||
},
|
||||
private => {
|
||||
env::commit(&(public_context.encode_utxo_for_owners(private_outputs.make_utxo_list())));
|
||||
},
|
||||
shielded | deshielded => {
|
||||
env::commit(&public_outputs);
|
||||
env::commit(&(public_context.encode_utxo_for_owners(private_outputs.make_utxo_list())));
|
||||
},
|
||||
}
|
||||
}
|
||||
1
template/zk_test_template/test_methods/src/lib.rs
Normal file
1
template/zk_test_template/test_methods/src/lib.rs
Normal file
@ -0,0 +1 @@
|
||||
include!(concat!(env!("OUT_DIR"), "/methods.rs"));
|
||||
Loading…
x
Reference in New Issue
Block a user