From 72fa9e29726aa2971563e0dee25bf39856612862 Mon Sep 17 00:00:00 2001 From: Gerald Teschl Date: Wed, 23 Oct 2024 19:48:46 +0200 Subject: [PATCH] Added factor_rho --- doc/kryptools.ipynb | 277 ++++++++++++++++++++++------------------ kryptools/factor_dix.py | 4 +- kryptools/factor_ecm.py | 4 +- kryptools/factor_pm1.py | 4 +- kryptools/factor_qs.py | 2 +- kryptools/factor_rho.py | 58 +++++++++ 6 files changed, 219 insertions(+), 130 deletions(-) create mode 100644 kryptools/factor_rho.py diff --git a/doc/kryptools.ipynb b/doc/kryptools.ipynb index 374a5af..1f44e75 100644 --- a/doc/kryptools.ipynb +++ b/doc/kryptools.ipynb @@ -1180,6 +1180,37 @@ "factor_pm1(832283)" ] }, + { + "cell_type": "markdown", + "id": "10ac68ee-77f3-4ea2-9906-4640b89a0113", + "metadata": {}, + "source": [ + "[Pollard $\\rho$](https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "6c0cb7c6-03e5-469f-8420-496813bd26ea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "487" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from kryptools.factor_rho import factor_rho\n", + "\n", + "factor_rho(832283)" + ] + }, { "cell_type": "markdown", "id": "3d847314-d9af-4a72-af15-374755d6ff93", @@ -1190,7 +1221,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 38, "id": "e68d0db0-7d1d-4a00-ad6e-10656c829708", "metadata": {}, "outputs": [ @@ -1200,7 +1231,7 @@ "487" ] }, - "execution_count": 37, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } @@ -1221,7 +1252,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 39, "id": "3b7c0474-835b-4b10-b0d3-a7b8f62eff23", "metadata": {}, "outputs": [ @@ -1231,7 +1262,7 @@ "1709" ] }, - "execution_count": 38, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -1252,7 +1283,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 40, "id": "8ee9854b-6ce0-43b6-906d-98f52fb74d8e", "metadata": {}, "outputs": [ @@ -1262,7 +1293,7 @@ "1709" ] }, - "execution_count": 39, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -1291,7 +1322,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 41, "id": "46ab20cc-8d39-4d7b-baef-f411cf34deb7", "metadata": {}, "outputs": [], @@ -1324,7 +1355,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 42, "id": "66ae327c-ff14-4bff-b12d-330c1787cd6b", "metadata": {}, "outputs": [], @@ -1347,7 +1378,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 43, "id": "d1ce2beb-4e60-4a56-9943-d47f1945f13a", "metadata": {}, "outputs": [], @@ -1370,7 +1401,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 44, "id": "fb231c7c-293f-4353-8048-1de3d26568e1", "metadata": {}, "outputs": [], @@ -1393,7 +1424,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 45, "id": "ad8d5929-488e-44bc-9263-91044a2886d4", "metadata": {}, "outputs": [ @@ -1425,7 +1456,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 46, "id": "882543ac-a2f7-4448-93bd-d9385d3457f6", "metadata": {}, "outputs": [ @@ -1466,7 +1497,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 47, "id": "4626c758-753b-4d3a-9ed0-99038ca5f280", "metadata": {}, "outputs": [ @@ -1477,7 +1508,7 @@ "[ 35 ]" ] }, - "execution_count": 46, + "execution_count": 47, "metadata": {}, "output_type": "execute_result" } @@ -1500,7 +1531,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 48, "id": "8a2f83a9-7114-43a1-baee-7f6bb6c45f30", "metadata": {}, "outputs": [ @@ -1511,7 +1542,7 @@ "[ 3 ]" ] }, - "execution_count": 47, + "execution_count": 48, "metadata": {}, "output_type": "execute_result" } @@ -1530,7 +1561,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 49, "id": "ef4a7e98-4d55-482b-a264-6aab3283e6ec", "metadata": {}, "outputs": [ @@ -1541,7 +1572,7 @@ "[ 1, 2, 3 ]" ] }, - "execution_count": 48, + "execution_count": 49, "metadata": {}, "output_type": "execute_result" } @@ -1561,7 +1592,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 50, "id": "3bec64e6-937f-4041-a710-cf42c95a8689", "metadata": {}, "outputs": [ @@ -1573,7 +1604,7 @@ "[ 7, 8, 12 ]" ] }, - "execution_count": 49, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } @@ -1596,7 +1627,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 51, "id": "ab222100-fc64-49b9-b5ef-2d6ae719f15c", "metadata": {}, "outputs": [ @@ -1608,7 +1639,7 @@ "[ 0, 0, 1 ]" ] }, - "execution_count": 50, + "execution_count": 51, "metadata": {}, "output_type": "execute_result" } @@ -1627,7 +1658,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 52, "id": "a99ed976-cbdd-4ce7-8a97-efcee7fd510d", "metadata": {}, "outputs": [ @@ -1637,7 +1668,7 @@ "-9" ] }, - "execution_count": 51, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -1656,7 +1687,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 53, "id": "64294f1f-d3ef-41b0-ad59-d68e69c89aba", "metadata": {}, "outputs": [ @@ -1677,7 +1708,7 @@ "[ 0, 0, 1 ]" ] }, - "execution_count": 52, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } @@ -1698,7 +1729,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 54, "id": "47961b0f-e632-4d1c-9445-de3c217bce10", "metadata": {}, "outputs": [ @@ -1708,7 +1739,7 @@ "2" ] }, - "execution_count": 53, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -1722,7 +1753,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 55, "id": "d9cfb165-6e94-413b-9a91-5c5aa3870aa3", "metadata": {}, "outputs": [ @@ -1743,7 +1774,7 @@ "[ 0, 0, 1 ]" ] }, - "execution_count": 54, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } @@ -1772,7 +1803,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 56, "id": "0f5a048b-767e-4848-bb23-c898365ee9dc", "metadata": {}, "outputs": [ @@ -1784,7 +1815,7 @@ "[ 0, 0, 1 ]" ] }, - "execution_count": 55, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -1806,7 +1837,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 57, "id": "61bad2d5-2fad-4f5a-b4cf-206fca8e09fd", "metadata": {}, "outputs": [ @@ -1817,7 +1848,7 @@ "[ 0, 1 ]" ] }, - "execution_count": 56, + "execution_count": 57, "metadata": {}, "output_type": "execute_result" } @@ -1841,7 +1872,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 58, "id": "5712d013-c037-4b03-9f62-13fdaaae855b", "metadata": {}, "outputs": [ @@ -1851,7 +1882,7 @@ "5.0" ] }, - "execution_count": 57, + "execution_count": 58, "metadata": {}, "output_type": "execute_result" } @@ -1872,7 +1903,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 59, "id": "0c56dbdc-caa4-4783-8c2f-8f586a8dc4a1", "metadata": {}, "outputs": [ @@ -1882,7 +1913,7 @@ "0.3521856535823236" ] }, - "execution_count": 58, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } @@ -1903,7 +1934,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 60, "id": "c3f8c523-114c-46b9-968d-4eb31615de4b", "metadata": {}, "outputs": [ @@ -1921,7 +1952,7 @@ "[ 0 ]" ] }, - "execution_count": 59, + "execution_count": 60, "metadata": {}, "output_type": "execute_result" } @@ -1947,7 +1978,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 61, "id": "b8ba76ae-092c-4f12-9a05-0b33fd54ebb2", "metadata": {}, "outputs": [ @@ -1965,7 +1996,7 @@ "[ 0 ]" ] }, - "execution_count": 60, + "execution_count": 61, "metadata": {}, "output_type": "execute_result" } @@ -1989,7 +2020,7 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 62, "id": "bd6ea8c7-85ba-4b8d-97d6-f3176e9a22d4", "metadata": {}, "outputs": [ @@ -2007,7 +2038,7 @@ "[ 0 ]" ] }, - "execution_count": 61, + "execution_count": 62, "metadata": {}, "output_type": "execute_result" } @@ -2032,7 +2063,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 63, "id": "ee41707c-8d92-468d-9a94-18b6bd44053c", "metadata": {}, "outputs": [ @@ -2044,7 +2075,7 @@ "[ 1, -1, 1 ]" ] }, - "execution_count": 62, + "execution_count": 63, "metadata": {}, "output_type": "execute_result" } @@ -2067,7 +2098,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 64, "id": "694c25e5-d01f-4acf-ab57-69bffb0fb9a8", "metadata": {}, "outputs": [ @@ -2079,7 +2110,7 @@ "[ 4, 2, 9 ]" ] }, - "execution_count": 63, + "execution_count": 64, "metadata": {}, "output_type": "execute_result" } @@ -2108,7 +2139,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 65, "id": "df0886b4-54d8-4fe7-a665-e9b908c41060", "metadata": {}, "outputs": [ @@ -2121,7 +2152,7 @@ "[ 49, -45, 29, 735 ]" ] }, - "execution_count": 64, + "execution_count": 65, "metadata": {}, "output_type": "execute_result" } @@ -2149,7 +2180,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 66, "id": "6ff2fa77-eebd-4f1c-a894-f3a9c294bfc7", "metadata": {}, "outputs": [ @@ -2159,7 +2190,7 @@ "0.9962449639096419" ] }, - "execution_count": 65, + "execution_count": 66, "metadata": {}, "output_type": "execute_result" } @@ -2178,7 +2209,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 67, "id": "e9009a78-4e62-40a8-b231-987bd4e42a86", "metadata": {}, "outputs": [ @@ -2188,7 +2219,7 @@ "367.329164515002" ] }, - "execution_count": 66, + "execution_count": 67, "metadata": {}, "output_type": "execute_result" } @@ -2211,7 +2242,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 68, "id": "5a07021d-93e6-41fe-8a66-523c0faad0f9", "metadata": {}, "outputs": [ @@ -2224,7 +2255,7 @@ "[ 823300, -1867613, 1986482, -1699706 ]" ] }, - "execution_count": 67, + "execution_count": 68, "metadata": {}, "output_type": "execute_result" } @@ -2244,7 +2275,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 69, "id": "7ef33518-5192-4603-8df3-21f4524aaf76", "metadata": {}, "outputs": [ @@ -2254,7 +2285,7 @@ "0.0002031966189726793" ] }, - "execution_count": 68, + "execution_count": 69, "metadata": {}, "output_type": "execute_result" } @@ -2273,7 +2304,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 70, "id": "fbef8bf6-10be-4aea-b672-ddadfa0b705f", "metadata": {}, "outputs": [ @@ -2283,7 +2314,7 @@ "0.015520618275460585" ] }, - "execution_count": 69, + "execution_count": 70, "metadata": {}, "output_type": "execute_result" } @@ -2302,7 +2333,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 71, "id": "7a652c7d-9726-47cc-b2a6-ad22c4dec5f4", "metadata": {}, "outputs": [ @@ -2315,7 +2346,7 @@ "[ 15226425 ]" ] }, - "execution_count": 70, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -2343,7 +2374,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 72, "id": "4e9e3dfb-564c-4a59-9d71-9e4be59b4cac", "metadata": {}, "outputs": [ @@ -2353,7 +2384,7 @@ "True" ] }, - "execution_count": 71, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -2373,7 +2404,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 73, "id": "b81fe34e-3337-484c-900d-21fa9d7a2945", "metadata": {}, "outputs": [ @@ -2383,7 +2414,7 @@ "False" ] }, - "execution_count": 72, + "execution_count": 73, "metadata": {}, "output_type": "execute_result" } @@ -2403,7 +2434,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 74, "id": "a871a91f-fff2-4645-b868-c31972228de2", "metadata": {}, "outputs": [ @@ -2413,7 +2444,7 @@ "True" ] }, - "execution_count": 73, + "execution_count": 74, "metadata": {}, "output_type": "execute_result" } @@ -2441,7 +2472,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 75, "id": "d3c69787-da17-4cd3-b282-a68ee4d177e6", "metadata": {}, "outputs": [ @@ -2451,7 +2482,7 @@ "(2 x^2 + 2, 4 x^3 + 3 x^2 + 2 x + 1)" ] }, - "execution_count": 74, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } @@ -2475,7 +2506,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 76, "id": "93326765-bb08-4c15-9d1a-97b1440ba986", "metadata": {}, "outputs": [ @@ -2485,7 +2516,7 @@ "8 x^5 + 6 x^4 + 12 x^3 + 8 x^2 + 4 x + 2" ] }, - "execution_count": 75, + "execution_count": 76, "metadata": {}, "output_type": "execute_result" } @@ -2504,7 +2535,7 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 77, "id": "81d433fc-6dcd-42a7-97ef-362913bf99c0", "metadata": {}, "outputs": [ @@ -2514,7 +2545,7 @@ "(2.0 x + 1.5, -2.0 x - 2.0)" ] }, - "execution_count": 76, + "execution_count": 77, "metadata": {}, "output_type": "execute_result" } @@ -2536,7 +2567,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": 78, "id": "f1e28f9a-0366-44a7-9649-d4d2b74a9280", "metadata": {}, "outputs": [ @@ -2546,7 +2577,7 @@ "(2.0 x + 1.5, -2.0 x - 2.0)" ] }, - "execution_count": 77, + "execution_count": 78, "metadata": {}, "output_type": "execute_result" } @@ -2565,7 +2596,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 79, "id": "1377b01d-eded-4d0b-892c-75740cb3e8a8", "metadata": {}, "outputs": [ @@ -2575,7 +2606,7 @@ "2 + 2 x^2" ] }, - "execution_count": 78, + "execution_count": 79, "metadata": {}, "output_type": "execute_result" } @@ -2589,7 +2620,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 80, "id": "424c40e8-564e-4c64-b0f1-5122f6c7a41e", "metadata": {}, "outputs": [], @@ -2607,7 +2638,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 81, "id": "f8094d46-9ca8-46dc-9be3-96eb363b3ade", "metadata": {}, "outputs": [ @@ -2617,7 +2648,7 @@ "(2 x + 3/2, -2 x - 2)" ] }, - "execution_count": 80, + "execution_count": 81, "metadata": {}, "output_type": "execute_result" } @@ -2639,7 +2670,7 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": 82, "id": "c121753b-dc2c-4000-9f14-b71b78f65d60", "metadata": {}, "outputs": [ @@ -2649,7 +2680,7 @@ "-2 x - 2" ] }, - "execution_count": 81, + "execution_count": 82, "metadata": {}, "output_type": "execute_result" } @@ -2669,7 +2700,7 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": 83, "id": "f26253d7-43fd-4242-96ca-5032a309e48e", "metadata": {}, "outputs": [ @@ -2679,7 +2710,7 @@ "2" ] }, - "execution_count": 82, + "execution_count": 83, "metadata": {}, "output_type": "execute_result" } @@ -2698,7 +2729,7 @@ }, { "cell_type": "code", - "execution_count": 83, + "execution_count": 84, "id": "1b33195a-0c3c-46c4-94b9-94c9ca0dee0e", "metadata": {}, "outputs": [ @@ -2708,7 +2739,7 @@ "4" ] }, - "execution_count": 83, + "execution_count": 84, "metadata": {}, "output_type": "execute_result" } @@ -2735,7 +2766,7 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": 85, "id": "cf284301-bb0d-44dc-b3cf-51ef187b01ad", "metadata": {}, "outputs": [ @@ -2745,7 +2776,7 @@ "x^4 + x^2 + x + 1" ] }, - "execution_count": 84, + "execution_count": 85, "metadata": {}, "output_type": "execute_result" } @@ -2777,7 +2808,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 86, "id": "85ce3cb2-faf5-4a75-b624-dfff2b2ce255", "metadata": {}, "outputs": [ @@ -2787,7 +2818,7 @@ "141" ] }, - "execution_count": 85, + "execution_count": 86, "metadata": {}, "output_type": "execute_result" } @@ -2819,7 +2850,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 87, "id": "6abad578-adfe-4389-b4dc-865494102c6a", "metadata": {}, "outputs": [ @@ -2844,7 +2875,7 @@ "[ 0x8c, 0xa1, 0x89, 0xd, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0xf, 0xb0, 0x54, 0xbb, 0x16 ]" ] }, - "execution_count": 86, + "execution_count": 87, "metadata": {}, "output_type": "execute_result" } @@ -2894,7 +2925,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 88, "id": "763cab51-af5d-423d-b8cf-f6436db1280a", "metadata": {}, "outputs": [ @@ -2904,7 +2935,7 @@ "- x + 1" ] }, - "execution_count": 87, + "execution_count": 88, "metadata": {}, "output_type": "execute_result" } @@ -2934,7 +2965,7 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 89, "id": "3e8b7f54-ea20-44ce-a0d3-e9cb6d7ef54b", "metadata": {}, "outputs": [ @@ -2951,7 +2982,7 @@ "False" ] }, - "execution_count": 88, + "execution_count": 89, "metadata": {}, "output_type": "execute_result" } @@ -2985,7 +3016,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": 90, "id": "728c317c-4f71-4a23-bdad-2a0f8f43c60d", "metadata": {}, "outputs": [ @@ -2995,7 +3026,7 @@ "True" ] }, - "execution_count": 89, + "execution_count": 90, "metadata": {}, "output_type": "execute_result" } @@ -3017,7 +3048,7 @@ }, { "cell_type": "code", - "execution_count": 90, + "execution_count": 91, "id": "99fed470-0bbb-4975-a391-c6d755a9a635", "metadata": {}, "outputs": [ @@ -3027,7 +3058,7 @@ "30" ] }, - "execution_count": 90, + "execution_count": 91, "metadata": {}, "output_type": "execute_result" } @@ -3054,7 +3085,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 92, "id": "284175c0-2153-4d71-9488-c6a902abfde4", "metadata": {}, "outputs": [ @@ -3123,7 +3154,7 @@ }, { "cell_type": "code", - "execution_count": 92, + "execution_count": 93, "id": "e6829610-8540-4ff3-9011-fe29a5397ca8", "metadata": {}, "outputs": [ @@ -3214,7 +3245,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 94, "id": "8d4c4b43-e593-407a-92f8-86b653a09c8a", "metadata": {}, "outputs": [ @@ -3283,7 +3314,7 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 95, "id": "143fbfa9-37f0-46a5-88e8-77881d3d9092", "metadata": {}, "outputs": [ @@ -3293,7 +3324,7 @@ "x^256 + 1" ] }, - "execution_count": 94, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } @@ -3319,7 +3350,7 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 96, "id": "703e1f66-401d-40be-a0f9-d3f08ced2b14", "metadata": {}, "outputs": [ @@ -3329,7 +3360,7 @@ "-1" ] }, - "execution_count": 95, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } @@ -3350,7 +3381,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": 97, "id": "84d4ef9e-1484-48c8-b5a8-75a00f178c72", "metadata": {}, "outputs": [ @@ -3360,7 +3391,7 @@ "6 x^5 + 5 x^4 + 4 x^3 + 3 x^2 + 2 x + 1" ] }, - "execution_count": 96, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } @@ -3402,7 +3433,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 98, "id": "9b566550-f23c-4fbc-8b7c-63bbee8e0c72", "metadata": {}, "outputs": [ @@ -3441,7 +3472,7 @@ }, { "cell_type": "code", - "execution_count": 98, + "execution_count": 99, "id": "55c29e0c-e0c2-409a-af23-6590fd9cc545", "metadata": {}, "outputs": [ @@ -3451,7 +3482,7 @@ "(113, 91)" ] }, - "execution_count": 98, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } @@ -3473,7 +3504,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 100, "id": "f980a2d0-4413-4812-a73d-1bab72a744f3", "metadata": {}, "outputs": [ @@ -3483,7 +3514,7 @@ "True" ] }, - "execution_count": 99, + "execution_count": 100, "metadata": {}, "output_type": "execute_result" } @@ -3502,7 +3533,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 101, "id": "2a3d8e7d-71b3-4a93-b6cc-803f00432c56", "metadata": {}, "outputs": [ @@ -3512,7 +3543,7 @@ "129" ] }, - "execution_count": 100, + "execution_count": 101, "metadata": {}, "output_type": "execute_result" } @@ -3532,7 +3563,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 102, "id": "76d3b602-4903-4218-b34c-a39fbe392ad1", "metadata": {}, "outputs": [ @@ -3542,7 +3573,7 @@ "0" ] }, - "execution_count": 101, + "execution_count": 102, "metadata": {}, "output_type": "execute_result" } @@ -3561,7 +3592,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 103, "id": "bb93b5fc-99c9-4977-8299-9342b84e143d", "metadata": {}, "outputs": [], @@ -3581,7 +3612,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 104, "id": "41e18357-da42-4cd7-bd4e-1c3355f1c69d", "metadata": {}, "outputs": [], @@ -3606,7 +3637,7 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 105, "id": "af81d5f2-a524-4555-8e74-06a0c9a7ea08", "metadata": {}, "outputs": [ @@ -3617,7 +3648,7 @@ " 0x03449ef2ed30a8deb96e584a08c329adbf1be87ce40f1a0e7b4e86178682c41a9c)" ] }, - "execution_count": 104, + "execution_count": 105, "metadata": {}, "output_type": "execute_result" } diff --git a/kryptools/factor_dix.py b/kryptools/factor_dix.py index ac6f3a1..2a82d87 100644 --- a/kryptools/factor_dix.py +++ b/kryptools/factor_dix.py @@ -7,7 +7,7 @@ from .nt import legendre_symbol from .factor_qs import bytexor, byteset, bytetest -def is_smooth(n: int, factorbase: list, lfb: int) -> bytes or None: +def is_smooth(n: int, factorbase: list, lfb: int) -> bytes|None: """Try to factor n with respect to a given factorbase. Upon success a bytestring, whose bits are the exponents with repect to the factorbase mod 2, is returned. Otherwise None.""" @@ -26,7 +26,7 @@ def is_smooth(n: int, factorbase: list, lfb: int) -> bytes or None: return None # the number factors if at the end nothing is left return factors -def factor_dixon(n: int) -> list: +def factor_dixon(n: int) -> int|None: """Find factors of n using the method of Dixon.""" # first determine the bound B for the factorbase: Choosing B=p^(1/u) Canfield-Erdös-Pomerance gives us # the expected running time |B|^2 u^u = u^(u+2) p^(2/u)/log(n). There is no explicit expression for the optimum, hence diff --git a/kryptools/factor_ecm.py b/kryptools/factor_ecm.py index d013ee8..69c2be5 100644 --- a/kryptools/factor_ecm.py +++ b/kryptools/factor_ecm.py @@ -49,7 +49,7 @@ def mult(k, P, c2, p): # Crandall and Pomerance: Primes (doi=10.1007/0-387-28979-8) # Algorithm 7.4.4 (Inversionless ECM) -def _ecm_parameters(B1: int, B2: int|None = None, D: int|None = None, primes: tuple|None = None): +def _ecm_parameters(B1: int, B2: int|None = None, D: int|None = None, primes: tuple|None = None) -> tuple: "Precompute parameters for the ECM method." # Stage-one/two limits must be even @@ -87,7 +87,7 @@ def _ecm_parameters(B1: int, B2: int|None = None, D: int|None = None, primes: tu return D, stage_one, stage_two_deltas -def factor_ecm(n: int, B1: int|None = None, B2: int|None = None, curves: int = 150, ecm_parameters: tuple|None = None, verbose: int = 0): +def factor_ecm(n: int, B1: int|None = None, B2: int|None = None, curves: int = 150, ecm_parameters: tuple|None = None, verbose: int = 0) -> int|None: "Factors a number n using Lentsta's ECM method." if ecm_parameters: diff --git a/kryptools/factor_pm1.py b/kryptools/factor_pm1.py index 456e982..2a18921 100644 --- a/kryptools/factor_pm1.py +++ b/kryptools/factor_pm1.py @@ -5,7 +5,7 @@ from math import gcd, log, isqrt, floor, log10 from .primes import sieve_eratosthenes -def _pm1_parameters(B1: int, B2: int|None = None, primes: tuple = None): +def _pm1_parameters(B1: int, B2: int|None = None, primes: tuple = None) -> tuple: "Precompute parameters for the p-1 method." if not B2: B2 = 100 * B1 @@ -27,7 +27,7 @@ def _pm1_parameters(B1: int, B2: int|None = None, primes: tuple = None): return stage_one, stage_two_deltas -def factor_pm1(n: int, B1: int|None = None, B2: int|None = None, x: int = 2, pm1_parameters: tuple = None, verbose: int = 0): +def factor_pm1(n: int, B1: int|None = None, B2: int|None = None, x: int = 2, pm1_parameters: tuple = None, verbose: int = 0) -> int|None: "Factors a number n using Pollard's p-1 method." if pm1_parameters: B1, B2, stage_one, stage_two_deltas = pm1_parameters diff --git a/kryptools/factor_qs.py b/kryptools/factor_qs.py index 18607c2..f3c9b3b 100644 --- a/kryptools/factor_qs.py +++ b/kryptools/factor_qs.py @@ -24,7 +24,7 @@ def bytetest(a: bytes, i: int) -> bytes: return (a[i1] & 2**i0) != 0 -def factor_qs(n: int, verbose: int = 0) -> list: +def factor_qs(n: int, verbose: int = 0) -> int|None: """Find factors of n using the quadratic sieve.""" # first determine the bound B for the factorbase: Choosing B=p^(1/u) Canfield-Erdös-Pomerance gives us # the expected running time |B|^2 u^u = u^(u+2) p^(2/u)/log(n). There is no explicit expression for the optimum, hence diff --git a/kryptools/factor_rho.py b/kryptools/factor_rho.py new file mode 100644 index 0000000..f2bbf3c --- /dev/null +++ b/kryptools/factor_rho.py @@ -0,0 +1,58 @@ +""" +Integer factorization: Pollard's rho method +""" + +from math import gcd, isqrt, floor, log10 +from random import randint + + +def factor_rho(n: int, x: int = 2, maxinit: int = 10, maxiter: int = 1000, brent: bool = True, verbose: int = 0) -> int|None: + """Factor n using the Pollard's rho method with start value x.""" + if not maxiter: + maxiter = 10 * isqrt(n) # stop iterating and try a different start value + + def f(x: int) -> int: + return (pow(x, 2, n) + 2) % n + + if verbose: + print(f"Factoring (Pollard rho, maxinit={maxinit}, maxiter={maxiter}): {n} ({floor(log10(n)) + 1} digits)") + + if verbose > 1: + print("Working ", end= "") + for _ in range(maxinit): + if verbose > 1: + print("X", end= "") + if not brent: # Floyd's cycle detection algorithm + i = 1 + x = f(x) # x_1=f(x_0) + y = f(x) # y_1=f(f(x_0)) + while gcd(x - y, n) == 1 and i < maxiter: + if verbose > 1 and i % 100 == 0: + print(".", end= "") + i += 1 + x = f(x) # f(x_j) + y = f(y) + y = f(y) # f(f(y_j)) + else: # Brent's cycle detection algorithm + i = 1 # search for a cycle length k < 2^i + k = 1 # cycle length + y = f(x) # f(x_0) + while gcd(x - y, n) == 1 and i < maxiter: + if verbose > 1 and (i+k) % 100 == 0: + print(".", end= "") + if i == k: # start a new power of 2 + x = y + i *= 2 + k = 0 + y = f(y) + k += 1 + # we found a collission (or hit max) + tmp = gcd(x - y, n) + if 1 < tmp < n: + if verbose > 1: + print("\nFactor found.") + return tmp + x = randint(1, n - 1) # new x_0 + if verbose > 1: + print("") +