Skip to content

Commit

Permalink
feat: rename group_threshold_cryptography_pre_release crate to ferveo…
Browse files Browse the repository at this point in the history
…_tpke
  • Loading branch information
piotr-roslaniec committed Oct 31, 2023
1 parent 52efe01 commit 40cf1c3
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 86 deletions.
58 changes: 29 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ferveo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ark-serialize = "0.4"
ark-std = "0.4"
bincode = "1.3"
ferveo-common = { package = "ferveo-common-pre-release", path = "../ferveo-common", version = "^0.1.1" }
group-threshold-cryptography = { package = "group-threshold-cryptography-pre-release", path = "../tpke", features = ["api", "test-common"], version = "^0.2.0" }
ferveo-tpke = { package = "ferveo-tpke", path = "../tpke", features = ["api", "test-common"], version = "^0.2.0" }
hex = "0.4.3"
itertools = "0.10.5"
measure_time = "0.8"
Expand Down
27 changes: 14 additions & 13 deletions ferveo/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::UniformRand;
use bincode;
use ferveo_common::serialization;
pub use ferveo_tpke::api::{
prepare_combine_simple, share_combine_precomputed, share_combine_simple,
Fr, G1Affine, G1Prepared, G2Affine, SecretBox, E,
};
use generic_array::{typenum::U48, GenericArray};
use group_threshold_cryptography as tpke;
use rand::RngCore;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
pub use tpke::api::{
prepare_combine_simple, share_combine_precomputed, share_combine_simple,
Fr, G1Affine, G1Prepared, G2Affine, SecretBox, E,
};

pub type PublicKey = ferveo_common::PublicKey<E>;
pub type Keypair = ferveo_common::Keypair<E>;
Expand All @@ -31,7 +30,8 @@ use crate::{
PubliclyVerifiableSS, Result,
};

pub type DecryptionSharePrecomputed = tpke::api::DecryptionSharePrecomputed;
pub type DecryptionSharePrecomputed =
ferveo_tpke::api::DecryptionSharePrecomputed;

// Normally, we would use a custom trait for this, but we can't because
// the arkworks will not let us create a blanket implementation for G1Affine
Expand All @@ -54,7 +54,8 @@ pub fn encrypt(
pubkey: &DkgPublicKey,
) -> Result<Ciphertext> {
let mut rng = rand::thread_rng();
let ciphertext = tpke::api::encrypt(message, aad, &pubkey.0, &mut rng)?;
let ciphertext =
ferveo_tpke::api::encrypt(message, aad, &pubkey.0, &mut rng)?;
Ok(Ciphertext(ciphertext))
}

