Skip to content

Commit c5a3da8

Browse files
committed
commit: return enumerated errors for EmbedCommit
1 parent b32f001 commit c5a3da8

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

commit_verify/src/embed.rs

+59-35
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,30 @@
2323
2424
use crate::{CommitEncode, CommitmentProtocol};
2525

26-
/// Trait for equivalence verification. Implemented for all types implemeting
26+
/// Error during commitment verification
27+
#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error, From)]
28+
#[display(doc_comments)]
29+
pub enum EmbedVerifyError<E: std::error::Error> {
30+
/// The verified commitment doesn't commit to the provided message.
31+
CommitmentMismatch,
32+
33+
/// The message is invalid since a commitment to it can't be created /
34+
/// exist.
35+
///
36+
/// Details: {0}
37+
#[from]
38+
InvalidMessage(E),
39+
40+
/// The proof of the commitment is invalid and the commitment can't be
41+
/// verified since the original container can't be restored from it.
42+
InvalidProof,
43+
44+
/// The proof of the commitment does not match to the proof generated for
45+
/// the same message during the verification.
46+
ProofMismatch,
47+
}
48+
49+
/// Trait for equivalence verification. Implemented for all types implementing
2750
/// `Eq`. For non-`Eq` types this trait provides way to implement custom
2851
/// equivalence verification used during commitment verification procedure.
2952
pub trait VerifyEq {
@@ -47,10 +70,15 @@ where
4770
{
4871
/// Restores original container before the commitment from the proof data
4972
/// and a container containing embedded commitment.
73+
///
74+
/// # Error
75+
///
76+
/// If the container can't be restored from the proof returns
77+
/// [`EmbedVerifyError::InvalidProof`].
5078
fn restore_original_container(
5179
&self,
5280
commit_container: &Container,
53-
) -> Result<Container, Container::VerifyError>;
81+
) -> Result<Container, EmbedVerifyError<Container::CommitError>>;
5482
}
5583

5684
/// Trait for *embed-commit-verify scheme*, where some data structure (named
@@ -82,7 +110,7 @@ where
82110
/// combination of the same message and container type (each of each will have
83111
/// its own `Proof` type defined as an associated generic).
84112
///
85-
/// Usually represents an uninstantiable type, but may be a structure
113+
/// Usually represents a non-instantiable type, but may be a structure
86114
/// containing commitment protocol configuration or context objects.
87115
///
88116
/// ```
@@ -109,14 +137,11 @@ where
109137
type Proof: EmbedCommitProof<Msg, Self, Protocol>;
110138

111139
/// Error type that may be reported during [`Self::embed_commit`] procedure.
112-
/// It may also be returned from [`Self::verify`] in case the proof data are
113-
/// invalid and the commitment can't be re-created.
140+
/// It may also be returned from [`Self::verify`] (wrapped into
141+
/// [`EmbedVerifyError`] in case the proof data are invalid and the
142+
/// commitment can't be re-created.
114143
type CommitError: std::error::Error;
115144

116-
/// Error type that may be reported during [`Self::verify`] procedure.
117-
/// It must be a subset of [`Self::CommitError`].
118-
type VerifyError: std::error::Error + From<Self::CommitError>;
119-
120145
/// Creates a commitment to a message and embeds it into the provided
121146
/// container (`self`) by mutating it and returning commitment proof.
122147
///
@@ -131,31 +156,28 @@ where
131156
/// [`Self::embed_commit`] procedure checking that the resulting proof and
132157
/// commitment matches the provided `self` and `proof`.
133158
///
134-
/// Errors if the provided commitment can't be created, i.e. the
135-
/// [`Self::embed_commit`] procedure for the original container, restored
136-
/// from the proof and current container, can't be performed. This means
137-
/// that the verification has failed and the commitment and proof are
138-
/// invalid. The function returns error in this case (ano not simply
139-
/// `false`) since this usually means the software error in managing
140-
/// container and proof data, or selection of a different commitment
141-
/// protocol parameters comparing to the ones used during commitment
142-
/// creation. In all these cases we'd like to provide devs with more
143-
/// information for debugging.
159+
/// # Errors
144160
///
145-
/// The proper way of using the function in a well-debugged software should
146-
/// be `if commitment.verify(...).expect("proof managing system") { .. }`.
147-
/// However if the proofs are provided by some sort of user/network input
148-
/// from an untrusted party, a proper form would be
149-
/// `if commitment.verify(...).unwrap_or(false) { .. }`.
150-
#[inline]
151-
fn verify(&self, msg: &Msg, proof: &Self::Proof) -> Result<bool, Self::VerifyError>
161+
/// Errors if the commitment doesn't pass the validation (see
162+
/// [`EmbedVerifyError`] variants for the cases when this may happen).
163+
fn verify(
164+
&self,
165+
msg: &Msg,
166+
proof: &Self::Proof,
167+
) -> Result<(), EmbedVerifyError<Self::CommitError>>
152168
where
153169
Self: VerifyEq,
154170
Self::Proof: VerifyEq,
155171
{
156172
let mut container_prime = proof.restore_original_container(self)?;
157173
let proof_prime = container_prime.embed_commit(msg)?;
158-
Ok(proof_prime.verify_eq(proof) && self.verify_eq(&container_prime))
174+
if !proof_prime.verify_eq(proof) {
175+
return Err(EmbedVerifyError::InvalidProof);
176+
}
177+
if !self.verify_eq(&container_prime) {
178+
return Err(EmbedVerifyError::CommitmentMismatch);
179+
}
180+
Ok(())
159181
}
160182

