Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update lcg: python3 integer division #6

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions sudo_root/crypto/lcg.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
"""Module to break Linear Congruential Generator"""


class Lcg(object):

"""Create LCG with a state to generate some random value,
or without state to break LCG with 3 consecutive known values.

Exemple:

from sudo_root.crypto import lcg

l = lcg.Lcg(multiplier, addend, mod, bits_hidden)
state = l.get_state(r0, r1, r2) # this are 3 consecutive output
l.next() give us the expected output

>>> from sudo_root.crypto import lcg
>>> l = lcg.Lcg(multiplier, addend, mod, bits_hidden)
>>> state = l.get_state(r0, r1, r2) # this are 3 consecutive output
>>> l.next() give us the expected output
"""

def __init__(self, multiplier, addend, mod, bits_hidden, state=None):
self.a = multiplier
self.b = addend
self.mod = mod
self.hidder = 1 << bits_hidden
self.state = state

def get_state(self, r0, r1, r2):
"break the LCG with 3 consecutive output."
t = self.hidder * r1 - self.a * self.hidder * r0 - self.b + self.hidder - 1
t = self.hidder * r1 - self.a * self.hidder * \
r0 - self.b + self.hidder - 1
t %= self.mod
end = (self.hidder * self.a - 1 - t) / self.mod
for k in range(1,end):

end = (self.hidder * self.a - 1 - t) // self.mod
for k in range(1, end):
h = t + self.mod * k
if (h % self.a) < self.hidder:
state = h / self.a + self.hidder * r0
if ((state * self.a + self.b) % self.mod * self.a + self.b)% self.mod / self.hidder == r2:
state = h // self.a + self.hidder * r0
if ((state * self.a + self.b) % self.mod * self.a + self.b) \
% self.mod // self.hidder == r2:
self.state = state
return self.state
return 0
return self.state

def next(self):
"The next value of the LCG."
print('state {}'.format(self.state))
self.state = (self.a * self.state + self.b) % self.mod
return self.state / self.hidder

return self.state // self.hidder