Skip to content

Commit

Permalink
Migrate all functions to rpc calls
Browse files Browse the repository at this point in the history
To simplify the user interface and make further development easier, all
functions provided by the application will be possible as RPC calls. In
this commit, only the proving and the verifying are done in this way.
Additionally, to streamline the process, the convertkeys function is
removed as it is only needed for proving and verifying and therefore can
be done on startup of the RPC server, removing any delays during the
proving step.
What remains to be done is: add newkeys to the RPC interface, and,
importantly, introduce security in the form of encryption of the file
holding the private key which is required for proving.
  • Loading branch information
AdamISZ committed Jul 16, 2024
1 parent 069c6f6 commit fb46ef8
Show file tree
Hide file tree
Showing 7 changed files with 557 additions and 422 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ edition = "2021"

[dependencies]
rand = { version = "0.8", default-features = false }
bulletproofs = { git = "https://github.com/AdamISZ/curve-trees", rev = "d76bd2837d4556b1d451055e4c381e5cb5abd00c" }
relations = { git = "https://github.com/AdamISZ/curve-trees", rev = "d76bd2837d4556b1d451055e4c381e5cb5abd00c" }
bulletproofs = { git = "https://github.com/AdamISZ/curve-trees", rev = "7268c22944595e2cfe52de623d33ece6f4882e5f" }
relations = { git = "https://github.com/AdamISZ/curve-trees", rev = "7268c22944595e2cfe52de623d33ece6f4882e5f" }
ark-ff = { version = "0.4.0"}
ark-ec = { version = "0.4.0"}
ark-serialize = { version = "0.4.0" }
Expand Down
396 changes: 36 additions & 360 deletions src/autct.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::PathBuf;
use clap::{Parser, CommandFactory, Command};

