Skip to content

Commit

Permalink
Setup holder to hold setup, prover and verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Nov 3, 2023
1 parent fde8236 commit 35ef5f1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
35 changes: 21 additions & 14 deletions moat-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::interactor::SetupHolder;
use crate::SeedableRng;
use bytecheck::CheckBytes;
use bytes::Bytes;
Expand Down Expand Up @@ -146,7 +147,7 @@ impl Command {
gas_limit: u64,
gas_price: u64,
request_json: Option<RequestJson>,
pp: &mut Option<PublicParameters>,
setup_holder: &mut Option<SetupHolder>,
) -> Result<(), Error> {
match self {
Command::SubmitRequest { request_path } => {
Expand Down Expand Up @@ -348,7 +349,7 @@ impl Command {
pos,
gas_limit,
gas_price,
pp,
setup_holder,
)
.await?;
}
Expand Down Expand Up @@ -500,7 +501,7 @@ impl Command {
pos: u64,
gas_limit: u64,
gas_price: u64,
pp_opt: &mut Option<PublicParameters>,
sh_opt: &mut Option<SetupHolder>,
) -> Result<BlsScalar, Error> {
let client =
RuskHttpClient::new(blockchain_access_config.rusk_address.clone());
Expand All @@ -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::<LicenseCircuit>(&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::<LicenseCircuit>(pp, LABEL)
.expect("Compiling circuit should succeed");

let opening = CitadelInquirer::get_merkle_opening(&client, pos)
.await?
.expect("Opening obtained successfully");
Expand All @@ -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");
Expand Down
22 changes: 14 additions & 8 deletions moat-cli/src/interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -62,7 +62,7 @@ fn menu_operation() -> Result<OpSelection, ErrorKind> {
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",
)?,
}))
Expand All @@ -75,15 +75,15 @@ fn menu_operation() -> Result<OpSelection, ErrorKind> {
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",
)?,
}))
}
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()?,
Expand All @@ -92,15 +92,15 @@ fn menu_operation() -> Result<OpSelection, ErrorKind> {
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",
)?,
}))
}
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()?,
Expand All @@ -123,6 +123,12 @@ fn menu_operation() -> Result<OpSelection, ErrorKind> {
})
}

pub struct SetupHolder {
pub pp: PublicParameters,
pub prover: Prover,
pub verifier: Verifier,
}

pub struct Interactor {
pub wallet_path: WalletPath,
pub psw: Password,
Expand All @@ -131,7 +137,7 @@ pub struct Interactor {
pub gas_limit: u64,
pub gas_price: u64,
pub request_json: Option<RequestJson>,
pub pp: Option<PublicParameters>,
pub setup_holder: Option<SetupHolder>,
}

impl Interactor {
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion moat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down

0 comments on commit 35ef5f1

Please sign in to comment.