Skip to content

Commit

Permalink
Add string <=> secret helper fn
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexiaChen committed Mar 3, 2021
1 parent 9bdf144 commit 56a94f5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 44 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ At first we convert our secret message into a numeric value if necessary. When c

```rust
let secret_message = String::from("Hello MPVSS.");
let secret = BigUint::from_bytes_be(&secret_message.as_bytes());

let mut dealer = Participant::new();
dealer.initialize();
Expand All @@ -62,7 +61,7 @@ The dealer splits the secret into shares, encrypts them and creates a proof so t
```rust
// Dealer that shares the secret among p1, p2 and p3.
let distribute_shares_box = dealer.distribute_secret(
secret.to_bigint().unwrap(),
string_to_secret(&secret_message),
vec![p1.publickey, p2.publickey, p3.publickey],
3,
);
Expand Down Expand Up @@ -132,11 +131,11 @@ let r3 = p3
.reconstruct(&share_boxs, &distribute_shares_box)
.unwrap();

let r1_str = String::from_utf8(r1.to_biguint().unwrap().to_bytes_be()).unwrap();
let r1_str = string_from_secret(&r1);
assert_eq!(secret_message.clone(), r1_str);
let r2_str = String::from_utf8(r2.to_biguint().unwrap().to_bytes_be()).unwrap();
let r2_str = string_from_secret(&r2);
assert_eq!(secret_message.clone(), r2_str);
let r3_str = String::from_utf8(r3.to_biguint().unwrap().to_bytes_be()).unwrap();
let r3_str = string_from_secret(&r3);
assert_eq!(secret_message.clone(), r3_str);
```

Expand Down
14 changes: 5 additions & 9 deletions examples/mpvss_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// Code is licensed under GPLv3.0 License.

use mpvss_rs::Participant;
use num_bigint::{BigUint, ToBigInt};
use mpvss_rs::{string_from_secret, string_to_secret};

fn main() {
let secret_message = String::from("Hello MPVSS Example.");
let secret = BigUint::from_bytes_be(&secret_message.as_bytes());
let mut dealer = Participant::new();
dealer.initialize();
let mut p1 = Participant::new();
Expand All @@ -18,7 +17,7 @@ fn main() {
p3.initialize();

let distribute_shares_box = dealer.distribute_secret(
secret.to_bigint().unwrap(),
string_to_secret(&secret_message),
vec![
p1.publickey.clone(),
p2.publickey.clone(),
Expand Down Expand Up @@ -69,14 +68,11 @@ fn main() {
let r2 = p2.reconstruct(&share_boxs, &distribute_shares_box).unwrap();
let r3 = p3.reconstruct(&share_boxs, &distribute_shares_box).unwrap();

let r1_str =
String::from_utf8(r1.to_biguint().unwrap().to_bytes_be()).unwrap();
let r1_str = string_from_secret(&r1);
assert_eq!(secret_message.clone(), r1_str);
let r2_str =
String::from_utf8(r2.to_biguint().unwrap().to_bytes_be()).unwrap();
let r2_str = string_from_secret(&r2);
assert_eq!(secret_message.clone(), r2_str);
let r3_str =
String::from_utf8(r3.to_biguint().unwrap().to_bytes_be()).unwrap();
let r3_str = string_from_secret(&r3);
assert_eq!(secret_message.clone(), r3_str);

println!("secret message: {}", secret_message);
Expand Down
17 changes: 6 additions & 11 deletions examples/mpvss_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// Code is licensed under GPLv3.0 License.

use mpvss_rs::Participant;
use num_bigint::{BigUint, ToBigInt};
use mpvss_rs::{string_from_secret, string_to_secret};

fn main() {
let secret_message = String::from("Hello Sub MPVSS Example.");
let secret = BigUint::from_bytes_be(&secret_message.as_bytes());
let mut dealer = Participant::new();
dealer.initialize();
let mut p1 = Participant::new();
Expand All @@ -20,7 +19,7 @@ fn main() {
p4.initialize();

let distribute_shares_box = dealer.distribute_secret(
secret.to_bigint().unwrap(),
string_to_secret(&secret_message),
vec![
p1.publickey.clone(),
p2.publickey.clone(),
Expand Down Expand Up @@ -84,17 +83,13 @@ fn main() {
let r3 = p3.reconstruct(&share_boxs, &distribute_shares_box).unwrap();
let r4 = p4.reconstruct(&share_boxs, &distribute_shares_box).unwrap();

let r1_str =
String::from_utf8(r1.to_biguint().unwrap().to_bytes_be()).unwrap();
let r1_str = string_from_secret(&r1);
assert_eq!(secret_message.clone(), r1_str);
let r2_str =
String::from_utf8(r2.to_biguint().unwrap().to_bytes_be()).unwrap();
let r2_str = string_from_secret(&r2);
assert_eq!(secret_message.clone(), r2_str);
let r3_str =
String::from_utf8(r3.to_biguint().unwrap().to_bytes_be()).unwrap();
let r3_str = string_from_secret(&r3);
assert_eq!(secret_message.clone(), r3_str);
let r4_str =
String::from_utf8(r4.to_biguint().unwrap().to_bytes_be()).unwrap();
let r4_str = string_from_secret(&r4);
assert_eq!(secret_message.clone(), r4_str);

println!("secret message: {}", secret_message);
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ mod util;

pub use participant::Participant;
pub use sharebox::{DistributionSharesBox, ShareBox};

use num_bigint::{BigInt, BigUint, ToBigInt};

pub fn string_to_secret(message: &str) -> BigInt {
BigUint::from_bytes_be(&message.as_bytes())
.to_bigint()
.unwrap()
}

pub fn string_from_secret(secret: &BigInt) -> String {
String::from_utf8(secret.to_biguint().unwrap().to_bytes_be()).unwrap()
}
24 changes: 5 additions & 19 deletions tests/mpvss_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@
// Code is licensed under GPLv3.0 License.

use mpvss_rs::Participant;
use num_bigint::{BigUint, ToBigInt};

#[test]
fn test_secret_str_utf8() {
let secret_message = String::from("Hello MPVSS.");
let secret = BigUint::from_bytes_be(&secret_message.as_bytes());
assert_eq!(
secret_message,
String::from_utf8(secret.to_bytes_be()).unwrap()
);
}
use mpvss_rs::{string_from_secret, string_to_secret};

#[test]
fn test_mpvss_distribute_verify_reconstruct() {
let secret_message = String::from("Hello MPVSS.");
let secret = BigUint::from_bytes_be(&secret_message.as_bytes());
let mut dealer = Participant::new();
dealer.initialize();
let mut p1 = Participant::new();
Expand All @@ -29,7 +18,7 @@ fn test_mpvss_distribute_verify_reconstruct() {
p3.initialize();

let distribute_shares_box = dealer.distribute_secret(
secret.to_bigint().unwrap(),
string_to_secret(&secret_message),
vec![
p1.publickey.clone(),
p2.publickey.clone(),
Expand Down Expand Up @@ -80,13 +69,10 @@ fn test_mpvss_distribute_verify_reconstruct() {
let r2 = p2.reconstruct(&share_boxs, &distribute_shares_box).unwrap();
let r3 = p3.reconstruct(&share_boxs, &distribute_shares_box).unwrap();

let r1_str =
String::from_utf8(r1.to_biguint().unwrap().to_bytes_be()).unwrap();
let r1_str = string_from_secret(&r1);
assert_eq!(secret_message.clone(), r1_str);
let r2_str =
String::from_utf8(r2.to_biguint().unwrap().to_bytes_be()).unwrap();
let r2_str = string_from_secret(&r2);
assert_eq!(secret_message.clone(), r2_str);
let r3_str =
String::from_utf8(r3.to_biguint().unwrap().to_bytes_be()).unwrap();
let r3_str = string_from_secret(&r3);
assert_eq!(secret_message.clone(), r3_str);
}

0 comments on commit 56a94f5

Please sign in to comment.