Skip to content

Commit

Permalink
refactor!: rename OutputProof to BlindedOutput
Browse files Browse the repository at this point in the history
- This is stringent with the naming of the other types in the crate.
  • Loading branch information
oetyng committed Mar 27, 2023
1 parent e1fc40f commit 4fc3c9f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 37 deletions.
3 changes: 2 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ impl TransactionBuilder {
&self.revealed_tx.outputs
}

/// build a DbcTransaction and associated secrets
/// Build the DbcTransaction by signing the inputs,
/// and generating the blinded outputs. Return a DbcBuilder.
pub fn build(self, rng: impl RngCore + CryptoRng) -> Result<DbcBuilder> {
let (transaction, revealed_amounts) = self.revealed_tx.sign(rng)?;

Expand Down
14 changes: 7 additions & 7 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tiny_keccak::{Hasher, Sha3};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::transaction::{DbcTransaction, OutputProof, RevealedAmount, RevealedInput};
use crate::transaction::{BlindedOutput, DbcTransaction, RevealedAmount, RevealedInput};
use crate::{
BlindedAmount, DbcContent, DerivationIndex, Error, Hash, Owner, Result, SpentProof,
SpentProofKeyVerifier, TransactionVerifier,
Expand Down Expand Up @@ -167,7 +167,7 @@ impl Dbc {
.outputs
.iter()
.find(|o| &self.public_key() == o.public_key())
.ok_or(Error::OutputProofNotFound)?
.ok_or(Error::BlindedOutputNotFound)?
.blinded_amount())
}

Expand Down Expand Up @@ -322,7 +322,7 @@ impl Dbc {
/// This is done by
/// 1. Decrypting the `revealed_amount_cipher` into a RevealedAmount.
/// 2. Forming a BlindedAmount out of the RevealedAmount.
/// 3. Comparing that instance with the one in the dbc output proof in the tx.
/// 3. Comparing that instance with the one in the dbc blinded output in the tx.
///
/// If the blinded amounts do not match, then the Dbc cannot be spent
/// using the RevealedAmount provided.
Expand All @@ -341,23 +341,23 @@ impl Dbc {
pub(crate) fn verify_amounts(&self, base_sk: &SecretKey) -> Result<()> {
let revealed_amount: RevealedAmount = self.revealed_amount(base_sk)?;
let blinded_amount = revealed_amount.blinded_amount(&Default::default());
let blinded_amount_in_tx = self.output_proof(base_sk)?.blinded_amount();
let blinded_amount_in_tx = self.blinded_output(base_sk)?.blinded_amount();

match blinded_amount == blinded_amount_in_tx {
true => Ok(()),
false => Err(Error::BlindedAmountsDoNotMatch),
}
}

/// The output proof for this Dbc, is found in
/// The blinded output for this Dbc, is found in
/// the transaction that gave rise to this Dbc.
fn output_proof(&self, base_sk: &SecretKey) -> Result<&OutputProof> {
fn blinded_output(&self, base_sk: &SecretKey) -> Result<&BlindedOutput> {
let owner = self.owner_once(base_sk)?.public_key();
self.transaction
.outputs
.iter()
.find(|o| owner.eq(o.public_key()))
.ok_or(Error::OutputProofNotFound)
.ok_or(Error::BlindedOutputNotFound)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub enum Error {
#[error("Dbc Content is not a member of transaction outputs")]
DbcContentNotPresentInTransactionOutput,

#[error("OutputProof not found in transaction outputs")]
OutputProofNotFound,
#[error("BlindedOutput not found in transaction outputs")]
BlindedOutputNotFound,

#[error("Missing spent transaction for at least one of the spent proofs")]
MissingSpentTransaction,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub use crate::{
},
token::Token,
transaction::{
Amount, DbcTransaction, Input, Output, OutputProof, RevealedAmount, RevealedInput,
Amount, BlindedOutput, DbcTransaction, Input, Output, RevealedAmount, RevealedInput,
RevealedTransaction,
},
verification::{get_blinded_amounts_from_transaction, TransactionVerifier},
Expand Down
22 changes: 11 additions & 11 deletions src/mock/spentbook.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::transaction::{DbcTransaction, OutputProof};
use crate::transaction::{BlindedOutput, DbcTransaction};
use blsttc::PublicKey;
use bulletproofs::PedersenGens;
use std::collections::{BTreeMap, HashMap};
Expand All @@ -21,18 +21,18 @@ use crate::{mock, BlindedAmount, Error, Hash, Result, SpentProofContent, SpentPr
/// it stores only a single copy of each Tx and includes indexes:
/// tx_hash --> Tx
/// public_key --> tx_hash
/// public_key --> OutputProof
/// public_key --> BlindedOutput
///
/// The public_key map eliminates a full table scan when matching
/// public keys for each input of logged Tx to public key of OutputProof in
/// public keys for each input of logged Tx to public key of BlindedOutput in
/// already-spent Txs.
///
/// This impl does duplicate the OutputProofs in the public_key index, which
/// This impl does duplicate the BlindedOutputs in the public_key index, which
/// is not ideal and should not be done for a "real" system.
///
/// Another approach would be to map public_key --> tx_hash. This eliminates
/// the need to store duplicate OutputProof. One could lookup the Tx with
/// the desired OutputProof, and then iterate through outputs to actually find it.
/// the need to store duplicate BlindedOutput. One could lookup the Tx with
/// the desired BlindedOutput, and then iterate through outputs to actually find it.
///
/// See the very first commit of this file For a naive impl that uses only
/// a single map<public_key, tx>.
Expand All @@ -42,7 +42,7 @@ pub struct SpentBookNode {

pub transactions: HashMap<Hash, DbcTransaction>,
pub public_keys: BTreeMap<PublicKey, Hash>,
pub outputs: BTreeMap<PublicKey, OutputProof>,
pub outputs: BTreeMap<PublicKey, BlindedOutput>,

pub genesis: (PublicKey, BlindedAmount), // genesis input (PublicKey, BlindedAmount)
}
Expand Down Expand Up @@ -125,10 +125,10 @@ impl SpentBookNode {
tx.inputs
.iter()
.map(|input| {
// look up matching OutputProof
// look up matching BlindedOutput
let pk = input.public_key();
let output_proof = self.outputs.get(&pk);
match output_proof {
let blinded_output = self.outputs.get(&pk);
match blinded_output {
Some(p) => Ok((input.public_key, p.blinded_amount())),
None => Err(Error::MissingAmountForPubkey(pk)),
}
Expand Down Expand Up @@ -168,7 +168,7 @@ impl SpentBookNode {
// Add tx_hash:tx to transaction entries. (primary data store)
let existing_tx = self.transactions.entry(tx_hash).or_insert_with(|| tx);

// Add public_key:output_proof to public_key index.
// Add public_key:blinded_output to public_key index.
for output in existing_tx.outputs.iter() {
let pk = *output.public_key();
self.outputs.entry(pk).or_insert_with(|| output.clone());
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod revealed_amount;

pub(crate) use error::Error;
pub use input::{Input, RevealedInput};
pub use output::{Amount, DbcTransaction, Output, OutputProof, RevealedTransaction};
pub use output::{Amount, BlindedOutput, DbcTransaction, Output, RevealedTransaction};
pub use revealed_amount::RevealedAmount;

type Result<T> = std::result::Result<T, Error>;
31 changes: 17 additions & 14 deletions src/transaction/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ impl Output {
}
}

/// An output with a revealed amount.
/// As this is meant to be blinded, it has the
/// blinding factor included (in the revealed amount instance).
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
struct RevealedOutput {
Expand Down Expand Up @@ -84,18 +87,18 @@ impl RevealedTransaction {
//
// notes:
// 1. output blinded amounts, range_proofs, and public_keys are bundled
// together in OutputProofs
// together in BlindedOutputs
let revealed_input_amounts = self.revealed_input_amounts();
let input_amounts = self.blinded_input_amounts();

// Adjust the outputs so that summed blinding factors of inputs and outputs are equal.
let adjusted_revealed_outputs =
self.adjusted_revealed_outputs(&revealed_input_amounts, &mut rng);
let output_proofs = self.output_range_proofs(&adjusted_revealed_outputs, &mut rng)?;
let blinded_outputs = self.blinded_outputs(&adjusted_revealed_outputs, &mut rng)?;

// Generate message to sign.
// note: must match message generated by DbcTransaction::verify()
let msg = gen_message_for_signing(&self.public_keys(), &input_amounts, &output_proofs);
let msg = gen_message_for_signing(&self.public_keys(), &input_amounts, &blinded_outputs);

// We create a signature for each input
let signed_inputs: Vec<Input> = self
Expand All @@ -112,7 +115,7 @@ impl RevealedTransaction {
Ok((
DbcTransaction {
inputs: signed_inputs,
outputs: output_proofs,
outputs: blinded_outputs,
},
revealed_output_amounts,
))
Expand Down Expand Up @@ -199,11 +202,11 @@ impl RevealedTransaction {
revealed_outputs
}

fn output_range_proofs(
fn blinded_outputs(
&self,
revealed_outputs: &[RevealedOutput],
mut rng: impl RngCore + CryptoRng,
) -> Result<Vec<OutputProof>> {
) -> Result<Vec<BlindedOutput>> {
let mut prover_ts = Transcript::new(MERLIN_TRANSCRIPT_LABEL);

let bp_gens = Self::bp_gens();
Expand All @@ -224,7 +227,7 @@ impl RevealedTransaction {
.decompress()
.ok_or(Error::FailedToDecompressBlindedAmount)?;

Ok(OutputProof {
Ok(BlindedOutput {
public_key: c.public_key,
range_proof,
blinded_amount,
Expand All @@ -239,7 +242,7 @@ impl RevealedTransaction {
fn gen_message_for_signing(
public_keys: &[PublicKey],
input_amounts: &[BlindedAmount],
output_proofs: &[OutputProof],
blinded_outputs: &[BlindedOutput],
) -> Vec<u8> {
// Generate message to sign.
let mut msg: Vec<u8> = Default::default();
Expand All @@ -251,8 +254,8 @@ fn gen_message_for_signing(
for r in input_amounts.iter() {
msg.extend(r.compress().as_bytes());
}
msg.extend("output_proofs".as_bytes());
for o in output_proofs.iter() {
msg.extend("blinded_outputs".as_bytes());
for o in blinded_outputs.iter() {
msg.extend(o.to_bytes());
}
msg.extend("end".as_bytes());
Expand All @@ -261,13 +264,13 @@ fn gen_message_for_signing(

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct OutputProof {
pub struct BlindedOutput {
public_key: PublicKey,
range_proof: RangeProof,
blinded_amount: BlindedAmount,
}

impl OutputProof {
impl BlindedOutput {
pub fn to_bytes(&self) -> Vec<u8> {
let mut v: Vec<u8> = Default::default();
v.extend(self.public_key.to_bytes().as_ref());
Expand All @@ -293,7 +296,7 @@ impl OutputProof {
#[derive(Debug, Clone)]
pub struct DbcTransaction {
pub inputs: Vec<Input>,
pub outputs: Vec<OutputProof>,
pub outputs: Vec<BlindedOutput>,
}

impl PartialEq for DbcTransaction {
Expand Down Expand Up @@ -398,7 +401,7 @@ impl DbcTransaction {
let output_sum: RistrettoPoint = self
.outputs
.iter()
.map(|i| i.blinded_amount)
.map(BlindedOutput::blinded_amount)
.map(RistrettoPoint::from)
.sum();

Expand Down

0 comments on commit 4fc3c9f

Please sign in to comment.