BastyZ / CriptoMersenne

0 stars 0 forks source link

Mersenne prime generator #1

Closed AnnBenavides closed 5 years ago

AnnBenavides commented 5 years ago

mersennePrime(x: int): int Generate a mersenne prime p less or equal to x, and return k given by p = 2^k - 1

Example code on java

// Program to generate 
// mersenne primes 
import java.io.*; 

class GFG { 

    // Generate all prime numbers 
    // less than n. 
    static void SieveOfEratosthenes(int n, 
                        boolean prime[]) 
    { 
        // Initialize all entries of 
        // boolean array as true. A 
        // value in prime[i] will finally 
        // be false if i is Not a prime, 
        // else true bool prime[n+1]; 
        for (int i = 0; i <= n; i++) 
            prime[i] = true; 

        for (int p = 2; p * p <= n; p++) 
        { 
            // If prime[p] is not changed 
            // , then it is a prime 
            if (prime[p] == true) 
            { 
                // Update all multiples of p 
                for (int i = p * 2; i <= n; i += p) 
                    prime[i] = false; 
            } 
        } 
    } 

    // Function to generate mersenne 
    // primes lessthan or equal to n 
    static void mersennePrimes(int n) 
    { 
        // Create a boolean array 
        // "prime[0..n]" 
        boolean prime[]=new boolean[n + 1]; 

        // Generating primes 
        // using Sieve 
        SieveOfEratosthenes(n, prime); 

        // Generate all numbers of 
        // the form 2^k - 1 and 
        // smaller than or equal to n. 
        for (int k = 2; (( 1 << k) - 1) <= n; k++) 
        { 
            long num = ( 1 << k) - 1; 

            // Checking whether number is 
            // prime and is one less then 
            // the power of 2 
            if (prime[(int)(num)]) 
                System.out.print(num + " "); 
        } 
    } 

    // Driven program 
    public static void main(String args[]) 
    { 
        int n = 31; 
        System.out.println("Mersenne prime"+ 
                    "numbers smaller than"+ 
                        "or equal to "+n); 

        mersennePrimes(n); 
    } 
} 

Source

BastyZ commented 5 years ago

Focus of function is changed, according to the paper it has to find an n exponent on 2^n -1 such that satisfies key generation rules

BastyZ commented 5 years ago

There were created two versions of this function due to different conditions to choose a prime where doing bit-to-bit encryption and block encryption