Skip to content

Commit

Permalink
inte
Browse files Browse the repository at this point in the history
  • Loading branch information
leontiadZen committed Sep 28, 2023
1 parent 196da5f commit 3461c9e
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 149 deletions.
11 changes: 3 additions & 8 deletions gotham-server/src/public_gotham.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use gotham_engine::types::*;
pub struct PublicGotham {
rocksdb_client: rocksdb::DB,
}

pub struct Authorizer{}
pub struct Config {
pub db: DB,
}
Expand All @@ -23,10 +23,10 @@ pub enum DB {
Local(rocksdb::DB),
}

impl Txauthorization for PublicGotham {
impl Txauthorization for Authorizer {
/// the granted function implements the logic of tx authorization. If no tx authorization is needed the function returns always true
fn granted(&self) -> Result<bool, DatabaseError> {
Result(true)
Ok(true)
}
}
fn get_settings_as_map() -> HashMap<String, String> {
Expand Down Expand Up @@ -57,11 +57,6 @@ impl PublicGotham {
}
}

impl Default for PublicGotham {
fn default() -> Self {
Self::new()
}
}

impl KeyGen for PublicGotham {}

Expand Down
6 changes: 4 additions & 2 deletions gotham-server/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::public_gotham::{Config, PublicGotham, DB};
use crate::public_gotham::{Authorizer, Config, PublicGotham, DB};
use rocket::{self, catch, catchers, routes, Build, Request, Rocket};
use std::collections::HashMap;
use tokio::sync::Mutex;
Expand All @@ -24,6 +24,7 @@ pub fn get_server(settings: HashMap<String, String>) -> Rocket<Build> {
db: get_db(settings.clone()),
};
let x = PublicGotham::new();
let tx = Authorizer{};
rocket::Rocket::build()
.register("/", catchers![internal_error, not_found, bad_request])
.mount(
Expand All @@ -40,11 +41,12 @@ pub fn get_server(settings: HashMap<String, String>) -> Rocket<Build> {
],
)
.manage(Mutex::new(Box::new(x) as Box<dyn gotham_engine::traits::Db>))
.manage(Mutex::new(Box::new(tx) as Box<dyn gotham_engine::traits::Txauthorization>))
.manage(db_config)
}

