<s> '''<STR_LIT>''' <EOL> from parser import stpcommands <EOL> from ciphers . cipher import AbstractCipher <EOL> from parser . stpcommands import getStringRightRotate as rotr <EOL> from parser . stpcommands import getStringLeftRotate as rotl <EOL> class SimonLinearCipher ( AbstractCipher ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> rot_alpha = <NUM_LIT:8> <EOL> rot_beta = <NUM_LIT:1> <EOL> rot_gamma = <NUM_LIT:2> <EOL> def getFormatString ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT:x>' , '<STR_LIT:y>' , '<STR_LIT:w>' ] <EOL> def createSTP ( self , stp_filename , parameters ) : <EOL> """<STR_LIT>""" <EOL> wordsize = parameters [ "<STR_LIT>" ] <EOL> rounds = parameters [ "<STR_LIT>" ] <EOL> weight = parameters [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in parameters : <EOL> self . rot_alpha = parameters [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> self . rot_beta = parameters [ "<STR_LIT>" ] [ <NUM_LIT:1> ] <EOL> self . rot_gamma = parameters [ "<STR_LIT>" ] [ <NUM_LIT:2> ] <EOL> with open ( stp_filename , '<STR_LIT:w>' ) as stp_file : <EOL> header = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( wordsize , <EOL> self . rot_alpha , <EOL> self . rot_beta , <EOL> self . rot_gamma , <EOL> rounds ) ) <EOL> stp_file . write ( header ) <EOL> x = [ "<STR_LIT>" . format ( i ) for i in range ( rounds + <NUM_LIT:1> ) ] <EOL> y = [ "<STR_LIT>" . format ( i ) for i in range ( rounds + <NUM_LIT:1> ) ] <EOL> b = [ "<STR_LIT>" . format ( i ) for i in range ( rounds + <NUM_LIT:1> ) ] <EOL> c = [ "<STR_LIT>" . format ( i ) for i in range ( rounds + <NUM_LIT:1> ) ] <EOL> and_out = [ "<STR_LIT>" . format ( i ) for i in range ( rounds + <NUM_LIT:1> ) ] <EOL> abits = [ "<STR_LIT>" . format ( i ) for i in range ( rounds + <NUM_LIT:1> ) ] <EOL> tmpWeight = [ "<STR_LIT>" . format ( j , i ) for i in range ( rounds ) <EOL> for j in range ( wordsize ) ] <EOL> sbits = [ "<STR_LIT>" . format ( j , i ) for i in range ( rounds ) <EOL> for j in range ( wordsize ) ] <EOL> pbits = [ "<STR_LIT>" . format ( j , i ) for i in range ( rounds ) <EOL> for j in range ( wordsize ) ] <EOL> w = [ "<STR_LIT>" . format ( i ) for i in range ( rounds ) ] <EOL> stpcommands . setupVariables ( stp_file , x , wordsize ) <EOL> stpcommands . setupVariables ( stp_file , y , wordsize ) <EOL> stpcommands . setupVariables ( stp_file , and_out , wordsize ) <EOL> stpcommands . setupVariables ( stp_file , b , wordsize ) <EOL> stpcommands . setupVariables ( stp_file , c , wordsize ) <EOL> stpcommands . setupVariables ( stp_file , abits , wordsize ) <EOL> stpcommands . setupVariables ( stp_file , w , wordsize ) <EOL> stpcommands . setupVariables ( stp_file , tmpWeight , wordsize ) <EOL> stpcommands . setupVariables ( stp_file , sbits , wordsize ) <EOL> stpcommands . setupVariables ( stp_file , pbits , wordsize ) <EOL> stpcommands . setupWeightComputation ( stp_file , weight , w , wordsize ) <EOL> for i in range ( rounds ) : <EOL> indicesFrom = i * wordsize <EOL> indicesTo = ( i + <NUM_LIT:1> ) * wordsize <EOL> self . setupSimonRound ( stp_file , x [ i ] , y [ i ] , x [ i + <NUM_LIT:1> ] , y [ i + <NUM_LIT:1> ] , <EOL> and_out [ i ] , b [ i ] , c [ i ] , abits [ i ] , w [ i ] , <EOL> tmpWeight [ indicesFrom : indicesTo ] , <EOL> sbits [ indicesFrom : indicesTo ] , <EOL> pbits [ indicesFrom : indicesTo ] , <EOL> wordsize ) <EOL> stpcommands . assertNonZero ( stp_file , x + y , wordsize ) <EOL> if parameters [ "<STR_LIT>" ] : <EOL> stpcommands . assertVariableValue ( stp_file , x [ <NUM_LIT:0> ] , x [ rounds ] ) <EOL> stpcommands . assertVariableValue ( stp_file , y [ <NUM_LIT:0> ] , y [ rounds ] ) <EOL> for key , value in parameters [ "<STR_LIT>" ] . items ( ) : <EOL> stpcommands . assertVariableValue ( stp_file , key , value ) <EOL> for char in parameters [ "<STR_LIT>" ] : <EOL> stpcommands . blockCharacteristic ( stp_file , char , wordsize ) <EOL> stpcommands . setupQuery ( stp_file ) <EOL> return <EOL> def setupSimonRound ( self , stp_file , x_in , y_in , x_out , y_out , and_out , b , c , <EOL> abits , w , tmpWeight , sbits , pbits , wordsize ) : <EOL> """<STR_LIT>""" <EOL> command = "<STR_LIT>" <EOL> deltarot = self . rot_alpha - self . rot_beta <EOL> lout = y_in <EOL> lin = "<STR_LIT>" . format ( x_in , rotr ( lout , self . rot_gamma , wordsize ) , y_out ) <EOL> command += "<STR_LIT>" . format ( x_out , y_in ) <EOL> tmp = "<STR_LIT>" . format ( <EOL> rotr ( lout , self . rot_alpha , wordsize ) , <EOL> rotr ( lout , self . rot_beta , wordsize ) , <EOL> lin ) <EOL> command += "<STR_LIT>" . format ( tmp , "<STR_LIT:0>" * ( wordsize // <NUM_LIT:4> ) ) <EOL> command += "<STR_LIT>" . format ( <EOL> y_out , x_in , rotr ( x_out , self . rot_gamma , wordsize ) , lin ) <EOL> loutRotated = rotr ( lout , deltarot , wordsize ) <EOL> command += "<STR_LIT>" . format ( tmpWeight [ <NUM_LIT:0> ] , lout , loutRotated ) <EOL> for i in range ( <NUM_LIT:1> , wordsize ) : <EOL> command += "<STR_LIT>" . format ( <EOL> tmpWeight [ i ] , lout , rotr ( tmpWeight [ i - <NUM_LIT:1> ] , deltarot , wordsize ) ) <EOL> abits = "<STR_LIT>" . format ( lout , tmpWeight [ <NUM_LIT:0> ] ) <EOL> for i in range ( <NUM_LIT:1> , wordsize ) : <EOL> abits = "<STR_LIT>" . format ( tmpWeight [ i ] , abits ) <EOL> command += "<STR_LIT>" . format ( w , abits ) <EOL> command += "<STR_LIT>" . format ( <EOL> sbits [ <NUM_LIT:0> ] , rotr ( "<STR_LIT>" . format ( <EOL> rotr ( lout , deltarot , wordsize ) , lout , rotr ( abits , deltarot , wordsize ) ) , <EOL> <NUM_LIT:1> , wordsize ) ) <EOL> command += "<STR_LIT>" . format ( <EOL> pbits [ <NUM_LIT:0> ] , rotl ( "<STR_LIT>" . format ( sbits [ <NUM_LIT:0> ] , lin ) , <NUM_LIT:2> * deltarot , wordsize ) ) <EOL> for i in range ( <NUM_LIT:1> , wordsize ) : <EOL> command += "<STR_LIT>" . format ( <EOL> sbits [ i ] , rotl ( "<STR_LIT>" . format ( <EOL> rotl ( sbits [ i - <NUM_LIT:1> ] , deltarot , wordsize ) , <EOL> rotr ( sbits [ i - <NUM_LIT:1> ] , self . rot_beta , wordsize ) ) , <EOL> deltarot , wordsize ) <EOL> ) <EOL> command += "<STR_LIT>" . format ( <EOL> pbits [ i ] , rotl ( "<STR_LIT>" . format ( <EOL> pbits [ i - <NUM_LIT:1> ] , sbits [ i ] , lin ) , <NUM_LIT:2> * deltarot , wordsize ) <EOL> ) <EOL> command += "<STR_LIT>" . format ( pbits [ wordsize - <NUM_LIT:1> ] , "<STR_LIT:0>" * ( wordsize // <NUM_LIT:4> ) ) <EOL> stp_file . write ( command ) <EOL> return </s>
