cascada.abstractproperty.opmodel module
Manipulate property models of bit-vector operations (w.r.t an abstract property).
Raised when the OpModel is not valid and a valid one was expected. |
|
Represent property models of bit-vector operations. |
|
Represent property models of operations with fixed operands. |
|
Return the partial property model of the given bit-vector partial operation. |
|
Represent the (trivial) property model of the identity function. |
|
Represent weak models of bit-vector operations w.r.t some property. |
|
Represent property models of bit-vector operations based on the branch number. |
|
Represent WDT-based models of bit-vector operations w.r.t some property. |
- exception cascada.abstractproperty.opmodel.InvalidOpModelError[source]
Bases:
Exception
Raised when the OpModel is not valid and a valid one was expected.
- class cascada.abstractproperty.opmodel.OpModel(input_prop)[source]
Bases:
object
Represent property models of bit-vector operations.
A (bit-vector) property model of a bit-vector
Operation
\(f\) for a particularProperty
is a set of bit-vector constraints that models the propagation probability ofProperty
over \(f\). SeeProperty
.Note
For the
Value
andDifference
Property
types, the propagation probability satisfies that for any fixed \(\alpha\), the sum of the probabilities of \(\alpha\) propagating to \(\beta\) (for all \(\beta\)) is equal to 1. In particular, if the propagation probability of \((\alpha, \beta)\) is 1, then \(\alpha\) uniquely propagates to \(\beta\).For the
LinearMask
property this is different (seelinear.opmodel.OpModel
).A model of \(f\) is mainly given by three bit-vector formulas or constraints: the
validity_constraint
constraint, theweight_constraint
and thepr_one_constraint
:The validity constraint (with inputs \((\alpha, \beta)\)) is True if and only if the propagation probability of \((\alpha, \beta)\) is non-zero, that is, \(P(\alpha, \beta) \neq 0\).
The weight constraint (with inputs \((w, \alpha, \beta)\) is True if and only if the bit-vector \(w\) is equals to the negative binary logarithm (weight) of the propagation probability of \((\alpha, \beta)\), that is, \(w = -log_2(P(\alpha, \beta)))\).
The probability-one constraint (with inputs \((\alpha, \beta)\)) is True if and only if the propagation probability of \((\alpha, \beta)\) is 1.
Note
The weight constraint is only defined for inputs \((w, \alpha, \beta)\) where the propagation probability of \((\alpha, \beta)\) is non-zero.
By default, the \(n_w\)-bit input
Variable
\(w\) of the weight constraint is interpreted as the non-negative integer \(w[0] + 2 w[1] + \dots + 2^{n_w−1} w[n_w − 1]\). However, since the propagation weight can be a non-integer value for some properties and functions, \(w\) can also be interpreted as the rational value \(2^{−l}(w[0] + 2w[1] + \dots + 2^{n_w−1} w[n_w − 1]\) for a given fixed number \(l\) (num_frac_bits
) of fractional bits.Moreover, weight constraints can also be True if \(w\) is equals to weight of the propagation probability up to some error bounded by
error
.An
OpModel
is defined for a type ofProperty
and a bit-vectorOperation
\(f\) . The input property \(\alpha\) is used when initializing theOpModel
object, and the output property \(\beta\) is given in the arguments of the methodsOpModel.validity_constraint
,OpModel.weight_constraint
andOpModel.pr_one_constraint
(among others).In some cases, the validity, weight or probability-one constraints use auxiliary external (different from the input and output properties) variables in the bit-vector expression. These external variables can be obtained from
external_vars_validity_constraint
,external_vars_weight_constraint
andexternal_vars_pr_one_constraint
respectively.Note
OpModel
of functions with scalar operands are not supported.This class is not meant to be instantiated but to provide a base class for creating models of some operation w.r.t some property, (see
XorModelBvAdd
,LinearModelBvAdd
orBitModelBvAdd
for some examples).Note
It is highly recommended that new models of bit-vector operations are tested with
TestOpModelGeneric
(seedifferential/tests/test_opmodel.py
orlinear/tests/test_opmodel.py
for some examples).- vrepr()[source]
Return an executable string representation.
This method returns a string so that
eval(self.vrepr())
andself
have the same content.
- validity_constraint(output_prop)[source]
Return the validity constraint for a given output
Property
.The validity constraint is a bit-vector expression that depends on the input property
OpModel.input_prop
and the output propertyoutput_prop
and it is True if and only if the input property propagates to the output property with non-zero probability.If both the input and output properties are constant values, this method returns the
Constant
0b1
or0b0
depending on whether the corresponding propagation has non-zero probability. Otherwise, this method returns a bit-vectorTerm
containing the input and output properties asVariable
objects.
- pr_one_constraint(output_prop)[source]
Return the probability-one constraint for a given output
Property
.Return the constraint that evaluates to True if the input
Property
propagates to the given outputProperty
with probability one.This method returns a bit-vector expression that depends on the input property
OpModel.input_prop
and the output propertyoutput_prop
and it is True if and only if the input property propagates to the output property with probability 1.If both the input and output properties are constant values, this method returns the
Constant
0b1
or0b0
depending on whether the corresponding propagation has probability 1. Otherwise, this method returns a bit-vectorTerm
containing the input and output properties asVariable
objects.
- weight_constraint(output_prop, weight_variable)[source]
Return the weight constraint for a given output
Property
and weightVariable
.The weight constraint is a bit-vector expression that depends on the input property
OpModel.input_prop
, the output propertyoutput_prop
and the weight variableweight_variable
. This expression is True if and only if the weight (negative binary logarithm of the probability) of the input property propagating to the output property is equals toweight_variable
.Note
It is assumed that the corresponding propagation has non-zero probability.
If the input and output properties and the weight variable are constant values, this method returns the
Constant
0b1
or0b0
depending on whether the corresponding propagation has the given weight. Otherwise, this method returns a bit-vectorTerm
containing the input and output properties and the weight asVariable
objects.Subclasses can either reimplement this method or implement the method
bv_weight
. In the last case, the weight constraint is defined asBvComp(weight_variable, self.bv_weight(output_prop))
.
- bv_weight(output_prop)[source]
Return the bit-vector weight for a given output
Property
.The bit-vector weight is a bit-vector expression that depends on the input property
OpModel.input_prop
and the output propertyoutput_prop
. This bit-vector function represents an approximation of the negative binary logarithm (weight) of the probability of the input property propagating to the output property.Note
This method is optional and subclasses of
OpModel
don’t need to implement it (as opposed toOpModel.weight_constraint
).It is assumed that the corresponding propagation has non-zero probability.
If the input and output properties are constant values, this method returns a
Constant
of bit-sizeweight_width
denoting the weight of the valid propagation. Otherwise, this method returns a bit-vectorTerm
containing the input and output properties asVariable
objects.
- max_weight()[source]
Return the maximum value the weight variable can achieve in
OpModel.weight_constraint
.
- weight_width()[source]
Return the width of the weight variable used
OpModel.weight_constraint
.
- decimal_weight(output_prop)[source]
Return the
decimal.Decimal
weight for a given constant outputProperty
.This method returns, as a decimal number, the weight (negative binary logarithm) of the probability of the input property propagating to the output property.
This method only works when the input property and the output property are constant values, but provides a better approximation than the bit-vector weight from
OpModel.weight_constraint
.
- num_frac_bits()[source]
Return the number of fractional bits used in the weight variable of
OpModel.weight_constraint
.If the number of fractional bits is
k
, then the bit-vector weight variablew
ofOpModel.weight_constraint
represents the number2^{-k} * bv2int(w)
. In particular, ifk == 0
, thenw
represents an integer number. Otherwise, thek
least significant bits ofw
denote the fractional part of the number represented byw
.
- error()[source]
Return the maximum difference between
OpModel.weight_constraint
and the exact weight.The exact weight is exact value (without error) of the negative binary logarithm (weight) of the propagation probability of \((\alpha, \beta)\).
Note
The exact weight can be computed in
TestOpModelGeneric.get_empirical_weight_slow
.This method returns an upper bound (in absolute value) of the maximum difference (over all input and output properties) between the bit-vector weight from
OpModel.weight_constraint
and the exact weight.Note that the exact weight might still differ from
decimal_weight
.
- external_vars_validity_constraint(output_prop)[source]
Return the list of external variables of
validity_constraint
for a given outputProperty
(an empty list by default).
- external_vars_pr_one_constraint(output_prop)[source]
Return the list of external variables of
pr_one_constraint
for a given outputProperty
(an empty list by default).
- external_vars_weight_constraint(output_prop, weight_variable)[source]
Return the list of external variables of
weight_constraint
for a given outputProperty
and weightVariable
(an empty list by default).
- class cascada.abstractproperty.opmodel.PartialOpModel[source]
Bases:
object
Represent property models of operations with fixed operands.
This class is not meant to be instantiated but to provide a base class for property models of operations with fixed operands generated through
make_partial_op_model
.
- cascada.abstractproperty.opmodel.make_partial_op_model(abstract_partial_op_model, fixed_args)[source]
Return the partial property model of the given bit-vector partial operation.
The argument
abstract_partial_op_model
is an (abstract) subclass ofPartialOpModel
containing the base operator.The argument
fixed_args
is atuple
, with the same length as the number of operands of the base operator, containingNone
, scalar orConstant
elements. Iffixed_args[i]
isNone
, the i-th operand is not fixed; otherwise, the i-th operand is replaced withfixed_args[i]
. See alsomake_partial_operation
.The resulting class is a subclass of
abstract_partial_op_model
.
- class cascada.abstractproperty.opmodel.ModelIdentity(input_prop)[source]
Bases:
cascada.abstractproperty.opmodel.OpModel
Represent the (trivial) property model of the identity function.
This model is used to rename a complex intermediate property with a new name.
- op
- validity_constraint(output_prop)[source]
Return the validity constraint for a given output
Property
.The validity constraint is a bit-vector expression that depends on the input property
OpModel.input_prop
and the output propertyoutput_prop
and it is True if and only if the input property propagates to the output property with non-zero probability.If both the input and output properties are constant values, this method returns the
Constant
0b1
or0b0
depending on whether the corresponding propagation has non-zero probability. Otherwise, this method returns a bit-vectorTerm
containing the input and output properties asVariable
objects.
- pr_one_constraint(output_prop)[source]
Return the probability-one constraint for a given output
Property
.Return the constraint that evaluates to True if the input
Property
propagates to the given outputProperty
with probability one.This method returns a bit-vector expression that depends on the input property
OpModel.input_prop
and the output propertyoutput_prop
and it is True if and only if the input property propagates to the output property with probability 1.If both the input and output properties are constant values, this method returns the
Constant
0b1
or0b0
depending on whether the corresponding propagation has probability 1. Otherwise, this method returns a bit-vectorTerm
containing the input and output properties asVariable
objects.
- bv_weight(output_prop)[source]
Return the bit-vector weight for a given output
Property
.The bit-vector weight is a bit-vector expression that depends on the input property
OpModel.input_prop
and the output propertyoutput_prop
. This bit-vector function represents an approximation of the negative binary logarithm (weight) of the probability of the input property propagating to the output property.Note
This method is optional and subclasses of
OpModel
don’t need to implement it (as opposed toOpModel.weight_constraint
).It is assumed that the corresponding propagation has non-zero probability.
If the input and output properties are constant values, this method returns a
Constant
of bit-sizeweight_width
denoting the weight of the valid propagation. Otherwise, this method returns a bit-vectorTerm
containing the input and output properties asVariable
objects.
- max_weight()[source]
Return the maximum value the weight variable can achieve in
OpModel.weight_constraint
.
- weight_width()[source]
Return the width of the weight variable used
OpModel.weight_constraint
.
- decimal_weight(output_prop)[source]
Return the
decimal.Decimal
weight for a given constant outputProperty
.This method returns, as a decimal number, the weight (negative binary logarithm) of the probability of the input property propagating to the output property.
This method only works when the input property and the output property are constant values, but provides a better approximation than the bit-vector weight from
OpModel.weight_constraint
.
- num_frac_bits()[source]
Return the number of fractional bits used in the weight variable of
OpModel.weight_constraint
.If the number of fractional bits is
k
, then the bit-vector weight variablew
ofOpModel.weight_constraint
represents the number2^{-k} * bv2int(w)
. In particular, ifk == 0
, thenw
represents an integer number. Otherwise, thek
least significant bits ofw
denote the fractional part of the number represented byw
.
- error()[source]
Return the maximum difference between
OpModel.weight_constraint
and the exact weight.The exact weight is exact value (without error) of the negative binary logarithm (weight) of the propagation probability of \((\alpha, \beta)\).
Note
The exact weight can be computed in
TestOpModelGeneric.get_empirical_weight_slow
.This method returns an upper bound (in absolute value) of the maximum difference (over all input and output properties) between the bit-vector weight from
OpModel.weight_constraint
and the exact weight.Note that the exact weight might still differ from
decimal_weight
.
- class cascada.abstractproperty.opmodel.WeakModel(input_prop)[source]
Bases:
object
Represent weak models of bit-vector operations w.r.t some property.
A weak model is an
OpModel
where the propagation weight of aProperty
pair \((\alpha, \beta)\) is one of the following cases:If \(\alpha = 0 = \beta\), the weight is
zero2zero_weight
.If \(\alpha \neq 0 \neq \beta\), the weight is
nonzero2nonzero_weight
.If \(\alpha = 0 \neq \beta\), the weight is
zero2nonzero_weight
.If \(\alpha \neq 0 = \beta\), the weight is
nonzero2zero_weight
.
If any of weights
zero2zero_weight
,nonzero2nonzero_weight
,zero2nonzero_weight
ornonzero2zero_weight
ismath.inf
, the propagation probability of the corresponding case is assumed to be 0.This class is not meant to be instantiated but to provide a base class for weak models of operations (generated for example through
differential.opmodel.get_weak_model
orlinear.opmodel.get_weak_model
).- zero2zero_weight
a
decimal.Decimal
denoting the weight of the zero to zero transitions (math.inf
if these transitions are invalid)
- nonzero2nonzero_weight
a
decimal.Decimal
denoting the weight of the non-zero to non-zero transitions (math.inf
if these transitions are invalid)
- zero2nonzero_weight
a
decimal.Decimal
denoting the weight of the zero to non-zero transitions (math.inf
if these transitions are invalid)
- nonzero2zero_weight
a
decimal.Decimal
denoting the weight of the non-zero to zero transitions (math.inf
if these transitions are invalid)
- precision
the number of fraction bits used in the bit-vector weight (0 if all non-
math.inf
attributes*_weight
are integer values)
- class cascada.abstractproperty.opmodel.BranchNumberModel(input_prop)[source]
Bases:
cascada.abstractproperty.opmodel.WeakModel
Represent property models of bit-vector operations based on the branch number.
Let \((y_0, y_1, \dots, y_m) = f(x_1, x_2, \dots, x_n)\) be a bit-vector function with input words \(x_i\) and output words \(y_j\). Given a property pair \(((P_{x_1}, \dots, P_{x_n}), (P_{y_1}, \dots, P_{y_n}))\) over \(f\), we say that the property \(P_{x_i}\) over \(x_i\) is active if \(P_{x_i} \neq 0\) (similar for \(P_{y_j}\)).
A
BranchNumberModel
of anOperation
\(f\) is anOpModel
where the propagation weight of aProperty
pair \((\alpha, \beta)\) is one of the four cases ofWeakModel
but with the additional constraint that if \((\alpha, \beta)\) is non-zero and its number of active words is strictly less than \(B\) then it is invalid.The number \(B\) is usually the branch number of \(f\), that is, the minimum number of active words (at the input and at the output) among all non-zero property pairs. Nevertheless, any \(2 \le B \le n + m\) can be used.
Since an
Operation
outputs a single bit-vector, the class attributeoutput_widths
ofBranchNumberModel
delimits the output words of \(f\) (for the counting of active output words).Note
To extract the output words from an
Operation
with aBranchNumberModel
, usePropExtract
instead ofExtract
.This class is not meant to be instantiated but to provide a base class for branch-number-based models of operations (generated for example through
differential.opmodel.get_branch_number_model
orlinear.opmodel.get_branch_number_model
).- branch_number
the branch number \(B\)
- zero2zero_weight
a
decimal.Decimal
denoting the weight of the zero to zero transitions (math.inf
if all these transitions are invalid)
- nonzero2nonzero_weight
a
decimal.Decimal
denoting the weight of the non-zero to non-zero transitions (math.inf
if all these transitions are invalid)
- zero2nonzero_weight
a
decimal.Decimal
denoting the weight of the zero to non-zero transitions (math.inf
if all these transitions are invalid)
- nonzero2zero_weight
a
decimal.Decimal
denoting the weight of the non-zero to zero transitions (math.inf
if all these transitions are invalid)
- precision
the number of fraction bits used in the bit-vector weight (0 if all non-
math.inf
attributes*_weight
are integer values)
- class cascada.abstractproperty.opmodel.WDTModel(input_prop)[source]
Bases:
object
Represent WDT-based models of bit-vector operations w.r.t some property.
A model based on a Weight Distribution Table (WDT) is an
OpModel
where the propagation weight is obtained from a 2-dimensional tableWDT
such thatWDT[a][b]
contains the propagation weight of the input-outputProperty
pair \((a, b)\).Note
For the
Difference
(resp.LinearMask
) property, the WDT is also known as the Difference Distribution Table or DDT (resp. Linear Approximation Table or LAT).This class is not meant to be instantiated but to provide a base class for WDT-based models of operations (generated for example through
differential.opmodel.get_wdt_model
orlinear.opmodel.get_wdt_model
).- weight_distribution_table
a 2-dimensional
tuple
containing the distribution of the propagations weights, wheremath.inf
entries correspond to invalid transitions
- loop_rows_then_columns
whether to extract the constraints from
weight_distribution_table
by looping first over the rows (input properties) and then over the columns (output properties) or vice-versa
- precision
the number of fraction bits used in the bit-vector weight (0 if all the weights in
weight_distribution_table
are integer values)