fn get_db(settings: HashMap<String, String>) -> DB {
let db_name = settings.get("db_name").unwrap_or(&"db".to_string()).clone();
let db_name = settings.get("db_name").unwrap().clone();
if !db_name.chars().all(|e| char::is_ascii_alphanumeric(&e)) {
panic!("DB name is illegal, may only contain alphanumeric characters");
}
Expand Down
2 changes: 1 addition & 1 deletion gotham-server/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ mod tests {
}

#[test]
fn key_gen_and_sign() {
fn unit_test_key_gen_and_sign() {
// Passthrough mode
env::set_var("region", "");
env::set_var("pool_id", "");
Expand Down
275 changes: 137 additions & 138 deletions integration-tests/tests/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,112 +11,112 @@ use two_party_ecdsa::curv::arithmetic::big_gmp::BigInt;
use two_party_ecdsa::curv::arithmetic::traits::Converter;
use two_party_ecdsa::curv::elliptic::curves::traits::ECPoint;

#[rocket::async_test]
async fn test_ecdsa_network() {
let mut rng = StepRng::new(0, 1);
rocket::tokio::spawn(spawn_server(8000, "ecdsa"));

let client_shim = ClientShim::new("http://localhost:8000".to_string(), None);

let two_seconds = time::Duration::from_millis(2000);
thread::sleep(two_seconds);

let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);

for y in 0..50i32 {
let x_pos = BigInt::from(y * 2 + 1);
let y_pos = BigInt::from(y);

let child_master_key = ps.master_key.get_child(vec![x_pos.clone(), y_pos.clone()]);
let pk = child_master_key.public.q.get_element();

let mut msg_buf = [0u8; 32];
rng.fill(&mut msg_buf);
let msg: BigInt = BigInt::from(&msg_buf[..]);

let signature = ecdsa::sign(
&client_shim,
msg.clone(),
&child_master_key,
x_pos,
y_pos,
&ps.id,
)
.expect("ECDSA signature failed");

let r = BigInt::to_vec(&signature.r);
let s = BigInt::to_vec(&signature.s);
let msg = Message::from_slice(&msg_buf).unwrap();

let mut sig = [0u8; 64];
sig[32 - r.len()..32].copy_from_slice(&r);
sig[32 + 32 - s.len()..].copy_from_slice(&s);

let sig = Signature::from_compact(&sig).unwrap();

SECP256K1.verify_ecdsa(&msg, &sig, &pk).unwrap();
}
}

async fn spawn_server(port: u32, db_name: &str) -> Rocket<Ignite> {
let settings = HashMap::<String, String>::from([
("db".to_string(), "local".to_string()),
("db_name".to_string(), db_name.to_string()),
]);
let rocket = server::get_server(settings);
let figment = rocket.figment().clone().merge((Config::PORT, port));
rocket.configure(figment).launch().await.unwrap()
}

#[test]
fn test_ecdsa_keygen() {
let settings = HashMap::<String, String>::from([
("db".into(), "local".into()),
("db_name".into(), "testEcdsaKeygen".into()),
]);
let rocket = server::get_server(settings);
let client = RocketClient::new(rocket);

let client_shim =
ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);
for _ in 0..10 {
let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
let _ = ps.master_key.public.q.get_element();
}
}
// #[rocket::async_test]
// async fn test_ecdsa_network() {
// let mut rng = StepRng::new(0, 1);
// rocket::tokio::spawn(spawn_server(8000, "ecdsa"));
//
// let client_shim = ClientShim::new("http://localhost:8000".to_string(), None);
//
// let two_seconds = time::Duration::from_millis(2000);
// thread::sleep(two_seconds);
//
// let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
//
// for y in 0..50i32 {
// let x_pos = BigInt::from(y * 2 + 1);
// let y_pos = BigInt::from(y);
//
// let child_master_key = ps.master_key.get_child(vec![x_pos.clone(), y_pos.clone()]);
// let pk = child_master_key.public.q.get_element();
//
// let mut msg_buf = [0u8; 32];
// rng.fill(&mut msg_buf);
// let msg: BigInt = BigInt::from(&msg_buf[..]);
//
// let signature = ecdsa::sign(
// &client_shim,
// msg.clone(),
// &child_master_key,
// x_pos,
// y_pos,
// &ps.id,
// )
// .expect("ECDSA signature failed");
//
// let r = BigInt::to_vec(&signature.r);
// let s = BigInt::to_vec(&signature.s);
// let msg = Message::from_slice(&msg_buf).unwrap();
//
// let mut sig = [0u8; 64];
// sig[32 - r.len()..32].copy_from_slice(&r);
// sig[32 + 32 - s.len()..].copy_from_slice(&s);
//
// let sig = Signature::from_compact(&sig).unwrap();
//
// SECP256K1.verify_ecdsa(&msg, &sig, &pk).unwrap();
// }
// }

// async fn spawn_server(port: u32, db_name: &str) -> Rocket<Ignite> {
// let settings = HashMap::<String, String>::from([
// ("db".to_string(), "local".to_string()),
// ("db_name".to_string(), db_name.to_string()),
// ]);
// let rocket = server::get_server(settings);
// let figment = rocket.figment().clone().merge((Config::PORT, port));
// rocket.configure(figment).launch().await.unwrap()
// }

// #[test]
// fn test_ecdsa_keygen() {
// let settings = HashMap::<String, String>::from([
// ("db".into(), "local".into()),
// ("db_name".into(), "testEcdsaKeygen".into()),
// ]);
// let rocket = server::get_server(settings);
// let client = RocketClient::new(rocket);
//
// let client_shim =
// ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);
// for _ in 0..10 {
// let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
// let _ = ps.master_key.public.q.get_element();
// }
// }

// #[test]
// fn test_ecdsa_key_derivation() {
// let settings = HashMap::<String, String>::from([
// ("db".into(), "local".into()),
// ("db_name".into(), "testEcdsaDerivation".into()),
// ]);
// let rocket = server::get_server(settings);
// let client = RocketClient::new(rocket);
//
// let client_shim =
// ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);
// let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
// for y in 0..1 {
// let x_pos = BigInt::from(y * 2 + 1);
// let y_pos = BigInt::from(y);
// let child_master_key = ps.master_key.get_child(vec![x_pos.clone(), y_pos.clone()]);
// let _ = child_master_key.public.q.get_element();
// }
// }

#[test]
fn test_ecdsa_key_derivation() {
let settings = HashMap::<String, String>::from([
("db".into(), "local".into()),
("db_name".into(), "testEcdsaDerivation".into()),
]);
let rocket = server::get_server(settings);
let client = RocketClient::new(rocket);

let client_shim =
ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);
let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
for y in 0..10 {
let x_pos = BigInt::from(y * 2 + 1);
let y_pos = BigInt::from(y);
let child_master_key = ps.master_key.get_child(vec![x_pos.clone(), y_pos.clone()]);
let _ = child_master_key.public.q.get_element();
}
}

