Skip to content

Commit 5ba30ad

Browse files
committed
Add quick function call and bump version
1 parent 0cfb017 commit 5ba30ad

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "boolean_function"
33
description = "Mathematical analysis of Boolean functions"
4-
version = "0.1.0"
4+
version = "0.1.1"
55
edition = "2021"
66
rust-version = "1.56.0"
77
authors = ["Thomas Prévost"]
@@ -21,6 +21,7 @@ fast-boolean-anf-transform = "0.0.3"
2121
gen-combinations = "0.1"
2222
enum_dispatch = "0.3"
2323
ouroboros = "0.18"
24+
hackfn = "0.1"
2425

2526
[dev-dependencies]
2627
rayon = "1.10"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ fn main() {
3838

3939
// How many variables does the function have?
4040
assert_eq!(f.variables_count(), 6);
41+
42+
// Compute Boolean function for a given input value
43+
assert_eq!(f(8), false);
4144

4245
// Check if the function is bent
4346
assert!(f.is_bent());

src/big_boolean_function.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use num_bigint::BigUint;
1111
use num_integer::binomial;
1212
use num_traits::{FromPrimitive, One, Zero};
1313
use std::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitXor, BitXorAssign, Mul, MulAssign, Not};
14+
use hackfn::hackfn;
1415

1516
/// Struct representing a boolean function with a big truth table.
1617
///
@@ -551,6 +552,13 @@ impl Not for BigBooleanFunction {
551552
}
552553
}
553554

555+
#[hackfn]
556+
impl BigBooleanFunction {
557+
fn call(&self, input_bits: u32) -> bool {
558+
self.compute_cellular_automata_rule(input_bits)
559+
}
560+
}
561+
554562
#[cfg(test)]
555563
mod tests {
556564
use crate::{AnfPolynomial, BigBooleanFunction, BooleanFunctionError, BooleanFunctionImpl};
@@ -592,6 +600,12 @@ mod tests {
592600
assert_eq!(boolean_function.compute_cellular_automata_rule(64), false);
593601
assert_eq!(boolean_function.compute_cellular_automata_rule(80), true);
594602
assert_eq!(boolean_function.compute_cellular_automata_rule(100), true);
603+
604+
assert_eq!(boolean_function(13), false);
605+
assert_eq!(boolean_function(62), false);
606+
assert_eq!(boolean_function(64), false);
607+
assert_eq!(boolean_function(80), true);
608+
assert_eq!(boolean_function(100), true);
595609
}
596610

597611
#[test]

src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub use small_boolean_function::SmallBooleanFunction;
3333
use std::collections::{HashMap, HashSet};
3434
use std::fmt::Debug;
3535
use std::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitXor, BitXorAssign, Mul, MulAssign, Not};
36+
use hackfn::hackfn;
3637
use crate::iterator::CloseBalancedFunctionIterator;
3738

