A few small convieniance functions for the simulator / integrations

This commit is contained in:
Sarah Jamie Lewis 2021-02-01 11:28:40 -08:00
parent a17f5ffe0d
commit 4231150c1c
1 changed files with 50 additions and 13 deletions

View File

@ -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<Scalar>);
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<RistrettoPoint>);
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)]