-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomial.java
103 lines (84 loc) · 2.72 KB
/
Polynomial.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.company;
import java.util.Scanner;
//Rijndael's Finite Field (AES): Multiplication of 2 polynomials represented in Galois Field 2^8 with a given irreducible polynomial
/*Test input:
* (01010111) x (10000011) mod (100011011)
* Output Expected: (11000001)*/
public class Polynomial {
//function for XOR for 2 binary strings
public static String XOR(String a, String b)
{
String answer = "";
for(int i=0; i<a.length(); i++)
{
if(a.charAt(i)==b.charAt(i))
{
answer = answer + "0";
}
else
{
answer = answer + "1";
}
}
return answer;
}
//function to left-shift by 1 bit
public static String leftshift(String a)
{
//empty string to store data
String answer = "";
answer = a.substring(1, a.length())+'0';
return answer;
}
//main function
public static void polynomial_main()
{
Scanner sc = new Scanner(System.in);
//Scan values of polynomials to be multiplied and m(x)
System.out.println("Powers of f(x) in binary: ");
String fx = sc.nextLine();
System.out.println("Powers of g(x) in binary: ");
String gx = sc.nextLine();
System.out.println("Powers of m(x) in binary: ");
String mx = sc.nextLine();
//new value of m(x) without the first term with x^8
String m_new = mx.substring(1, mx.length());
int count = fx.length()-fx.indexOf('1');
String x[] = new String[count+1];
String temp = gx;
//array to store x*f(x) values
x[0] = gx;
//Store final answer
String tempx = "";
//calculate x*f(x) and store in array x
for(int i = 1; i <= count; i++)
{
if(temp.charAt(0) == '1') //left shift and xor with the new value of m because bit 0 is 1
{
temp = leftshift(temp);
temp = XOR(temp, m_new);
x[i]=temp;
}
else //left shift without xor because bit 0 is 0
{
temp = leftshift(temp);
x[i] = temp;
}
}
//Initialise all bits at tempx as 0
for(int i=0;i<fx.length();i++)
{
tempx = tempx + "0";
}
//Choose only those values of x*f(x) we need, xor them and store final result in array tempx
for(int i=0;i<fx.length();i++)
{
if(fx.charAt(i)=='1')
{
tempx = XOR(x[(fx.length()-i)-1],tempx);
}
}
//Printing final answer
System.out.println("Multiplication of ("+fx+ " * "+gx+") = " +tempx);
}
}