diff --git a/polyfuzzy/src/fmd2_compact/mod.rs b/polyfuzzy/src/fmd2_compact/mod.rs index cc750e4..a47c4c5 100644 --- a/polyfuzzy/src/fmd2_compact/mod.rs +++ b/polyfuzzy/src/fmd2_compact/mod.rs @@ -90,6 +90,11 @@ impl CompactPublicKey { } } + /// Return the threshold of this [`CompactPublicKey`]. + pub fn threshold(&self) -> usize { + self.polynomial.coeffs.len() - 1 + } + fn from_poly(polynomial: EncodedPolynomial) -> Self { let fingerprint = { let mut hasher = Sha256::new(); @@ -129,6 +134,39 @@ impl CompressedCompactPublicKey { coeffs: self.coeffs, }) } + + /// Return the threshold of this [`CompressedCompactPublicKey`]. + pub fn threshold(&self) -> usize { + self.coeffs.len() - 1 + } + + /// Return a compact byte representation of the coefficients + /// of this public key. + pub fn to_coeff_repr(&self) -> Vec { + self.coeffs + .iter() + .map(|coeff| coeff.compress().0) + .collect::>() + .into_flattened() + } + + /// Parse a compact byte representation of the polynomial coefficients + /// of a public key. + pub fn from_coeff_repr(repr: &[u8]) -> Option { + if repr.len() % 32 != 0 { + return None; + } + + let mut buf = [0u8; 32]; + + repr.chunks(32) + .map(|chunk| { + buf.copy_from_slice(chunk); + curve25519_dalek::ristretto::CompressedRistretto(buf).decompress() + }) + .collect::>>() + .map(|coeffs| Self { coeffs }) + } } /// The evaluations of the secret polynomial @@ -452,6 +490,18 @@ mod tests { } } + #[test] + fn test_pubkey_thres() { + let mut csprng = rand_core::OsRng; + + let gamma = 20; + let threshold = 1; + let mut compact_multi_fmd2 = MultiFmd2CompactScheme::new(gamma, threshold); + + let (_, pk) = compact_multi_fmd2.generate_keys(&mut csprng); + assert_eq!(pk.threshold(), threshold); + } + fn hash_into_64_bytes(bytes: &[u8]) -> [u8; 64] { let mut hasher = Sha512::new(); hasher.update(bytes);