#[test]
fn test_ecdsa_key_signing() {
// #[rocket::async_test]
fn integration_test_ecdsa_key_signing() {
let mut rng = StepRng::new(0, 1);
let settings = HashMap::<String, String>::from([
("db".into(), "local".into()),
("db_name".into(), "testEcdsaSigning".into()),
]);
let rocket = server::get_server(settings);
let client = RocketClient::new(rocket);

let client_shim =
ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);
ClientShim::new_with_client("http://localhost:8008".to_string(), None, client);
let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
let x_pos = BigInt::from(1);
let y_pos = BigInt::from(2);
Expand Down Expand Up @@ -153,48 +153,47 @@ fn test_ecdsa_key_signing() {
}
}

#[test]
fn test_ecdsa_long() {
let mut rng = StepRng::new(0, 1);
let settings = HashMap::<String, String>::from([
("db".into(), "local".into()),
("db_name".into(), "ecdsaLong".into()),
]);
let rocket = server::get_server(settings);
let client = RocketClient::new(rocket);

let client_shim =
ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);

let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);

for y in 0..20 {
let x_pos = BigInt::from(y * 2 + 1);
let y_pos = BigInt::from(y);

let child_master_key = ps.master_key.get_child(vec![x_pos.clone(), y_pos.clone()]);
let pk = child_master_key.public.q.get_element();

let mut msg_buf = [0u8; 32];
rng.fill(&mut msg_buf);
let msg: BigInt = BigInt::from(&msg_buf[..]);

let signature = ecdsa::sign(&client_shim, msg, &child_master_key, x_pos, y_pos, &ps.id)
.expect("ECDSA signature failed");

let r = BigInt::to_vec(&signature.r);
let s = BigInt::to_vec(&signature.s);
let msg = Message::from_slice(&msg_buf).unwrap();

let mut sig = [0u8; 64];
sig[32 - r.len()..32].copy_from_slice(&r);
sig[32 + 32 - s.len()..].copy_from_slice(&s);

let sig = Signature::from_compact(&sig).unwrap();

SECP256K1.verify_ecdsa(&msg, &sig, &pk).unwrap();
}
}
// #[test]
// fn integration_test_ecdsa_long() {
// let mut rng = StepRng::new(0, 1);
// let settings = HashMap::<String, String>::from([
// ("db_name".into(), "testEcdsaLong".into()),
// ]);
// let rocket = server::get_server(settings);
// let client = RocketClient::new(rocket);
//
// let client_shim =
// ClientShim::new_with_client("http://localhost:8009".to_string(), None, client);
//
// let ps: ecdsa::PrivateShare = ecdsa::get_master_key(&client_shim);
//
// for y in 0..1 {
// let x_pos = BigInt::from(y * 2 + 1);
// let y_pos = BigInt::from(y);
//
// let child_master_key = ps.master_key.get_child(vec![x_pos.clone(), y_pos.clone()]);
// let pk = child_master_key.public.q.get_element();
//
// let mut msg_buf = [0u8; 32];
// rng.fill(&mut msg_buf);
// let msg: BigInt = BigInt::from(&msg_buf[..]);
//
// let signature = ecdsa::sign(&client_shim, msg, &child_master_key, x_pos, y_pos, &ps.id)
// .expect("ECDSA signature failed");
//
// let r = BigInt::to_vec(&signature.r);
// let s = BigInt::to_vec(&signature.s);
// let msg = Message::from_slice(&msg_buf).unwrap();
//
// let mut sig = [0u8; 64];
// sig[32 - r.len()..32].copy_from_slice(&r);
// sig[32 + 32 - s.len()..].copy_from_slice(&s);
//
// let sig = Signature::from_compact(&sig).unwrap();
//
// SECP256K1.verify_ecdsa(&msg, &sig, &pk).unwrap();
// }
// }

struct RocketClient(pub rocket::local::blocking::Client);

Expand Down

0 comments on commit 3461c9e

Please sign in to comment.