Skip to content

Commit

Permalink
fix: make dbc_packet.rs build again
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-da authored and dirvine committed Feb 17, 2022
1 parent 2bf247e commit 6670818
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/dbc_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{AmountSecrets, DbcHelper};
use crate::{AmountSecrets, BlsHelper};
use blstrs::group::GroupEncoding;
use blstrs::G1Affine;
use blsttc::{Ciphertext, PublicKey};
Expand Down Expand Up @@ -42,7 +42,7 @@ impl From<(OwnerPublicKey, AmountSecrets)> for DbcContent {
// Create a new DbcContent for signing.
fn from(params: (OwnerPublicKey, AmountSecrets)) -> Self {
let (owner, amount_secrets) = params;
let pubkey = DbcHelper::blstrs_to_blsttc_pubkey(&owner);
let pubkey = BlsHelper::blstrs_to_blsttc_pubkey(&owner);
let amount_secrets_cipher = pubkey.encrypt(&amount_secrets.to_bytes());

Self {
Expand All @@ -57,7 +57,7 @@ impl From<(PublicKey, AmountSecrets)> for DbcContent {
fn from(params: (PublicKey, AmountSecrets)) -> Self {
let (pubkey, amount_secrets) = params;
let amount_secrets_cipher = pubkey.encrypt(&amount_secrets.to_bytes());
let owner = DbcHelper::blsttc_to_blstrs_pubkey(&pubkey);
let owner = BlsHelper::blsttc_to_blstrs_pubkey(&pubkey);

Self {
owner,
Expand Down
9 changes: 6 additions & 3 deletions src/dbc_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{Dbc, Error, PublicKey, Result};
use crate::{BlsHelper, Dbc, Error, PublicKey, Result};
use blsttc::SecretKeySet;
use rand::Rng;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -95,7 +95,8 @@ impl From<SecretKeySet> for DerivedKeySet {
/// After reissue, the client constructs the Dbc and then
/// puts it into a DbcPacket along with the DerivedKeySet
///
#[derive(Clone, Deserialize, Serialize)]
// #[derive(Clone, Deserialize, Serialize)]
#[derive(Clone)]
pub struct DbcPacket {
dbc: Dbc,
owner_keyset: DerivedKeySet,
Expand All @@ -111,7 +112,9 @@ impl DbcPacket {
}

fn verify_owner_derivation_index(&self) -> Result<()> {
match self.dbc.owner() == self.owner_keyset.derived_public_key() {
let derived_pk =
BlsHelper::blsttc_to_blstrs_pubkey(&self.owner_keyset.derived_public_key());
match self.dbc.owner() == derived_pk {
true => Ok(()),
false => Err(Error::DerivedOwnerKeyDoesNotMatch),
}
Expand Down
41 changes: 23 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod amount_secrets;
mod builder;
mod dbc;
mod dbc_content;
// mod dbc_packet;
mod dbc_packet;
mod error;
mod key_manager;
mod mint;
Expand All @@ -25,7 +25,7 @@ pub use crate::{
builder::{DbcBuilder, Output, ReissueRequestBuilder, TransactionBuilder},
dbc::{Dbc, KeyImage},
dbc_content::{Amount, DbcContent},
// dbc_packet::{DbcPacket, DerivedKeySet},
dbc_packet::{DbcPacket, DerivedKeySet},
error::{Error, Result},
key_manager::{
KeyManager, NodeSignature, PublicKey, PublicKeySet, Signature, SimpleKeyManager,
Expand Down Expand Up @@ -117,22 +117,6 @@ pub struct DbcHelper {}
#[cfg(feature = "dkg")]
impl DbcHelper {
#[allow(dead_code)]
pub(crate) fn blsttc_to_blstrs_sk(sk: SecretKey) -> Scalar {
let bytes = sk.to_bytes();
println!("sk bytes: {:?}", bytes);
Scalar::from_bytes_be(&bytes).unwrap()
}

pub(crate) fn blsttc_to_blstrs_pubkey(pk: &PublicKey) -> G1Affine {
let bytes = pk.to_bytes();
G1Affine::from_compressed(&bytes).unwrap()
}

pub(crate) fn blstrs_to_blsttc_pubkey(pk: &G1Affine) -> PublicKey {
let bytes = pk.to_compressed();
PublicKey::from_bytes(bytes).unwrap()
}

pub fn decrypt_amount_secrets(
owner: &bls_dkg::outcome::Outcome,
ciphertext: &Ciphertext,
Expand All @@ -151,6 +135,27 @@ impl DbcHelper {
}
}

pub(crate) struct BlsHelper {}

impl BlsHelper {
#[allow(dead_code)]
pub(crate) fn blsttc_to_blstrs_sk(sk: SecretKey) -> Scalar {
let bytes = sk.to_bytes();
println!("sk bytes: {:?}", bytes);
Scalar::from_bytes_be(&bytes).unwrap()
}

pub(crate) fn blsttc_to_blstrs_pubkey(pk: &PublicKey) -> G1Affine {
let bytes = pk.to_bytes();
G1Affine::from_compressed(&bytes).unwrap()
}

pub(crate) fn blstrs_to_blsttc_pubkey(pk: &G1Affine) -> PublicKey {
let bytes = pk.to_compressed();
PublicKey::from_bytes(bytes).unwrap()
}
}

pub(crate) fn sha3_256(input: &[u8]) -> [u8; 32] {
use tiny_keccak::{Hasher, Sha3};

Expand Down
16 changes: 8 additions & 8 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ mod tests {

use crate::{
tests::{TinyInt, TinyVec},
Dbc, DbcBuilder, DbcHelper, ReissueRequestBuilder, SimpleKeyManager, SimpleSigner,
SpentProofShare,
BlsHelper, Dbc, DbcBuilder, DbcHelper, ReissueRequestBuilder, SimpleKeyManager,
SimpleSigner, SpentProofShare,
};

#[quickcheck]
Expand Down Expand Up @@ -283,7 +283,7 @@ mod tests {

let output_owner = crate::bls_dkg_id(&mut rng);
let output_owner_pk =
DbcHelper::blsttc_to_blstrs_pubkey(&output_owner.public_key_set.public_key());
BlsHelper::blsttc_to_blstrs_pubkey(&output_owner.public_key_set.public_key());

let (reissue_tx, revealed_commitments, _material) = crate::TransactionBuilder::default()
.add_input_by_secrets(
Expand Down Expand Up @@ -426,7 +426,7 @@ mod tests {
.add_outputs(owner_amounts_and_keys.clone().into_iter().map(
|(public_key, (amount, _))| crate::Output {
amount,
public_key: DbcHelper::blsttc_to_blstrs_pubkey(&public_key),
public_key: BlsHelper::blsttc_to_blstrs_pubkey(&public_key),
},
))
.build(&mut rng8)
Expand Down Expand Up @@ -487,12 +487,12 @@ mod tests {
.iter()
.map(|dbc| {
let (_, sks) =
&owner_amounts_and_keys[&DbcHelper::blstrs_to_blsttc_pubkey(&dbc.owner())];
&owner_amounts_and_keys[&BlsHelper::blstrs_to_blsttc_pubkey(&dbc.owner())];
let amount_secrets = AmountSecrets::try_from((
&sks.secret_key(),
&dbc.content.amount_secrets_cipher,
))?;
let secret_key_blstrs = DbcHelper::blsttc_to_blstrs_sk(sks.secret_key());
let secret_key_blstrs = BlsHelper::blsttc_to_blstrs_sk(sks.secret_key());

// note: decoy inputs can be created from OutputProof + dbc owner's pubkey.
let decoy_inputs = gen_decoy_inputs(num_decoy_inputs, &mut rng8);
Expand All @@ -506,7 +506,7 @@ mod tests {
.iter()
.map(|amount| crate::Output {
amount: *amount,
public_key: DbcHelper::blsttc_to_blstrs_pubkey(
public_key: BlsHelper::blsttc_to_blstrs_pubkey(
&outputs_owner.public_key_set.public_key(),
),
})
Expand Down Expand Up @@ -689,7 +689,7 @@ mod tests {

let input_owner = crate::bls_dkg_id(&mut rng);
let owner_pubkey =
DbcHelper::blsttc_to_blstrs_pubkey(&input_owner.public_key_set.public_key());
BlsHelper::blsttc_to_blstrs_pubkey(&input_owner.public_key_set.public_key());

let output = Output {
public_key: owner_pubkey,
Expand Down

0 comments on commit 6670818

Please sign in to comment.