Skip to content

Commit

Permalink
commit: return enumerated errors for ConvolveCommit
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 15, 2023
1 parent 62e177b commit b32f001
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
47 changes: 29 additions & 18 deletions commit_verify/src/convolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
use crate::{CommitEncode, CommitmentProtocol, VerifyEq};

/// Error during commitment verification
#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
#[display(doc_comments)]
#[allow(clippy::enum_variant_names)]
pub enum ConvolveVerifyError {
/// The verified commitment doesn't commit to the provided message.
InvalidCommitment,
/// The message is invalid since a commitment to it can't be created /
/// exist.
InvalidMessage,
/// The proof of the commitment is invalid and the commitment can't be
/// verified.
InvalidProof,
}

/// Proof type used by [`ConvolveCommit`] protocol.
pub trait ConvolveCommitProof<Msg, Source, Protocol>
where
Expand All @@ -49,34 +64,30 @@ where
/// that the resulting commitment matches the provided one in the
/// `commitment` parameter.
///
/// Errors if the commitment can't be created, i.e. the
/// [`ConvolveCommit::convolve_commit`] procedure for the original,
/// restored from the proof, can't be performed. This means that the
/// verification has failed and the commitment and/or the proof are
/// invalid. The function returns error in this case (ano not simply
/// `false`) since this usually means the software error in managing
/// container and proof data, or selection of a different commitment
/// protocol parameters comparing to the ones used during commitment
/// creation. In all these cases we'd like to provide devs with more
/// information for debugging.
/// # Errors
///
/// The proper way of using the function in a well-debugged software should
/// be `if commitment.verify(...).expect("proof managing system") { .. }`.
/// However if the proofs are provided by some sort of user/network input
/// from an untrusted party, a proper form would be
/// `if commitment.verify(...).unwrap_or(false) { .. }`.
/// Errors if the commitment doesn't pass the validation (see
/// [`ConvolveVerifyError`] variants for the cases when this may happen).
fn verify(
&self,
msg: &Msg,
commitment: &Source::Commitment,
) -> Result<bool, Source::CommitError>
) -> Result<(), ConvolveVerifyError>
where
Self: VerifyEq,
{
let original = self.restore_original(commitment);
let suppl = self.extract_supplement();
let (commitment_prime, proof) = original.convolve_commit(suppl, msg)?;
Ok(commitment.verify_eq(&commitment_prime) && self.verify_eq(&proof))
let (commitment_prime, proof) = original
.convolve_commit(suppl, msg)
.map_err(|_| ConvolveVerifyError::InvalidMessage)?;
if !self.verify_eq(&proof) {
return Err(ConvolveVerifyError::InvalidProof);
}
if !commitment.verify_eq(&commitment_prime) {
return Err(ConvolveVerifyError::InvalidCommitment);
}
Ok(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion commit_verify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod digest;

pub use commit::{CommitVerify, StrictEncodedProtocol, TryCommitVerify, VerifyError};
pub use conceal::Conceal;
pub use convolve::{ConvolveCommit, ConvolveCommitProof};
pub use convolve::{ConvolveCommit, ConvolveCommitProof, ConvolveVerifyError};
pub use digest::{Digest, DigestExt, Ripemd160, Sha256};
pub use embed::{EmbedCommitProof, EmbedCommitVerify, VerifyEq};
pub use encode::{strategies, CommitEncode, CommitStrategy};
Expand Down

0 comments on commit b32f001

Please sign in to comment.