Recommended value for the variable for Linear Congruential Generator - cryptography

I am trying to generate 48 pseudoraandom number by using linear congruential generator. I am only using alphabet from a to z for my plaintext which will be for mapping 0 to 25.
I am using the algorithm for the linear congruential generator below
Xn+1 = (aXn + c) mod m
where Xn is the sequence of pseudorandom values, and
• Modulus: m, 0 < m
• Multiplier: a, 0 < a < m
• Increment: c, 0 ≤ c < m
• Seed: X0, 0 ≤ X0 < m
I am using the algorithm for the linear congruential generator below
Xn+1 = (aXn + c) mod m
X2 = (7(11) + 11) mod 18 = 16
X3 = (7(16) + 11) mod 18 = 15
X4 = (7(15) + 11) mod 18 = 8
What would be the recommended value for the seed, increment, multiplier and modulus. Keystream need to be from 0 to 26 as I need to map the number to the corresponding letter.
I want unique and less repetition.
Please kindly advise.
Thank you

Related

Inequality constraints of convex relaxation with McCormick envelope

I have a nonconvex optimization problem for which I am calculating a lower bound using the McCormick envelope. Each bilinear term is replaced with an auxiliary variable which has the following constraints defined:
w_{ij} >= x_i^L * x_j + x_i * x_j^L - x_i^L * x_j^L
w_{ij} >= x_i^U * x_j + x_i * x_j^U - x_i^U * x_j^U
w_{ij} <= x_i^U * x_j + x_i * x_j^L - x_i^U * x_j^L
w_{ij} <= x_i^L * x_j + x_i * x_j^U - x_i^L * x_j^U
where
x_U <= x <= x_L
I am given a function taking in several arguments:
def convex_bounds(n,m,c,H,Q,A,b,lb,ub):
# n is the number of optimization variables
# m is the number of eq constraints
# H = positive, semidefinite matrix from objetcive function (n x n)
# Q is (mxn) x n
# A is m x n
# b is RHS of non linear eq constraints (m x 1)
# c,lb,ub are vectors size (n x 1)
......................................
# Create matrix B & b_ineq for inequality constraints
# where B*x <= b_ineq
B = np.eye(3)
b_ineq = np.array((10,10,10))
## these values would work in a scenario with no bilinear terms
My problem is that I don't know how to specify the inequality constraints matrix B and vector b_ineq. For this particular exercise my variables are x1, x2 and x3 with bounds 0 (x_L) and 10 (x_U). My bilinear terms are x_12 and x_23 (which will lead to auxiliary variables w_12 and w_23). How can I specify the known bounds (0 and 10) for x1,x2 and x3 and the calculated ones (as in the theory pasted above) in B and b_ineq?
I don't actually know how to proceed with this.

McDonald's sells Chicken McNuggets only in packages of 6, 9 or 20. What is the largest number of McNuggets that cannot be bought exactly?

