Skip to content

Commit

Permalink
make verify_finish take IntoIterator
Browse files Browse the repository at this point in the history
`IntoIterator<Item = VerifierMessage>` is even more flexible than
`TryInto<[VerifierMessage; N]>` and is also infallible, eliminating an
error case and simplifying the generic parameters on `verify_finish`.
  • Loading branch information
tgeoghegan committed Sep 24, 2021
1 parent 14dd072 commit 534d9cf
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/vdaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::pcp::{decide, prove, query, PcpError, Proof, Value, Verifier};
use crate::prng::{Prng, PrngError};
use crate::vdaf::suite::{Key, KeyDeriver, KeyStream, Suite, SuiteError};
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use std::convert::TryFrom;
use std::iter::IntoIterator;

/// Errors emitted by this module.
#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -374,27 +375,21 @@ pub fn verify_start<V: Value>(
/// [`VerifierMessage`] messages broadcast by all of the aggregators and produces the aggregator's
/// input share.
// TODO(cjpatton) Check for ciphersuite mismatch between `state` and `msgs` and among `msgs`.
pub fn verify_finish<M, E, V, const N: usize>(
state: AggregatorState<V>,
msgs: M,
) -> Result<V, VdafError>
pub fn verify_finish<M, V>(state: AggregatorState<V>, msgs: M) -> Result<V, VdafError>
where
V: Value,
M: TryInto<[VerifierMessage<V::Field>; N], Error = E>,
E: std::fmt::Debug,
M: IntoIterator<Item = VerifierMessage<V::Field>>,
{
let msgs: [VerifierMessage<V::Field>; N] = msgs.try_into().map_err(|e| {
VdafError::Uncategorized(format!(
"input could not be converted to VerifierMessage slice: {:?}",
e
))
})?;

if msgs.is_empty() {
return Err(VdafError::Uncategorized(
"verify_finish(): expected at least one inbound messages; got none".to_string(),
));
}
let mut msgs = msgs.into_iter().peekable();

let verifier_length = match msgs.peek() {
Some(message) => message.verifier_share.as_slice().len(),
None => {
return Err(VdafError::Uncategorized(
"verify_finish(): expected at least one inbound messages; got none".to_string(),
));
}
};

let (input_share, mut joint_rand_seed) = match state {
AggregatorState::Wait {
Expand All @@ -410,7 +405,7 @@ where
};

// Combine the verifier messages.
let mut verifier_data = vec![V::Field::zero(); msgs[0].verifier_share.as_slice().len()];
let mut verifier_data = vec![V::Field::zero(); verifier_length];
for msg in msgs {
if msg.verifier_share.as_slice().len() != verifier_data.len() {
return Err(VdafError::Uncategorized(format!(
Expand Down Expand Up @@ -480,8 +475,7 @@ mod tests {
// Aggregators decide whether the input is valid based on the verifier messages.
let mut output = vec![Field64::zero(); input.as_slice().len()];
for state in states {
let output_share =
verify_finish::<_, _, _, NUM_SHARES>(state, verifiers.clone()).unwrap();
let output_share = verify_finish(state, verifiers.clone()).unwrap();
for (x, y) in output.iter_mut().zip(output_share.as_slice()) {
*x += *y;
}
Expand Down

0 comments on commit 534d9cf

Please sign in to comment.