Expand All @@ -64,7 +65,7 @@ pub fn decrypt_with_shared_secret(
shared_secret: &SharedSecret,
) -> Result<Vec<u8>> {
let dkg_public_params = DkgPublicParameters::default();
tpke::api::decrypt_with_shared_secret(
ferveo_tpke::api::decrypt_with_shared_secret(
&ciphertext.0,
aad,
&shared_secret.0,
Expand All @@ -74,7 +75,7 @@ pub fn decrypt_with_shared_secret(
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Eq)]
pub struct Ciphertext(tpke::api::Ciphertext);
pub struct Ciphertext(ferveo_tpke::api::Ciphertext);

impl Ciphertext {
pub fn header(&self) -> Result<CiphertextHeader> {
Expand All @@ -88,7 +89,7 @@ impl Ciphertext {

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct CiphertextHeader(tpke::api::CiphertextHeader);
pub struct CiphertextHeader(ferveo_tpke::api::CiphertextHeader);

/// The ferveo variant to use for the decryption share derivation.
#[derive(
Expand Down Expand Up @@ -347,7 +348,7 @@ impl AggregatedTranscript {
#[serde_as]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DecryptionShareSimple {
share: tpke::api::DecryptionShareSimple,
share: ferveo_tpke::api::DecryptionShareSimple,
#[serde_as(as = "serialization::SerdeAs")]
domain_point: Fr,
}
Expand Down Expand Up @@ -389,13 +390,13 @@ pub fn combine_shares_simple(shares: &[DecryptionShareSimple]) -> SharedSecret {
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SharedSecret(pub tpke::api::SharedSecret<E>);
pub struct SharedSecret(pub ferveo_tpke::api::SharedSecret<E>);

#[cfg(test)]
mod test_ferveo_api {
use ferveo_tpke::SecretBox;
use itertools::izip;
use rand::{prelude::StdRng, SeedableRng};
use tpke::SecretBox;

use crate::{api::*, dkg::test_common::*};

Expand Down
8 changes: 4 additions & 4 deletions ferveo/src/bindings_wasm.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
extern crate group_threshold_cryptography as tpke;

use std::{
convert::{TryFrom, TryInto},
fmt,
str::FromStr,
};

use ferveo_common::{FromBytes, ToBytes};
use ferveo_tpke::SecretBox;
use js_sys::Error;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use tpke::SecretBox;
use wasm_bindgen::prelude::*;
use wasm_bindgen_derive::TryFromJsValue;

Expand Down Expand Up @@ -202,7 +200,9 @@ generate_common_methods!(DecryptionShareSimple);
#[derive(TryFromJsValue)]
#[wasm_bindgen]
#[derive(Clone, Debug, derive_more::AsRef, derive_more::From)]
pub struct DecryptionSharePrecomputed(tpke::api::DecryptionSharePrecomputed);
pub struct DecryptionSharePrecomputed(
ferveo_tpke::api::DecryptionSharePrecomputed,
);

generate_common_methods!(DecryptionSharePrecomputed);

Expand Down
71 changes: 42 additions & 29 deletions ferveo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
extern crate alloc;

use ark_ec::pairing::Pairing;
use group_threshold_cryptography as tpke;
use itertools::zip_eq;

#[cfg(feature = "bindings-python")]
Expand All @@ -31,7 +30,7 @@ pub use validator::*;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
ThresholdEncryptionError(#[from] tpke::Error),
ThresholdEncryptionError(#[from] ferveo_tpke::Error),

/// DKG is not in a valid state to deal PVSS shares
#[error("Invalid DKG state to deal PVSS shares")]
Expand Down Expand Up @@ -127,9 +126,8 @@ mod test_dkg_full {
use ark_poly::EvaluationDomain;
use ark_std::test_rng;
use ferveo_common::Keypair;
use group_threshold_cryptography as tpke;
use group_threshold_cryptography::{
DecryptionSharePrecomputed, DecryptionShareSimple, SecretBox,
use ferveo_tpke::{
self, DecryptionSharePrecomputed, DecryptionShareSimple, SecretBox,
SharedSecret,
};
use itertools::izip;
Expand All @@ -142,7 +140,7 @@ mod test_dkg_full {
fn make_shared_secret_simple_tdec(
dkg: &PubliclyVerifiableDkg<E>,
aad: &[u8],
ciphertext_header: &tpke::CiphertextHeader<E>,
ciphertext_header: &ferveo_tpke::CiphertextHeader<E>,
validator_keypairs: &[Keypair<E>],
) -> (
PubliclyVerifiableSS<E, Aggregated>,
Expand Down Expand Up @@ -178,11 +176,12 @@ mod test_dkg_full {
.collect::<Vec<_>>();
assert_eq!(domain_points.len(), decryption_shares.len());

// TODO: Consider refactor this part into tpke::combine_simple and expose it
// as a public API in tpke::api
// TODO: Consider refactor this part into ferveo_tpke::combine_simple and expose it
// as a public API in ferveo_tpke::api

let lagrange_coeffs = tpke::prepare_combine_simple::<E>(domain_points);
let shared_secret = tpke::share_combine_simple::<E>(
let lagrange_coeffs =
ferveo_tpke::prepare_combine_simple::<E>(domain_points);
let shared_secret = ferveo_tpke::share_combine_simple::<E>(
&decryption_shares,
&lagrange_coeffs,
);
Expand All @@ -202,7 +201,7 @@ mod test_dkg_full {
let msg = "my-msg".as_bytes().to_vec();
let aad: &[u8] = "my-aad".as_bytes();
let public_key = dkg.public_key();
let ciphertext = tpke::encrypt::<E>(
let ciphertext = ferveo_tpke::encrypt::<E>(
SecretBox::new(msg.clone()),
aad,
&public_key,
Expand All @@ -217,7 +216,7 @@ mod test_dkg_full {
validator_keypairs.as_slice(),
);

let plaintext = tpke::decrypt_with_shared_secret(
let plaintext = ferveo_tpke::decrypt_with_shared_secret(
&ciphertext,
aad,
&shared_secret,
Expand All @@ -241,7 +240,7 @@ mod test_dkg_full {
let msg = "my-msg".as_bytes().to_vec();
let aad: &[u8] = "my-aad".as_bytes();
let public_key = dkg.public_key();
let ciphertext = tpke::encrypt::<E>(
let ciphertext = ferveo_tpke::encrypt::<E>(
SecretBox::new(msg.clone()),
aad,
&public_key,
Expand Down Expand Up @@ -279,10 +278,10 @@ mod test_dkg_full {
assert_eq!(domain_points.len(), decryption_shares.len());

let shared_secret =
tpke::share_combine_precomputed::<E>(&decryption_shares);
ferveo_tpke::share_combine_precomputed::<E>(&decryption_shares);

// Combination works, let's decrypt
let plaintext = tpke::decrypt_with_shared_secret(
let plaintext = ferveo_tpke::decrypt_with_shared_secret(
&ciphertext,
aad,
&shared_secret,
Expand All @@ -301,9 +300,13 @@ mod test_dkg_full {
let msg = "my-msg".as_bytes().to_vec();
let aad: &[u8] = "my-aad".as_bytes();
let public_key = dkg.public_key();
let ciphertext =
tpke::encrypt::<E>(SecretBox::new(msg), aad, &public_key, rng)
.unwrap();
let ciphertext = ferveo_tpke::encrypt::<E>(
SecretBox::new(msg),
aad,
&public_key,
rng,
)
.unwrap();

let (pvss_aggregated, decryption_shares, _) =
make_shared_secret_simple_tdec(
Expand Down Expand Up @@ -364,9 +367,13 @@ mod test_dkg_full {
let msg = "my-msg".as_bytes().to_vec();
let aad: &[u8] = "my-aad".as_bytes();
let public_key = &dkg.public_key();
let ciphertext =
tpke::encrypt::<E>(SecretBox::new(msg), aad, public_key, rng)
.unwrap();
let ciphertext = ferveo_tpke::encrypt::<E>(
SecretBox::new(msg),
aad,
public_key,
rng,
)
.unwrap();

// Create an initial shared secret
let (_, _, old_shared_secret) = make_shared_secret_simple_tdec(
Expand Down Expand Up @@ -496,9 +503,11 @@ mod test_dkg_full {
assert_eq!(domain_points.len(), security_threshold as usize);
assert_eq!(decryption_shares.len(), security_threshold as usize);

let lagrange = tpke::prepare_combine_simple::<E>(domain_points);
let new_shared_secret =
tpke::share_combine_simple::<E>(decryption_shares, &lagrange);
let lagrange = ferveo_tpke::prepare_combine_simple::<E>(domain_points);
let new_shared_secret = ferveo_tpke::share_combine_simple::<E>(
decryption_shares,
&lagrange,
);

assert_eq!(
old_shared_secret, new_shared_secret,
Expand All @@ -517,9 +526,13 @@ mod test_dkg_full {
let msg = "my-msg".as_bytes().to_vec();
let aad: &[u8] = "my-aad".as_bytes();
let public_key = &dkg.public_key();
let ciphertext =
tpke::encrypt::<E>(SecretBox::new(msg), aad, public_key, rng)
.unwrap();
let ciphertext = ferveo_tpke::encrypt::<E>(
SecretBox::new(msg),
aad,
public_key,
rng,
)
.unwrap();

// Create an initial shared secret
let (_, _, old_shared_secret) = make_shared_secret_simple_tdec(
Expand Down Expand Up @@ -594,10 +607,10 @@ mod test_dkg_full {
})
.collect();

let lagrange = tpke::prepare_combine_simple::<E>(
let lagrange = ferveo_tpke::prepare_combine_simple::<E>(
&domain_points[..security_threshold as usize],
);
let new_shared_secret = tpke::share_combine_simple::<E>(
let new_shared_secret = ferveo_tpke::share_combine_simple::<E>(
&decryption_shares[..security_threshold as usize],
&lagrange,
);
Expand Down
Loading

0 comments on commit 40cf1c3

Please sign in to comment.