integer_arithmetic_test

Comprehensive arithmetic tests for the integer module.

These tests validate integer module behavior with GCD operations and integer conversions, specifically designed to catch Python 3.12+ compatibility issues like the Py_SIZE() vs lv_tag bug.

Tests cover: 1. Integer conversion correctness (Python int <-> integer) 2. GCD operations and isCoPrime() method 3. Modular arithmetic (modular inverse, modular operations) 4. Regression tests for Python 3.12+ compatibility 5. Integration tests that mirror real scheme usage

class integer_arithmetic_test.ArithmeticOperationsTest(methodName='runTest')[source]

Bases: TestCase

Test basic arithmetic operations on integer objects.

test_addition()[source]

Test integer addition.

test_comparison()[source]

Test integer comparison operations.

test_division()[source]

Test integer division.

test_exponentiation()[source]

Test integer exponentiation.

test_multiplication()[source]

Test integer multiplication.

test_subtraction()[source]

Test integer subtraction.

class integer_arithmetic_test.GCDOperationsTest(methodName='runTest')[source]

Bases: TestCase

Test GCD operations with various integer types.

test_gcd_edge_cases()[source]

Test gcd edge cases.

test_gcd_integer_objects()[source]

Test gcd() with integer objects.

test_gcd_mixed_types()[source]

Test gcd() with mixed Python int and integer objects.

test_gcd_python_ints()[source]

Test gcd() with Python integers.

class integer_arithmetic_test.IntegerConversionTest(methodName='runTest')[source]

Bases: TestCase

Test integer conversion correctness between Python int and integer objects.

test_common_rsa_exponents()[source]

Verify that common RSA exponents convert correctly.

test_integer_from_integer()[source]

Test creating integer from another integer object.

test_large_values()[source]

Test large values that require multiple digits in PyLongObject.

test_negative_values()[source]

Test negative integer conversion.

test_round_trip_conversion()[source]

Verify round-trip conversion: Python int -> integer -> Python int preserves value.

test_small_values()[source]

Test edge cases with small values.

class integer_arithmetic_test.IntegrationSchemeTest(methodName='runTest')[source]

Bases: TestCase

Integration tests that mirror real cryptographic scheme usage.

test_paillier_pattern()[source]

Test Paillier-like integer encoding pattern.

test_rsa_coprime_search_pattern()[source]

Test the RSA keygen coprime search pattern.

This mirrors the pattern used in pkenc_rsa.py to find e coprime to phi_N.

test_rsa_encryption_decryption_pattern()[source]

Test RSA encryption/decryption with integer operations.

test_serialization_roundtrip()[source]

Test serialization and deserialization of integer objects.

class integer_arithmetic_test.IsCoPrimeTest(methodName='runTest')[source]

Bases: TestCase

Test isCoPrime() method for coprimality checking.

test_coprime_common_exponents()[source]

Test isCoPrime() with common RSA exponents vs typical phi_N values.

test_coprime_edge_cases()[source]

Test isCoPrime() edge cases.

test_coprime_with_integer_objects()[source]

Test isCoPrime() with integer objects as arguments.

class integer_arithmetic_test.ModularArithmeticTest(methodName='runTest')[source]

Bases: TestCase

Test modular arithmetic operations.

test_integer_without_modulus()[source]

Test integer behavior when modulus is not set.

test_modular_exponentiation()[source]

Test modular exponentiation.

test_modular_inverse_basic()[source]

Test basic modular inverse computation.

test_modular_inverse_rsa_exponent()[source]

Test modular inverse with RSA-like parameters.

test_modular_operations_respect_modulus()[source]

Test that modular operations respect the modulus.

class integer_arithmetic_test.Python312CompatibilityTest(methodName='runTest')[source]

Bases: TestCase

Regression tests for Python 3.12+ compatibility.

These tests specifically target the Py_SIZE() vs lv_tag bug that was fixed. The bug caused incorrect digit count extraction for multi-digit integers.

test_65537_regression()[source]

Test the specific value that exposed the Python 3.12+ bug.

In the buggy version, integer(65537) returned a huge incorrect value like 12259964326940877255866161939725058870607969088809533441.

test_digit_boundary_values()[source]

Test values at digit boundaries (multiples of 2^30).

test_mpz_to_pylong_roundtrip()[source]

Test that mpzToLongObj correctly creates Python integers.

This tests the reverse direction: GMP mpz_t -> Python int.

test_multi_digit_integers()[source]

Test integers that require multiple digits in PyLongObject.

Python uses 30-bit digits internally. Values >= 2^30 require multiple digits. The bug was in extracting the digit count from lv_tag.

test_sign_handling()[source]

Test sign handling for negative integers.

In Python 3.12+, sign is stored in lv_tag bits 0-1: - 0 = positive - 1 = zero - 2 = negative