Skip to content

Commit 46cc9cc

Browse files
committed
Add near-bent test
1 parent 44fe9e0 commit 46cc9cc

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/lib.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,24 @@ pub trait BooleanFunctionImpl: Debug {
386386
== ((1 << self.variables_count()) - (1 << (self.variables_count() >> 1))) >> 1
387387
}
388388

389+
/// Returns `true` if the Boolean function is near-bent.
390+
///
391+
/// A $n$-variable Boolean function is said to be *near-bent* if $n$ is odd,
392+
/// and its [Walsh-Hamamard](#method.walsh_hadamard_values) spectrum contains all and only the 3 values $\\{0, \pm 2^{\frac{n+1}{2}}\\}$.
393+
fn is_near_bent(&self) -> bool {
394+
if self.variables_count() & 1 == 0 {
395+
return false;
396+
}
397+
let absolute_walsh_allowed_value = 1 << ((self.variables_count() + 1) >> 1);
398+
let walsh_hadamard_spectrum = (0..=self.get_max_input_value())
399+
.map(|x| self.walsh_hadamard_transform(x))
400+
.collect::<HashSet<_>>();
401+
402+
walsh_hadamard_spectrum.len() == 3 && walsh_hadamard_spectrum.iter().all(|w| {
403+
*w == 0 || *w == absolute_walsh_allowed_value || *w == -absolute_walsh_allowed_value
404+
})
405+
}
406+
389407
/// Returns, if it exists, an annihilator function, its degree and the dimension of annihilator vector space.
390408
///
391409
/// The annihilator of a Boolean function $f$ is a non-null Boolean function $g$ such that:
@@ -642,7 +660,7 @@ pub trait BooleanFunctionImpl: Debug {
642660
self.biguint_truth_table().to_u64()
643661
}
644662

645-
// TODO almost (near?) bent, mul (and tt)
663+
// TODO, mul (and tt)
646664
}
647665

648666
/// This type is used to store a boolean function with any number of variables.
@@ -1325,6 +1343,22 @@ mod tests {
13251343
assert!(!boolean_function.is_bent());
13261344
}
13271345

1346+
#[test]
1347+
fn test_is_near_bent() {
1348+
let boolean_function = BooleanFunction::from_hex_string_truth_table("f9")
1349+
.unwrap();
1350+
assert!(boolean_function.is_near_bent());
1351+
1352+
let boolean_function = BooleanFunction::from_hex_string_truth_table("ff")
1353+
.unwrap();
1354+
assert!(!boolean_function.is_near_bent());
1355+
1356+
// even variable count -> cannot be near-bent
1357+
let boolean_function = BooleanFunction::from_hex_string_truth_table("f9f9")
1358+
.unwrap();
1359+
assert!(!boolean_function.is_near_bent());
1360+
}
1361+
13281362
#[test]
13291363
fn test_annihilator() {
13301364
let boolean_function =

0 commit comments

Comments
 (0)