Java Program to Implement Coppersmith Freivald’s Algorithm

This is the java implementation of classic Coppersmith-Freivalds’ algorithm to check whether the multiplication of matrix A and B equals the given matrix C. It does it by checking A*(B*r)-(C*r) where r is any random column vector consisting only 0/1 as its elements. If this value is zero algorithm prints Yes, No otherwise.

Here is the source code of the Java Program to Implement Coppersmith Freivald’s Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a sample program to check whether the matrix c is equal to the multiplication of a and b
//implementation of Coppersmith Freivalds Algorithm 
import java.util.Random;
import java.util.Scanner;
 
public class Coppersmith_Freivalds_Algorithm 
{
    public static void main(String args[])
    {
        System.out.println("Enter the dimesion of the matrices: ");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        System.out.println("Enter the 1st matrix: ");
        double a[][] = new double[n][n];
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                a[i][j] = input.nextDouble();
            }
        }
 
        System.out.println("Enter the 2st matrix: ");
        double b[][] = new double[n][n];
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                b[i][j] = input.nextDouble();
            }
        }
 
        System.out.println("Enter the result matrix: ");
        double c[][] = new double[n][n];
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                c[i][j] = input.nextDouble();
            }
        }
 
        //random generation of the r vector containing only 0/1 as its elements
        double [][]r = new double[n][1];
        Random random = new Random();
        for(int i=0; i<n; i++)
        {
            r[i][0] = random.nextInt(2);
        }
 
        //test A * (b*r) - (C*) = 0
        double br[][] = new double[n][1];
        double cr[][] = new double[n][1];
        double abr[][] = new double[n][1];
        br = multiplyVector(b, r, n);
        cr = multiplyVector(c, r, n);
        abr = multiplyVector(a, br, n);
 
        //check for all zeros in abr
        boolean flag = true; 
        for(int i=0; i<n; i++)
        {
            if(abr[i][0] == 0)
                continue;
            else
                flag = false;
        }
        if(flag == true)
            System.out.println("Yes");
        else
            System.out.println("No");
 
        input.close();
    }
 
    public static double[][] multiplyVector(double[][] a, double[][] b, int n)
    {
        double result[][] = new double[n][1];
        for (int i = 0; i < n; i++) 
        {
            for (int j = 0; j < 1; j++) 
            {
                for (int k = 0; k < n; k++)
                {	 
                    result[i][j] = result[i][j] + a[i][k] * b[k][j];
                }
            }
        }
        return result;
    }
}

Output:

$ javac Coppersmith_Freivalds_Algorithm.java
$ java Coppersmith_Freivalds_Algorithm
Enter the dimesion of the matrices: 
2
Enter the 1st matrix: 
2 3 
3 4
Enter the 2st matrix: 
1 0
1 2
Enter the result matrix: 
6 5
8 7
 
Yes

Related posts:

Java Program to Represent Graph Using Adjacency List
Reactive WebSockets with Spring 5
Java – Write an InputStream to a File
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java – Generate Random String
Các nguyên lý thiết kế hướng đối tượng – SOLID
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Implement Threaded Binary Tree
Spring @RequestParam Annotation
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
The Difference Between map() and flatMap()
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
@DynamicUpdate with Spring Data JPA
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Spring Cloud AWS – EC2
Guide to CountDownLatch in Java
Spring Security Registration – Resend Verification Email
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Lập trình đa luồng với CompletableFuture trong Java 8
How to Manually Authenticate User with Spring Security
Java Program to Find Nearest Neighbor for Dynamic Data Set
Converting between an Array and a List in Java
Java Program to Implement RoleList API
Java Program to Generate a Sequence of N Characters for a Given Specific Case
What is Thread-Safety and How to Achieve it?
Java Program to Implement Variable length array
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Getting Started with Stream Processing with Spring Cloud Data Flow
Spring WebClient Requests with Parameters
Guide to WeakHashMap in Java