Skip to content

Commit

Permalink
feat: decouple deps and remove bls_dkg
Browse files Browse the repository at this point in the history
-- Change 1: re-export blsttc, blst_ringct --

The idea is to give API consumers the option to use the exported crates
without needing explicit Cargo.toml entries.

This in turn decouples dependencies and reduces conflicts.

See rationale/discussion here:
rust-lang/api-guidelines#176

-- Change 2: use deps from within blsttc, blst_ringct --

blsttc and blst_ringct now reexport deps used in their public APIs.

So we remove those deps from our Cargo.toml and 'use' the sub-deps
in our code.

One gross thing remains which is that both crates use rand and
they could differ.  (although at present they are the same.)

This means that sn_dbc APIs that take Rng arg to be passed to
blsttc API can potentially be different than those that take
an Rng arg to be passed to blst_ringct API.  Although the compiler
presently let's us pass the same rng to both.  It's a footgun!

For now, I've created an 'rng' module to make it easy to pass the
appropriate rng, and also I updated the test cases, example, bench
to use this methodology, to demonstrate best practice.

Perhaps/probably a better long-term approach would be to have
blst_ringct integrate/depend/use blsttc, so that sn_dbc has only
a single dep and is not trying to resolve the matter.

-- Change 3: remove bls_dkg dependency/feature --

bls_dkg was only being used for a single function used by
test cases.  It was simple to replace this function with
blsttc calls which simplifies things a lot.

Cargo changes:

* use publshed quickcheck 1.0.3
* use dan-da/blsttc/reexport_pr
* remove rand, rand_core deps
* remove xor_name dep
* remove bls_dkg dep
* remove dkg feature
* remove blstrs dep
* use single-line format for all deps. (more concise)

lib.rs changes:
* re-export blst_ringct and blsttc
* export additional types used by builder module public API.
* add rng module, to simplfy rng usage for callers.
* modify bls_dkg_id() to use blsttc directly, without bls_dkg

Code changes:

* update mint-repl and reissue bench to use updated sn_dbc API.
* update tests to separate ringct rng from blsttc rng.
* update SimpleSigner From impl now that bls_dks is removed.
  • Loading branch information
dan-da committed Mar 24, 2022
1 parent e6098cc commit 48f4b84
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 242 deletions.
54 changes: 8 additions & 46 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,21 @@ authors = [ "MaidSafe Developers <dev@maidsafe.net>" ]
edition = "2018"

[features]
dkg = [ "bls_dkg" ]
serdes = [ "serde", "ringct-serde" ]
ringct-serde = [ "blst_ringct/serde" ]

[dependencies]
thiserror = "1.0.24"
quickcheck_macros = "1"
rand = "0.8.0"
quickcheck = "1.0.3"
blsttc = {git = "https://github.com/dan-da/blsttc", branch = "reexport_pr"}
blst_ringct = {git = "https://github.com/dan-da/blst-ringct", branch = "fix_deps_pr"}
hex = "0.4.3"
rand_core = "0.6.3"

[dependencies.blsttc]
git = "https://github.com/dan-da/blsttc"
branch = "sn_dbc_integration"

[dependencies.xor_name]
git = "https://github.com/iancoleman/xor_name"
branch = "remove_osrng"

[dependencies.quickcheck]
git = "https://github.com/davidrusu/quickcheck.git"
branch = "only-build-debug-reprs-on-failure"

[dependencies.blst_ringct]
git = "https://github.com/maidsafe/blst-ringct"

[dependencies.blstrs]
git = "https://github.com/davidrusu/blstrs.git"
branch = "bulletproofs-fixes"

[dependencies.bulletproofs]
git = "https://github.com/davidrusu/blst-bulletproofs.git"
branch = "bls12-381-curve"

[dependencies.bls_dkg]
git = "https://github.com/dan-da/bls_dkg.git"
branch = "sn_dbc_integration"
version = "~0.9.1"
optional = true

[dependencies.tiny-keccak]
version = "2.0.0"
features = [ "sha3" ]

[dependencies.serde]
version = "1.0.133"
features = [ "derive", "rc" ]
optional = true
tiny-keccak = {version = "2.0.0", features = [ "sha3" ]}
serde = {version = "1.0.133", features = [ "derive", "rc" ], optional = true}

[dev-dependencies]
anyhow = "1.0.40"
rand = "0.8.0"
rustyline = "8.0.0"
bincode = "1.3.3"
criterion = "0.3.5"
Expand All @@ -70,17 +33,16 @@ criterion = "0.3.5"
version = "0.7.0"
features = [ "flamegraph" ]