161183
/// Phantom method used to add `Protocol` generic parameter to the trait.
@@ -207,18 +229,18 @@ pub(crate) mod test_helpers {
207229
});
208230

209231
// Testing verification
210-
assert!(commitment.clone().verify(msg, &proof).unwrap());
232+
assert!(commitment.clone().verify(msg, &proof).is_ok());
211233

212234
messages.iter().for_each(|m| {
213235
// Testing that commitment verification succeeds only
214236
// for the original message and fails for the rest
215-
assert_eq!(commitment.clone().verify(m, &proof).unwrap(), m == msg);
237+
assert_eq!(commitment.clone().verify(m, &proof).is_ok(), m == msg);
216238
});
217239

218240
acc.iter().for_each(|cmt| {
219241
// Testing that verification against other commitments
220242
// returns `false`
221-
assert!(!cmt.clone().verify(msg, &proof).unwrap());
243+
assert!(!cmt.clone().verify(msg, &proof).is_ok());
222244
});
223245

224246
// Detecting collision: each message should produce a unique
@@ -253,18 +275,18 @@ pub(crate) mod test_helpers {
253275
});
254276

255277
// Testing verification
256-
assert!(SUPPLEMENT.verify(msg, &commitment).unwrap());
278+
assert!(SUPPLEMENT.verify(msg, &commitment).is_ok());
257279

258280
messages.iter().for_each(|m| {
259281
// Testing that commitment verification succeeds only
260282
// for the original message and fails for the rest
261-
assert_eq!(SUPPLEMENT.verify(m, &commitment).unwrap(), m == msg);
283+
assert_eq!(SUPPLEMENT.verify(m, &commitment).is_ok(), m == msg);
262284
});
263285

264286
acc.iter().for_each(|commitment| {
265287
// Testing that verification against other commitments
266288
// returns `false`
267-
assert!(!SUPPLEMENT.verify(msg, commitment).unwrap());
289+
assert!(!SUPPLEMENT.verify(msg, commitment).is_ok());
268290
});
269291

270292
// Detecting collision: each message should produce a unique
@@ -303,7 +325,10 @@ mod test {
303325
impl<T> EmbedCommitProof<T, DummyVec, TestProtocol> for DummyProof
304326
where T: AsRef<[u8]> + Clone + CommitEncode
305327
{
306-
fn restore_original_container(&self, _: &DummyVec) -> Result<DummyVec, Error> {
328+
fn restore_original_container(
329+
&self,
330+
_: &DummyVec,
331+
) -> Result<DummyVec, EmbedVerifyError<Error>> {
307332
Ok(DummyVec(self.0.clone()))
308333
}
309334
}
@@ -313,7 +338,6 @@ mod test {
313338
{
314339
type Proof = DummyProof;
315340
type CommitError = Error;
316-
type VerifyError = Error;
317341

318342
fn embed_commit(&mut self, msg: &T) -> Result<Self::Proof, Self::CommitError> {
319343
let proof = self.0.clone();

commit_verify/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub use commit::{CommitVerify, StrictEncodedProtocol, TryCommitVerify, VerifyErr
6262
pub use conceal::Conceal;
6363
pub use convolve::{ConvolveCommit, ConvolveCommitProof, ConvolveVerifyError};
6464
pub use digest::{Digest, DigestExt, Ripemd160, Sha256};
65-
pub use embed::{EmbedCommitProof, EmbedCommitVerify, VerifyEq};
65+
pub use embed::{EmbedCommitProof, EmbedCommitVerify, EmbedVerifyError, VerifyEq};
6666
pub use encode::{strategies, CommitEncode, CommitStrategy};
6767
pub use id::CommitmentId;
6868

0 commit comments

Comments
 (0)