You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

112 rivejä
3.0 KiB

  1. try:
  2. import unittest2 as unittest
  3. except ImportError:
  4. import unittest
  5. import pytest
  6. try:
  7. from gmpy2 import mpz
  8. GMPY = True
  9. except ImportError: # pragma: no cover
  10. try:
  11. from gmpy import mpz
  12. GMPY = True
  13. except ImportError:
  14. GMPY = False
  15. from ._sha3 import shake_256
  16. from ._compat import bytes_to_int, int_to_bytes
  17. B2I_VECTORS = [
  18. (b"\x00\x01", "big", 1),
  19. (b"\x00\x01", "little", 0x0100),
  20. (b"", "big", 0),
  21. (b"\x00", "little", 0),
  22. ]
  23. @pytest.mark.parametrize("bytes_in,endian,int_out", B2I_VECTORS)
  24. def test_bytes_to_int(bytes_in, endian, int_out):
  25. out = bytes_to_int(bytes_in, endian)
  26. assert out == int_out
  27. class TestBytesToInt(unittest.TestCase):
  28. def test_bytes_to_int_wrong_endian(self):
  29. with self.assertRaises(ValueError):
  30. bytes_to_int(b"\x00", "middle")
  31. def test_int_to_bytes_wrong_endian(self):
  32. with self.assertRaises(ValueError):
  33. int_to_bytes(0, byteorder="middle")
  34. @pytest.mark.skipif(GMPY == False, reason="requires gmpy or gmpy2")
  35. def test_int_to_bytes_with_gmpy():
  36. assert int_to_bytes(mpz(1)) == b"\x01"
  37. I2B_VECTORS = [
  38. (0, None, "big", b""),
  39. (0, 1, "big", b"\x00"),
  40. (1, None, "big", b"\x01"),
  41. (0x0100, None, "little", b"\x00\x01"),
  42. (0x0100, 4, "little", b"\x00\x01\x00\x00"),
  43. (1, 4, "big", b"\x00\x00\x00\x01"),
  44. ]
  45. @pytest.mark.parametrize("int_in,length,endian,bytes_out", I2B_VECTORS)
  46. def test_int_to_bytes(int_in, length, endian, bytes_out):
  47. out = int_to_bytes(int_in, length, endian)
  48. assert out == bytes_out
  49. SHAKE_256_VECTORS = [
  50. (
  51. b"Message.",
  52. 32,
  53. b"\x78\xa1\x37\xbb\x33\xae\xe2\x72\xb1\x02\x4f\x39\x43\xe5\xcf\x0c"
  54. b"\x4e\x9c\x72\x76\x2e\x34\x4c\xf8\xf9\xc3\x25\x9d\x4f\x91\x2c\x3a",
  55. ),
  56. (
  57. b"",
  58. 32,
  59. b"\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb\x24"
  60. b"\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5\x76\x2f",
  61. ),
  62. (
  63. b"message",
  64. 32,
  65. b"\x86\x16\xe1\xe4\xcf\xd8\xb5\xf7\xd9\x2d\x43\xd8\x6e\x1b\x14\x51"
  66. b"\xa2\xa6\x5a\xf8\x64\xfc\xb1\x26\xc2\x66\x0a\xb3\x46\x51\xb1\x75",
  67. ),
  68. (
  69. b"message",
  70. 16,
  71. b"\x86\x16\xe1\xe4\xcf\xd8\xb5\xf7\xd9\x2d\x43\xd8\x6e\x1b\x14\x51",
  72. ),
  73. (
  74. b"message",
  75. 64,
  76. b"\x86\x16\xe1\xe4\xcf\xd8\xb5\xf7\xd9\x2d\x43\xd8\x6e\x1b\x14\x51"
  77. b"\xa2\xa6\x5a\xf8\x64\xfc\xb1\x26\xc2\x66\x0a\xb3\x46\x51\xb1\x75"
  78. b"\x30\xd6\xba\x2a\x46\x65\xf1\x9d\xf0\x62\x25\xb1\x26\xd1\x3e\xed"
  79. b"\x91\xd5\x0d\xe7\xb9\xcb\x65\xf3\x3a\x46\xae\xd3\x6c\x7d\xc5\xe8",
  80. ),
  81. (
  82. b"A" * 1024,
  83. 32,
  84. b"\xa5\xef\x7e\x30\x8b\xe8\x33\x64\xe5\x9c\xf3\xb5\xf3\xba\x20\xa3"
  85. b"\x5a\xe7\x30\xfd\xbc\x33\x11\xbf\x83\x89\x50\x82\xb4\x41\xe9\xb3",
  86. ),
  87. ]
  88. @pytest.mark.parametrize("msg,olen,ohash", SHAKE_256_VECTORS)
  89. def test_shake_256(msg, olen, ohash):
  90. out = shake_256(msg, olen)
  91. assert out == bytearray(ohash)