Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions crates/daphne/src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::{
fmt,
io::{Cursor, Read},
};
use subtle::ConstantTimeEq;

// Batch modes
const BATCH_MODE_TIME_INTERVAL: u8 = 0x01;
Expand Down Expand Up @@ -1485,18 +1486,8 @@ impl ParameterizedDecode<DapVersion> for PlaintextInputShare {
}
}

// NOTE ring provides a similar function, but as of version 0.16.20, it doesn't compile to
// wasm32-unknown-unknown.
pub fn constant_time_eq(left: &[u8], right: &[u8]) -> bool {
if left.len() != right.len() {
return false;
}

let mut r = 0;
for (x, y) in left.iter().zip(right) {
r |= x ^ y;
}
r == 0
bool::from(left.ct_eq(right))
}

pub(crate) fn encode_u16_bytes(bytes: &mut Vec<u8>, input: &[u8]) -> Result<(), CodecError> {
Expand Down Expand Up @@ -1705,6 +1696,14 @@ mod test {
));
}

#[test]
fn constant_time_eq_matches_slice_equality() {
assert!(constant_time_eq(b"", b""));
assert!(constant_time_eq(b"same bytes", b"same bytes"));
assert!(!constant_time_eq(b"same bytes", b"same bytez"));
assert!(!constant_time_eq(b"short", b"shorter"));
}

fn partial_batch_selector_encode_decode(version: DapVersion) {
const TEST_DATA_DRAFT09: &[u8] = &[1];
const TEST_DATA_LATEST: &[u8] = &[1, 0, 0];
Expand Down
16 changes: 15 additions & 1 deletion crates/daphne/src/vdaf/prio3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use prio::{
},
};
use std::io::Cursor;
use subtle::ConstantTimeEq;

impl Prio3Config {
pub(crate) fn shard(
Expand All @@ -35,10 +36,11 @@ impl Prio3Config {
(DapVersion::Latest, Prio3Config::Count, DapMeasurement::U64(measurement))
if measurement < 2 =>
{
let measurement = bool::from(measurement.ct_eq(&1));
let vdaf = Prio3::new_count(2).map_err(|e| {
VdafError::Dap(fatal_error!(err = ?e, "initializing {self:?} failed"))
})?;
shard_then_encode(&vdaf, task_id, &(measurement != 0), nonce)
shard_then_encode(&vdaf, task_id, &measurement, nonce)
}
(
DapVersion::Latest,
Expand Down Expand Up @@ -612,6 +614,18 @@ mod test {
assert_eq!(got, DapAggregateResult::U64(3));
}

#[test]
fn count_rejects_non_binary_measurement() {
let got = Prio3Config::Count.shard(
DapVersion::Latest,
DapMeasurement::U64(2),
&[0; 16],
crate::messages::TaskId([0; 32]),
);

assert!(got.is_err());
}

#[test]
fn roundtrip_sum() {
let mut t = AggregationJobTest::new(
Expand Down