RSA_numbers_factored.pyFor type hinting:
IntList2 = NewType('IntList2', List[Tuple[int, int]])
IntList4 = NewType('IntList4', List[Tuple[int, int, int, int]])
RSA_factored_2 = NewType('RSA_factored_2', List[Tuple[int, int, int, int, Dict[int, int], Dict[int, int]]])
RSA_factored = NewType('RSA_factored', IntList4)
RSA_unfactored = NewType('RSA_unfactored', IntList2)
RSA_number = NewType('RSA_number', Union[RSA_factored_2, RSA_factored, RSA_unfactored])
RSA number n = RSA-l:
RSA_unfactored: [l,n]
RSA_factored: [l,n,p,q] (n = p * q)
RSA_factored_2: [l,n,p,q,pm1,qm1] (n = p * q, Xm1 factorization dict of X-1)
v1.11
v1.10
| New makefile doc | pylint | validate targets |
v1.9
v1.8
v1.7
v1.6
v1.5
v1.4
v1.3
v1.2
v1.1
v1.0
v0.2
v0.1
Global variable descriptions:
SECTION0SECTION0()
int helper functions
bitsbits(n: int) → int
returns bit-length of n
Example:
Of biggest RSA number.
>>> bits(rsa[-1][1])
2048
>>>
digitsdigits(n: int) → int
returns number of decimal digits of n
Example:
Of biggest RSA number.
>>> digits(rsa[-1][1])
617
>>>
SECTION1SECTION1()
Robert Chapman 2010 code from https://math.stackexchange.com/a/5883/1084297 with small changes:
modsmods(a: int, n: int) → int
returns “signed” a (mod n), in range -n//2..n//2
powmodspowmods(a: int, r: int, n: int) → int
returns “signed” a**r (mod n), in range -n//2..n//2
quosquos(a: int, n: int) → int
returns equivalent of “a//n” for signed mod
gremgrem(w: Tuple[int, int], z: Tuple[int, int]) → Tuple[int, int]
returns remainder in Gaussian integers when dividing w by z
ggcdggcd(w: Tuple[int, int], z: Tuple[int, int]) → Tuple[int, int]
returns greatest common divisor for gaussian integers
Example:
Demonstrates how ggcd() can be used to determine sum of squares.
>>> powmods(13,2,17)
-1
>>> ggcd((17,0),(13,1))
(4, -1)
>>> 4**2+(-1)**2
17
>>>
root4m1root4m1(p: int) → int
returns sqrt(-1) (mod p)
sq2sq2(p: int) → Tuple[int, int]
Args:
p: asserts if p is no prime =1 (mod 4).Returns:
Example:
Determine unique sum of two squares for prime 233.
>>> sq2(233)
(13, 8)
>>>
SECTION2SECTION2()
Functions dealing with representations of int as sum of two squares
sq2dsq2d(p: int) → Tuple[int, int]
Args:
p: asserts if not odd prime.Returns:
Example:
Determine unique difference of two squares for prime 11 (= 6**2 - 5**2).
>>> sq2d(11)
(6, 5)
>>>
square_sum_prodsquare_sum_prod(n: Union[int, RSA_number]) → Union[IntList2, IntList4]
Args:
n: int or RSA_number.Returns:
Example:
For prime 233 and composite number RSA-59.
>>> square_sum_prod(233)
[13, 8]
>>>
>>> r = rsa[0]
>>> s = square_sum_prod(r)
>>> (s[0]**2 + s[1]**2) * (s[2]**2 + s[3]**2) == r[1]
True
>>>
square_sums_square_sums_(s: List[int]) → Type[IntList2]
Args:
s: List of int returned by square_sum_prod(n).Returns:
Example:
For composite number RSA-59.
>>> r = rsa[0]
>>> s = square_sum_prod(r)
>>> square_sums_(s)
[[93861205413769670113229603198, 250662312444502854557140314865], [264836754409721537369435955610, 38768728061109707828243001823]]
>>> for a,b in square_sums_(s):
... a**2 + b**2 == r[1]
...
True
True
>>>
square_sumssquare_sums(
L: List[int],
revt: bool = False,
revl: bool = False,
uniq: bool = False
) → Type[IntList2]
Args:
L: List of int.revt: sorting direction for tuples.revl: sorting direction for list.uniq: eliminate duplicates if True.Returns:
Example:
For list corresponding to number 5*5*13 (5 = 2**2 + 1**2, 13 = 3**2 + 2**2).
>>> s = [2, 1, 2, 1, 3, 2]
>>> square_sums(s)
[[1, 18], [6, 17], [10, 15], [10, 15]]
>>> square_sums(s, revt=True, revl=True)
[[18, 1], [17, 6], [15, 10], [15, 10]]
>>> square_sums(s, uniq=True)
[[1, 18], [6, 17], [10, 15]]
>>> for a,b in square_sums(s, uniq=True):
... assert a**2 + b**2 == 5*5*13
...
>>>
sqtstsqtst(L: List[int], k: int, dbg: int = 0) → None
Note:
sqtst() verifies that 2**(k-1) == unique #sum_of_squares by many asserts for all k-element subsets of l
Args:
L: list of distinct primes =1 (mod 4)k: size of subsetsdbg: 0=without debug output, 1-3 with more and moreExample:
>>> smp1m4[0:3]
[5, 13, 17]
>>> sqtst(smp1m4[0:3], 2, dbg=3)
(0, 1)
[2, 1, 3, 2]
[[1, 8], [4, 7]]
(0, 2)
[2, 1, 4, 1]
[[2, 9], [6, 7]]
(1, 2)
[3, 2, 4, 1]
[[5, 14], [10, 11]]
>>>
>>> sqtst(smp1m4[0:20], 7)
>>>
to_squares_sumto_squares_sum(sqrtm1: int, p: int) → Type[IntList2]
much faster in case cypari2 is available
Args:
sqrtm1: sqrt(-1) (mod p).p: prime p =1 (mod 4).Returns:
Example:
>>> to_squares_sum(11, 61)
(6, -5)
>>>
to_sqrtm1to_sqrtm1(xy: Type[IntList2], n: int) → int
Args:
xy: xy[0]2 + xy[1]2 == n.n: number =1 (mod 4).Returns:
Example:
>>> to_sqrtm1((14,5),221)
47
>>> 47**2%221==221-1
True
>>>
SECTION3SECTION3()
Functions working on “rsa” list
idxidx(rsa_: List[RSA_number], L: int) → int
Args:
rsa_: list of RSA numbersL: bit-length or decimal-digit-length of RSA numberReturns:
has_factorshas_factors(
r: Type[RSA_number],
mod4: Union[NoneType, int, Tuple[int, int]] = None
) → bool
Args:
r: an RSA numbermod4: optional restriction (remainder mod 4 for number or its both prime factors)Returns:
has_factors_2has_factors_2(
r: Type[RSA_number],
mod4: Union[NoneType, int, Tuple[int, int]] = None
) → bool
Args:
r: an RSA numbermod4: optional restriction (remainder mod 4 for number or its both prime factors)Returns:
Example:
For RSA-100
>>> r=rsa[2]
>>> has_factors_2(r)
True
>>> l,n,p,q,pm1,qm1 = r
>>> l
100
>>>
>>> q
40094690950920881030683735292761468389214899724061
>>> qm1
{2: 2, 5: 1, 41: 1, 2119363: 1, 602799725049211: 1, 38273186726790856290328531: 1}
>>>
without_factorswithout_factors(r: Type[RSA_number], mod4: Optional[int] = None) → bool
Args:
r: an RSA numbermod4: optional restriction (remainder mod 4 for number)Returns:
SECTION4SECTION4()
primeprod_f functions, passing p and q instead n=p*q much faster than sympy.f
primeprod_totientprimeprod_totient(p: int, q: int) → int
Args:
p,q: odd primes.Returns:
primeprod_reduced_totientprimeprod_reduced_totient(p: int, q: int) → int
Args:
p,q: odd primes.Returns:
SECTION5SECTION5()
Functions on factorization dictionaries.
[as returned by sympy.factorint() (in rsa[x][4] for p-1 and rsa[x][5] for q-1) ]
dict_intdict_int(d: Dict[int, int]) → int
Args:
d: factorization dictionary.Returns:
dict_totientdict_totient(d: Dict[int, int]) → int
Args:
d: factorization dictionary.Returns:
dictprod_totientdictprod_totient(d1: Dict[int, int], d2: Dict[int, int]) → int
Args:
d1,d2: factorization dictionaries.Returns:
dictprod_reduced_totientdictprod_reduced_totient(d1: Dict[int, int], d2: Dict[int, int]) → int
Args:
d1,d2: factorization dictionaries.Returns:
SECTION6SECTION6()
Validation functions, rsa list
validate_squaresvalidate_squares() → None
avoid R0915 pylint too-many-statements warning for validate()
validatevalidate(rsa_, doprint: bool = False) → None
Assert many identities to assure data consistency and generate demo output for non RSA-class functionality. Gets executed by RSA().validate().
Args:
rsa_: list of rsa entries.RSARSA convenience class.
__init____init__()
avoid W0201 pylint warning
factoredfactored(mod4: Union[NoneType, int, Tuple[int, int]] = None) → Type[IntList4]
Args:
mod4: optional restriction (remainder mod 4 for number or its both prime factors).Returns:
Example:
>>> len(rsa)
56
>>> len(RSA.factored())
25
>>> len(RSA.factored(mod4=3))
13
>>> len(RSA.factored(mod4=1))
12
>>> len(RSA.factored(mod4=(1,1)))
5
>>> len(RSA.factored(mod4=(3,3)))
7
>>>
factored_2factored_2(
mod4: Union[NoneType, int, Tuple[int, int]] = None
) → List[RSA_number]
Args:
mod4: optional restriction (remainder mod 4 for number or its both prime factors).Returns:
getget(x: int) → Type[RSA_number]
Args:
x: RSA number length.Returns:
get_get_(x: Union[int, RSA_number]) → Type[RSA_number]
Args:
x: RSA number length or RSA_number.Returns:
indexindex(x: int) → int
Args:
x: bit-length or decimal-digit-length of RSA number.Returns:
reduced_totientreduced_totient(x: Union[int, RSA_number]) → int
Args:
x: RSA number length or RSA_number.Returns:
reduced_totient_2reduced_totient_2(x: Union[int, RSA_number]) → int
Args:
x: RSA number length or RSA_number.Returns:
sort_factorssort_factors() → None
make p the bigger of factors by switching if needed
square_diffssquare_diffs(x: Union[int, RSA_number]) → Type[IntList2]
Args:
x: RSA number length or RSA_number.Returns:
Example:
>>> t = RSA.get(250)
>>> n = t[1]
>>> [a,b],[c,d] = RSA.square_diffs(t)
>>> (a**2 - b**2) == n and (c**2 - d**2) == n
True
>>>
square_sumssquare_sums(x: Union[int, RSA_number]) → Type[IntList2]
Args:
x: RSA number length or RSA_number.Returns:
Example:
>>> t = RSA.get(129)
>>> n = t[1]
>>> [a,b],[c,d] = RSA.square_sums(t)
>>> (a**2 + b**2) == n and (c**2 + d**2) == n
True
>>> len({a,b,c,d})
4
>>>
square_sums_4square_sums_4(x: Union[int, RSA_number]) → Tuple[int, int, int, int]
Args:
x: RSA_number length or RSA_numberReturns:
Example:
>>> p,q = 13,29
>>> n = p*q
>>> sq4 = RSA.square_sums_4([0,0,p,q])
>>> sq4
(15, 4, 6, 10)
>>> sum([x**2 for x in sq4]) == n
True
>>> sum([x**2 for x in RSA.square_sums_4(129)]) == RSA.get(129)[1]
True
>>> RSA.square_sums_4(59)
(179348979911745603741332779404, 85487774497975933628103176206, 105946792191696573364448656521, 144715520252806281192691658344)
>>>
svgsvg(n: Union[int, RSA_number], scale: int) → str
Generate prime factors svg.
to_sqrtm1to_sqrtm1(xy: Type[IntList2], p: int) → int
shortcut
to_squares_sumto_squares_sum(sqrtm1: int, p: int) → Type[IntList2]
shortcut
totienttotient(x: Union[int, RSA_number]) → int
Args:
x: RSA number length or RSA_number.Returns:
totient_2totient_2(x: Union[int, RSA_number]) → int
Args:
x: RSA number length or RSA_number.Returns:
unfactoredunfactored(mod4: Optional[int] = None) → Type[IntList2]
Args:
mod4: optional restriction (remainder mod 4 for number).Returns:
Example:
>>> len(rsa)
56
>>> len(RSA.factored())
25
>>> len(RSA.unfactored())
31
>>> len(RSA.unfactored(1))
17
>>> len(RSA.unfactored(3))
14
>>>
validatevalidate(doprint: bool = False) → None
Assert many identities to assure data consistency and optionally generate demo output (executed if __name__ == “__main__”).
Example:
$ python RSA_numbers_factored.py
with p-1 and q-1 factorizations (n=p*q): 25
59 digits, 79 digits,100 digits,110 digits,120 digits,129 digits,130 digits,
140 digits,150 digits,155 digits,160 digits,170 digits,576 bits ,180 digits,
190 digits,640 bits ,200 digits,210 digits,704 bits ,220 digits,230 digits,
232 digits,768 bits ,240 digits,250 digits,
without (p-1) and (q-1) factorizations, but p and q: 0
have not been factored sofar: 31
260 digits,270 digits,896 bits ,280 digits,290 digits,300 digits,309 digits,
1024 bits ,310 digits,320 digits,330 digits,340 digits,350 digits,360 digits,
370 digits,380 digits,390 digits,400 digits,410 digits,420 digits,430 digits,
440 digits,450 digits,460 digits,1536 bits ,470 digits,480 digits,490 digits,
500 digits,617 digits,2048 bits (=617 digits)
$
This file was automatically generated via lazydocs.