-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cs
committed
Oct 1, 2024
1 parent
0c639d0
commit 075afba
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
tests/unit/cipher_modules/generic_functions_vectorized_bit_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from claasp.cipher_modules.generic_functions_vectorized_bit import * | ||
import numpy as np | ||
|
||
|
||
def test_byte_vector_XOR(): | ||
input_values = [np.arange(8, dtype = np.uint8).reshape((2,4)), np.arange(240, 248, dtype = np.uint8).reshape((2,4))] | ||
expected_result = input_values[0]^input_values[1] | ||
xor_result = byte_vector_XOR(input_values) | ||
assert np.all(xor_result == expected_result) | ||
|
||
def test_byte_vector_OR(): | ||
input_values = [np.arange(8, dtype = np.uint8).reshape((2,4)), np.arange(240, 248, dtype = np.uint8).reshape((2,4))] | ||
expected_result = input_values[0] | input_values[1] | ||
xor_result = byte_vector_OR(input_values) | ||
assert np.all(xor_result == expected_result) | ||
|
||
def test_byte_vector_AND(): | ||
input_values = [np.arange(8, dtype = np.uint8).reshape((2,4)), np.arange(240, 248, dtype = np.uint8).reshape((2,4))] | ||
expected_result = input_values[0]&input_values[1] | ||
xor_result = byte_vector_AND(input_values) | ||
assert np.all(xor_result == expected_result) | ||
|
||
def test_byte_vector_NOT(): | ||
input_values = [np.arange(8, dtype = np.uint8).reshape((2,4))] | ||
expected_result = input_values[0]^0xff | ||
xor_result = byte_vector_NOT(input_values) | ||
assert np.all(xor_result == expected_result) | ||
|
||
def test_byte_vector_MODADD(): | ||
bits = 48 | ||
A = [0xcafecafecafe] | ||
B = [0xdecadecadeca] | ||
input_values = [integer_array_to_evaluate_vectorized_input(A, bits), integer_array_to_evaluate_vectorized_input(B, bits)] | ||
expected_result = integer_array_to_evaluate_vectorized_input([(A[0]+B[0]) % (2**bits)], bits) | ||
modadd_result = byte_vector_MODADD(input_values) | ||
assert np.all(modadd_result == expected_result) | ||
|
||
def test_byte_vector_MODSUB(): | ||
bits = 48 | ||
A = [0xcafecafecafe] | ||
B = [0xdecadecadeca] | ||
input_values = [integer_array_to_evaluate_vectorized_input(A, bits), integer_array_to_evaluate_vectorized_input(B, bits)] | ||
expected_result = integer_array_to_evaluate_vectorized_input([(A[0]-B[0]) % (2**bits)], bits) | ||
modsub_result = byte_vector_MODSUB(input_values) | ||
|
||
assert np.all(modsub_result == expected_result) | ||
|
||
def test_byte_vector_ROTATE(): | ||
bits = 12 | ||
input_values = integer_array_to_evaluate_vectorized_input([0, 0xfff], bits) | ||
rotate_result = byte_vector_ROTATE([input_values], rotation_amount = -4, input_bit_size=bits) | ||
assert np.all(rotate_result==input_values) | ||
|
||
bits = 12 | ||
input_values = integer_array_to_evaluate_vectorized_input([0, 0xff], bits) | ||
expected_result = integer_array_to_evaluate_vectorized_input([0, 0x1fe], bits) | ||
rotate_result = byte_vector_ROTATE([input_values], rotation_amount = -1, input_bit_size=bits) | ||
|
||
print("In :", evaluate_vectorized_outputs_to_integers([input_values.transpose()], bits)) | ||
print("Exp:", evaluate_vectorized_outputs_to_integers([expected_result.transpose()], bits)) | ||
print("Out:",evaluate_vectorized_outputs_to_integers([rotate_result.transpose()], bits)) | ||
|
||
assert np.all(rotate_result==expected_result) | ||
|