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