Skip to content

Commit

Permalink
mastic: Remove bits field from Mastic struct
Browse files Browse the repository at this point in the history
This is redundant since `Vidpf` now needs to know the bit length.
  • Loading branch information
cjpatton committed Jan 13, 2025
1 parent b22c783 commit 400d9e1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
7 changes: 2 additions & 5 deletions src/vdaf/mastic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ pub struct Mastic<T: Type> {
id: [u8; 4],
pub(crate) szk: Szk<T>,
pub(crate) vidpf: Vidpf<VidpfWeight<T::Field>>,
/// The length of the private attribute associated with any input.
pub(crate) bits: usize,
}

impl<T: Type> Mastic<T> {
Expand All @@ -76,7 +74,6 @@ impl<T: Type> Mastic<T> {
id: algorithm_id.to_le_bytes(),
szk,
vidpf,
bits,
})
}

Expand Down Expand Up @@ -143,7 +140,7 @@ impl<T: Type> ParameterizedDecode<Mastic<T>> for MasticPublicShare<VidpfWeight<T
mastic: &Mastic<T>,
bytes: &mut Cursor<&[u8]>,
) -> Result<Self, CodecError> {
VidpfPublicShare::decode_with_param(&(mastic.bits, mastic.vidpf.weight_parameter), bytes)
VidpfPublicShare::decode_with_param(&mastic.vidpf, bytes)
}
}

Expand Down Expand Up @@ -262,7 +259,7 @@ impl<T: Type> Mastic<T> {
szk_random: [Seed<SEED_SIZE>; 2],
joint_random_opt: Option<Seed<SEED_SIZE>>,
) -> Result<(<Self as Vdaf>::PublicShare, Vec<<Self as Vdaf>::InputShare>), VdafError> {
if alpha.len() != self.bits {
if alpha.len() != usize::from(self.vidpf.bits) {
return Err(VdafError::Vidpf(VidpfError::InvalidInputLength));
}

Expand Down
29 changes: 12 additions & 17 deletions src/vidpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,18 +500,16 @@ impl<W: VidpfValue> Encode for VidpfPublicShare<W> {
}
}

impl<W: VidpfValue> ParameterizedDecode<(usize, W::ValueParameter)> for VidpfPublicShare<W> {
fn decode_with_param(
(bits, weight_parameter): &(usize, W::ValueParameter),
bytes: &mut Cursor<&[u8]>,
) -> Result<Self, CodecError> {
impl<W: VidpfValue> ParameterizedDecode<Vidpf<W>> for VidpfPublicShare<W> {
fn decode_with_param(vidpf: &Vidpf<W>, bytes: &mut Cursor<&[u8]>) -> Result<Self, CodecError> {
let bits = usize::from(vidpf.bits);
let packed_control_len = (bits + 3) / 4;
let mut packed_control_bits = vec![0u8; packed_control_len];
bytes.read_exact(&mut packed_control_bits)?;
let unpacked_control_bits: BitVec<u8, Lsb0> = BitVec::from_vec(packed_control_bits);

// Control bits
let mut control_bits = Vec::with_capacity(*bits);
let mut control_bits = Vec::with_capacity(bits);
for chunk in unpacked_control_bits[0..bits * 2].chunks(2) {
control_bits.push([(chunk[0] as u8).into(), (chunk[1] as u8).into()]);
}
Expand All @@ -523,20 +521,21 @@ impl<W: VidpfValue> ParameterizedDecode<(usize, W::ValueParameter)> for VidpfPub

// Seeds
let seeds = std::iter::repeat_with(|| Seed::decode(bytes).map(|seed| seed.0))
.take(*bits)
.take(bits)
.collect::<Result<Vec<_>, _>>()?;

// Weights
let weights = std::iter::repeat_with(|| W::decode_with_param(weight_parameter, bytes))
.take(*bits)
.collect::<Result<Vec<_>, _>>()?;
let weights =
std::iter::repeat_with(|| W::decode_with_param(&vidpf.weight_parameter, bytes))
.take(bits)
.collect::<Result<Vec<_>, _>>()?;

let proofs = std::iter::repeat_with(|| {
let mut proof = [0; VIDPF_PROOF_SIZE];
bytes.read_exact(&mut proof)?;
Ok::<_, CodecError>(proof)
})
.take(*bits)
.take(bits)
.collect::<Result<Vec<_>, _>>()?;

let cw = seeds
Expand Down Expand Up @@ -840,16 +839,12 @@ mod tests {
let ctx = b"appliction context";
let input = VidpfInput::from_bytes(&[0xFF]);
let weight = TestWeight::from(vec![21.into(), 22.into(), 23.into()]);
let (_, public, _, _) = vidpf_gen_setup(ctx, &input, &weight);
let (vidpf, public, _, _) = vidpf_gen_setup(ctx, &input, &weight);

let bytes = public.get_encoded().unwrap();
assert_eq!(public.encoded_len().unwrap(), bytes.len());

let decoded = VidpfPublicShare::<TestWeight>::get_decoded_with_param(
&(8, TEST_WEIGHT_LEN),
&bytes,
)
.unwrap();
let decoded = VidpfPublicShare::get_decoded_with_param(&vidpf, &bytes).unwrap();
assert_eq!(public, decoded);
}

Expand Down

0 comments on commit 400d9e1

Please sign in to comment.