test for dup_filter module

This commit is contained in:
Ian Goldberg 2021-04-27 08:53:22 -04:00
parent 23d66aaae3
commit befacce4ef
3 changed files with 40 additions and 3 deletions

View File

@ -3,13 +3,13 @@
* the table of seen ids in memory, but a production one would of course * the table of seen ids in memory, but a production one would of course
* use a disk-backed database. */ * use a disk-backed database. */
use std::cmp::Eq;
use std::collections::HashMap; use std::collections::HashMap;
use std::hash::Hash; use std::hash::Hash;
use std::cmp::Eq;
/// Each instance of DupFilter maintains its own independent table of /// Each instance of DupFilter maintains its own independent table of
/// seen ids. IdType will typically be Scalar. /// seen ids. IdType will typically be Scalar.
#[derive(Default,Debug)] #[derive(Default, Debug)]
pub struct DupFilter<IdType> { pub struct DupFilter<IdType> {
seen_table: HashMap<IdType, ()>, seen_table: HashMap<IdType, ()>,
} }
@ -25,7 +25,7 @@ impl<IdType: Hash + Eq + Copy> DupFilter<IdType> {
/// As atomically as possible, check to see if the id is in the seen /// As atomically as possible, check to see if the id is in the seen
/// table, and add it if not. Return Ok(()) if it was not already /// table, and add it if not. Return Ok(()) if it was not already
/// in the table, and Err(()) if it was. /// in the table, and Err(()) if it was.
pub fn filter(&mut self, id: &IdType) -> Result<(),()> { pub fn filter(&mut self, id: &IdType) -> Result<(), ()> {
match self.seen_table.insert(*id, ()) { match self.seen_table.insert(*id, ()) {
None => Ok(()), None => Ok(()),
Some(()) => Err(()), Some(()) => Err(()),

View File

@ -17,6 +17,8 @@ The notation follows that of the paper "Hyphae: Social Secret Sharing"
#[macro_use] #[macro_use]
extern crate zkp; extern crate zkp;
pub mod dup_filter;
use sha2::Sha512; use sha2::Sha512;
use rand::rngs::OsRng; use rand::rngs::OsRng;

View File

@ -1,5 +1,8 @@
use lox::dup_filter;
use lox::BridgeDb; use lox::BridgeDb;
use curve25519_dalek::scalar::Scalar;
#[test] #[test]
fn test_openinvite() { fn test_openinvite() {
let bdb = BridgeDb::new(20); let bdb = BridgeDb::new(20);
@ -8,3 +11,35 @@ fn test_openinvite() {
let res = BridgeDb::verify(inv, bdb.pubkey); let res = BridgeDb::verify(inv, bdb.pubkey);
println!("{:?}", res); println!("{:?}", res);
} }
#[test]
fn test_dup_filter() {
let mut df1: dup_filter::DupFilter<Scalar> = Default::default();
let mut df2: dup_filter::DupFilter<Scalar> = Default::default();
let mut rng = rand::thread_rng();
let s1 = Scalar::random(&mut rng);
let s2 = Scalar::random(&mut rng);
let s3 = Scalar::random(&mut rng);
let s4 = Scalar::random(&mut rng);
let s5 = Scalar::random(&mut rng);
// Check basic behaviour
assert_eq!(df1.check(&s1), false);
assert_eq!(df1.filter(&s1), Ok(()));
assert_eq!(df1.check(&s1), true);
assert_eq!(df1.filter(&s1), Err(()));
// Ensure different instances of DupFilter have different tables
assert_eq!(df2.check(&s1), false);
assert_eq!(df2.filter(&s1), Ok(()));
assert_eq!(df2.filter(&s1), Err(()));
assert_eq!(df2.check(&s1), true);
// Check multiple ids
assert_eq!(df1.check(&s2), false);
assert_eq!(df1.filter(&s3), Ok(()));
assert_eq!(df1.filter(&s4), Ok(()));
assert_eq!(df1.filter(&s3), Err(()));
assert_eq!(df1.check(&s1), true);
assert_eq!(df1.filter(&s1), Err(()));
assert_eq!(df1.filter(&s5), Ok(()));
println!("df1 = {:?}", df1);
println!("df2 = {:?}", df2);
}