Formatting / Cleanup

This commit is contained in:
Sarah Jamie Lewis 2021-02-13 20:39:57 -08:00
parent 85dc8ed9ad
commit 6ddc684755
1 changed files with 18 additions and 21 deletions

View File

@ -22,14 +22,14 @@ use brute_force::adaptors;
#[cfg(feature = "entangled")]
use brute_force::brute_force;
#[cfg(feature = "bulk_verify")]
use std::sync::mpsc::channel;
#[cfg(feature = "bulk_verify")]
use rayon::iter::IndexedParallelIterator;
#[cfg(feature = "bulk_verify")]
use rayon::iter::IntoParallelRefIterator;
#[cfg(feature = "bulk_verify")]
use rayon::iter::ParallelIterator;
#[cfg(feature = "bulk_verify")]
use std::sync::mpsc::channel;
/// A tag is a probabilistic cryptographic structure. When constructed for a given `TaggingKey`
/// it will pass the `DetectionKey::test_tag` 100% of the time. For other tagging keys
@ -202,9 +202,7 @@ impl<const GAMMA: u8> RootSecret<{ GAMMA }> {
let sk_i = Scalar::random(&mut rng);
secret.push(sk_i);
}
RootSecret::<GAMMA> {
secret: secret
}
RootSecret::<GAMMA> { secret: secret }
}
/// extract a detection key for a given false positive (p = 2^-n)
@ -236,9 +234,7 @@ impl<const GAMMA: u8> RootSecret<{ GAMMA }> {
let pk_i = g.mul(sk_i);
tagging_key.push(pk_i);
}
TaggingKey::<GAMMA>{
0: tagging_key
}
TaggingKey::<GAMMA> { 0: tagging_key }
}
/// a hash function that takes 3 ristretto points as a parameter and outputs 0 or 1.
@ -358,7 +354,7 @@ impl<const GAMMA: u8> DetectionKey<{ GAMMA }> {
let b_i = k_i ^ c_i;
if b_i != 1 {
if b_i != 1 {
return false;
}
// assert that the plaintext is all 1's
@ -367,11 +363,10 @@ impl<const GAMMA: u8> DetectionKey<{ GAMMA }> {
return result;
}
/// A bulk testing function that takes in an vector of detection keys and returns a vector
/// of indexes where the tag matched.
#[cfg(feature = "bulk_verify")]
pub fn test_tag_bulk(detection_keys: &Vec<DetectionKey<{GAMMA}>>, tag: &Tag<{ GAMMA }>) -> Vec<usize> {
pub fn test_tag_bulk(detection_keys: &Vec<DetectionKey<{ GAMMA }>>, tag: &Tag<{ GAMMA }>) -> Vec<usize> {
// A few checks to make sure the tag is well formed.
// All zeros in u or y can lead to a tag that validates against *all* tagging keys
// That doesn't seem like a great idea, so we return false to be safe.
@ -395,8 +390,8 @@ impl<const GAMMA: u8> DetectionKey<{ GAMMA }> {
let (tx, rx) = channel();
// for each secret part...
let mut results : Vec<(usize)> = vec![];
detection_keys.par_iter().enumerate().for_each_with(tx.clone(), |tx,(index,detection_key)| {
let mut results: Vec<usize> = vec![];
detection_keys.par_iter().enumerate().for_each_with(tx.clone(), |tx, (index, detection_key)| {
let mut result = true;
for (i, x_i) in detection_key.0.iter().enumerate() {
// re-derive the key from the tag
@ -421,7 +416,11 @@ impl<const GAMMA: u8> DetectionKey<{ GAMMA }> {
result = result & (b_i == 1);
}
if result {
tx.send(index);
match tx.send(index) {
_ => {
// TODO...surface this error...
}
}
}
});
@ -429,9 +428,7 @@ impl<const GAMMA: u8> DetectionKey<{ GAMMA }> {
loop {
let result = rx.recv();
match result {
Ok(index) => {
results.push(index)
}
Ok(index) => results.push(index),
_ => {
break;
}
@ -574,7 +571,7 @@ impl<const GAMMA: u8> TaggingKey<{ GAMMA }> {
#[cfg(test)]
mod tests {
use crate::{RootSecret, Tag, DetectionKey};
use crate::{DetectionKey, RootSecret, Tag};
use bit_vec::BitVec;
use curve25519_dalek::ristretto::RistrettoPoint;
use curve25519_dalek::scalar::Scalar;
@ -662,11 +659,11 @@ mod tests {
let tagging_keys: Vec<TaggingKey<24>> = secrets.iter().map(|x| x.tagging_key()).collect();
// it takes ~15 minutes on a standard desktop to find a length=24 match for 2 parties, so for testing let's keep things light
let entangled_tag = TaggingKey::generate_entangled_tag(tagging_keys, 16);
let detection_keys = secrets.iter().map(|x|x.extract_detection_key(16)).collect();
let detection_keys = secrets.iter().map(|x| x.extract_detection_key(16)).collect();
let results = DetectionKey::test_tag_bulk(&detection_keys, &entangled_tag);
for result in results.iter() {
assert_eq!(*result, true);
for (index, rindex) in results.iter().enumerate() {
assert_eq!(index, *rindex);
}
}