Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit fa860a5

Browse files
committed
Added Algorithms to solve DLP
1 parent dd28d42 commit fa860a5

File tree

43 files changed

+1257
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1257
-10
lines changed
370 Bytes
Loading
299 Bytes
Loading
354 Bytes
Loading
1.21 KB
Loading
668 Bytes
Loading
391 Bytes
Loading
558 Bytes
Loading
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Baby Step Giant Step Algorithm
2+
3+
**Prerequisites**:
4+
1. [Cyclic Groups](https://github.com/ashutosh1206/Crypton/blob/master/Discrete-Logarithm-Problem/README.md#cyclic-groups)
5+
2. [Discrete Logarithm Problem](https://github.com/ashutosh1206/Crypton/blob/master/Discrete-Logarithm-Problem/README.md)
6+
7+
A method to reduce the time complexity of solving DLP is to use **Baby Step Giant Step Algorithm**. While brute forcing DLP takes polynomial time of the order ![picture](Pictures/2.gif), Baby Step Giant Step Algorithm can compute the value of `x` in ![picture](Pictures/1.gif) polynomial time complexity. Here, `n` is the order of the group.
8+
9+
This algorithm is a tradeoff between time and space complexity, as we will see when we discuss the algorithm.
10+
11+
## Algorithm
12+
`x` can be expressed as **x = i*m + j**, where ![picture](Pictures/3.gif) and `0 <= i,j < m`.
13+
14+
Hence, we have:
15+
![picture](Pictures/4.gif)
16+
![picture](Pictures/5.gif)
17+
18+
We can now use the above property for solving DLP as follows:
19+
1. Iterate `j` in its range and store all values of ![picture](Pictures/6.gif) with corresponding values of `j` in a lookup table.
20+
2. Run through each possible iteration of `i` and check if ![picture](Pictures/7.gif) exists in the table (ie. check if ![picture](Pictures/7.gif) == ![picture](Pictures/6.gif)).
21+
+ If it does then return **i*m + j** as the value of `x`
22+
+ Else, continue
23+
24+
## Shortcomings
25+
Although the algorithm is more efficient as compared to plain brute-forcing, other algorithms of the same time complexity (Pollard's rho) are used for solving DLPs because of the fact that storing the look up table requires quite a lot of space.
26+
27+
## Implementation
28+
I wrote an implementation of the above algorithm in python/sage:
29+
30+
```python
31+
from sage.all import *
32+
33+
def bsgs(g, y, p):
34+
mod_size = len(bin(p-1)[2:])
35+
36+
print "[+] Using BSGS algorithm to solve DLP"
37+
print "[+] Modulus size: " + str(mod_size) + ". Warning! BSGS not space efficient\n"
38+
39+
m = ceil(sqrt(p-1))
40+
# Baby Step
41+
lookup_table = {pow(g, j, p): j for j in range(m)}
42+
# Giant Step pre-computation
43+
c = pow(g, m*(p-2), p)
44+
# Giant Steps
45+
for i in range(m):
46+
temp = (y*pow(c, i, p)) % p
47+
if temp in lookup_table:
48+
# x found
49+
return i*m + lookup_table[temp]
50+
return None
51+
```
52+
You can check out the complete code here: [bsgs.py](bsgs.py)
53+
54+
## Resources & References
55+
1. [Rahul Sridhar- Survey of Discrete Log Algorithms](https://fortenf.org/e/crypto/2017/12/03/survey-of-discrete-log-algos.html)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from sage.all import *
2+
3+
def bsgs(g, y, p):
4+
"""
5+
Reference:
6+
7+
To solve DLP: y = g^x % p and get the value of x.
8+
We use the property that x = i*m + j, where m = ceil(sqrt(n))
9+
10+
:parameters:
11+
g : int/long
12+
Generator of the group
13+
y : int/long
14+
Result of g**x % p
15+
p : int/long
16+
Group over which DLP is generated. Commonly p is a prime number
17+
18+
:variables:
19+
m : int/long
20+
Upper limit of baby steps / giant steps
21+
x_poss : int/long
22+
Values calculated in each giant step
23+
c : int/long
24+
Giant Step pre-computation: c = g^(-m) % p
25+
i, j : int/long
26+
Giant Step, Baby Step variables
27+
lookup_table: dictionary
28+
Dictionary storing all the values computed in the baby step
29+
"""
30+
mod_size = len(bin(p-1)[2:])
31+
32+
print "[+] Using BSGS algorithm to solve DLP"
33+
print "[+] Modulus size: " + str(mod_size) + ". Warning! BSGS not space efficient\n"
34+
35+
m = ceil(sqrt(p-1))
36+
# Baby Step
37+
lookup_table = {pow(g, j, p): j for j in range(m)}
38+
# Giant Step pre-computation
39+
c = pow(g, m*(p-2), p)
40+
# Giant Steps
41+
for i in range(m):
42+
temp = (y*pow(c, i, p)) % p
43+
if temp in lookup_table:
44+
# x found
45+
return i*m + lookup_table[temp]
46+
return None
47+
48+
49+
if __name__ == "__main__":
50+
try:
51+
assert pow(2, bsgs(2, 4178319614, 6971096459), 6971096459) == 4178319614
52+
assert pow(3, bsgs(3, 362073897, 2500000001), 2500000001) == 362073897
53+
except:
54+
print "[+] Function inconsistent and incorrect, check the implementation"
226 Bytes
Loading

0 commit comments

Comments
 (0)