patternpythonMinor
Implementations of rotate, shift, XOR, NOT and AND
Viewed 0 times
shiftimplementationsxorandrotatenot
Problem
I am going through the example of SHA256 over at Wikipedia and wanted to make sure I understood all the operations of the manipulation math before trying to actually attempt the implementation itself. The comments explain the entire thing.
Want I want to know is:
```
# Test rig for simple SHA256 operations
# rightrotate
# rightshift
# XOR
# NOT
# AND
# We'll do most operations to two input binary values
# We'll choose a misc value for rotate and shift
# We'll only operate rotate and shift on the first input
def printtest_set ( test_set ):
""
for x in range(len(test_set[0])):
print(test_set[0][x],'\t\t',test_set[1][x])
if __name__ == '__main__':
# Construct expected datatype
# 32 bit
# test_set some variables up
# rotate value
rotate_amount = 7
# shift amount
shift_amount = 7
# Create the array
test_set = [[0 for x in range(7)] for x in range(2)]
# Titles
test_set[0][0] = 'Input 1'
test_set[0][1] = 'Input 2'
test_set[0][2] = 'rightrotate'
test_set[0][3] = 'rightshift'
test_set[0][4] = 'XOR '
test_set[0][5] = 'NOT '
test_set[0][6] = 'AND '
# Values
test_set[1][0] = '00010001000100010001000100010001'
test_set[1][1] = '1010101010100001'
test_set[1][2] = ''
test_set[1][3] = ''
test_set[1][4] = ''
test_set[1][5] = ''
test_set[1][6] = ''
print ('\n\nInputs:\n')
printtest_set(test_set)
# test_set all data to the same le
Want I want to know is:
- Are my operations correct? The SHA256 code seems to be limited to manipulating 32 bit values in a 512 bit array, so I have made the first input 32 bit to make sure that this example rig is adequate.
- Can my operations be trivially improved? For example, am I making some awful decisions that are causing massive slowdown? I know compared to real crypto it's not going to even compare, I'm not doing real crypto, I'm just playing, but I would like to know if I have made some mega obvious efficiency mistakes.
```
# Test rig for simple SHA256 operations
# rightrotate
# rightshift
# XOR
# NOT
# AND
# We'll do most operations to two input binary values
# We'll choose a misc value for rotate and shift
# We'll only operate rotate and shift on the first input
def printtest_set ( test_set ):
""
for x in range(len(test_set[0])):
print(test_set[0][x],'\t\t',test_set[1][x])
if __name__ == '__main__':
# Construct expected datatype
# 32 bit
# test_set some variables up
# rotate value
rotate_amount = 7
# shift amount
shift_amount = 7
# Create the array
test_set = [[0 for x in range(7)] for x in range(2)]
# Titles
test_set[0][0] = 'Input 1'
test_set[0][1] = 'Input 2'
test_set[0][2] = 'rightrotate'
test_set[0][3] = 'rightshift'
test_set[0][4] = 'XOR '
test_set[0][5] = 'NOT '
test_set[0][6] = 'AND '
# Values
test_set[1][0] = '00010001000100010001000100010001'
test_set[1][1] = '1010101010100001'
test_set[1][2] = ''
test_set[1][3] = ''
test_set[1][4] = ''
test_set[1][5] = ''
test_set[1][6] = ''
print ('\n\nInputs:\n')
printtest_set(test_set)
# test_set all data to the same le
Solution
I don't know about optimization, but
This would allow you to simply many of your algorithms, using element-based loops instead of indices-based ones. For example, you could compute
In Python, try to use indices-based loops (with
test_set seems to scream for a dict. It seems more appropriate than a list of lists, especially since the indices are known and the "titles" never mutate:test_set = {
'Input 1' : '00010001000100010001000100010001',
'Input 2' : '1010101010100001'
'rightrotate' : ''
'rightshift' : ''
'XOR ' : ''
'NOT ' : ''
'AND ' : ''
}This would allow you to simply many of your algorithms, using element-based loops instead of indices-based ones. For example, you could compute
length_max with the following one-liner (untested):length_max = max([len(elem) for elem in test_set.values()])In Python, try to use indices-based loops (with
range) only if you have to, only if you do need the index. Otherwise, try to always use element-based loops.Code Snippets
test_set = {
'Input 1' : '00010001000100010001000100010001',
'Input 2' : '1010101010100001'
'rightrotate' : ''
'rightshift' : ''
'XOR ' : ''
'NOT ' : ''
'AND ' : ''
}length_max = max([len(elem) for elem in test_set.values()])Context
StackExchange Code Review Q#54091, answer score: 5
Revisions (0)
No revisions yet.