From 4231150c1cbf1a0b1abab4c33ef54570e0c0b799 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 1 Feb 2021 11:28:40 -0800 Subject: [PATCH] A few small convieniance functions for the simulator / integrations --- src/lib.rs | 63 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e8cf965..e783732 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,13 +22,25 @@ use std::ops::{Add, Mul, Sub}; /// * Correctness: Valid tags constructed for a specific public key will always validate when tested using the detection key /// * Fuzziness: Invalid tags will produce false positives with probability _p_ related to the security property (_γ_) /// * Security: An adversarial server with access to the detection key is unable to distinguish false positives from true positives. (Detection Ambiguity) -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct FuzzyTag { u: RistrettoPoint, y: Scalar, ciphertexts: BitVec, } +impl Display for FuzzyTag { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!( + f, + "{} {} {}", + hex::encode(self.u.compress().as_bytes()), + hex::encode(self.y.as_bytes()), + hex::encode(self.ciphertexts.to_bytes()) + ) + } +} + /// The complete secret key. Can't directly be used for testing. Instead you will need to generate /// a FuzzyDetectionKey using extract #[derive(Debug, Serialize, Deserialize)] @@ -50,9 +62,30 @@ impl FuzzySecretKey { /// A collection of "secret" data that can be used to determine if a `FuzzyTag` was intended for /// the derived public key with probability p -#[derive(Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct FuzzyDetectionKey(Vec); +impl FuzzyDetectionKey { + /// a convenient id for a detection key for internal accounting purposes + /// do not expose this to applications + pub fn id(&self) -> String { + let mut hash = sha3::Sha3_256::new(); + for s in self.0.iter() { + hash.update(s.as_bytes()) + } + format!( + "{}", + hex::encode(hash.finalize().as_slice()), + ) + } +} + +impl Display for FuzzyDetectionKey { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.id()) + } +} + impl FuzzyDetectionKey { /// calculate the ideal false positive rate of this detection key /// ``` @@ -133,6 +166,20 @@ impl FuzzyDetectionKey { pub struct FuzzyPublicKey(Vec); impl FuzzyPublicKey { + + /// a convenient id for a public key for internal accounting purposes + /// do not expose this to applications + pub fn id(&self) -> String { + let mut hash = sha3::Sha3_256::new(); + for s in self.0.iter() { + hash.update(s.compress().as_bytes()) + } + format!( + "{}", + hex::encode(hash.finalize().as_slice()), + ) + } + /// generate a new tag for this public key /// Example: /// ``` @@ -182,17 +229,7 @@ impl FuzzyPublicKey { } } -impl Display for FuzzyTag { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!( - f, - "{} {} {}", - hex::encode(self.u.compress().as_bytes()), - hex::encode(self.y.as_bytes()), - hex::encode(self.ciphertexts.to_bytes()) - ) - } -} + /// An identity keypair for generating and validating fuzzy meta tags. #[derive(Debug, Serialize, Deserialize)]