cascada.abstractproperty.characteristic module
Manage non-symbolic characteristics w.r.t an abstract property.
Represent the auxiliary data of |
|
Represent characteristics over bit-vector functions w.r.t some property. |
|
Represent characteristics over encryption functions w.r.t some property. |
|
Represent characteristics over ciphers w.r.t some property. |
- class cascada.abstractproperty.characteristic.EmpiricalWeightData(aux_weights, num_input_samples, seed, C_code, num_external_samples=None)[source]
Bases:
objectRepresent the auxiliary data of
empirical_ch_weight.This class stores the auxiliary data (the auxiliary empirical weights and the input parameters) of characteristic empirical weights computed through the method
Characteristic.compute_empirical_ch_weight.- weight_avg_aux_prs
the negative binary logarithm (weight) of the average of the underlying probabilities of the auxiliary empirical weights
- num_aux_weights
the total number of auxiliary weights computed (including weights with value
math.inf)
- num_input_samples
the number of inputs sampled
- num_external_samples
the number of external samples used (equal to the number of auxiliary weights if there are two or more)
- seed
the seed used to sample bit-vectors
- C_code
whether the on-the-fly C implementations was used
- aux_weights
the list of auxiliary empirical weights
- vrepr()[source]
Return an executable string representation.
This method returns a string so that
eval(self.vrepr())returns a newEmpiricalWeightDataobject with the same content.
- class cascada.abstractproperty.characteristic.Characteristic(input_prop, output_prop, assign_outprop_list, ch_model, external_props=None, free_props=None, empirical_ch_weight=None, empirical_data_list=None, is_valid=True)[source]
Bases:
objectRepresent characteristics over bit-vector functions w.r.t some property.
Given a
BvFunction\(f\) and itsSSAdecomposition into a list of simple assignments \(x_{i+1} \leftarrow f_i(x_i)\), a characteristic is a trail of properties \((\Delta_{x_0} \mapsto \Delta_{x_1} \mapsto \dots \mapsto \Delta_{x_r})\) containing the input and outputPropertypair \((\Delta_{x_{i}}, \Delta_{x_{i+1}})\) of each assignment \(x_{i+1} \leftarrow f_i(x_i)\), where \(f_i\) is a bit-vectorOperation. The initial property \(\Delta_{x_0}\) is called the input property of the characteristic, and the last property \(\Delta_{x_r}\) is called the output property of the characteristic.If a
BvFunction\(f\) contains external variables, recall the external variables appear in theSSAas input operands in some assignments. Thus, the properties associated to the external variables appear in the characteristic trail as input properties of those assignments.Note
For example, given the function \(f(x) = (x \oplus 1) \boxplus z\) with external variable \(z\), a characteristic over \(f\) is a trail of properties given by 2 property pairs:
The first property pair \((\Delta_x, \Delta_{x'})\) is over the assignment \(x' \leftarrow f_0(x) = x \oplus 1\).
The second property pair \(((\Delta_{x'}, \Delta_z), \Delta_{x''})\) is over the assignment \(x'' \leftarrow f_1(x', z) = x' \boxplus z\).
The propagation probability of a characteristic, or simply the characteristic probability, is the product of the propagation probabilities of the
Propertypairs \((\Delta_{x_{i}} \mapsto \Delta_{x_{i+1}})\) over \(f_i\).The propagation weight of a characteristic is the negative binary logarithm of the characteristic probability, that is, the sum of the propagation weights of the
Propertypairs \((\Delta_{x_{i}} \mapsto \Delta_{x_{i+1}})\).Note
For a function \(f(x) = g(x, x)\) with duplicated input \(x\), if the property model of \(f\) is not implemented but the model of \(g\) is, then the property model of \(f\) is approximated with the model of \(g\).
For example, the
XorDiffmodel of the unary operation \(f(x) = x \boxplus x\) is approximated byXorModelBvAddwith the same difference for the first and second operands.The characteristic probability is an approximation of the propagation probability of the property pair \((\Delta_{x_0}, \Delta_{x_r})\) over \(f\), but the accuracy of this approximation varies depending on the
Propertytype, the function \(f\) and theSSAdecomposition of \(f\). For some properties such asDifferenceandLinearMask, if \(f\) has external variables then the characteristic probability approximates the propagation probability of \((\Delta_{x_0}, \Delta_{x_r})\) averaged over the set of all values of the external variables.A
Characteristicis defined for aabstractproperty.chmodel.ChModel(providing the type ofProperty, theBvFunction\(f\), theSSAdecomposition, and the (symbolic)Propertypropagations over the non-trivial assignments asabstractproperty.opmodel.OpModelobjects).Note
A
Characteristicobject represents an instance of characteristic (where all the properties and weights are constant values), whereas aabstractproperty.chmodel.ChModelobject represents the (symbolic) model of a characteristic.A
Characteristicalso requires the values of the properties \(\Delta_{x_i}\) in the trail. The property values can be given asConstantobjects orPropertyofConstantobjects.Note
To initialize a
Characteristic, all the constant values of the input, output, external and assignment-output properties must be given, even for properties not affecting the trail. These free symbolic properties can be given in thefree_propslist to denote that their value does not affect the characteristic.If the underlying bit-vector function does not have external variables, or the attribute
external_var2propof the underlying characteristic model maps all external variables to constantPropertyobjects, then the initialization argumentexternal_propscan be omitted.If the bit-vector function \(f\) of the given
abstractproperty.chmodel.ChModelis aRoundBasedFunctionincludingadd_round_outputscalls in itseval, then the characteristic over each round can be obtaining by combiningsplitandget_round_separators.To initialize an invalid
Characteristic(with zero characteristic probability), the initialization argumentis_valid=Falsemust be provided (by default,is_valid=True).This class is not meant to be instantiated but to provide a base class for creating characteristic w.r.t some property, (see
differential.characteristic.Characteristic,linear.characteristic.Characteristicoralgebraic.characteristic.Characteristicfor some examples).- ch_model
the underlying
abstractproperty.chmodel.ChModel.
- ch_weight
the decimal weight of the characteristic computed as the sum of
assignment_weights.
- assignment_weights
the decimal weight of the
Property\((\Delta_{x_{i}} \mapsto \Delta_{x_{i+1}})\) over each non-trivial assignment \(x_{i+1} \leftarrow f_i(x_i)\), computed from theabstractproperty.opmodel.OpModel.decimal_weightmethod of theabstractproperty.opmodel.OpModelobjects stored intuple_assign_outprop2op_model.
- empirical_ch_weight
the empirical weight of the characteristic (available after calling
compute_empirical_ch_weight).
- empirical_data_list
a list of
EmpiricalWeightDataobjects containing the auxiliary data of the empirical weight (see alsocompute_empirical_ch_weight).
- external_props
a list containing the (constant)
Propertyof the external variables of the function.
- tuple_assign_outprop2op_model
a tuple where each element is a pair containing: (1) the output (constant)
Property\(\Delta_{x_{i+1}}\) of the non-trivial assignment \(x_{i+1} \leftarrow f_i(x_i)\) and (2) theabstractproperty.opmodel.OpModelof this assignment with a (constant) inputProperty\(\Delta_{x_{i}}\).
- free_props
a list of (symbolic)
Propertyobjects of thech_model, whose values do not affect the characteristic, and were replaced by constant properties ininput_prop,output_prop,external_propsortuple_assign_outprop2op_model.
- var_prop2ct_prop
a
collections.OrderedDictmapping each symbolicPropertyin the trail to its constant property.
- static get_properties_for_initialization(ch_model, var2ct, free_props=None)[source]
Return the properties needed to initialize a
Characteristicobject.Given a
abstractproperty.chmodel.ChModeland a dictionary mappingVariableobjects (representing the symbolic properties) to theirConstantvalues, this method returns the following objects:input_prop,output_prop,external_propsandassign_outprop_listfor theCharacteristic.__init__method.Symbolic properties not affecting the characteristics can be given in
free_propsand they will be set to 0.
- vrepr(ignore_external_props=False)[source]
Return an executable string representation.
This method returns a string so that
eval(self.vrepr())returns a newCharacteristicobject with the same content.
- srepr()[source]
Return a short string representation of the characteristic.
The short representation includes the characteristic weight rounded up to 3 fractional digits (and similar for the empirical weight if defined) and the input and output properties, printed in hexadecimal but omitting the prefix
0x.Doctest included in
vrepr.
- signature(ch_signature_type)[source]
Return the signature of the characteristic.
This method is similar that
abstractproperty.chmodel.ChModel.signaturebut for non-symbolic characteristic.
- split(prop_separators)[source]
Split into multiple
Characteristicobjects given the list of property separators.Given the
Characteristic\(c\), this method returns a list of characteristics \(c_1, c_2, ..., c_n\), such that their composition is equal to the main characteristic \(c\).The argument
prop_separatorsis a list containing lists of symbolic properties. The \(i\)-th property list denote the last properties of the \(i\)-thabstractproperty.chmodel.ChModel. In other words, the \((i+1)\)-th characteristic immediately starts after the last property inprop_separators[i].To split into \(n\) characteristics,
prop_separatorsmust contain \(n-1\) lists, as the property list of the last characteristic is not given (its last properties are the output properties of the main characteristic).This method internally calls
abstractproperty.chmodel.ChModel.splitto split the underlying characteristic modelch_model.
- compute_empirical_ch_weight(num_input_samples=None, num_external_samples=None, split_by_max_weight=None, split_by_rounds=False, seed=None, C_code=False, num_parallel_processes=None)[source]
Compute and store the empirical weight.
Given the characteristic \((\Delta_{x_0} \mapsto \Delta_{x_1} \mapsto \dots \mapsto \Delta_{x_r})\) over \(f\), the empirical weight is an estimation of the propagation weight of \((\Delta_{x_0}, \Delta_{x_r})\) empirically obtained by evaluating \(f\) for many sampled inputs and external samples (if \(f\) has external variables).
Note
The empirical weight only takes into account the input and output properties; intermediate properties in the trail are ignored.
Note it is possible for the empirical weight to be smaller than
ch_weightin some cases and to be larger in others.If \(f\) does not have external variables, in general (depends on the
Property) the empirical weight is computed by \(f\)-evaluatingnum_input_samplesrandom inputs satisfyinginput_propand counting the number of outputs satisfyingoutput_prop. This procedure is called here the basic subroutine, and it varies fromPropertytoProperty.If \(f\) contains external variables, the basic subroutine is repeated
num_external_samplestimes, where the external variables are fixed in each iteration to random values satisfyingexternal_props. As a result,num_external_samplesauxiliary empirical weights are obtained, and the final empirical weight is taken as the negative binary logarithm of the average of the underlying probabilities of the auxiliary empirical weights.Note
The average is taken on the underlying probabilities since these probabilities might be zero in some cases (and their corresponding weights
math.inf).This method computes the empirical weight and stores it in
empirical_ch_weight. Moreover, the input parameters and the auxiliary weights is stored inempirical_data_listas a list ofEmpiricalWeightDataobjects. This list contains multiple objects only if the characteristic was split into multiple ones (see below); in that case, this list contains anEmpiricalWeightDataobject for each characteristic the main one was split into.If one of the arguments
split_by_max_weightorsplit_by_roundsis enabled, the characteristic is firstsplitinto multiple characteristics and then the empirical weights of the new characteristics are computed (by callingcompute_empirical_ch_weighton each new characteristic). The final empirical weight is taken as the sum of the empirical weights of the new characteristics. If the empirical weight of one of the new characteristics ismath.inf, the ongoing computations are aborted and the unfinished empirical weights are stored asEmpiricalWeightDataobjects withmath.infweight,num_input_samples=0andnum_external_samples=0.Note
When the characteristic is split, the final empirical weight is more an approximation of the characteristic weight that the weight of the input-output pair, as now the empirical weight computation also takes into account the intermediate properties that are part of the input and output properties of the new characteristics are also taken into account.
- Parameters
num_input_samples – the number of inputs to sample.
num_external_samples – the number of external samples (set to 0 if the underlying bit-vector function does not contain external variables).
split_by_max_weight – an optional number; if given, computes the empirical weight by splitting the characteristic into multiple characteristics (each one with maximum weight less than
max(split_by_max_weight, max(self.assignment_weights))).split_by_rounds – if
True, computes the empirical weight by splitting the characteristic into multiple characteristics, each one covering one round (only available if the underlying function of the characteristic model is aRoundBasedFunctionincludingadd_round_outputscalls in itseval)seed – the seed for sampling random bit-vectors
C_code – whether to use a faster C implementation generated on-the-fly
num_parallel_processes – if not
NoneandC_code=True, the auxiliary empirical weights are computed in parallel (using the given number of worker processes)
- classmethod random(ch_model, seed, external_props=None)[source]
Return a random
Characteristicwith givenabstractproperty.chmodel.ChModel.- Parameters
ch_model – the underlying characteristic model
seed – the seed used to sample bit-vectors
external_props – a list containing the (constant)
Propertyof the external variables orNoneto sample random properties for the external variables.
- get_formatted_logged_msgs()[source]
Return the list of logged messages.
If
self.ch_model.funcincludeslog_msgcalls in itseval, this method return the list of messages logged with the format field objects applied. Otherwise, an empty list is returned.In the first case, the
Variableobjects ofself.ch_model.ssaappearing in the format field objects are replaced with their associated constantPropertyobjects.
- get_round_separators()[source]
Return the round separators if
self.ch_model.funcis aRoundBasedFunction.If
self.ch_model.funcincludesadd_round_outputscalls in itseval, this method returns a list with the (symbolic) roundPropertyoutputs delimiting the rounds. Otherwise,Noneis returned.In the first case, this list contains
num_rounds - 1entries, where thei-th entry is the list of (symbolic)Propertyoutputs of thei-th round. In particular, the output properties of the last round are not included in this list.The list returned by this method is meant to be used as the argument of
Characteristic.splitto get theCharacteristicobject of each round.
- dotprinting(repeat=True, vrepr_label=False, **kwargs)[source]
Return the DOT description of the expression tree of
tuple_assign_outprop2op_model.See also
printing.dotprinting.- Parameters
repeat – whether to use different nodes for common subexpressions (default True)
vrepr_label – whether to use the verbose representation (
Term.vrepr) to label the nodes (default False)kwargs – additional arguments passed to
printing.dotprinting
- class cascada.abstractproperty.characteristic.EncryptionCharacteristic(input_prop, output_prop, assign_outprop_list, ch_model, external_props=None, free_props=None, empirical_ch_weight=None, empirical_data_list=None, is_valid=True)[source]
Bases:
objectRepresent characteristics over encryption functions w.r.t some property.
Given a
Cipher, anEncryptionCharacteristicis a characteristic (seeCharacteristic) for a particularPropertyover theCipher.encryption(where theCipher.key_scheduleis ignored).As opposed to
Characteristic, anEncryptionCharacteristicis defined for aabstractproperty.chmodel.EncryptionChModel.This class is not meant to be instantiated but to provide a base class for creating characteristic over encryption functions, (see
differential.characteristic.EncryptionCharacteristic,linear.characteristic.EncryptionCharacteristicoralgebraic.characteristic.EncryptionCharacteristicfor some examples).- split(prop_separators)[source]
Split into multiple
Characteristicobjects given the list of property separators.Given an
EncryptionCharacteristic, this method callsCharacteristic.splitto split the characteristic.Note
The new split characteristics are instances of
Characteristicand not ofEncryptionCharacteristic.
- classmethod random(ch_model, seed)[source]
Return a random
EncryptionCharacteristicwith givenabstractproperty.chmodel.EncryptionChModel.- Parameters
ch_model – the underlying characteristic model of the encryption function
seed – the seed used to sample bit-vectors
- class cascada.abstractproperty.characteristic.CipherCharacteristic(ks_input_prop, ks_output_prop, ks_assign_outprop_list, enc_input_prop, enc_output_prop, enc_assign_outprop_list, cipher_ch_model, ks_free_props=None, enc_free_props=None, ks_empirical_ch_weight=None, ks_empirical_data_list=None, enc_empirical_ch_weight=None, enc_empirical_data_list=None, ks_is_valid=True, enc_is_valid=True)[source]
Bases:
objectRepresent characteristics over ciphers w.r.t some property.
A
CipherCharacteristicis a pair ofCharacteristicobjects where one covers theCipher.key_scheduleand the other one covers theCipher.encryption.Note
The characteristic over the
Cipher.encryptionis an instance ofCharacteristicand not an instance ofEncryptionCharacteristic.The round key properties in the encryption characteristic (the properties of the external variables of the encryption
SSA) are set to the output properties of the key-schedule characteristic.This class is not meant to be instantiated but to provide a base class for creating characteristic over ciphers, (see
differential.characteristic.CipherCharacteristicoralgebraic.characteristic.CipherCharacteristicfor some examples).- cipher_ch_model
the underlying
abstractproperty.chmodel.CipherChModel
- ks_characteristic
the
Characteristicover the key schedule
- enc_characteristic
the
Characteristicover the encryption function
- static get_properties_for_initialization(cipher_ch_model, var2ct, ks_free_props=None, enc_free_props=None)[source]
Return the properties needed to initialize a
CipherCharacteristicobject.Given a
abstractproperty.chmodel.CipherChModeland a dictionary mappingVariableobjects (representing the symbolic properties) to theirConstantvalues, this method returns the following objects:ks_input_prop,ks_output_prop,ks_assign_outprop_list,enc_input_prop,enc_output_propandenc_assign_outprop_listfor theCipherCharacteristic.__init__method.Symbolic properties not affecting the characteristics can be given in
ks_free_propsandenc_free_props, and they will be set to 0.
- vrepr()[source]
Return an executable string representation.
This method returns a string so that
eval(self.vrepr())returns a newCipherCharacteristicobject with the same content.
- srepr()[source]
Return a short string representation of the cipher characteristic.
See also
Characteristic.srepr.
- signature(ch_signature_type)[source]
Return the signature of the cipher characteristic.
See also
Characteristic.signature.
- classmethod random(cipher_ch_model, seed)[source]
Return a random
CipherCharacteristicwith givenabstractproperty.chmodel.CipherChModel.- Parameters
cipher_ch_model – the underlying cipher characteristic model
seed – the seed used to sample bit-vectors
- get_formatted_logged_msgs()[source]
Return the list of logged messages.
See also
Characteristic.get_formatted_logged_msgs.