From 4d30db4b6c4394a27028aac3dc2d1c0d4268ae34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cain=C3=A3=20Costa?= Date: Sun, 4 Aug 2024 17:35:49 -0300 Subject: [PATCH] minor: extract modules --- core/programs/guest/src/bin/apply.rs | 4 +- core/src/programs/apply.rs | 73 +++++++++++++++++++ core/src/programs/compose.rs | 30 ++++++++ core/src/programs/mod.rs | 102 +-------------------------- 4 files changed, 108 insertions(+), 101 deletions(-) create mode 100644 core/src/programs/apply.rs create mode 100644 core/src/programs/compose.rs diff --git a/core/programs/guest/src/bin/apply.rs b/core/programs/guest/src/bin/apply.rs index fef2aa7..7741699 100644 --- a/core/programs/guest/src/bin/apply.rs +++ b/core/programs/guest/src/bin/apply.rs @@ -1,8 +1,8 @@ #![no_std] -use mugraph_core::programs::verify; +use mugraph_core::programs::apply; use risc0_zkvm::guest::env; fn main() { - verify(env::read()).unwrap(); + apply(env::read()).unwrap(); } diff --git a/core/src/programs/apply.rs b/core/src/programs/apply.rs new file mode 100644 index 0000000..08dcadd --- /dev/null +++ b/core/src/programs/apply.rs @@ -0,0 +1,73 @@ +use risc0_zkvm::guest::env; + +use crate::{error::Result, types::*}; + +pub fn apply(req: Request) -> Result { + match req.data { + Operation::UNSAFE_Mint { .. } => { + // Do nothing. + } + Operation::Split { + ref input, + ref outputs, + } => { + assert_ne!(outputs.len(), 0); + + if let Some(program_id) = input.data.program_id { + env::verify(program_id, &req.data.to_bytes()?) + .expect("Failed to run input program."); + } + + let mut output_total = 0; + + for output in outputs { + assert_eq!(output.asset_id, input.data.asset_id); + assert_ne!(output.amount, 0); + + output_total += output.amount; + } + + assert_eq!(input.data.amount, output_total); + } + Operation::Join { + ref inputs, + ref output, + } => { + assert_ne!(inputs.len(), 0); + + let asset_id = inputs[0].data.asset_id; + assert_eq!(asset_id, output.asset_id); + + let mut input_total = 0; + + for input in inputs { + assert_eq!(asset_id, input.data.asset_id); + assert_ne!(0, input.data.amount); + + if let Some(program_id) = input.data.program_id { + env::verify(program_id, &req.data.to_bytes()?) + .expect("Failed to run input program."); + } + + input_total += input.data.amount; + } + + assert_eq!(input_total, output.amount); + } + Operation::Consume { + ref input, + ref output, + } => { + assert_eq!(input.data.asset_id, output.asset_id); + assert_ne!(0, input.data.amount); + assert_eq!(input.data.amount, output.amount); + + if let Some(program_id) = input.data.program_id { + env::verify(program_id, &req.data.to_bytes()?) + .expect("Failed to run input program."); + } + } + } + + req.data.id() +} diff --git a/core/src/programs/compose.rs b/core/src/programs/compose.rs new file mode 100644 index 0000000..1d77b2b --- /dev/null +++ b/core/src/programs/compose.rs @@ -0,0 +1,30 @@ +use risc0_zkvm::guest::env; + +use crate::{error::Result, types::*}; + +pub fn compose(req: Request>) -> Result { + let mut nullifiers = Vec::new(); + + for operation in req.data.iter() { + env::verify(req.manifest.programs.apply, operation.id()?.as_ref()).unwrap(); + + match operation { + Operation::UNSAFE_Mint { .. } => { + // Do nothing. + } + Operation::Consume { input, .. } => { + nullifiers.push(input.hash()?); + } + Operation::Split { input, .. } => { + nullifiers.push(input.hash()?); + } + Operation::Join { inputs, .. } => { + for input in inputs { + nullifiers.push(input.hash()?); + } + } + } + } + + Ok(Reaction { nullifiers }) +} diff --git a/core/src/programs/mod.rs b/core/src/programs/mod.rs index 2af4989..9ccb66b 100644 --- a/core/src/programs/mod.rs +++ b/core/src/programs/mod.rs @@ -1,100 +1,4 @@ -use risc0_zkvm::guest::env; +mod apply; +mod compose; -use crate::{error::Result, types::*}; - -pub fn compose(req: Request>) -> Result { - let mut nullifiers = Vec::new(); - - for operation in req.data.iter() { - env::verify(req.manifest.programs.apply, operation.id()?.as_ref()).unwrap(); - - match operation { - Operation::UNSAFE_Mint { .. } => { - // Do nothing. - } - Operation::Consume { input, .. } => { - nullifiers.push(input.hash()?); - } - Operation::Split { input, .. } => { - nullifiers.push(input.hash()?); - } - Operation::Join { inputs, .. } => { - for input in inputs { - nullifiers.push(input.hash()?); - } - } - } - } - - Ok(Reaction { nullifiers }) -} - -pub fn verify(req: Request) -> Result { - match req.data { - Operation::UNSAFE_Mint { .. } => { - // Do nothing. - } - Operation::Split { - ref input, - ref outputs, - } => { - assert_ne!(outputs.len(), 0); - - if let Some(program_id) = input.data.program_id { - env::verify(program_id, &req.data.to_bytes()?) - .expect("Failed to run input program."); - } - - let mut output_total = 0; - - for output in outputs { - assert_eq!(output.asset_id, input.data.asset_id); - assert_ne!(output.amount, 0); - - output_total += output.amount; - } - - assert_eq!(input.data.amount, output_total); - } - Operation::Join { - ref inputs, - ref output, - } => { - assert_ne!(inputs.len(), 0); - - let asset_id = inputs[0].data.asset_id; - assert_eq!(asset_id, output.asset_id); - - let mut input_total = 0; - - for input in inputs { - assert_eq!(asset_id, input.data.asset_id); - assert_ne!(0, input.data.amount); - - if let Some(program_id) = input.data.program_id { - env::verify(program_id, &req.data.to_bytes()?) - .expect("Failed to run input program."); - } - - input_total += input.data.amount; - } - - assert_eq!(input_total, output.amount); - } - Operation::Consume { - ref input, - ref output, - } => { - assert_eq!(input.data.asset_id, output.asset_id); - assert_ne!(0, input.data.amount); - assert_eq!(input.data.amount, output.amount); - - if let Some(program_id) = input.data.program_id { - env::verify(program_id, &req.data.to_bytes()?) - .expect("Failed to run input program."); - } - } - } - - req.data.id() -} +pub use self::{apply::*, compose::*};