Skip to content

Commit 20882b8

Browse files
committed
Added diy RNG_div_all_coprime.py which takes a number and divides it by
all numbers below it that are coprime, then adds it to the keystream Added correlogram function to autocorrelation.py to iterate autocorrelation calculation for every time lag up to a limit Created div_digits.py which is a function to get decimal digits of a division of two numbers Renamed keytest1.py to freq_analysis.py
1 parent e67de30 commit 20882b8

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

RNG_div_all_Coprime.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from div_digits import divide
2+
from math import gcd
3+
import numpy as np
4+
5+
def DivAllCoPrimeRNG(limit,dp=100000):
6+
data = []
7+
v2 = -1
8+
for i in range(limit):
9+
if gcd(i+1,limit)==1:
10+
a = divide(limit,i+1,dp)
11+
v1 = np.array(a)
12+
if isinstance(v2,int):
13+
v2 = v1
14+
else:
15+
v2 = np.add(v1,v2)
16+
data = v2.tolist()
17+
print(len(data))
18+
return data
19+
20+
from autocorrelation import correlogram
21+
print(correlogram(DivAllCoPrimeRNG(2011,100000),20))
22+

autocorrelation.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#!/usr/bin/env python3
2-
import numpy as np
3-
42
def autocovariance(data,k=1):
53
n = len(data)
64
mean = sum(data)/len(data)
@@ -14,5 +12,12 @@ def autocorrelation(data,k=1):
1412
acf = autocovariance(data,k)/autocovariance(data,0)
1513
return acf
1614

17-
data = [23.4,24.1,44.4,25.5,29.2,37.1,45.0,61.8,56.7,62.3,30.9,69.6,11.0,56.6,83.4,33.9,75.5,87.3,55.4,95.2]
18-
print(autocorrelation(data,2))
15+
def correlogram(data,maxk=1000):
16+
correlogram = {}
17+
for k in range(maxk+1):
18+
correlogram[k] = autocorrelation(data,k)
19+
return correlogram
20+
21+
22+
#data = [23.4,24.1,44.4,25.5,29.2,37.1,45.0,61.8,56.7,62.3,30.9,69.6,11.0,56.6,83.4,33.9,75.5,87.3,55.4,95.2]
23+
#print(autocorrelation(data,2))

div_digits.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from mpmath import mp
2+
3+
def divide(num1,num2,dp=100000):
4+
f = num2//(10*num1) + 3
5+
mp.dps = dp+f
6+
ans = mp.fdiv(num1,num2)
7+
ans_str = str(ans)
8+
ans_lst = []
9+
index = ans_str.find(".")
10+
for char in ans_str:
11+
if char == ".":
12+
continue
13+
ans_lst.append(int(char))
14+
ans_lst = ans_lst[index:]
15+
while len(ans_lst) <= dp:
16+
ans_lst.append(0)
17+
return ans_lst[:dp]
18+

encryption1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ def encrypt(message):
3636
encrypted = "".join(encrypted_lst_char)
3737
return encrypted
3838

39-
print(encrypt("Secret Message"))
39+
if __name__ == "__main__":
40+
print(encrypt("Secret Message"))

keytest1.py renamed to freq_analysis.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from encryption1 import generatekey
22

3-
key = generatekey(100000)
4-
distribution={}
5-
for i in key:
6-
distribution[i] = distribution.get(i,0) + 1
7-
print(distribution)
3+
def freq_analysis(key):
4+
distribution={}
5+
for i in key:
6+
distribution[i] = distribution.get(i,0) + 1
7+
return distribution
88

9+
print(freq_analysis(generatekey(100000)))
910
#Frequency analysis of pi digits
1011
"""
1112
from pi import pi

0 commit comments

Comments
 (0)