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:
ExceptionRaised when the OpModel is not valid and a valid one was expected.
- class cascada.abstractproperty.opmodel.OpModel(input_prop)[source]
 Bases:
objectRepresent property models of bit-vector operations.
A (bit-vector) property model of a bit-vector
Operation\(f\) for a particularPropertyis a set of bit-vector constraints that models the propagation probability ofPropertyover \(f\). SeeProperty.Note
For the
ValueandDifferencePropertytypes, 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
LinearMaskproperty this is different (seelinear.opmodel.OpModel).A model of \(f\) is mainly given by three bit-vector formulas or constraints: the
validity_constraintconstraint, theweight_constraintand 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
OpModelis defined for a type ofPropertyand a bit-vectorOperation\(f\) . The input property \(\alpha\) is used when initializing theOpModelobject, and the output property \(\beta\) is given in the arguments of the methodsOpModel.validity_constraint,OpModel.weight_constraintandOpModel.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_constraintandexternal_vars_pr_one_constraintrespectively.Note
OpModelof 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,LinearModelBvAddorBitModelBvAddfor some examples).Note
It is highly recommended that new models of bit-vector operations are tested with
TestOpModelGeneric(seedifferential/tests/test_opmodel.pyorlinear/tests/test_opmodel.pyfor some examples).- vrepr()[source]
 Return an executable string representation.
This method returns a string so that
eval(self.vrepr())andselfhave 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_propand the output propertyoutput_propand 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
Constant0b1or0b0depending on whether the corresponding propagation has non-zero probability. Otherwise, this method returns a bit-vectorTermcontaining the input and output properties asVariableobjects.
- 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
Propertypropagates to the given outputPropertywith probability one.This method returns a bit-vector expression that depends on the input property
OpModel.input_propand the output propertyoutput_propand 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
Constant0b1or0b0depending on whether the corresponding propagation has probability 1. Otherwise, this method returns a bit-vectorTermcontaining the input and output properties asVariableobjects.
- weight_constraint(output_prop, weight_variable)[source]
 Return the weight constraint for a given output
Propertyand weightVariable.The weight constraint is a bit-vector expression that depends on the input property
OpModel.input_prop, the output propertyoutput_propand 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
Constant0b1or0b0depending on whether the corresponding propagation has the given weight. Otherwise, this method returns a bit-vectorTermcontaining the input and output properties and the weight asVariableobjects.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_propand 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
OpModeldon’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
Constantof bit-sizeweight_widthdenoting the weight of the valid propagation. Otherwise, this method returns a bit-vectorTermcontaining the input and output properties asVariableobjects.
- 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.Decimalweight 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 variablewofOpModel.weight_constraintrepresents the number2^{-k} * bv2int(w). In particular, ifk == 0, thenwrepresents an integer number. Otherwise, thekleast significant bits ofwdenote the fractional part of the number represented byw.
- error()[source]
 Return the maximum difference between
OpModel.weight_constraintand 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_constraintand 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_constraintfor 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_constraintfor 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_constraintfor a given outputPropertyand weightVariable(an empty list by default).
- class cascada.abstractproperty.opmodel.PartialOpModel[source]
 Bases:
objectRepresent 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_modelis an (abstract) subclass ofPartialOpModelcontaining the base operator.The argument
fixed_argsis atuple, with the same length as the number of operands of the base operator, containingNone, scalar orConstantelements. 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.OpModelRepresent 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_propand the output propertyoutput_propand 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
Constant0b1or0b0depending on whether the corresponding propagation has non-zero probability. Otherwise, this method returns a bit-vectorTermcontaining the input and output properties asVariableobjects.
- 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
Propertypropagates to the given outputPropertywith probability one.This method returns a bit-vector expression that depends on the input property
OpModel.input_propand the output propertyoutput_propand 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
Constant0b1or0b0depending on whether the corresponding propagation has probability 1. Otherwise, this method returns a bit-vectorTermcontaining the input and output properties asVariableobjects.
- 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_propand 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
OpModeldon’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
Constantof bit-sizeweight_widthdenoting the weight of the valid propagation. Otherwise, this method returns a bit-vectorTermcontaining the input and output properties asVariableobjects.
- 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.Decimalweight 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 variablewofOpModel.weight_constraintrepresents the number2^{-k} * bv2int(w). In particular, ifk == 0, thenwrepresents an integer number. Otherwise, thekleast significant bits ofwdenote the fractional part of the number represented byw.
- error()[source]
 Return the maximum difference between