// This handles config items with syntax: "a:b, c:d,.."
fn get_params_from_config_string(params: String) -> Result<(Vec<String>, Vec<String>), Box<dyn Error>> {
pub fn get_params_from_config_string(params: String) -> Result<(Vec<String>, Vec<String>), Box<dyn Error>> {
let pairs: Vec<String> = params.split(",").map(|s| s.to_string()).collect();
let mut kss: Vec<String> = vec![];
let mut cls: Vec<String> = vec![];
Expand Down Expand Up @@ -46,7 +46,7 @@ https://stackoverflow.com/a/75981247
#[clap(version, about="Anonymous Usage Tokens from Curve Trees")]
pub struct AutctConfig {
/// `mode` is one of: "newkeys", "prove",
/// "serve", "convertkeys" or "request"
/// "serve" or "verify"
#[arg(short('M'), long, required=false)]
#[clap(verbatim_doc_comment)]
pub mode: Option<String>,
Expand Down
289 changes: 270 additions & 19 deletions src/lib.rs

Large diffs are not rendered by default.

47 changes: 43 additions & 4 deletions src/rpcclient.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use autct::utils::APP_DOMAIN_LABEL;

use toy_rpc::Client;
use autct::config::AutctConfig;
use crate::config::AutctConfig;
use crate::utils::*;


// import everything including the client stub generated by the macro
use autct::rpc::*;
use crate::rpc::*;
use std::error::Error;
use std::fs;

pub async fn do_request(autctcfg: AutctConfig) -> Result<RPCProofVerifyResponse, Box<dyn Error>>{
pub async fn verify(autctcfg: AutctConfig) -> Result<RPCProofVerifyResponse, Box<dyn Error>>{
let rpc_port = autctcfg.rpc_port;
let host: &str= &autctcfg.rpc_host.clone().unwrap();
let port_str: &str = &rpc_port.unwrap().to_string();
Expand Down Expand Up @@ -38,3 +40,40 @@ pub async fn do_request(autctcfg: AutctConfig) -> Result<RPCProofVerifyResponse,
Ok(result)
}

pub async fn prove(autctcfg: AutctConfig) -> Result<RPCProverResponse, Box<dyn Error>>{
let rpc_port = autctcfg.rpc_port;
let host: &str= &autctcfg.rpc_host.clone().unwrap();
let port_str: &str = &rpc_port.unwrap().to_string();
let addr: String = format!("{}:{}", host, port_str);

// request must specify *only one* context label, keyset.
// This will be checked by the server but we can check it here also.
let (cls, kss) = autctcfg.clone()
.get_context_labels_and_keysets().unwrap();
if kss.len() != 1 || cls.len() != 1 {
return Err("You may only specify one context_label:keyset in the request".into())
}
let req: RPCProverRequest = RPCProverRequest {
keyset: autctcfg.keysets.unwrap(),
depth: autctcfg.depth.unwrap(),
generators_length_log_2: autctcfg.generators_length_log_2.unwrap(),
user_label: autctcfg.user_string.unwrap(),
key_credential: autctcfg.privkey_file_str.unwrap(),
bc_network: autctcfg.bc_network.unwrap()
};
let mut client = Client::dial(&addr).await.unwrap();
// we set a very generous timeout for proving requests, though they should
// usually be in the sub 15s area.
client.set_default_timeout(std::time::Duration::from_secs(120));
let result = client
.r_p_c_prover().prove(req)
.await;
match result {
Ok(x) => return Ok(x),
Err(x) => {
println!("Error in rpc client prove call: {}", &x);
return Err(x.into());
}
}
}

46 changes: 32 additions & 14 deletions src/rpcserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

use ark_serialize::{ CanonicalDeserialize,
Compress, Validate};
use autct::utils::{get_curve_tree, get_leaf_commitments, APP_DOMAIN_LABEL};
use crate::rpc::RPCProverVerifierArgs;
use crate::utils::{get_curve_tree, get_leaf_commitments, convert_keys, APP_DOMAIN_LABEL};
use tokio::{task, net::TcpListener};
use std::fs;
use std::error::Error;
use std::io::Cursor;
use std::sync::{Arc, Mutex};
use toy_rpc::Server;
use std::iter::zip;

use autct::{rpc::RPCProofVerifier, utils};
use autct::config::AutctConfig;
use autct::keyimagestore::{KeyImageStore, create_new_store};
use crate::{rpc::RPCProofVerifier, rpc::RPCProver, utils};
use crate::config::AutctConfig;
use crate::keyimagestore::{KeyImageStore, create_new_store};
use relations::curve_tree::{SelRerandParameters, CurveTree};
use ark_secp256k1::{Config as SecpConfig, Fq as SecpBase};
use ark_secq256k1::Config as SecqConfig;
use ark_ec::short_weierstrass::{Affine, SWCurveConfig};
use utils::CustomError;

pub async fn do_serve(autctcfg: AutctConfig) -> Result<(), CustomError>{
pub async fn do_serve(autctcfg: AutctConfig) -> Result<(), Box<dyn Error>>{
let (context_labels, keyset_file_locs) = autctcfg
.clone().get_context_labels_and_keysets().unwrap();
let rpc_port = autctcfg.rpc_port.unwrap();
Expand All @@ -35,7 +36,16 @@ pub async fn do_serve(autctcfg: AutctConfig) -> Result<(), CustomError>{
let mut Js: Vec<Affine<SecpConfig>> = vec![];
let mut kss: Vec<Arc<Mutex<KeyImageStore<Affine<SecpConfig>>>>> = vec![];
for (fl, cl) in zip(keyset_file_locs.iter(), context_labels.iter()) {
// this part is what consumes time, so we do it upfront on startup of the rpc server,
// for every keyset that we are serving.
// also note that for now there are no errors returned by convert_keys hence unwrap()
// TODO add info to interface so user knows why the startup is hanging
convert_keys::<SecpBase,
SecpConfig,
SecqConfig>(fl.to_string(), autctcfg.generators_length_log_2.unwrap()).unwrap();
let leaf_commitments = get_leaf_commitments(&(fl.to_string() + ".p"));

// Actually creating the curve tree is much less time consuming (a few seconds for most trees)
let (curve_tree2, _) = get_curve_tree::
<SecpBase, SecpConfig, SecqConfig>(
leaf_commitments,
Expand Down Expand Up @@ -70,19 +80,27 @@ pub async fn do_serve(autctcfg: AutctConfig) -> Result<(), CustomError>{
let G = SecpConfig::GENERATOR;
//let J = utils::get_generators(autctcfg.context_label.as_ref().unwrap().as_bytes());
let H = sr_params.even_parameters.pc_gens.B_blinding.clone();
let prover_verifier_args = RPCProverVerifierArgs {
sr_params,
keyset_file_locs,
context_labels,
curve_trees,
G,
H,
Js,
ks: kss
};
let verifier_service = Arc::new(
RPCProofVerifier{
sr_params,
keyset_file_locs,
context_labels,
curve_trees,
G,
H,
Js,
ks: kss}
prover_verifier_args: prover_verifier_args.clone()}
);
let prover_service = Arc::new(
RPCProver{
prover_verifier_args}
);
let server = Server::builder()
.register(verifier_service) // register service
.register(prover_service)
.build();
let listener = TcpListener::bind(&addr).await.unwrap();

Expand Down
Loading

0 comments on commit fb46ef8

Please sign in to comment.