Question is from MIT OCW Course Number 6.00, As Taught in Fall 2008:
Here is a theorem below:
If it is possible to buy x, x+1,…, x+5 sets of McNuggets, for some x, then it is possible to buy any number of McNuggets >= x, given that McNuggets come in 6, 9 and 20 packs.
Using the theorem above, write an exhaustive search to find the largest number of McNuggets that cannot be bought in exact quantity, i.e. write an iterative program that finds the largest number of McNuggets that cannot be bought in exact quantity. Format of search should follow the outline below:
Hypothesise possible instances of numbers of McNuggets that cannot be purchased exactly, starting with 1.
For each possible instance, called n, test if there exists non-negative integers a, b, and c, such that 6a+9b+20c = n.
If n McNuggets cannot be bought in exact quantity, save n.
When have found 6 consecutive values of n where 6a+9b+20c = n, the last answer that was saved (not the last value of n that had a solution) is the correct answer, since from the theorem, any amount larger than this saved value of n can also be bought in exact quantity
The error is in line 14 of the code below and this is the error:
elif(6*a + 9*b + 20*c < n_saved or 6*a + 9*b + 20*c > n_saved):
^
SyntaxError: invalid syntax
Here is the code:
def largest_not(a, b, c, n, n_saved):
a = 0
b = 0
c = 0
n = 0
n_saved = 0
for a in range (10):
for b in range (10):
for c in range (10):
for n in range (10):
for n_saved in range (10):
if (6*a + 9*b + 20*c == n):
print (n)
elif(6*a + 9*b + 20*c < n_saved or 6*a + 9*b + 20*c > n_saved):
print (n_saved)
if (n - n_saved > 5):
print "Largest number of McNuggets that cannot be bought in exact quantity: " + "<" + n_saved + ">"
else :
print "Have not found the largest number of McNuggets that cannot be bought in exact quantity."
a=6
b=9
c=20
largest_not(a, b, c, n, n_saved)
Here is a way to solve this:
def check(n):
"""check if n nuggets can be bought exactly with packs of 6, 9 and 20"""
for a in range(20):
for b in range(20):
for c in range(20):
if (6*a+9*b+20*c==n):
return True
return False
### look for a serie of 6 successives n
### to apply the theorem
nb_i = 0 ## number of successive good n found
sv_i = 0 ## last good n found
bad_i = 0 ## last bad n found
for i in range(1, 100):
if (check(i)):
nb_i+=1
sv_i=i
else:
bad_i=i
nb_i=0
sv_i=0
if nb_i==6:
print "Solved: the biggest number of nuggets you cannot buy exactly is: " + bad_i
break
result is:
Solved: the biggest number of nuggets you cannot buy exactly is: 43
Your elif needs to be inline with your if. However, your algorithm will also not work.
I got 43 as the largest number not possible: I'm sure this solution could be optimised.
def check(n):
# a in [0..max times 6 goes into n]
for a in range(0, n // 6 + 1):
# b in [0..max times 9 goes into remainder]
for b in range((n - 6*a) // 9 + 1):
# c in [0..max times 20 goes into remainder]
for c in range(0, (n - 6*a - 9*b) // 20 + 1):
if 6*a + 9*b + 20*c == n:
return (a, b, c)
return None
def largest_not():
n = 1
last_n_not_possible = 1
while (n - last_n_not_possible <= 6):
can_purchase = check(n)
if can_purchase is not None:
print("Can purchase ", n, ' = ', can_purchase[0],'*6 + ', can_purchase[1],'*9 + ', can_purchase[2], '*20', sep='')
else:
print("Can't purchase", n)
last_n_not_possible = n
n = n + 1
return last_n_not_possible
print("Answer: ", largest_not())

How the time complexity of gcd is Θ(logn)?

I was solving a time-complexity question on Interview Bit as given in the below image.
The answer given is Θ(theta)(logn) and I am not able to grasp how the logn term arrive here in the time complexity of this program.
Can someone please explain how the answer is theta of logn?
Theorem given any x, gcd(n, m) where n < fib(x) is recursive called equal or less than x times.
Note: fib(x) is fibonacci(x), where fib(x) = fib(x-1) + fib(x-2)
Prove
Basis
every n <= fib(1), gcd(n, m) is gcd(1,m) only recursive once
Inductive step
assume the theorem is hold for every number less than x, which means:
calls(gcd(n, m)) <= x for every n <= fib(x)
consider n where n <= fib(x+1)
if m > fib(x)
calls(gcd(n, m))
= calls(gcd(m, (n-m))) + 1
= calls(gcd(n-m, m%(n-m))) + 2 because n - m <= fib(x-1)
<= x - 1 + 2
= x + 1
if m <= fib(x)
calls(gcd(n, m))
= calls(gcd(m, (n%m))) + 1 because m <= fib(x)
<= x + 1
So the theorem also holds for x + 1, as mathematical induction, the theorem holds for every x.
Conclusion
gcd(n,m) is Θ(reverse fib) which is Θ(logn)
This algorithm generates a decreasing sequence of integer (m, n) pairs. We can try to prove that such sequence decays fast enough.
Let's say we start with m_1 and n_1, with m_1 < n_1.
At each step we take n_1 % m_1, which is < m_1, and repeat recursively on the pair m_2 = n_1 % m_1 and n_2 = m_1.
Now, let's say n_1 % m_1 = m_1 - p for some p where 0 < p < m_1.
We have max(m_2, n_2) = m_1 - p.
Let's take another step (m_2, n_2) -> (m_3, n_3), we can easily see that max(m_3, n_3) < p, but clearly it is also true that max(m_3, n_3) < m_1 - p as the sequence is strictly decreasing.
So we can write max(m_3, n_3) < min(m_1 - p, p), where min(m_1 - p, p) = m_1 / 2. This result expresses the fact that the sequence decreases geometrically, therefore the algorithm has to terminate in at most log_2(m_1) steps.

Rewrite Resurrence Function to the Idea of Dynamic Programming

Can someone help me?
Rewrite the pseudo-code of Count(n) using the idea of Dynamic Programming. And determine the Time Complexity.
Test(n)
If n=1 return 1
Tn=0
For k=1 to n-1
Tn = Tn + Test(k) * Test(n-k)
Return Tn
Add Memoization to get a DP solution from a recursion one:
Python Code:
d = {}
def test(n):
if n == 1:
return 1
if d.get(n) is not None:
return d[n]
ans = 0
for k in range(1, n):
ans += test(k) * test(n - k)
d[n] = ans
return ans
You can check(It's Catalan numbers indeed, learn more about it in OEIS):
for i in range(1, 10):
print str(i) + ' ' + str(test(i))
Output:
1 1
2 1
3 2
4 5
5 14
6 42
7 132
8 429
9 1430
Time Complexity is O(n^2). Because calculate a state is O(n)(for k from 1 to n - 1), and we need calculate n state in total to get test(n).
In fact, we can achieve a O(n) solution since it's Catalan numbers...

RSA crypto - How to choose p, q and e correctly?

Recently I've found some troubles after having those e, n and d value.
I had this set
(p=3, q=11)
so n = 33 and Euler(n)=20.
I choose
e=3, calculated d = 7.
For message x=49, the signature would be
s = x^d mod n = 49^7 mod 33 = 25.
Someone would verify it like ver(s) = s^e mod n = 16 != x (Fake?)
What 've I done wrong.
As the previous answerer rather (too) succinctly put - naive RSA will only recover the exact original value when it is less than the modulus (n). Your message x = 49 is larger than your modulus (n = 33), so you will not be able to decrypt the ciphertext back to the original value of x.
If you were to try again with a smaller message, e.g. x = 25 things will work fine:
Encrypt:
C = xe mod n
= 253 mod 33
= 16
Decrypt:
x = Cd mod n
= 167 mod 33
= 25
This is not wrong:
49=16 mod 33