[dev-dependencies.sn_dbc]
path = "."
features = [ "dkg", "serdes" ]
[dev-dependencies.sn_dbc]
path = "."
features = [ "serdes" ]

[target."cfg(unix)".dev-dependencies]
termios = "0.3.3"

[[bench]]
name = "reissue"
harness = false
required-features = [ "dkg" ]

[[example]]
name = "mint-repl"
Expand Down
79 changes: 46 additions & 33 deletions benches/reissue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
#![allow(clippy::from_iter_instead_of_collect)]

use sn_dbc::{
Amount, Dbc, GenesisBuilderMock, Owner, OwnerOnce, Result, SpentBookNodeMock,
rng, Amount, Dbc, GenesisBuilderMock, Owner, OwnerOnce, Result, SpentBookNodeMock,
TransactionVerifier,
};

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::SeedableRng as SeedableRng8;

const N_OUTPUTS: u32 = 100;

fn bench_reissue_1_to_100(c: &mut Criterion) {
let mut rng8 = rand::rngs::StdRng::from_seed([0u8; 32]);
let mut rng_ct = rng::ringct::from_seed([0u8; 32]);
let mut rng_ttc = rng::blsttc::from_seed([1u8; 32]);

let (mut spentbook, starting_dbc) =
generate_dbc_of_value(N_OUTPUTS as Amount, &mut rng8).unwrap();
generate_dbc_of_value(N_OUTPUTS as Amount, &mut rng_ct, &mut rng_ttc).unwrap();

let mut dbc_builder = sn_dbc::TransactionBuilder::default()
.add_input_by_secrets(
Expand All @@ -33,14 +33,16 @@ fn bench_reissue_1_to_100(c: &mut Criterion) {
.unwrap(),
starting_dbc.amount_secrets_bearer().unwrap(),
vec![], // never any decoys for genesis
&mut rng8,
&mut rng_ct,
)
.add_outputs_by_amount((0..N_OUTPUTS).into_iter().map(|_| {
let owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng8), &mut rng8);
let owner_once = OwnerOnce::from_owner_base(
Owner::from_random_secret_key(&mut rng_ttc),
&mut rng_ttc,
);
(1, owner_once)
}))
.build(&mut rng8)
.build(&mut rng_ct)
.unwrap();

for (key_image, tx) in dbc_builder.inputs() {
Expand All @@ -51,25 +53,29 @@ fn bench_reissue_1_to_100(c: &mut Criterion) {
let spent_proofs = dbc_builder.spent_proofs().unwrap();
let tx = &dbc_builder.transaction;

let guard = pprof::ProfilerGuard::new(100).unwrap();
c.bench_function(&format!("reissue split 1 to {}", N_OUTPUTS), |b| {
let guard = pprof::ProfilerGuard::new(100).unwrap();

b.iter(|| {
TransactionVerifier::verify(&spentbook.key_manager, black_box(tx), &spent_proofs)
.unwrap();
});

if let Ok(report) = guard.report().build() {
let file =
std::fs::File::create(&format!("reissue_split_1_to_{}.svg", N_OUTPUTS)).unwrap();
report.flamegraph(file).unwrap();
};
});
if let Ok(report) = guard.report().build() {
let file = std::fs::File::create(&format!("reissue_split_1_to_{}.svg", N_OUTPUTS)).unwrap();
report.flamegraph(file).unwrap();
};
}

fn bench_reissue_100_to_1(c: &mut Criterion) {
let mut rng8 = rand::rngs::StdRng::from_seed([0u8; 32]);
let mut rng_ct = rng::ringct::from_seed([0u8; 32]);
let mut rng_ttc = rng::ringct::from_seed([1u8; 32]);
let num_decoys = 0;

let (mut spentbook_node, starting_dbc) =
generate_dbc_of_value(N_OUTPUTS as Amount, &mut rng8).unwrap();
generate_dbc_of_value(N_OUTPUTS as Amount, &mut rng_ct, &mut rng_ttc).unwrap();

let mut dbc_builder = sn_dbc::TransactionBuilder::default()
.add_input_by_secrets(
Expand All @@ -80,14 +86,16 @@ fn bench_reissue_100_to_1(c: &mut Criterion) {
.unwrap(),
starting_dbc.amount_secrets_bearer().unwrap(),
vec![], // never any decoy inputs for genesis
&mut rng8,
&mut rng_ct,
)
.add_outputs_by_amount((0..N_OUTPUTS).into_iter().map(|_| {
let owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng8), &mut rng8);
let owner_once = OwnerOnce::from_owner_base(
Owner::from_random_secret_key(&mut rng_ttc),
&mut rng_ttc,
);
(1, owner_once)
}))
.build(&mut rng8)
.build(&mut rng_ct)
.unwrap();

for (key_image, tx) in dbc_builder.inputs() {
Expand All @@ -97,7 +105,7 @@ fn bench_reissue_100_to_1(c: &mut Criterion) {
let dbcs = dbc_builder.build(&spentbook_node.key_manager).unwrap();

let output_owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng8), &mut rng8);
OwnerOnce::from_owner_base(Owner::from_random_secret_key(&mut rng_ttc), &mut rng_ttc);

let mut merge_dbc_builder = sn_dbc::TransactionBuilder::default()
.add_inputs_by_secrets(
Expand All @@ -106,14 +114,14 @@ fn bench_reissue_100_to_1(c: &mut Criterion) {
(
owner_once.as_owner().secret_key().unwrap(),
amount_secrets,
spentbook_node.random_decoys(num_decoys, &mut rng8),
spentbook_node.random_decoys(num_decoys, &mut rng_ct),
)
})
.collect(),
&mut rng8,
&mut rng_ct,
)
.add_output_by_amount(N_OUTPUTS as Amount, output_owner_once)
.build(&mut rng8)
.build(&mut rng_ct)
.unwrap();

