Skip to content

Commit 5011fd9

Browse files
committed
add factoring wrapper script for gmp-ecm and isprime script
1 parent a3e3cc6 commit 5011fd9

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

ecmfactor

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/python3
2+
#
3+
# SPDX-License-Identifier: 0BSD
4+
# Part of badkeys: https://badkeys.info/
5+
#
6+
# Simple wrapper for factoring a hex input number with gmp-ecm (Elliptic
7+
# Curve Method) and default settings.
8+
# Needs gmp-ecm: https://gitlab.inria.fr/zimmerma/ecm
9+
#
10+
# Usage example (has non-trivial, 64-bit prime factor):
11+
# ./ecmfactor 95050b185a7e9147cb60a84729b239e0deac19b4ba4137ff87f4ccae8c13b6ab
12+
13+
import argparse
14+
import subprocess
15+
16+
ap = argparse.ArgumentParser()
17+
ap.add_argument("input", nargs="+", help="Value to be factored (hex number)")
18+
args = ap.parse_args()
19+
20+
for nhex in args.input:
21+
n = str(int(nhex, 16))
22+
23+
proc = subprocess.Popen(["ecm", "-q", "1000000"], text=True,
24+
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
25+
26+
out = proc.communicate(input=str(n))[0].strip().split(" ")
27+
if len(out) == 1:
28+
print(f"WARNING: Could not factor {nhex}")
29+
continue
30+
factor, cofactor = out
31+
print(factor)
32+
factor = int(factor)
33+
cofactor = int(cofactor)
34+
35+
print(f"Factor: {factor:02x}")
36+
print(f"Cofactor: {cofactor:02x}")

isprime

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/python3
2+
#
3+
# SPDX-License-Identifier: 0BSD
4+
# Part of badkeys: https://badkeys.info/
5+
6+
import argparse
7+
8+
import gmpy2
9+
10+
ap = argparse.ArgumentParser()
11+
ap.add_argument("maybeprime", nargs="+", help="Input value (hex)")
12+
args = ap.parse_args()
13+
14+
for maybeprime in args.maybeprime:
15+
if gmpy2.is_prime(int(maybeprime, 16)):
16+
print(f"Prime: {maybeprime}")
17+
else:
18+
print(f"Not prime: {maybeprime}")

0 commit comments

Comments
 (0)