Skip to content

Commit f4dc2ee

Browse files
committed
Compute BooleanFunction from ANF string representation
1 parent ddbee82 commit f4dc2ee

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

src/anf_polynom.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use itertools::Itertools;
66
use num_bigint::BigUint;
77
use num_traits::{One, Zero};
88
use std::fmt::Display;
9-
use crate::BooleanFunctionError;
9+
use fast_boolean_anf_transform::fast_bool_anf_transform_unsigned;
10+
use crate::{BigBooleanFunction, BooleanFunction, BooleanFunctionError, SmallBooleanFunction};
11+
use crate::utils::fast_anf_transform_biguint;
1012

1113
#[derive(Debug, Clone, Eq, PartialEq)]
1214
enum PolynomialFormat {
@@ -127,7 +129,7 @@ impl AnfPolynomial {
127129
///
128130
/// Representation must be in the form "`x0*x2*x3 + x2*x3 + x1 + 1`".
129131
///
130-
/// X's index start at 0, meaning the maximum index is variable count - 1.
132+
/// X's index starts at 0, meaning the maximum index is variable count - 1.
131133
///
132134
/// # Parameters:
133135
/// - `anf_polynomial`: The string representation of the ANF form
@@ -209,6 +211,31 @@ impl AnfPolynomial {
209211
PolynomialFormat::Big(_) => crate::BooleanFunctionType::Big
210212
}
211213
}
214+
215+
/// Convert ANF polynomial to the corresponding Boolean Function, using fast ANF transform algorithm
216+
///
217+
/// # Returns
218+
/// A Boolean function corresponding to the polynomial
219+
pub fn to_boolean_function(&self) -> BooleanFunction {
220+
match &self.polynomial {
221+
PolynomialFormat::Small(polynomial) => {
222+
BooleanFunction::Small(
223+
SmallBooleanFunction::from_truth_table(
224+
fast_bool_anf_transform_unsigned(*polynomial, self.num_variables),
225+
self.num_variables
226+
).unwrap()
227+
)
228+
}
229+
PolynomialFormat::Big(polynomial) => {
230+
BooleanFunction::Big(
231+
BigBooleanFunction::from_truth_table(
232+
fast_anf_transform_biguint(polynomial, self.num_variables),
233+
self.num_variables
234+
)
235+
)
236+
}
237+
}
238+
}
212239
}
213240

214241
/// Display implementation for `AnfPolynomial`.
@@ -225,7 +252,7 @@ mod tests {
225252
use crate::anf_polynom::AnfPolynomial;
226253
use num_bigint::BigUint;
227254
use num_traits::{Num, One, Zero};
228-
use crate::BooleanFunctionError;
255+
use crate::{BooleanFunctionError, BooleanFunctionImpl};
229256

230257
#[test]
231258
fn test_get_polynomial_small() {
@@ -357,4 +384,17 @@ mod tests {
357384
assert!(anf_polynomial.is_err());
358385
assert_eq!(anf_polynomial.unwrap_err(), BooleanFunctionError::ErrorParsingAnfString);
359386
}
387+
388+
#[test]
389+
fn test_to_boolean_function() {
390+
let rule_30_anf_str = "x0*x1 + x0 + x1 + x2";
391+
let rule_30_polynomial = AnfPolynomial::from_str(rule_30_anf_str, 3).unwrap();
392+
let rule_30_function = rule_30_polynomial.to_boolean_function();
393+
assert_eq!(rule_30_function.printable_hex_truth_table(), "1e");
394+
395+
let anf_str = "x0*x1*x2*x3*x4*x5*x6 + x7";
396+
let anf_polynomial = AnfPolynomial::from_str(anf_str, 8).unwrap();
397+
let boolean_function = anf_polynomial.to_boolean_function();
398+
assert_eq!(boolean_function.printable_hex_truth_table(), "7fffffffffffffffffffffffffffffff80000000000000000000000000000000");
399+
}
360400
}

src/lib.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,9 +916,25 @@ impl BooleanFunction {
916916
}
917917
Ok(BigBooleanFunction::from_truth_table(truth_table.clone(), num_variables).into())
918918
}
919+
920+
/// Computes Boolean Function from string ANF representation
921+
///
922+
/// The ANF string representation must be in exact form "`x0*x2*x3 + x2*x3 + x1 + 1`".
923+
///
924+
/// X's index starts at 0, meaning the maximum index is variable count - 1.
925+
///
926+
/// # Parameters:
927+
/// - `anf_polynomial`: The string representation of the ANF form
928+
/// - `num_variables`: Variable count of the polynomial
929+
///
930+
/// # Returns
931+
/// The BooleanFunction corresponding to the ANF string representation, or an error if the input string doesn't respect the format and `unsafe_disable_safety_checks` feature is not activated.
932+
pub fn from_anf_polynomial_str(anf_polynomial: &str, num_variables: usize) -> Result<BooleanFunction, BooleanFunctionError> {
933+
Ok(AnfPolynomial::from_str(anf_polynomial, num_variables)?.to_boolean_function())
934+
}
919935
}
920936

921-
// TODO from polynomial etc.
937+
// TODO from AnfPolynomial
922938

923939
#[cfg(test)]
924940
mod tests {
@@ -2152,4 +2168,15 @@ mod tests {
21522168
assert!(close_balanced_iterator.next().unwrap().is_balanced());
21532169
}
21542170
}
2171+
2172+
#[test]
2173+
fn test_from_anf_polynomial_str() {
2174+
let rule_30_anf_str = "x0*x1 + x0 + x1 + x2";
2175+
let rule_30_function = BooleanFunction::from_anf_polynomial_str(rule_30_anf_str, 3).unwrap();
2176+
assert_eq!(rule_30_function.printable_hex_truth_table(), "1e");
2177+
2178+
let anf_str = "x0*x1*x2*x3*x4*x5*x6 + x7";
2179+
let boolean_function = BooleanFunction::from_anf_polynomial_str(anf_str, 8).unwrap();
2180+
assert_eq!(boolean_function.printable_hex_truth_table(), "7fffffffffffffffffffffffffffffff80000000000000000000000000000000");
2181+
}
21552182
}

0 commit comments

Comments
 (0)