diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..b355306 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,66 @@ +max_width = 200 +hard_tabs = false +tab_spaces = 4 +newline_style = "Auto" +use_small_heuristics = "Default" +indent_style = "Block" +wrap_comments = false +format_code_in_doc_comments = false +comment_width = 80 +normalize_comments = false +normalize_doc_attributes = false +license_template_path = "" +format_strings = false +format_macro_matchers = false +format_macro_bodies = true +empty_item_single_line = true +struct_lit_single_line = true +fn_single_line = false +where_single_line = false +imports_indent = "Block" +imports_layout = "Mixed" +merge_imports = false +reorder_imports = true +reorder_modules = true +reorder_impl_items = false +type_punctuation_density = "Wide" +space_before_colon = false +space_after_colon = true +spaces_around_ranges = false +binop_separator = "Front" +remove_nested_parens = true +combine_control_expr = true +overflow_delimited_expr = false +struct_field_align_threshold = 0 +enum_discrim_align_threshold = 0 +match_arm_blocks = true +force_multiline_blocks = false +fn_args_layout = "Tall" +brace_style = "SameLineWhere" +control_brace_style = "AlwaysSameLine" +trailing_semicolon = true +trailing_comma = "Vertical" +match_block_trailing_comma = false +blank_lines_upper_bound = 1 +blank_lines_lower_bound = 0 +edition = "2015" +version = "One" +inline_attribute_width = 0 +merge_derives = true +use_try_shorthand = false +use_field_init_shorthand = false +force_explicit_abi = true +condense_wildcard_suffixes = false +color = "Auto" +required_version = "1.4.21" +unstable_features = false +disable_all_formatting = false +skip_children = false +hide_parse_errors = false +error_on_line_overflow = false +error_on_unformatted = false +report_todo = "Never" +report_fixme = "Never" +ignore = [] +emit_mode = "Files" +make_backup = false diff --git a/src/lib.rs b/src/lib.rs index f80d2b5..31345a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,32 +1,38 @@ #![feature(test)] -use std::ops::{Mul, Sub, Add}; -use curve25519_dalek::scalar::Scalar; -use curve25519_dalek::ristretto::RistrettoPoint; -use rand::rngs::OsRng; -use curve25519_dalek::digest::Digest; -use sha3::{Sha3_512}; -use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT; -use std::fmt::{Display, Formatter}; use bit_vec::BitVec; +use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT; +use curve25519_dalek::digest::Digest; +use curve25519_dalek::ristretto::RistrettoPoint; +use curve25519_dalek::scalar::Scalar; +use rand::rngs::OsRng; +use sha3::Sha3_512; use std::fmt; +use std::fmt::{Display, Formatter}; +use std::ops::{Add, Mul, Sub}; #[derive(Debug)] pub struct FuzzyMetaTag { u: RistrettoPoint, y: Scalar, - ciphertexts: BitVec + ciphertexts: BitVec, } impl Display for FuzzyMetaTag { 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())) + write!( + f, + "{} {} {}", + hex::encode(self.u.compress().as_bytes()), + hex::encode(self.y.as_bytes()), + hex::encode(self.ciphertexts.to_bytes()) + ) } } #[derive(Debug)] pub struct FuzzyMetaTagKey { s_keys: Vec, - p_keys: Vec + p_keys: Vec, } impl FuzzyMetaTagKey { @@ -41,15 +47,20 @@ impl FuzzyMetaTagKey { s_keys.push(sk_i); p_keys.push(pk_i); } - FuzzyMetaTagKey { - s_keys, - p_keys - } + FuzzyMetaTagKey { s_keys, p_keys } } - 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 + 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; } fn g(u: RistrettoPoint, points: &BitVec) -> Scalar { @@ -59,9 +70,9 @@ impl FuzzyMetaTagKey { pub fn flag(&self) -> FuzzyMetaTag { let mut rng = OsRng::default(); let g = RISTRETTO_BASEPOINT_POINT; - let r = Scalar::random(&mut rng); + let r = Scalar::random(&mut rng); let u = g.mul(r); - let z = Scalar::random(&mut rng); + let z = Scalar::random(&mut rng); let w = g.mul(z); let mut ciphertexts = BitVec::new(); @@ -73,11 +84,7 @@ impl FuzzyMetaTagKey { let m = FuzzyMetaTagKey::g(u, &ciphertexts); let y = r.invert().mul(z.sub(m)); - return FuzzyMetaTag { - u, - y, - ciphertexts - } + return FuzzyMetaTag { u, y, ciphertexts }; } pub fn test(&self, tag: &FuzzyMetaTag) -> bool { @@ -86,23 +93,21 @@ impl FuzzyMetaTagKey { let g = RISTRETTO_BASEPOINT_POINT; let w = g.mul(m).add(tag.u.mul(tag.y)); - for (i,x_i) in self.s_keys.iter().enumerate() { + for (i, x_i) in self.s_keys.iter().enumerate() { let k_i = FuzzyMetaTagKey::h(tag.u, tag.u.mul(x_i), w); let c_i = match tag.ciphertexts.get(i).unwrap() { true => 0x01, - false => 0x00, + false => 0x00, }; let b_i = k_i ^ c_i; if b_i != 1 { - return false + return false; } } - return true + return true; } } - - #[cfg(test)] mod tests { extern crate test; @@ -122,17 +127,21 @@ mod tests { #[bench] fn generate(b: &mut Bencher) { - let number_of_messages = 1000; - let key = FuzzyMetaTagKey::generate(3); - let mut false_positives = 0; - for _i in 0..number_of_messages { - let key2 = FuzzyMetaTagKey::generate(3); - let tag = key2.flag(); - assert!(key2.test(&tag)); - if key.test(&tag) == true { - false_positives += 1; - } - } - println!("Expected False Positive Rate: {}\nActual False Positive Rate: {}", (2.0_f64).powi(-3), (false_positives as f64 / number_of_messages as f64)); + let number_of_messages = 1000; + let key = FuzzyMetaTagKey::generate(3); + let mut false_positives = 0; + for _i in 0..number_of_messages { + let key2 = FuzzyMetaTagKey::generate(3); + let tag = key2.flag(); + assert!(key2.test(&tag)); + if key.test(&tag) == true { + false_positives += 1; + } + } + println!( + "Expected False Positive Rate: {}\nActual False Positive Rate: {}", + (2.0_f64).powi(-3), + (false_positives as f64 / number_of_messages as f64) + ); } -} \ No newline at end of file +}