OpModel.weight_constraintand 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_constraintand the exact weight.Note that the exact weight might still differ from
decimal_weight.
- class cascada.abstractproperty.opmodel.WeakModel(input_prop)[source]
 Bases:
objectRepresent weak models of bit-vector operations w.r.t some property.
A weak model is an
OpModelwhere the propagation weight of aPropertypair \((\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_weightornonzero2zero_weightismath.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_modelorlinear.opmodel.get_weak_model).- zero2zero_weight
 a
decimal.Decimaldenoting the weight of the zero to zero transitions (math.infif these transitions are invalid)
- nonzero2nonzero_weight
 a
decimal.Decimaldenoting the weight of the non-zero to non-zero transitions (math.infif these transitions are invalid)
- zero2nonzero_weight
 a
decimal.Decimaldenoting the weight of the zero to non-zero transitions (math.infif these transitions are invalid)
- nonzero2zero_weight
 a
decimal.Decimaldenoting the weight of the non-zero to zero transitions (math.infif these transitions are invalid)
- precision
 the number of fraction bits used in the bit-vector weight (0 if all non-
math.infattributes*_weightare integer values)
- class cascada.abstractproperty.opmodel.BranchNumberModel(input_prop)[source]
 Bases:
cascada.abstractproperty.opmodel.WeakModelRepresent 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
BranchNumberModelof anOperation\(f\) is anOpModelwhere the propagation weight of aPropertypair \((\alpha, \beta)\) is one of the four cases ofWeakModelbut 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
Operationoutputs a single bit-vector, the class attributeoutput_widthsofBranchNumberModeldelimits the output words of \(f\) (for the counting of active output words).Note
To extract the output words from an
Operationwith aBranchNumberModel, usePropExtractinstead 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_modelorlinear.opmodel.get_branch_number_model).- branch_number
 the branch number \(B\)
- zero2zero_weight
 a
decimal.Decimaldenoting the weight of the zero to zero transitions (math.infif all these transitions are invalid)
- nonzero2nonzero_weight
 a
decimal.Decimaldenoting the weight of the non-zero to non-zero transitions (math.infif all these transitions are invalid)
- zero2nonzero_weight
 a
decimal.Decimaldenoting the weight of the zero to non-zero transitions (math.infif all these transitions are invalid)
- nonzero2zero_weight
 a
decimal.Decimaldenoting the weight of the non-zero to zero transitions (math.infif all these transitions are invalid)
- precision
 the number of fraction bits used in the bit-vector weight (0 if all non-
math.infattributes*_weightare integer values)
- class cascada.abstractproperty.opmodel.WDTModel(input_prop)[source]
 Bases:
objectRepresent WDT-based models of bit-vector operations w.r.t some property.
A model based on a Weight Distribution Table (WDT) is an
OpModelwhere the propagation weight is obtained from a 2-dimensional tableWDTsuch thatWDT[a][b]contains the propagation weight of the input-outputPropertypair \((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_modelorlinear.opmodel.get_wdt_model).- weight_distribution_table
 a 2-dimensional
tuplecontaining the distribution of the propagations weights, wheremath.infentries correspond to invalid transitions
- loop_rows_then_columns
 whether to extract the constraints from
weight_distribution_tableby 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_tableare integer values)