Skip to content

Commit

Permalink
Merge pull request anoma#20 from piotr-roslaniec/simple-decryption
Browse files Browse the repository at this point in the history
Closes anoma#5
  • Loading branch information
piotr-roslaniec committed Jan 11, 2023
2 parents f26552d + 6dc7173 commit b2b4809
Show file tree
Hide file tree
Showing 17 changed files with 461 additions and 157 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/ferveo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: ferveo

on:
pull_request:
push:
branches:
- main
tags:
- v*

env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: "-Dwarnings"

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- target: x86_64-unknown-linux-gnu
rust: 1.63 # MSRV, `cargo msrv`
- target: x86_64-unknown-linux-gnu
rust: stable
steps:
- uses: actions/checkout@v1
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
override: true
- run: ${{ matrix.deps }}
- run: cargo check --all-features
- run: cargo test --release --all-features
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions ferveo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ark-ff = "0.3"
ark-serialize = "0.3"
ark-poly = "0.3"
rand = "0.8"
rand_old = { package = "rand", version = "0.7" } # used by benchmarks/pairing.rs
either = "1.6.1"
hex = "0.4.2"
miracl_core = "2.3.0"
Expand Down
1 change: 1 addition & 0 deletions ferveo/benches/benchmarks/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ pub fn bench_batch_inverse(c: &mut Criterion) {
criterion::BenchmarkId::new("BLS12-381 Batch inverse", n),
&a,
|b, a| {
#[allow(clippy::unit_arg)]
b.iter(|| black_box(ark_ff::batch_inversion(&mut a.clone())));
},
);
Expand Down
2 changes: 1 addition & 1 deletion ferveo/benches/benchmarks/pvdkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn gen_validators(
.map(|i| TendermintValidator {
power: i as u64,
address: format!("validator_{}", i),
public_key: keypairs[i as usize].public(),
public_key: keypairs[i].public(),
})
.collect(),
)
Expand Down
2 changes: 1 addition & 1 deletion ferveo/examples/pvdkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn gen_validators(
.map(|i| TendermintValidator {
power: i as u64,
address: format!("validator_{}", i),
public_key: keypairs[i as usize].public(),
public_key: keypairs[i].public(),
})
.collect(),
)
Expand Down
4 changes: 2 additions & 2 deletions ferveo/src/dkg/pv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ impl<E: PairingEngine> PubliclyVerifiableDkg<E> {
/// Returns the public key generated by the DKG
pub fn final_key(&self) -> E::G1Affine {
self.vss
.iter()
.map(|(_, vss)| vss.coeffs[0].into_projective())
.values()
.map(|vss| vss.coeffs[0].into_projective())
.sum::<E::G1Projective>()
.into_affine()
}
Expand Down
31 changes: 13 additions & 18 deletions tpke-wasm/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
pub fn bench_encrypt_combine(c: &mut Criterion) {
use tpke_wasm::*;

fn bench_encrypt(
num_shares: usize,
num_entities: usize,
threshold: usize,
) -> impl Fn() {
fn bench_encrypt(num_shares: usize, threshold: usize) -> impl Fn() {
let message = "my-secret-message".as_bytes().to_vec();
let aad = "my-aad".as_bytes().to_vec();
let setup = Setup::new(threshold, num_shares, num_entities);
let setup = Setup::new(threshold, num_shares);
move || {
let message = message.clone();
let aad = aad.clone();
black_box(encrypt(&message, &aad, &setup.public_key));
}
}

fn bench_combine(
num_shares: usize,
num_entities: usize,
threshold: usize,
) -> impl Fn() {
fn bench_combine(num_shares: usize, threshold: usize) -> impl Fn() {
let message = "my-secret-message".as_bytes().to_vec();
let aad = "my-aad".as_bytes().to_vec();
let setup = Setup::new(threshold, num_shares, num_entities);
let setup = Setup::new(threshold, num_shares);
let ciphertext = encrypt(&message.to_vec(), &aad, &setup.public_key);
let participant_payloads: Vec<ParticipantPayload> = setup
.decrypter_indexes()
Expand All @@ -45,6 +37,7 @@ pub fn bench_encrypt_combine(c: &mut Criterion) {
move || {
let setup = setup.clone();
let decryption_shares = decryption_shares.clone();
#[allow(clippy::unit_arg)]
black_box({
let mut ss_builder = SharedSecretBuilder::new(&setup);
for share in decryption_shares {
Expand All @@ -59,17 +52,19 @@ pub fn bench_encrypt_combine(c: &mut Criterion) {
group.sample_size(10);

for num_shares in [8, 16, 32, 64, 128].iter() {
let encrypt_fn = bench_encrypt(*num_shares, *num_shares, *num_shares);
let encrypt_fn = bench_encrypt(*num_shares, *num_shares);
group.measurement_time(core::time::Duration::new(30, 0));
group.bench_function(format!("tpke-wasm::encrypt - num_shares={}, num_entities={}, threshold={}", num_shares, num_shares, num_shares), |b| {
b.iter(|| encrypt_fn())
});
#[allow(clippy::redundant_closure)]
b.iter(|| encrypt_fn())
});

let combine_fn = bench_combine(*num_shares, *num_shares, *num_shares);
let combine_fn = bench_combine(*num_shares, *num_shares);
group.measurement_time(core::time::Duration::new(30, 0));
group.bench_function(format!("tpke-wasm::combine - num_shares={}, num_entities={}, threshold={}", num_shares, num_shares, num_shares), |b| {
b.iter(|| combine_fn())
});
#[allow(clippy::redundant_closure)]
b.iter(|| combine_fn())
});
}
}

Expand Down
25 changes: 13 additions & 12 deletions tpke-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub type E = ark_bls12_381::Bls12_381;
pub type TpkePublicKey = ark_bls12_381::G1Affine;
pub type TpkePrivateKey = ark_bls12_381::G2Affine;
pub type TpkeCiphertext = tpke::Ciphertext<E>;
pub type TpkeDecryptionShare = tpke::DecryptionShare<E>;
pub type TpkePublicDecryptionContext = tpke::PublicDecryptionContext<E>;
pub type TpkeDecryptionShare = tpke::DecryptionShareFast<E>;
pub type TpkePublicDecryptionContext = tpke::PublicDecryptionContextFast<E>;
pub type TpkeSharedSecret =
<ark_bls12_381::Bls12_381 as ark_ec::PairingEngine>::Fqk;

Expand Down Expand Up @@ -163,22 +163,21 @@ pub struct Setup {
#[wasm_bindgen]
impl Setup {
#[wasm_bindgen(constructor)]
pub fn new(
threshold: usize,
shares_num: usize,
num_entities: usize,
) -> Self {
pub fn new(threshold: usize, shares_num: usize) -> Self {
set_panic_hook();

let mut rng = rand::thread_rng();
let (public_key, private_key, contexts) =
tpke::setup::<E>(threshold, shares_num, num_entities, &mut rng);
tpke::setup_fast::<E>(threshold, shares_num, &mut rng);
let private_contexts = contexts
.clone()
.into_iter()
.map(|x| {
PrivateDecryptionContext(
tpke::api::PrivateDecryptionContext::new(&x.b_inv, x.index),
tpke::api::PrivateDecryptionContext::new(
&x.setup_params.b_inv,
x.index,
),
)
})
.collect();
Expand Down Expand Up @@ -286,9 +285,11 @@ impl SharedSecretBuilder {
}

let prepared_blinded_key_shares =
tpke::prepare_combine(&self.contexts, &self.shares);
let shared_secret =
tpke::share_combine(&self.shares, &prepared_blinded_key_shares);
tpke::prepare_combine_fast(&self.contexts, &self.shares);
let shared_secret = tpke::share_combine_fast(
&self.shares,
&prepared_blinded_key_shares,
);
SharedSecret(shared_secret)
}
}
Expand Down
9 changes: 3 additions & 6 deletions tpke-wasm/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ pub fn participant_payload_serialization() {
// TODO: Build a ciphertext from scratch
let threshold = 3;
let shares_num = 5;
let num_entities = 5;
let message = "my-secret-message".as_bytes().to_vec();
let aad = "my-aad".as_bytes().to_vec();
let setup = Setup::new(threshold, shares_num, num_entities);
let setup = Setup::new(threshold, shares_num);
let ciphertext = encrypt(&message, &aad, &setup.public_key);

let participant_payload =
Expand All @@ -34,11 +33,10 @@ pub fn participant_payload_serialization() {
fn encrypts_and_decrypts() {
let threshold = 3;
let shares_num = 5;
let num_entities = 5;
let message = "my-secret-message".as_bytes().to_vec();
let aad = "my-aad".as_bytes().to_vec();

let setup = Setup::new(threshold, shares_num, num_entities);
let setup = Setup::new(threshold, shares_num);

let ciphertext = encrypt(&message, &aad, &setup.public_key);
let plaintext = decrypt(&ciphertext, &setup.private_key);
Expand All @@ -52,7 +50,6 @@ fn encrypts_and_decrypts() {
fn threshold_encryption() {
let threshold = 16 * 2 / 3;
let shares_num = 16;
let num_entities = 5;
let message = "my-secret-message".as_bytes().to_vec();
let aad = "my-aad".as_bytes().to_vec();

Expand All @@ -61,7 +58,7 @@ fn threshold_encryption() {
//

// Initialize the DKG setup
let setup = Setup::new(threshold, shares_num, num_entities);
let setup = Setup::new(threshold, shares_num);

// Encrypt the message
let ciphertext = encrypt(&message, &aad, &setup.public_key);
Expand Down
23 changes: 12 additions & 11 deletions tpke/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ pub fn bench_decryption(c: &mut Criterion) {
let threshold = num_shares * 2 / 3;

let (pubkey, _, contexts) =
setup::<E>(threshold, num_shares, num_entities, &mut rng);
setup_fast::<E>(threshold, num_shares, &mut rng);

// let mut messages: Vec<[u8; NUM_OF_TX]> = vec![];
let mut messages: Vec<Vec<u8>> = vec![];
let mut ciphertexts: Vec<Ciphertext<E>> = vec![];
let mut dec_shares: Vec<Vec<DecryptionShare<E>>> =
let mut dec_shares: Vec<Vec<DecryptionShareFast<E>>> =
Vec::with_capacity(ciphertexts.len());
for j in 0..num_msg {
// let mut msg: [u8; NUM_OF_TX] = [0u8; NUM_OF_TX];
Expand All @@ -41,16 +41,16 @@ pub fn bench_decryption(c: &mut Criterion) {
dec_shares[j].push(ctx.create_share(&ciphertexts[j]));
}
}
let prepared_blinded_key_shares = prepare_combine(
let prepared_blinded_key_shares = prepare_combine_fast(
&contexts[0].public_decryption_contexts,
&dec_shares[0],
);

move || {
let shares: Vec<Vec<DecryptionShare<E>>> = dec_shares.clone();
let shares: Vec<Vec<DecryptionShareFast<E>>> = dec_shares.clone();

for i in 0..ciphertexts.len() {
black_box(share_combine(
black_box(share_combine_fast(
&shares[i],
&prepared_blinded_key_shares,
));
Expand All @@ -71,12 +71,12 @@ pub fn bench_decryption(c: &mut Criterion) {
let threshold = num_shares * 2 / 3;

let (pubkey, _, contexts) =
setup::<E>(threshold, num_shares, num_entities, &mut rng);
setup_fast::<E>(threshold, num_shares, &mut rng);

// let mut messages: Vec<[u8; NUM_OF_TX]> = vec![];
let mut messages: Vec<Vec<u8>> = vec![];
let mut ciphertexts: Vec<Ciphertext<E>> = vec![];
let mut dec_shares: Vec<Vec<DecryptionShare<E>>> =
let mut dec_shares: Vec<Vec<DecryptionShareFast<E>>> =
Vec::with_capacity(ciphertexts.len());
for j in 0..num_msg {
// let mut msg: [u8; NUM_OF_TX] = [0u8; NUM_OF_TX];
Expand All @@ -95,16 +95,16 @@ pub fn bench_decryption(c: &mut Criterion) {
move || {
let rng = &mut ark_std::test_rng();
let c: Vec<Ciphertext<E>> = ciphertexts.clone();
let shares: Vec<Vec<DecryptionShare<E>>> = dec_shares.clone();
let shares: Vec<Vec<DecryptionShareFast<E>>> = dec_shares.clone();

contexts[0].batch_verify_decryption_shares(&c, &shares, rng);
let prepared_blinded_key_shares = prepare_combine(
let prepared_blinded_key_shares = prepare_combine_fast(
&contexts[0].public_decryption_contexts,
&dec_shares[0],
);

for i in 0..ciphertexts.len() {
black_box(share_combine(
black_box(share_combine_fast(
&shares[i],
&prepared_blinded_key_shares,
));
Expand All @@ -128,7 +128,8 @@ pub fn bench_decryption(c: &mut Criterion) {
);
group.measurement_time(core::time::Duration::new(30, 0));
group.bench_function(format!("share_combine: {} validators threshold {}*2/3 - #msg {} - msg-size = {} bytes", num_validators, num_shares, msg_num, msg_size), |b| {
b.iter(|| a())
#[allow(clippy::redundant_closure)]
b.iter(|| a())
});

/* let a = block_propose_bench(msg_num, num_shares, 150, msg_size);
Expand Down
4 changes: 2 additions & 2 deletions tpke/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type E = ark_bls12_381::Bls12_381;
type TpkePublicKey = ark_bls12_381::G1Affine;
type TpkePrivateKey = ark_bls12_381::G2Affine;
type TpkeCiphertext = crate::Ciphertext<E>;
type TpkeDecryptionShare = crate::DecryptionShare<E>;
type TpkePublicDecryptionContext = crate::PublicDecryptionContext<E>;
type TpkeDecryptionShare = crate::DecryptionShareFast<E>;
type TpkePublicDecryptionContext = crate::PublicDecryptionContextFast<E>;
type TpkeSharedSecret =
<ark_bls12_381::Bls12_381 as ark_ec::PairingEngine>::Fqk;

Expand Down
Loading

0 comments on commit b2b4809

Please sign in to comment.