diff --git a/crates/lox-library/src/dup_filter.rs b/crates/lox-library/src/dup_filter.rs index 48681ed..72f00df 100644 --- a/crates/lox-library/src/dup_filter.rs +++ b/crates/lox-library/src/dup_filter.rs @@ -3,13 +3,13 @@ * the table of seen ids in memory, but a production one would of course * use a disk-backed database. */ +use std::cmp::Eq; use std::collections::HashMap; use std::hash::Hash; -use std::cmp::Eq; /// Each instance of DupFilter maintains its own independent table of /// seen ids. IdType will typically be Scalar. -#[derive(Default,Debug)] +#[derive(Default, Debug)] pub struct DupFilter { seen_table: HashMap, } @@ -25,7 +25,7 @@ impl DupFilter { /// 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 /// 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, ()) { None => Ok(()), Some(()) => Err(()), diff --git a/crates/lox-library/src/lib.rs b/crates/lox-library/src/lib.rs index ad541d7..dc17633 100644 --- a/crates/lox-library/src/lib.rs +++ b/crates/lox-library/src/lib.rs @@ -17,6 +17,8 @@ The notation follows that of the paper "Hyphae: Social Secret Sharing" #[macro_use] extern crate zkp; +pub mod dup_filter; + use sha2::Sha512; use rand::rngs::OsRng; diff --git a/crates/lox-library/tests/tests.rs b/crates/lox-library/tests/tests.rs index 1373662..ad6bc4b 100644 --- a/crates/lox-library/tests/tests.rs +++ b/crates/lox-library/tests/tests.rs @@ -1,5 +1,8 @@ +use lox::dup_filter; use lox::BridgeDb; +use curve25519_dalek::scalar::Scalar; + #[test] fn test_openinvite() { let bdb = BridgeDb::new(20); @@ -8,3 +11,35 @@ fn test_openinvite() { let res = BridgeDb::verify(inv, bdb.pubkey); println!("{:?}", res); } + +#[test] +fn test_dup_filter() { + let mut df1: dup_filter::DupFilter = Default::default(); + let mut df2: dup_filter::DupFilter = 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); +}