paranoid_crypto icon indicating copy to clipboard operation
paranoid_crypto copied to clipboard

gmpy2 port

Open alext-w opened this issue 1 year ago • 0 comments

Here's the update/port to gmpy2 (replacing gmpy) that we discussed in #19.

There's one open to discuss before merging, and a couple of notes:

  1. [OPEN] As mentioned in the discussion, I've ported the RNG code to do the right incantations, but the GMP-related RNG tests (testFindBiasImplGmp* + testGmp) fail right now because they expect an LCG, while the way gmpy2 initializes GMP's RNG has changed vs gmpy, and now it is an MT-based one. There is no way to change that via current gmpy2 API. I haven't had enough time to dig deeper into the tests themselves to see if they could be adjusted to catch an MT - so any and all suggestions on how to tackle this piece are welcome. The exact list of tests failing is at the end of this description, and the corresponding piece of code is marked with TODO.
  2. The porting itself was relatively straightforward with just some function name and incoming type changes. I also opted for making minimal changes by using the import gmpy2 as gmpy, so all the existing calls using gmpy.* would still work.
  3. Bookworm impplemented PEP668, that necessitates some adjustments in install instructions and the Dockerfile. I used a "fallback" switch in the latter, as there's not much value in using a virtual environment there.
  4. As gmpy2 is available in the distro repositories for at least Debian and Fedora, I've opted for using those instead of pulling it from PyPi as it was done with gmpy. That removes the need for gmpy2 compilation during installation, making it less failure-prone and quicker as a bonus, so I think it's a worthwhile change, but I'm certainly open to any alternative opinions on this.
  5. I've explicitly pinned the Dockerfile base image to bookworm to avoid any potential future silent breakage compared to when latest is used and the base updates. My proposal is to control this explicitly and move to the next one only after it's known to work.
  6. I also took the liberty of adding standard Python build artifacts to .gitignore in a separate commit, while at it.
List of failing RNG tests (7 of them):
======================================================================
FAIL: testFindBiasImplGmp128 (lib.randomness_tests.lattice_suite_test.LatticeSuiteTest)
lib.randomness_tests.lattice_suite_test.LatticeSuiteTest.testFindBiasImplGmp128
testFindBiasImplGmp128(128)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/paranoid-user/.local/lib/python3.11/site-packages/absl/testing/parameterized.py", line 321, in bound_param_test
    return test_method(self, *testcase_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/paranoid-user/paranoid_crypto/lib/randomness_tests/lattice_suite_test.py", line 55, in testFindBiasImpl
    self.assertAlmostEqual(0.0, p_value)
AssertionError: 0.0 != 0.8581001545263605 within 7 places (0.8581001545263605 difference)

======================================================================
FAIL: testFindBiasImplGmp16 (lib.randomness_tests.lattice_suite_test.LatticeSuiteTest)
lib.randomness_tests.lattice_suite_test.LatticeSuiteTest.testFindBiasImplGmp16
testFindBiasImplGmp16(16)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/paranoid-user/.local/lib/python3.11/site-packages/absl/testing/parameterized.py", line 321, in bound_param_test
    return test_method(self, *testcase_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/paranoid-user/paranoid_crypto/lib/randomness_tests/lattice_suite_test.py", line 55, in testFindBiasImpl
    self.assertAlmostEqual(0.0, p_value)
AssertionError: 0.0 != 0.1093731516214754 within 7 places (0.1093731516214754 difference)

======================================================================
FAIL: testFindBiasImplGmp20 (lib.randomness_tests.lattice_suite_test.LatticeSuiteTest)
lib.randomness_tests.lattice_suite_test.LatticeSuiteTest.testFindBiasImplGmp20
testFindBiasImplGmp20(20)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/paranoid-user/.local/lib/python3.11/site-packages/absl/testing/parameterized.py", line 321, in bound_param_test
    return test_method(self, *testcase_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/paranoid-user/paranoid_crypto/lib/randomness_tests/lattice_suite_test.py", line 55, in testFindBiasImpl
    self.assertAlmostEqual(0.0, p_value)
AssertionError: 0.0 != 0.43880437354200713 within 7 places (0.43880437354200713 difference)

======================================================================
FAIL: testFindBiasImplGmp28 (lib.randomness_tests.lattice_suite_test.LatticeSuiteTest)
lib.randomness_tests.lattice_suite_test.LatticeSuiteTest.testFindBiasImplGmp28
testFindBiasImplGmp28(28)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/paranoid-user/.local/lib/python3.11/site-packages/absl/testing/parameterized.py", line 321, in bound_param_test
    return test_method(self, *testcase_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/paranoid-user/paranoid_crypto/lib/randomness_tests/lattice_suite_test.py", line 55, in testFindBiasImpl
    self.assertAlmostEqual(0.0, p_value)
AssertionError: 0.0 != 0.10383315573674291 within 7 places (0.10383315573674291 difference)

======================================================================
FAIL: testFindBiasImplGmp32 (lib.randomness_tests.lattice_suite_test.LatticeSuiteTest)
lib.randomness_tests.lattice_suite_test.LatticeSuiteTest.testFindBiasImplGmp32
testFindBiasImplGmp32(32)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/paranoid-user/.local/lib/python3.11/site-packages/absl/testing/parameterized.py", line 321, in bound_param_test
    return test_method(self, *testcase_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/paranoid-user/paranoid_crypto/lib/randomness_tests/lattice_suite_test.py", line 55, in testFindBiasImpl
    self.assertAlmostEqual(0.0, p_value)
AssertionError: 0.0 != 0.5894791814531202 within 7 places (0.5894791814531202 difference)

======================================================================
FAIL: testFindBiasImplGmp64 (lib.randomness_tests.lattice_suite_test.LatticeSuiteTest)
lib.randomness_tests.lattice_suite_test.LatticeSuiteTest.testFindBiasImplGmp64
testFindBiasImplGmp64(64)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/paranoid-user/.local/lib/python3.11/site-packages/absl/testing/parameterized.py", line 321, in bound_param_test
    return test_method(self, *testcase_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/paranoid-user/paranoid_crypto/lib/randomness_tests/lattice_suite_test.py", line 55, in testFindBiasImpl
    self.assertAlmostEqual(0.0, p_value)
AssertionError: 0.0 != 0.9040601262600247 within 7 places (0.9040601262600247 difference)

======================================================================
FAIL: testGmp (lib.randomness_tests.rng_test.RngTest.testGmp)
lib.randomness_tests.rng_test.RngTest.testGmp
Regression test.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/paranoid-user/paranoid_crypto/lib/randomness_tests/rng_test.py", line 47, in testGmp
    self.assertEqual(0x43eca180f7892ceb,
AssertionError: 4894464470325734635 != 3809530840154749064

----------------------------------------------------------------------

Closes #19.

alext-w avatar Feb 28 '24 18:02 alext-w