for (key_image, tx) in merge_dbc_builder.inputs() {
Expand All @@ -124,25 +132,29 @@ fn bench_reissue_100_to_1(c: &mut Criterion) {
let spent_proofs = merge_dbc_builder.spent_proofs().unwrap();
let tx = &merge_dbc_builder.transaction;

let guard = pprof::ProfilerGuard::new(100).unwrap();
c.bench_function(&format!("reissue merge {} to 1", N_OUTPUTS), |b| {
let guard = pprof::ProfilerGuard::new(100).unwrap();

b.iter(|| {
TransactionVerifier::verify(&spentbook_node.key_manager, black_box(tx), &spent_proofs)
.unwrap();
});

if let Ok(report) = guard.report().build() {
let file =
std::fs::File::create(&format!("reissue_merge_{}_to_1.svg", N_OUTPUTS)).unwrap();
report.flamegraph(file).unwrap();
};
});
if let Ok(report) = guard.report().build() {
let file = std::fs::File::create(&format!("reissue_merge_{}_to_1.svg", N_OUTPUTS)).unwrap();
report.flamegraph(file).unwrap();
};
}

fn generate_dbc_of_value(
amount: Amount,
rng8: &mut (impl rand::RngCore + rand_core::CryptoRng),
rng_ct: &mut (impl rng::ringct::rand::RngCore + rng::ringct::rand::CryptoRng),
rng_ttc: &mut (impl rng::blsttc::rand::RngCore + rng::blsttc::rand::CryptoRng),
) -> Result<(SpentBookNodeMock, Dbc)> {
let (mut spentbook_node, genesis_dbc, _genesis_material, _amount_secrets) =
GenesisBuilderMock::init_genesis_single(rng8)?;
GenesisBuilderMock::init_genesis_single(rng_ct)?;

let output_amounts = vec![amount, sn_dbc::GenesisMaterial::GENESIS_AMOUNT - amount];

Expand All @@ -151,13 +163,14 @@ fn generate_dbc_of_value(
genesis_dbc.owner_once_bearer()?.secret_key()?,
genesis_dbc.amount_secrets_bearer()?,
vec![], // never any decoys for genesis
rng8,
rng_ct,
)
.add_outputs_by_amount(output_amounts.into_iter().map(|amount| {
let owner_once = OwnerOnce::from_owner_base(Owner::from_random_secret_key(rng8), rng8);
let owner_once =
OwnerOnce::from_owner_base(Owner::from_random_secret_key(rng_ttc), rng_ttc);
(amount, owner_once)
}))
.build(rng8)?;
.build(rng_ct)?;

for (key_image, tx) in dbc_builder.inputs() {
let spent_proof_share = spentbook_node.log_spent(key_image, tx)?;
Expand Down
Loading

0 comments on commit 48f4b84

Please sign in to comment.