Skip to content

Commit

Permalink
optimize mp creation, add parameter for phase 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jirigav committed Oct 4, 2023
1 parent 7798836 commit 9ee3c62
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub(crate) struct Args {
#[arg(short, long, default_value_t = 100)]
pub(crate) min_difference: usize,

/// Number of patterns combined into a multipattern.
#[arg(long, default_value_t = 50)]
pub(crate) top_n: usize,

/// Number of patterns combined into a multipattern.
#[arg(short, long, default_value_t = 2)]
pub(crate) patterns_combined: usize,
Expand Down
24 changes: 14 additions & 10 deletions src/distinguishers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,22 @@ impl Distinguisher for MultiPattern {
}

pub(crate) fn best_multi_pattern(data: &Data, patterns: &[Pattern], n: usize) -> MultiPattern {
let len_m1 = data.data.len() - 1;
let mut best_mp: Option<MultiPattern> = None;
let mut max_z = 0.0;
for ps in patterns.iter().combinations(n) {
let mut mp = MultiPattern::new(ps.iter().map(|x| x.to_owned().clone()).collect_vec());
mp.count = Some(
data.data
.par_iter()
.enumerate()
.map(|(i, blocks)| mp.evaluate(blocks, data.mask, i == len_m1))
.sum::<u32>() as usize,
);
let patterns_sets = patterns.iter().combinations(n).collect_vec();

let mut mps = patterns_sets
.par_iter()
.map(|ps| MultiPattern::new(ps.iter().map(|x| x.to_owned().clone()).collect_vec()))
.collect::<Vec<_>>();

let mut it = data.data.iter().peekable();
while let Some(blocks) = it.next() {
let is_last = it.peek().is_none();
mps.par_iter_mut()
.for_each(|mp| mp.increase_count(mp.evaluate(blocks, data.mask, is_last) as usize));
}
for mut mp in mps {
let z = mp.z_score(data.num_of_blocks);
if f64::abs(z) > f64::abs(max_z) {
best_mp = Some(mp);
Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ fn parse_args(s: Vec<&str>) -> Args {
block_size: s[1].trim().parse().unwrap(),
k: s[2].trim().parse().unwrap(),
min_difference: s[3].trim().parse().unwrap(),
patterns_combined: s[4].trim().parse().unwrap(),
base_pattern_size: s[5].trim().parse().unwrap(),
validation_and_testing_split: s[6].trim().parse().unwrap(),
hist: s[7].trim().parse().unwrap(),
top_n: s[4].trim().parse().unwrap(),
patterns_combined: s[5].trim().parse().unwrap(),
base_pattern_size: s[6].trim().parse().unwrap(),
validation_and_testing_split: s[7].trim().parse().unwrap(),
hist: s[8].trim().parse().unwrap(),
config: false,
}
}
Expand Down

0 comments on commit 9ee3c62

Please sign in to comment.