3839
/// Internal representation of Boolean function
@@ -72,6 +73,8 @@ pub trait BooleanFunctionImpl: Debug {
7273

7374
/// Computes the value of the Boolean function for a given input, as a 32-bit unsigned integer.
7475
///
76+
/// This is equivalent of calling directly your Boolean function object as a function.
77+
///
7578
/// # Parameters
7679
/// - `input_bits`: The input value for which to compute the Boolean function value, the least significant bit being the first variable.
7780
///
@@ -80,6 +83,16 @@ pub trait BooleanFunctionImpl: Debug {
8083
///
8184
/// # Panics
8285
/// If the input value is greater than the maximum input value, and the `unsafe_disable_safety_checks` feature is not enabled.
86+
///
87+
/// # Example
88+
/// ```rust
89+
/// use boolean_function::BooleanFunction;
90+
/// use boolean_function::BooleanFunctionImpl;
91+
///
92+
/// let boolean_function = BooleanFunction::from_hex_string_truth_table("abce1234").unwrap();
93+
/// assert_eq!(boolean_function.compute_cellular_automata_rule(8), false);
94+
/// assert_eq!(boolean_function(8), false); // directly as a function
95+
/// ```
8396
fn compute_cellular_automata_rule(&self, input_bits: u32) -> bool;
8497

8598
/// Computes the Walsh-Hadamard transform of the Boolean function for a given point.
@@ -890,6 +903,13 @@ impl TryFrom<&str> for BooleanFunction {
890903
}
891904
}
892905

906+
#[hackfn]
907+
impl BooleanFunction {
908+
fn call(&self, input_bits: u32) -> bool {
909+
self.compute_cellular_automata_rule(input_bits)
910+
}
911+
}
912+
893913
impl BooleanFunction {
894914
/// Creates a new BooleanFunction from a hexadecimal string representing the truth table.
895915
///
@@ -1168,6 +1188,12 @@ mod tests {
11681188
assert_eq!(boolean_function.compute_cellular_automata_rule(8), false);
11691189
assert_eq!(boolean_function.compute_cellular_automata_rule(23), true);
11701190

1191+
assert_eq!(boolean_function(0), false);
1192+
assert_eq!(boolean_function(1), false);
1193+
assert_eq!(boolean_function(4), true);
1194+
assert_eq!(boolean_function(8), false);
1195+
assert_eq!(boolean_function(23), true);
1196+
11711197
let boolean_function =
11721198
BooleanFunction::from_hex_string_truth_table("7969817CC5893BA6AC326E47619F5AD0")
11731199
.unwrap();
@@ -1176,6 +1202,12 @@ mod tests {
11761202
assert_eq!(boolean_function.compute_cellular_automata_rule(64), false);
11771203
assert_eq!(boolean_function.compute_cellular_automata_rule(80), true);
11781204
assert_eq!(boolean_function.compute_cellular_automata_rule(100), true);
1205+
1206+
assert_eq!(boolean_function(13), false);
1207+
assert_eq!(boolean_function(62), false);
1208+
assert_eq!(boolean_function(64), false);
1209+
assert_eq!(boolean_function(80), true);
1210+
assert_eq!(boolean_function(100), true);
11791211
}
11801212

11811213
#[test]

src/small_boolean_function.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use itertools::{enumerate, Itertools};
1010
use num_bigint::BigUint;
1111
use num_integer::binomial;
1212
use std::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitXor, BitXorAssign, Mul, MulAssign, Not};
13+
use hackfn::hackfn;
1314

1415
/// Struct representing a boolean function with a big truth table.
1516
///
@@ -22,7 +23,7 @@ pub struct SmallBooleanFunction {
2223
truth_table: u64,
2324
}
2425

25-
impl SmallBooleanFunction {
26+
impl SmallBooleanFunction {
2627
/// Creates a new [SmallBooleanFunction] from a truth table and the number of variables.
2728
///
2829
/// # Parameters
@@ -573,6 +574,13 @@ impl Not for SmallBooleanFunction {
573574
}
574575
}
575576

577+
#[hackfn]
578+
impl SmallBooleanFunction {
579+
fn call(&self, input_bits: u32) -> bool {
580+
self.compute_cellular_automata_rule(input_bits)
581+
}
582+
}
583+
576584
#[cfg(test)]
577585
mod tests {
578586
use crate::{AnfPolynomial, BooleanFunctionError, BooleanFunctionImpl, SmallBooleanFunction};
@@ -615,6 +623,12 @@ mod tests {
615623
assert_eq!(boolean_function.compute_cellular_automata_rule(4), true);
616624
assert_eq!(boolean_function.compute_cellular_automata_rule(8), false);
617625
assert_eq!(boolean_function.compute_cellular_automata_rule(23), true);
626+
627+
assert_eq!(boolean_function(0), false);
628+
assert_eq!(boolean_function(1), false);
629+
assert_eq!(boolean_function(4), true);
630+
assert_eq!(boolean_function(8), false);
631+
assert_eq!(boolean_function(23), true);
618632
}
619633

620634
#[test]

0 commit comments

Comments
 (0)