Performance Improvements thanks to @hdevalence

This commit is contained in:
Sarah Jamie Lewis 2021-02-01 13:25:26 -08:00
parent 4231150c1c
commit e383dd8378
1 changed files with 16 additions and 26 deletions

View File

@ -1,4 +1,5 @@
#![deny(missing_docs)]
#![feature(array_methods)]
#![feature(external_doc)]
#![doc(include = "../README.md")]
#![doc(include = "../ANONYMITY.md")]
@ -7,13 +8,14 @@ use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT;
use curve25519_dalek::digest::Digest;
use curve25519_dalek::ristretto::RistrettoPoint;
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::traits::MultiscalarMul;
use rand::rngs::OsRng;
use serde::Deserialize;
use serde::Serialize;
use sha3::Sha3_512;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::ops::{Add, Mul, Sub};
use std::ops::{Mul, Sub};
/// A tag is a probabilistic cryptographic structure. When constructed for a given `FuzzyPublicKey`
/// it will pass the `FuzzyDetectionKey::test` 100% of the time. For other public keys
@ -73,10 +75,7 @@ impl FuzzyDetectionKey {
for s in self.0.iter() {
hash.update(s.as_bytes())
}
format!(
"{}",
hex::encode(hash.finalize().as_slice()),
)
format!("{}", hex::encode(hash.finalize().as_slice()),)
}
}
@ -135,7 +134,7 @@ impl FuzzyDetectionKey {
// w = g^m + g^(z-m)
// w = g^z
// See below for a full explanation as to the reason for this:
let w = g.mul(m).add(tag.u.mul(tag.y));
let w = RistrettoPoint::multiscalar_mul(&[m, tag.y], &[g, tag.u]);
// for each secret key part...
let mut result = true;
@ -166,7 +165,6 @@ 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 {
@ -174,10 +172,7 @@ impl FuzzyPublicKey {
for s in self.0.iter() {
hash.update(s.compress().as_bytes())
}
format!(
"{}",
hex::encode(hash.finalize().as_slice()),
)
format!("{}", hex::encode(hash.finalize().as_slice()),)
}
/// generate a new tag for this public key
@ -229,8 +224,6 @@ impl FuzzyPublicKey {
}
}
/// An identity keypair for generating and validating fuzzy meta tags.
#[derive(Debug, Serialize, Deserialize)]
pub struct FuzzyTagKeyPair {
@ -279,24 +272,21 @@ impl FuzzyTagKeyPair {
self.secret_key.extract(n)
}
/// a hash function that takes 3 risretto points as a parameter and outputs 0 or 1.
/// a hash function that takes 3 ristretto points as a parameter and outputs 0 or 1.
fn h(u: RistrettoPoint, h: RistrettoPoint, w: RistrettoPoint) -> u8 {
let hash = sha3::Sha3_256::digest(
format!(
"{}{}{}",
hex::encode(u.compress().as_bytes()),
hex::encode(h.compress().as_bytes()),
hex::encode(w.compress().as_bytes())
)
.as_bytes(),
);
return hash.as_slice()[0] & 0x01;
let mut hash = sha3::Sha3_256::new();
hash.update(u.compress().as_bytes());
hash.update(h.compress().as_bytes());
hash.update(w.compress().as_bytes());
return hash.finalize().as_slice()[0] & 0x01;
}
/// a hash function which takes a ristretto point and a vector of ciphertexts and ouputs a
/// a hash function which takes a ristretto point and a vector of ciphertexts and outputs a
/// ristretto scalar.
fn g(u: RistrettoPoint, points: &BitVec) -> Scalar {
Scalar::hash_from_bytes::<Sha3_512>(format!("{}{}", hex::encode(u.compress().as_bytes()), hex::encode(points.to_bytes())).as_bytes())
let mut input = points.to_bytes().as_slice().to_vec();
input.extend_from_slice(u.compress().as_bytes());
Scalar::hash_from_bytes::<Sha3_512>(input.as_slice())
}
}