-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncrypt.sv
213 lines (175 loc) · 6.42 KB
/
Encrypt.sv
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* Created By Hamna Mohiuddin @hamnamohi as a Google Summer of Code 2024 Project.
DESCRIPTION:
Encryption in Baby Kyber involves generating a ciphertext from a given message using the public key,
random polynomial vectors, and error polynomials. The process includes polynomial matrix multiplication,
modular reduction, and the addition of error vectors.
The message is converted into a polynomial based on its binary representation.
For example, the binary representation of the number 11 is 1011, leading to the polynomial mb = x3 + x + 1.
This polynomial is then scaled by multiplying it with ⌊ q / 2 ⌉ = 9 , where q = 17, resulting in the scaled
polynomial m = 9x3+ 9x +9.
The public key, consisting of a matrix A and a vector t, is multiplied by the random polynomial vector r to
compute the intermediate values u and v. These intermediate results are reduced modulo q and combined with error vectors e1 and e2,
as well as the scaled message polynomial m, to form the final ciphertext:
u = AT*r + e1
v = tT*r + e2 +m
*/
module Encrypt (
input logic clk,
input logic rst_n,
input logic enable,
input logic [31:0] message,
input logic signed [31:0] r [1:0][3:0],
input logic signed [31:0] e1 [1:0][3:0],
input logic signed [31:0] e2 [3:0],
input logic signed [31:0] combined_output [1:0][3:0][3:0],
output logic signed [31:0] ciphertext[1:0] [1:0][3:0]
);
logic signed [31:0] transposed_matrix [3:0][3:0];
logic signed [31:0] poly_out0 [3:0];
logic signed [31:0] poly_out1 [3:0];
logic signed [31:0] poly_out2 [3:0];
logic signed [31:0] poly_out3 [3:0];
logic signed [31:0] poly_out4 [3:0];
logic signed [31:0] poly_out5 [3:0];
logic signed [31:0] added [3:0];
logic signed [31:0] added1 [3:0];
logic signed [31:0] added2 [3:0];
logic signed [3:0] coefficients;
logic signed [31:0] coefficients_scaled [3:0];
logic signed [31:0] u [1:0][3:0];
logic signed [31:0] v [3:0];
logic signed [31:0] temp [3:0];
MatrixTranspose transpose_inst (
.matrix_in(combined_output[0]),
.matrix_out(transposed_matrix)
);
PolynomialMatrixMultiplication poly_mult_inst (
.clk(clk),
.rst_n(rst_n),
.enable(enable),
.polynomial1(transposed_matrix[0]),
.polynomial2(r[0]),
.polynomial_out(poly_out0)
);
PolynomialMatrixMultiplication poly_mult_inst1 (
.clk(clk),
.rst_n(rst_n),
.enable(enable),
.polynomial1(transposed_matrix[1]),
.polynomial2(r[1]),
.polynomial_out(poly_out1)
);
PolynomialMatrixMultiplication poly_mult_inst2 (
.clk(clk),
.rst_n(rst_n),
.enable(enable),
.polynomial1(transposed_matrix[2]),
.polynomial2(r[0]),
.polynomial_out(poly_out2)
);
PolynomialMatrixMultiplication poly_mult_inst3 (
.clk(clk),
.rst_n(rst_n),
.enable(enable),
.polynomial1(transposed_matrix[3]),
.polynomial2(r[1]),
.polynomial_out(poly_out3)
);
PolynomialMatrixMultiplication poly_mult_inst4 (
.clk(clk),
.rst_n(rst_n),
.enable(enable),
.polynomial1(combined_output[1][0]),
.polynomial2(r[0]),
.polynomial_out(poly_out4)
);
PolynomialMatrixMultiplication poly_mult_inst5 (
.clk(clk),
.rst_n(rst_n),
.enable(enable),
.polynomial1(combined_output[1][1]),
.polynomial2(r[1]),
.polynomial_out(poly_out5)
);
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
for (int j = 0; j < 4; j++) begin
coefficients[j] <= 0;
end
end else if (enable) begin
for (int i = 0; i < 4; i++) begin
coefficients[i] <= message[i];
end
end
end
always_comb begin
for (int i = 0; i < 4; i++) begin
added[i] = 0;
added1[i] = 0;
added2[i] = 0;
u[0][i] = 0;
u[1][i] = 0;
end
if (enable) begin
for (int i = 0; i < 4; i++) begin
added[i] = (poly_out0[i] + poly_out1[i]);
added1[i] = (poly_out2[i] + poly_out3[i]);
added2[i] = (poly_out4[i] + poly_out5[i]);
if (added[i] < 0) begin
added[i] = added[i];
end else begin
added[i] = (added[i] % 17);
end
if (added1[i] < 0) begin
added1[i] = added1[i] ;
end else begin
added1[i] = (added1[i] % 17);
end
if (added2[i] < 0) begin
added2[i] = added2[i];
end else begin
added2[i] = (added2[i] % 17);
end
end
for (int i = 0; i < 4; i++) begin
u[0][i] = added[i] + e1[0][i];
u[1][i] = added1[i] + e1[1][i];
if ( u[0][i] < 0) begin
u[0][i] = u[0][i] ;
end else begin
u[0][i] = ( u[0][i] % 17);
end
if ( u[1][i] < 0) begin
u[1][i] = u[1][i] ;
end else begin
u[1][i] = ( u[1][i] % 17);
end
end
end
end
always_comb begin
for (int i = 0; i < 4; i++) begin
if (coefficients[i] == 1) begin
coefficients_scaled[i] = 9; // Multiply by qhalf = 9
end else begin
coefficients_scaled[i] = 0;
end
end
end
always_comb begin
for (int i = 0; i < 4; i++) begin
v[i] = (added2[i] + e2[i]) - coefficients_scaled[3-i];
if ( v[i] < 0) begin
v[i] = v[i] + 17;
end else begin
v[i] = ( v[i] % 17);
end
end
for (int i = 0; i < 2; i++) begin
for (int j = 0; j < 4; j++) begin
ciphertext[0][i][j] = u[i][j];
ciphertext[1][0][j] = v[j];
end
end
end
endmodule