aries_bbssignatures/
verifier.rsuse crate::errors::{BBSError, BBSErrorKind};
use crate::keys::prelude::*;
use crate::pok_sig::prelude::*;
use crate::{
HashElem, ProofChallenge, ProofNonce, ProofRequest, RandomElem, SignatureMessage,
SignatureProof,
};
use std::collections::BTreeSet;
pub struct Verifier;
impl Verifier {
pub fn new_proof_request(
revealed_message_indices: &[usize],
verkey: &PublicKey,
) -> Result<ProofRequest, BBSError> {
let revealed_messages = revealed_message_indices
.iter()
.copied()
.collect::<BTreeSet<usize>>();
for i in &revealed_messages {
if *i > verkey.h.len() {
return Err(BBSErrorKind::PublicKeyGeneratorMessageCountMismatch(
*i,
verkey.h.len(),
)
.into());
}
}
Ok(ProofRequest {
revealed_messages,
verification_key: verkey.clone(),
})
}
pub fn verify_signature_pok(
proof_request: &ProofRequest,
signature_proof: &SignatureProof,
nonce: &ProofNonce,
) -> Result<Vec<SignatureMessage>, BBSError> {
let mut challenge_bytes = signature_proof.proof.get_bytes_for_challenge(
proof_request.revealed_messages.clone(),
&proof_request.verification_key,
);
challenge_bytes.extend_from_slice(&nonce.to_bytes_uncompressed_form()[..]);
let challenge_verifier = ProofChallenge::hash(&challenge_bytes);
match signature_proof.proof.verify(
&proof_request.verification_key,
&signature_proof.revealed_messages,
&challenge_verifier,
)? {
PoKOfSignatureProofStatus::Success => Ok(signature_proof
.revealed_messages
.iter()
.map(|(_, m)| *m)
.collect::<Vec<SignatureMessage>>()),
e => Err(BBSErrorKind::InvalidProof { status: e }.into()),
}
}
pub fn generate_proof_nonce() -> ProofNonce {
ProofNonce::random()
}
pub fn create_challenge_hash(
proofs: &[SignatureProof],
proof_requests: &[ProofRequest],
nonce: &ProofNonce,
claims: Option<&[&[u8]]>,
) -> Result<ProofChallenge, BBSError> {
let mut bytes = Vec::new();
for pr in proofs.iter().zip(proof_requests.iter()) {
let (p, r) = pr;
bytes.extend_from_slice(
p.proof
.get_bytes_for_challenge(r.revealed_messages.clone(), &r.verification_key)
.as_slice(),
);
}
bytes.extend_from_slice(&nonce.to_bytes_uncompressed_form()[..]);
if let Some(claim) = claims {
for c in claim {
bytes.extend_from_slice(c);
}
}
let challenge = ProofChallenge::hash(&bytes);
Ok(challenge)
}
}