Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions crates/aptos-batch-encryption/benches/fptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub fn digest(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, _, _, _, _) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, _) = FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data: String = String::from("");
Expand All @@ -40,8 +39,7 @@ pub fn encrypt(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, _dk, _, _, _, _) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, _dk, _, _) = FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = rng
.sample_iter(&Alphanumeric)
Expand All @@ -66,8 +64,7 @@ pub fn verify_ct(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, _dk, _, _, _, _) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, _dk, _, _) = FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand All @@ -87,8 +84,7 @@ pub fn eval_proofs_compute_all(c: &mut Criterion) {
for batch_size in [32, 128, 256, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, _, _, _, _) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, _) = FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -116,8 +112,7 @@ pub fn eval_proofs_compute_all_2(c: &mut Criterion) {
for batch_size in [32, 128, 256, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, _, _, _, _) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, _) = FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -146,8 +141,8 @@ pub fn derive_decryption_key_share(c: &mut Criterion) {
let t = n * 2 / 3 + 1;
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(t, n);
let (ek, dk, _, msk_shares, _, _) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, msk_shares) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -176,8 +171,8 @@ pub fn verify_decryption_key_share(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, vks, msk_shares, _, _) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, vks, msk_shares) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -209,8 +204,8 @@ pub fn reconstruct_decryption_key(c: &mut Criterion) {
let t = n * 2 / 3 + 1;
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(t, n);
let (ek, dk, _, msk_shares, _, _) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, msk_shares) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -243,8 +238,8 @@ pub fn decrypt(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, _, msk_shares, _, _) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, msk_shares) =
FPTX::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down
36 changes: 18 additions & 18 deletions crates/aptos-batch-encryption/benches/fptx_succinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub fn digest(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, _, _, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data: String = String::from("");
Expand All @@ -40,8 +40,8 @@ pub fn encrypt(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, _dk, _, _, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, _dk, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = rng
.sample_iter(&Alphanumeric)
Expand All @@ -66,8 +66,8 @@ pub fn verify_ct(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, _dk, _, _, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, _dk, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand All @@ -87,8 +87,8 @@ pub fn eval_proofs_compute_all(c: &mut Criterion) {
for batch_size in [32, 128, 256, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, _, _, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -116,8 +116,8 @@ pub fn eval_proofs_compute_all_2(c: &mut Criterion) {
for batch_size in [32, 128, 256, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, _, _, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -148,8 +148,8 @@ pub fn derive_decryption_key_share(c: &mut Criterion) {
let t = n * 2 / 3 + 1;
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(t, n);
let (ek, dk, _, msk_shares, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, msk_shares) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -178,8 +178,8 @@ pub fn verify_decryption_key_share(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, vks, msk_shares, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, vks, msk_shares) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -211,8 +211,8 @@ pub fn reconstruct_decryption_key(c: &mut Criterion) {
let t = n * 2 / 3 + 1;
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(t, n);
let (ek, dk, _, msk_shares, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, msk_shares) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down Expand Up @@ -245,8 +245,8 @@ pub fn decrypt(c: &mut Criterion) {
for batch_size in [32, 128, 512, 2048] {
let mut rng = thread_rng();
let tc = ShamirThresholdConfig::new(1, 1);
let (ek, dk, _, msk_shares, _, _) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc, &tc).unwrap();
let (ek, dk, _, msk_shares) =
FPTXSuccinct::setup_for_testing(rng.r#gen(), batch_size, 1, &tc).unwrap();

let msg: String = String::from("hi");
let associated_data = String::from("");
Expand Down
94 changes: 18 additions & 76 deletions crates/aptos-batch-encryption/src/schemes/fptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,58 +49,37 @@ impl BatchThresholdEncryption for FPTX {
fn setup(
digest_key: &Self::DigestKey,
pvss_public_params: &<Self::SubTranscript as Subtranscript>::PublicParameters,
subtranscript_happypath: &Self::SubTranscript,
subtranscript_slowpath: &Self::SubTranscript,
tc_happypath: &Self::ThresholdConfig,
tc_slowpath: &Self::ThresholdConfig,
subtranscript: &Self::SubTranscript,
threshold_config: &Self::ThresholdConfig,
current_player: Player,
msk_share_decryption_key: &<Self::SubTranscript as Subtranscript>::DecryptPrivKey,
) -> Result<(
Self::EncryptionKey,
Vec<Self::VerificationKey>,
Self::MasterSecretKeyShare,
Vec<Self::VerificationKey>,
Self::MasterSecretKeyShare,
)> {
(subtranscript_happypath.get_dealt_public_key()
== subtranscript_slowpath.get_dealt_public_key())
.then_some(())
.ok_or(BatchEncryptionError::HappySlowPathMismatchError)?;

let mpk_g2: G2Affine = subtranscript_happypath.get_dealt_public_key().as_g2();
let mpk_g2: G2Affine = subtranscript.get_dealt_public_key().as_g2();

let ek = EncryptionKey::new(mpk_g2, digest_key.tau_g2);

let vks_happypath: Vec<Self::VerificationKey> = tc_happypath
let vks: Vec<Self::VerificationKey> = threshold_config
.get_players()
.into_iter()
.map(|p| Self::VerificationKey {
player: p,
mpk_g2,
vk_g2: subtranscript_happypath
.get_public_key_share(tc_happypath, &p)
vk_g2: subtranscript
.get_public_key_share(threshold_config, &p)
.as_g2(),
})
.collect();

let vks_slowpath: Vec<Self::VerificationKey> = tc_slowpath
.get_players()
.into_iter()
.map(|p| Self::VerificationKey {
player: p,
mpk_g2,
vk_g2: subtranscript_slowpath
.get_public_key_share(tc_slowpath, &p)
.as_g2(),
})
.collect();

let msk_share_happypath = Self::MasterSecretKeyShare {
let msk_share = Self::MasterSecretKeyShare {
mpk_g2,
player: current_player,
shamir_share_eval: subtranscript_happypath
shamir_share_eval: subtranscript
.decrypt_own_share(
tc_happypath,
threshold_config,
&current_player,
msk_share_decryption_key,
pvss_public_params,
Expand All @@ -109,76 +88,39 @@ impl BatchThresholdEncryption for FPTX {
.into_fr(),
};

let msk_share_slowpath = Self::MasterSecretKeyShare {
mpk_g2,
player: current_player,
shamir_share_eval: subtranscript_slowpath
.decrypt_own_share(
tc_slowpath,
&current_player,
msk_share_decryption_key,
pvss_public_params,
)
.0
.into_fr(),
};
(vks[msk_share.player.get_id()].vk_g2
== G2Affine::generator() * msk_share.shamir_share_eval)
.then_some(())
.ok_or(BatchEncryptionError::VKMSKMismatchError)?;

for (vks, msk_share) in [
(&vks_happypath, &msk_share_happypath),
(&vks_slowpath, &msk_share_slowpath),
] {
(vks[msk_share.player.get_id()].vk_g2
== G2Affine::generator() * msk_share.shamir_share_eval)
.then_some(())
.ok_or(BatchEncryptionError::VKMSKMismatchError)?;
}

Ok((
ek,
vks_happypath,
msk_share_happypath,
vks_slowpath,
msk_share_slowpath,
))
Ok((ek, vks, msk_share))
}

fn setup_for_testing(
seed: u64,
max_batch_size: usize,
number_of_rounds: usize,
tc_happypath: &Self::ThresholdConfig,
tc_slowpath: &Self::ThresholdConfig,
threshold_config: &Self::ThresholdConfig,
) -> Result<(
Self::EncryptionKey,
Self::DigestKey,
Vec<Self::VerificationKey>,
Vec<Self::MasterSecretKeyShare>,
Vec<Self::VerificationKey>,
Vec<Self::MasterSecretKeyShare>,
)> {
let mut rng = <StdRng as SeedableRng>::seed_from_u64(seed);

let digest_key = DigestKey::new(&mut rng, max_batch_size, number_of_rounds)
.ok_or(anyhow!("Failed to create digest key"))?;
let msk = Fr::rand(&mut rng);
let (mpk, vks_happypath, msk_shares_happypath) =
key_derivation::gen_msk_shares(msk, &mut rng, tc_happypath);
let (_, vks_slowpath, msk_shares_slowpath) =
key_derivation::gen_msk_shares(msk, &mut rng, tc_slowpath);
let (mpk, vks, msk_shares) =
key_derivation::gen_msk_shares(msk, &mut rng, threshold_config);

let ek = EncryptionKey {
sig_mpk_g2: mpk.0,
tau_g2: digest_key.tau_g2,
};

Ok((
ek,
digest_key,
vks_happypath,
msk_shares_happypath,
vks_slowpath,
msk_shares_slowpath,
))
Ok((ek, digest_key, vks, msk_shares))
}

fn encrypt<R: CryptoRng + RngCore>(
Expand Down
Loading
Loading