Skip to content

Commit

Permalink
feat: expose a public API which allows to build a SpentProof from a g…
Browse files Browse the repository at this point in the history
…iven set of proof shares
  • Loading branch information
bochaco authored and joshuef committed Jul 28, 2022
1 parent bdd649f commit d25a01b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
33 changes: 4 additions & 29 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ use std::{

use crate::{
rand::{CryptoRng, RngCore},
AmountSecrets, Commitment, Dbc, DbcContent, Error, Hash, IndexedSignatureShare, KeyImage,
OwnerOnce, Result, SpentProof, SpentProofContent, SpentProofKeyVerifier, SpentProofShare,
TransactionVerifier,
AmountSecrets, Commitment, Dbc, DbcContent, Error, Hash, KeyImage, OwnerOnce, Result,
SpentProof, SpentProofKeyVerifier, SpentProofShare, TransactionVerifier,
};

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -484,36 +483,12 @@ impl DbcBuilder {

/// build spent proofs from shares.
pub fn spent_proofs(&self) -> Result<BTreeSet<SpentProof>> {
let transaction_hash = Hash::from(self.transaction.hash());
let spent_proofs: BTreeSet<SpentProof> = self
.spent_proof_shares
.iter()
.map(|(key_image, shares)| {
let any_share = shares
.iter()
.next()
.ok_or(Error::MissingSpentProofShare(*key_image))?;

let spentbook_pub_key = any_share.spentbook_pks().public_key();
let spentbook_sig = any_share.spentbook_pks.combine_signatures(
shares
.iter()
.map(SpentProofShare::spentbook_sig_share)
.map(IndexedSignatureShare::threshold_crypto),
)?;

let public_commitments: Vec<Commitment> = any_share.public_commitments().clone();

let spent_proof = SpentProof {
content: SpentProofContent {
key_image: *key_image,
transaction_hash: Hash::from(self.transaction.hash()),
public_commitments,
},
spentbook_pub_key,
spentbook_sig,
};

Ok(spent_proof)
SpentProof::try_from_proof_shares(*key_image, transaction_hash, shares.iter())
})
.collect::<Result<_>>()?;

Expand Down
32 changes: 32 additions & 0 deletions src/spent_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,38 @@ pub struct SpentProof {
}

impl SpentProof {
/// Attempts to build a SpentProof by combining a given set of proof shares
pub fn try_from_proof_shares<'a>(
key_image: KeyImage,
transaction_hash: Hash,
shares: impl Iterator<Item = &'a SpentProofShare>,
) -> Result<Self> {
let mut peekable_shares = shares.peekable();
let any_share = peekable_shares
.peek()
.cloned()
.ok_or(Error::MissingSpentProofShare(key_image))?;

let spentbook_pub_key = any_share.spentbook_pks().public_key();
let spentbook_sig = any_share.spentbook_pks.combine_signatures(
peekable_shares
.map(SpentProofShare::spentbook_sig_share)
.map(IndexedSignatureShare::threshold_crypto),
)?;

let public_commitments: Vec<Commitment> = any_share.public_commitments().clone();

Ok(SpentProof {
content: SpentProofContent {
key_image,
transaction_hash,
public_commitments,
},
spentbook_pub_key,
spentbook_sig,
})
}

/// get KeyImage of input Dbc
pub fn key_image(&self) -> &KeyImage {
&self.content.key_image
Expand Down

0 comments on commit d25a01b

Please sign in to comment.