A new package called boolfun is available for R users. The package provides tools to handle Boolean functions, in particular for cryptographic purposes. This document guides the user through some (code) examples and gives a feel of what can be done with the package.
A Boolean function is a mapping
The number of Boolean functions with
In R, type install.packages("boolfun")
in order to install the package
and library(boolfun)
to load it.
A Boolean function is instantiated with its truth table, a character or
integer vector representing the output values of the function. For
example, f <- BooleanFunction("01101010")
defines a Boolean function
with g <- BooleanFunction(c(tt(f),tt(f))
defines a Boolean function with f
’s truth
table.
In order to represent the truth table as a vector of return values
without ambiguity, a total order needs to be defined over the input
assignments. The position of the element
Methods of the BooleanFunction
object can be called in two ways, using
functional notation as in tt(f)
or using object oriented notation as
in f$tt()
. This is a feature of any object that inherits from Object
defined in the R.oo package
(Bengtsson 2003).
An overview of all public methods is given in Figure 1 with a short description.
method | returned value |
---|---|
n()
|
number of input variables n |
tt()
|
truth table (vector of integers) |
wh()
|
walsh spectrum (vector of integers) |
anf()
|
algebraic normal form (vector of integers) |
ANF()
|
algebraic normal form as
Polynomial
|
deg()
|
algebraic degree |
nl()
|
nonlinearity |
ai()
|
algebraic immunity |
ci()
|
correlation immunity |
res()
|
resiliency |
isBal()
|
TRUE if function is
balanced
|
isCi(t)
|
TRUE if ci()
returns t
|
isRes(t)
|
TRUE if res()
returns t
|
BooleanFunction
.
Also some generic functions such as print()
or equals()
are
overloaded so that they support instances of BooleanFunction
. Any
additional information can be found in the document displayed by the R
command vignette("boolfun")
.
Note that an object Polynomial
is also implemented in the
boolfun package and is
discussed in the following section. The other sections give some
examples on how to use the package in order to visualize properties or
to analyze random Boolean functions.
Let
Simply put,
The recent package
multipol (Hankin 2008)
allows the user to handle multivariate polynomials but is not well
suited to handle operations over polynomial Boolean rings. In
boolfun, an S3 object
Polynomial
is defined and implements basic functionality to manipulate
the algebraic normal form of Boolean functions.
> f <- BooleanFunction("01011010")
> p <- f$ANF()
> data.class(p)
[1] "Polynomial"
> q <- Polynomial("0101")
> p
[1] "x1 + x3"
> q
[1] "x1*x2 + x1"
> p * q
[1] "x1*x2*x3 + x1*x2 + x1*x3 + x1"
> p * q + q
[1] "x1*x2*x3 + x1*x3"
> deg(p * q + q)
[1] 3
In this example, p
holds the algebraic normal form of f
which is
represented in Polynomial
by a vector of length [[]]
can
be used to evaluate the polynomial for a particular assignment.
> r <- p * q + q
> x <- c(0, 1, 1) # x1=0, x2=1, x3=1
> if( length(x) != r$n() ) stop("this is an error")
> p[[x]]
[1] 1
> x[3] <- 0
> p[[x]]
[1] 0
The Polynomial
object inherits from
R.oo’s Object
and Figure
2 gives an overview of the implemented methods.
method | returned value |
---|---|
n()
|
number of input variables n |
deg()
|
algebraic degree |
anf()
|
vector of coefficients for 2n monomials |
len()
|
returns 2n |
string()
|
algebraic normal form as character string |
*
|
multiplication returns a new
Polynomial
|
+
|
addition returns a new
Polynomial
|
[[
|
evaluates the polynomial |
Polynomial
.
Addition and multiplication are executed using C code. The addition is
straightforward given two vectors of coefficients as it consists in
making a component-wise exclusive or of the input vectors. The
multiplication is built upon the addition of two polynomials and the
multiplication of two monomials. Hence addition is computed in
Depending on its cryptographic application, a Boolean function needs to satisfy a set of properties in order to be used safely. Some of those properties cannot be satisfied simultaneously as there are trade-offs between them. In this section we focus on two properties, resilience and algebraic immunity, and show how R can be used to illustrate the trade-off that exists between them.
The algebraic immunity of a Boolean function
Resilience is a combination of two properties: balancedness and
correlation immunity. A function is balanced if its truth table contains
as many zeros as ones and correlation immune of order
The following example illustrates the trade-off between algebraic immunity and resilience.
n <- 3
N <- 2^2^n # number of functions with n var
allRes <- vector("integer", N)
allAIs <- vector("integer", N)
for( i in 1:N ) \{ # forall Boolean function
f <- BooleanFunction( toBin(i-1,2^n) )
allRes[i] <- res(f) # get its resiliency
allAIs[i] <- ai(f) # and algebraic immunity
\}
xlabel <- "Truth tables as integers"
plot( x=1:N, y=allRes, type="b",
xlab=xlabel, ylab="Resiliency" )
plot( x=1:N, y=allAIs, type="b",
xlab=xlabel, ylab="Algebraic immunity" )
The example code plots the resilience and the algebraic immunity of all
functions with
Observe that the search space of size
Now let us consider the functions achieving a good tradeoff between
algebraic immunity and resilience. The sum ai(f) + res(f)
is computed
for each function f
with 3 variables according to the following code
and plotted in Figure 5.
plot( 1:N, allRes+allAIs, type="b",
xlab="f", ylab="ai(f)+res(f)" )
Note that for all function f
, the value res(f) + ai(f)
never reaches
the value max(allRes) + max(allAIs)
, hence the tradeoff. Also this
figure suggests which part of the search space should be explored in
order to find optimal tradeoffs.
Figures 3 and 4 suggest that some properties are more likely to be observed in a random Boolean function. For example, there are more functions having maximal algebraic immunity than functions having maximal resilience. The following example gives a better idea of what is expected in random Boolean functions.
n <- 8
data <- data.frame( matrix(nrow=0,ncol=4) )
names(data) <- c( "deg", "ai", "nl", "res" )
for( i in 1:1000 ) \{ # for 1000 random functions
randomTT <- round(runif(2^n, 0, 1))
randomBF <- BooleanFunction(randomTT)
data[i,] <-c( deg(randomBF), ai(randomBF),
nl(randomBF), res(randomBF))
\}
After the code is executed, data
holds the values of four properties
(columns) for 1000 random Boolean functions with
> mean(data)
deg ai nl res
7.479 3.997 103.376 -0.939
> sd(data)
deg ai nl res
0.5057814 0.0547174 3.0248593 0.2476698
It is also very easy to apply statistical tests using R. For example, in
(Englund et al. 2007; Aumasson et al. 2009) cryptographic pseudorandom generators are tested for
non random behavior. Those tests consider the qchisq
from the package
stats as suggested by the
following code.
data <- getTheBooleanFunctions()
chistat <- computeChiStat(data)
outcome <- "random"
if(chistat > qchisq(0.99, df=n))
outcome <- "cipher"
print(outcome)
A free open source package to manipulate Boolean functions is available
at cran.r-project.org. The package also
provides tools to handle Boolean polynomials (multivariate polynomials
over
boolfun, R.oo, multipol, stats
This article is converted from a Legacy LaTeX article using the texor package. The pdf version is the official version. To report a problem with the html, refer to CONTRIBUTE on the R Journal homepage.
Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".
For attribution, please cite this work as
hamme, "Cryptographic Boolean Functions with R", The R Journal, 2011
BibTeX citation
@article{RJ-2011-007, author = {hamme, Frédéric Lafitte,Dirk Van Heule and Julien Van}, title = {Cryptographic Boolean Functions with R}, journal = {The R Journal}, year = {2011}, note = {https://rjournal.github.io/}, volume = {3}, issue = {1}, issn = {2073-4859}, pages = {44-47} }