2020-10-29

The Meet In The Middle Algorithm Or The Collision Algorithm

I'm working on the Meet In The Middle Algorithm; also known as the "Collision Algorithm". Here's the code

Python 3 Program To Calculate The Discrete Logarithm Problem Using The Collision Algorithm

import math;
def discreteLogarithm(a, b, m):  
  
    n = int(math.sqrt (m) + 1); 
  
    # Calculate a ^ n  
    an = 1; 
    for i in range(n): 
        an = (an * a) % m; 
  
    value = [0 for i in range(m)]; 
  
    # Store all values of a^(n*i) of LHS 
    cur = an; 
    for i in range(1, n + 1): 
        if (value[ cur ] == 0): 
            value[ cur ] = i; 
        cur = (cur * an) % m; 
      
    cur = b; 
    for i in range(n + 1): 
          
        # Calculate (a ^ j) * b and check 
        # for collision 
        if (value[cur] > 0): 
            ans = value[cur] * n - i; 
            if (ans < m): 
                return ans; 
        cur = (cur * a) % m; 
  
    return -1; 
  
# Driver code 
a = 2;
 
b = 3;
 
m = 5; 

print(discreteLogarithm(a, b, m)); 
  
a = 3;
 
b = 7;
 
m = 11; 

print(discreteLogarithm(a, b, m)); 
  
# This code is contributed by mits.

I actually have a problem considering a section of this code

1

If the line of the code below can be modified to split the total number (n-1) into different arrays using a common difference until the original number (n-1) is exhausted. Something like this; 0 to 29 (0 to 3, 4 to 7, 8 to 11, 12 to 15, 16 to 19, 20 to 23, 24 to 27, 28 to 29). The main point is that my goal is for the array i.e (0 to 29) to be dispersed to a certain number of arrays like the example till the major number (29) is exhausted into parts using the common difference (4). And I'm also thinking if the script can be manipulated to print out an array when brute-forcing on each array is complete. Here's the code;

# Store all values of a^(n*i) of LHS 
cur = an; 
for i in range(1, n + 1): 
    if (value[ cur ] == 0): 
        value[ cur ] = i; 
    cur = (cur * an) % m; 

(For i in (0 to n-1); split into components in numerical order with common difference P till N-1 is exhausted; compute, store i.a and print each array when brute force on the array is complete. Where p = common difference a = multiplicator point (2) )

I would love to get all the help I can with this (I'm a rookie in this)



from Recent Questions - Stack Overflow https://ift.tt/2G6GrVN
https://ift.tt/eA8V8J

No comments:

Post a Comment