Java Program to Implement Naor-Reingold Pseudo Random Function

Last Updated : 04 Dec, 2020

Naor-Reingold Pseudo-Random Function is a function of generating random numbers. Moni Naor and Omer Reingold described efficient constructions for various cryptographic primitives in the private key as well as public-key cryptography.

Example:

```Input : N = 5
Output: 9.0, 9.0, 3.0, 9.0, 3.0

Input : N = 7
Output: 9.0, 81.0, 9.0, 9.0, 3.0, 3.0, 9.0```

Algorithm:

• Declare the variables p, l, g, n, x and arrays a[] and arr[]
• Take input from the user for generating random numbers
• Generate random numbers and use the defined approach:
```Let p and l be prime numbers with l|pâˆ’1.
Select an element g Îµ Fp* of multiplicative order l.
Then for each n-dimensional vector a = (a0,a1, ..., an).

They define the function as:
fa(x)=ga0.a1x1a2x2â€¦..anxn Îµ Fp```
• Print the random numbers

Below is the implementation of the Naor-Reingold Pseudo-Random Function:

Java

 `// Java Program to Implement Naor-Reingold ` `// Pseudo Random Function ` `import` `java.util.*; ` `public` `class` `Main { ` `    ``public` `static` `void` `randomNumbers() ` `    ``{ ` `        ``// Creating arrays and defining variables ` `        ``int` `p = ``7``, l = ``2``, g = ``3``, n = ``6``, x; ` ` `  `        ``int` `a[] = { ``1``, ``2``, ``2``, ``1` `}; ` ` `  `        ``int` `arr[] = ``new` `int``[``4``]; ` ` `  `        ``Random random = ``new` `Random(); ` ` `  `        ``int` `num = ``10``; ` `        ``System.out.println(``"The Random numbers are: "``); ` ` `  `        ``// Generating Random Numbers using ` `        ``// Naor-Reingold Pseudo Random Function approach ` `        ``for` `(``int` `i = ``0``; i < num; i++) { ` `            ``x = random.nextInt(num) % ``16``; ` ` `  `            ``for` `(``int` `j = ``3``; j >= ``0``; j--) { ` `                ``arr[j] = x % ``2``; ` `                ``x /= ``2``; ` `            ``} ` `            ``int` `mult = ``1``; ` ` `  `            ``for` `(``int` `k = ``0``; k < ``4``; k++) { ` `                ``mult *= Math.pow(a[k], arr[k]); ` `            ``} ` `            ``System.out.print(Math.pow(g, mult) + ``", "``); ` `        ``} ` `    ``} ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``randomNumbers(); ` `    ``} ` `}`

Output

```The Random numbers are:
9.0, 9.0, 3.0, 81.0, 3.0, 81.0, 9.0, 9.0, 3.0, 3.0, ```

Share your thoughts in the comments