Skip to content

Commit

Permalink
Reformatted with rustfmt
Browse files Browse the repository at this point in the history
- Converted from nightly build stable
- Deleted some now unsupported settings in rustfmt.toml
- Cleaned up some clippy warings and other code issues
- Added documentation comments to the code
  • Loading branch information
John Basrai authored and John Basrai committed Sep 27, 2024
1 parent 6374c18 commit 16c9e8e
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 164 deletions.
15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8"
rand = "0"
num-bigint = { version = "0.4", features = ["rand"] }
hex = "0.4.3"
tonic = "0.9"
prost = "0.11"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } # async rust runtime
hex = "0"
tonic = "0"
prost = "0"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] } # async rust runtime
clap = { version = "4", features = ["derive","cargo"] }
anyhow = "1"
rpassword = "7"

[build-dependencies]
tonic-build = "0.9"
tonic-build = "0"

[[bin]]
name = "server"
path = "./src/server.rs"

[[bin]]
name = "client"
path = "./src/client.rs"
path = "./src/client.rs"
5 changes: 2 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
fn main()
{
fn main() {
tonic_build::configure()
.build_server(true)
.out_dir("src/") // you can change the generated code's location
.compile(
.compile_protos(
&["proto/zkp_auth.proto"],
&["proto/"], // specify the root location to search proto dependencies
)
Expand Down
2 changes: 1 addition & 1 deletion proto/zkp_auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ service Auth {
rpc Register(RegisterRequest) returns (RegisterResponse) {}
rpc CreateAuthenticationChallenge(AuthenticationChallengeRequest) returns (AuthenticationChallengeResponse) {}
rpc VerifyAuthentication(AuthenticationAnswerRequest) returns (AuthenticationAnswerResponse) {}
}
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "nightly"
channel = "stable"
13 changes: 0 additions & 13 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,4 @@ edition = "2021"
array_width = 70
fn_params_layout = "Compressed"
fn_call_width = 80
fn_single_line = true
space_before_colon = false
spaces_around_ranges = true
unstable_features = true

wrap_comments = true
max_width = 100
comment_width = 90

# These work only for nightly builds (of rust toolchain), as of rust v 1.71
control_brace_style = "AlwaysNextLine"
brace_style = "AlwaysNextLine"
struct_field_align_threshold = 40
enum_discrim_align_threshold = 40
65 changes: 26 additions & 39 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use clap::Parser;
use num_bigint::BigUint;
use rpassword::prompt_password;

pub mod zkp_auth
{
pub mod zkp_auth {
include!("./zkp_auth.rs");
}

Expand All @@ -16,16 +16,11 @@ use zkp_chaum_pedersen::ZKP;
#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
/// Chaum-Pedersen Zero Knowledge Proof (client)
struct Args
{
struct Args {
/// User name
#[arg(short, long, required = true)]
user_name: String,

/// Password
#[arg(short, long, required = true)]
password: String,

/// Server endpoint
#[arg(
short,
Expand All @@ -37,62 +32,56 @@ struct Args
}

#[tokio::main]
async fn main() -> Result<(), String>
{
let args = &Args::parse();
async fn main() -> Result<()> {
let args = Args::parse();

let (alpha, beta, p, q) = ZKP::get_constants();
let zkp = ZKP::new(&p, &q, &alpha, &beta);

let mut client = match AuthClient::connect(args.server.clone()).await
{
let mut client = match AuthClient::connect(args.server.clone()).await {
Ok(x) => x,
Err(e) =>
{
return Err(format!("Connect failed to {}: {:?}", args.server, e));
Err(e) => {
return Err(anyhow!("Failed to register user on server {}: {:?}", args.server, e));
}
};

println!("✅ Connected to the server");
let password = BigUint::from_bytes_be(args.password.trim().as_bytes());

let password = prompt_password("Password: ")?;
let password = BigUint::from_bytes_be(password.trim().as_bytes());
let (y1, y2) = zkp.compute_pair(&password);

let username = args.user_name.clone();
let request = RegisterRequest {
user: username.clone(),
y1: y1.to_bytes_be(),
y2: y2.to_bytes_be(),
y1: y1.to_bytes_be(),
y2: y2.to_bytes_be(),
};

let _ = match client.register(request).await
{
let _ = match client.register(request).await {
Ok(x) => x,
Err(e) =>
{
return Err(format!("Could not register user name with server {:?}", e));
Err(e) => {
return Err(anyhow!("Could not register user name with server {:?}", e));
}
};
println!("✅ Registration was successful");

println!("Please provide the password (to login):");
let password = BigUint::from_bytes_be(args.password.trim().as_bytes());
let password = prompt_password("Password (to login): ")?;
let password = BigUint::from_bytes_be(password.trim().as_bytes());

let k = ZKP::generate_random_number_below(&q);
let (r1, r2) = zkp.compute_pair(&k);

let request = AuthenticationChallengeRequest {
user: username,
r1: r1.to_bytes_be(),
r2: r2.to_bytes_be(),
r1: r1.to_bytes_be(),
r2: r2.to_bytes_be(),
};

let response = match client.create_authentication_challenge(request).await
{
let response = match client.create_authentication_challenge(request).await {
Ok(x) => x,
Err(e) =>
{
return Err(format!("Could not request challenge to server {:?}", e));
Err(e) => {
return Err(anyhow!("Could not request challenge to server {:?}", e));
}
}
.into_inner();
Expand All @@ -106,12 +95,10 @@ async fn main() -> Result<(), String>
s: s.to_bytes_be(),
};

let response = match client.verify_authentication(request).await
{
let response = match client.verify_authentication(request).await {
Ok(x) => x,
Err(e) =>
{
return Err(format!("Could not verify authentication in server {:?}", e));
Err(e) => {
return Err(anyhow!("Could not verify authentication in server {:?}", e));
}
}
.into_inner();
Expand Down
Loading

0 comments on commit 16c9e8e

Please sign in to comment.