From 35ef5f1e477a520dc38f4088c96937690e68daf2 Mon Sep 17 00:00:00 2001 From: Milosz Muszynski Date: Fri, 3 Nov 2023 17:14:31 +0100 Subject: [PATCH] Setup holder to hold setup, prover and verifier --- moat-cli/src/command.rs | 35 +++++++++++++++++++++-------------- moat-cli/src/interactor.rs | 22 ++++++++++++++-------- moat-cli/src/main.rs | 2 +- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/moat-cli/src/command.rs b/moat-cli/src/command.rs index 99b4221..ccb81d8 100644 --- a/moat-cli/src/command.rs +++ b/moat-cli/src/command.rs @@ -4,6 +4,7 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +use crate::interactor::SetupHolder; use crate::SeedableRng; use bytecheck::CheckBytes; use bytes::Bytes; @@ -146,7 +147,7 @@ impl Command { gas_limit: u64, gas_price: u64, request_json: Option, - pp: &mut Option, + setup_holder: &mut Option, ) -> Result<(), Error> { match self { Command::SubmitRequest { request_path } => { @@ -348,7 +349,7 @@ impl Command { pos, gas_limit, gas_price, - pp, + setup_holder, ) .await?; } @@ -500,7 +501,7 @@ impl Command { pos: u64, gas_limit: u64, gas_price: u64, - pp_opt: &mut Option, + sh_opt: &mut Option, ) -> Result { let client = RuskHttpClient::new(blockchain_access_config.rusk_address.clone()); @@ -509,21 +510,26 @@ impl Command { let challenge = JubJubScalar::from(0xcafebabeu64); let mut rng = StdRng::seed_from_u64(0xbeef); - println!("performing setup"); - let pp: &PublicParameters = match pp_opt { - Some(pp) => pp, + let setup_holder = match sh_opt { + Some(sh) => sh, _ => { + println!("performing setup"); let pp = PublicParameters::setup(1 << CAPACITY, &mut rng) .expect("Initializing public parameters should succeed"); - *pp_opt = Some(pp); - pp_opt.as_ref().unwrap() + println!("compiling circuit"); + let (prover, verifier) = + Compiler::compile::(&pp, LABEL) + .expect("Compiling circuit should succeed"); + let sh = SetupHolder { + pp, + prover, + verifier, + }; + *sh_opt = Some(sh); + sh_opt.as_ref().unwrap() } }; - println!("compiling circuit"); - let (prover, verifier) = Compiler::compile::(pp, LABEL) - .expect("Compiling circuit should succeed"); - let opening = CitadelInquirer::get_merkle_opening(&client, pos) .await? .expect("Opening obtained successfully"); @@ -534,14 +540,15 @@ impl Command { let circuit = LicenseCircuit::new(&cpp, &sc); println!("calculating proof"); - let (proof, public_inputs) = prover + let (proof, public_inputs) = setup_holder + .prover .prove(&mut rng, &circuit) .expect("Proving should succeed"); assert!(!public_inputs.is_empty()); let session_id = public_inputs[0]; - verifier + setup_holder.verifier .verify(&proof, &public_inputs) .expect("Verifying the circuit should succeed"); println!("proof validated locally"); diff --git a/moat-cli/src/interactor.rs b/moat-cli/src/interactor.rs index 848b72c..42b3c0b 100644 --- a/moat-cli/src/interactor.rs +++ b/moat-cli/src/interactor.rs @@ -7,7 +7,7 @@ use crate::error::CliError; use crate::prompt; use crate::{Command, Menu}; -use dusk_plonk::prelude::PublicParameters; +use dusk_plonk::prelude::{Prover, PublicParameters, Verifier}; use dusk_wallet::WalletPath; use moat_core::{Error, RequestJson}; use requestty::{ErrorKind, Question}; @@ -62,7 +62,7 @@ fn menu_operation() -> Result { CommandMenuItem::SubmitRequest => { OpSelection::Run(Box::from(Command::SubmitRequest { request_path: prompt::request_pathbuf( - "request (e.g. moat-cli/request2.json)", + "request", "moat-cli/request2.json", )?, })) @@ -75,7 +75,7 @@ fn menu_operation() -> Result { CommandMenuItem::ListRequestsLP => { OpSelection::Run(Box::from(Command::ListRequestsLP { lp_config_path: prompt::request_pathbuf( - "LP config (e.g. moat-cli/lp2.json)", + "LP config", "moat-cli/lp2.json", )?, })) @@ -83,7 +83,7 @@ fn menu_operation() -> Result { CommandMenuItem::IssueLicenseLP => { OpSelection::Run(Box::from(Command::IssueLicenseLP { lp_config_path: prompt::request_pathbuf( - "LP config (e.g. moat-cli/lp2.json)", + "LP config", "moat-cli/lp2.json", )?, request_hash: prompt::request_request_hash()?, @@ -92,7 +92,7 @@ fn menu_operation() -> Result { CommandMenuItem::ListLicenses => { OpSelection::Run(Box::from(Command::ListLicenses { request_path: prompt::request_pathbuf( - "request (e.g. moat-cli/request2.json)", + "request", "moat-cli/request2.json", )?, })) @@ -100,7 +100,7 @@ fn menu_operation() -> Result { CommandMenuItem::UseLicense => { OpSelection::Run(Box::from(Command::UseLicense { request_path: prompt::request_pathbuf( - "request (e.g. moat-cli/request2.json)", + "request", "moat-cli/request2.json", )?, license_hash: prompt::request_license_hash()?, @@ -123,6 +123,12 @@ fn menu_operation() -> Result { }) } +pub struct SetupHolder { + pub pp: PublicParameters, + pub prover: Prover, + pub verifier: Verifier, +} + pub struct Interactor { pub wallet_path: WalletPath, pub psw: Password, @@ -131,7 +137,7 @@ pub struct Interactor { pub gas_limit: u64, pub gas_price: u64, pub request_json: Option, - pub pp: Option, + pub setup_holder: Option, } impl Interactor { @@ -150,7 +156,7 @@ impl Interactor { self.gas_limit, self.gas_price, self.request_json.clone(), - &mut self.pp, + &mut self.setup_holder, ) .await; if result.is_err() { diff --git a/moat-cli/src/main.rs b/moat-cli/src/main.rs index 4ddfef8..b2bd627 100644 --- a/moat-cli/src/main.rs +++ b/moat-cli/src/main.rs @@ -60,7 +60,7 @@ async fn main() -> Result<(), CliError> { gas_limit, gas_price, request_json: Some(request_json), - pp: None, + setup_holder: None, }; interactor.run_loop().await?;