-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmasked_mul.vhdl
432 lines (391 loc) · 13.4 KB
/
masked_mul.vhdl
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
---------------------------------------------------------------------------
--
-- Copyright (C) 2016 Stiftung Secure Information and
-- Communication Technologies SIC and
-- Graz University of Technology
-- Contact: http://opensource.iaik.tugraz.at
--
-- This file is part of AES DOM.
--
-- $BEGIN_LICENSE:DEFAULT$
-- Commercial License Usage
-- Licensees holding valid commercial licenses may use this file in
-- accordance with the commercial license agreement provided with the
-- Software or, alternatively, in accordance with the terms contained in
-- a written agreement between you and SIC. For further information
-- contact us at http://opensource.iaik.tugraz.at.
--
-- GNU General Public License Usage
-- Alternatively, this file may be used under the terms of the GNU
-- General Public License version 3.0 as published by the Free Software
-- Foundation and appearing in the file LICENSE.GPL included in the
-- packaging of this file. Please review the following information to
-- ensure the GNU General Public License version 3.0 requirements will be
-- met: http://www.gnu.org/copyleft/gpl.html.
--
-- This software is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this software. If not, see http://www.gnu.org/licenses/.
--
-- $END_LICENSE:DEFAULT$
--
----------------------------------------------------------------------------
-- ### masked_mul.vhdl ###
--
--
-- Description:
-- Different variants of masked multipliers
--
--
-- Initial Version: Date: 22. September 2015 by Hannes Gross IAIK
--
------------------------------------------------------------------------------
library ieee; use ieee.std_logic_1164.all;
entity masked_mul is
generic (
VARIANT : string := "Hybrid"; -- or "Classic" Implementation Variant
PIPELINED : string := "no";
USE_TWO_MASKS : string := "no"; -- or "no", two masks saves one FF at output
DATA_WIDTH : integer := 4
);
port (
-- Clock and reset
ClkxCI : in std_logic;
RstxBI : in std_logic;
-- Shares and masks
AmxDI : in std_logic_vector(DATA_WIDTH-1 downto 0); -- share A
MaxDI : in std_logic_vector(DATA_WIDTH-1 downto 0); -- mask of A
BmxDI : in std_logic_vector(DATA_WIDTH-1 downto 0); -- share B
MbxDI : in std_logic_vector(DATA_WIDTH-1 downto 0); -- mask of B
Mq1xDI : in std_logic_vector(DATA_WIDTH-1 downto 0); -- remasking mask
Mq2xDI : in std_logic_vector(DATA_WIDTH-1 downto 0); -- optional 2nd mask
-- Outputs
QmxDO : out std_logic_vector(DATA_WIDTH-1 downto 0); -- share Q
MqxDO : out std_logic_vector(DATA_WIDTH-1 downto 0) -- mask of Q
);
end masked_mul;
-------------------------------------------------------------------
architecture behavorial of masked_mul is
signal QmxDN, QmxDP : std_logic_vector(DATA_WIDTH-1 downto 0);
signal MqxDN, MqxDP : std_logic_vector(DATA_WIDTH-1 downto 0);
-- Intermediates
signal I1xD, I2xD, I3xD, I4xD : std_logic_vector(DATA_WIDTH-1 downto 0);
signal I5xD, I6xD, I7xD : std_logic_vector(DATA_WIDTH-1 downto 0);
-- Synchrionization FF's
signal FF1xDN, FF2xDN, FF3xDN, FF4xDN : std_logic_vector(DATA_WIDTH-1 downto 0);
signal FF1xDP, FF2xDP, FF3xDP, FF4xDP : std_logic_vector(DATA_WIDTH-1 downto 0);
begin
-------------------------------------------------------------------
-- Hybrid Masked Multiplier, TI like with two shares, and one remasking mask
-- (pipelined)
hybrid_mul_variant_two_masks_pipelined: if (VARIANT = "Hybrid") and (USE_TWO_MASKS = "yes") and (PIPELINED = "yes") generate
-- I1 = Am * Bm
gf2_mul_1: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => BmxDI,
QxDO => I1xD);
-- I2 = Bm * Ma
gf2_mul_2: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => BmxDI,
BxDI => MaxDI,
QxDO => I2xD);
-- I3 = Am * Mb
gf2_mul_3: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => MbxDI,
QxDO => I3xD);
-- I4 = Mb * Ma
gf2_mul_4: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => MbxDI,
BxDI => MaxDI,
QxDO => I4xD);
-- purpose: Addup signals
-- type : combinational
adders_comb: process (FF1xDP, FF2xDP, FF3xDP, FF4xDP, I1xD, I2xD, I3xD,
I4xD, Mq1xDI, Mq2xDI) is
begin -- process adders_comb
FF4xDN <= I4xD xor Mq1xDI;
FF3xDN <= I3xD xor Mq2xDI;
FF2xDN <= I2xD xor Mq2xDI;
FF1xDN <= I1xD xor Mq1xDI;
-- do not use flip flop at output
MqxDP <= FF2xDP xor FF1xDP; -- this is the new mask
QmxDP <= FF4xDP xor FF3xDP; -- masked data
end process adders_comb;
end generate hybrid_mul_variant_two_masks_pipelined;
-------------------------------------------------------------------
-- Hybrid Masked Multiplier, TI like with two shares, and one remasking mask,
-- (pipelined)
hybrid_mul_variant_one_mask_pipelined: if (VARIANT = "Hybrid") and (USE_TWO_MASKS = "no") and (PIPELINED = "yes") generate
-- I1 = Am * Bm
gf2_mul_1: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => BmxDI,
QxDO => I1xD);
-- I2 = Bm * Ma
gf2_mul_2: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => BmxDI,
BxDI => MaxDI,
QxDO => I2xD);
-- I3 = Am * Mb
gf2_mul_3: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => MbxDI,
QxDO => I3xD);
-- I4 = Mb * Ma
gf2_mul_4: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => MbxDI,
BxDI => MaxDI,
QxDO => I4xD);
-- purpose: Addup signals
-- type : combinational
adders_comb: process (FF1xDP, FF2xDP, FF3xDP, FF4xDP, I1xD, I2xD, I3xD,
I4xD, Mq1xDI) is
begin -- process adders_comb
FF4xDN <= I2xD xor Mq1xDI;
FF3xDN <= I1xD;
FF2xDN <= I4xD;
FF1xDN <= I3xD xor Mq1xDI;
--MqxDN <= FF2xDP xor FF1xDP; -- this is the new mask
--QmxDN <= FF4xDP xor FF3xDP; -- masked data
MqxDP <= FF2xDP xor FF1xDP; -- this is the new mask
QmxDP <= FF4xDP xor FF3xDP; -- masked data
end process adders_comb;
-- purpose: Register process for optional output FF
-- type : sequential
-- inputs : ClkxCI, RstxBI
-- outputs:
--register_proc_seq_opt : process (ClkxCI, RstxBI) is
--begin -- process register_proc_seq
-- if RstxBI = '0' then -- asynchronous reset (active low)
-- QmxDP <= (others => '0');
-- MqxDP <= (others => '0');
-- elsif ClkxCI'event and ClkxCI = '1' then -- rising clock edge
-- QmxDP <= QmxDN;
-- MqxDP <= MqxDN;
-- end if;
--end process register_proc_seq_opt;
end generate hybrid_mul_variant_one_mask_pipelined;
-------------------------------------------------------------------
-- Hybrid Masked Multiplier, TI like with two shares, and one remasking mask
hybrid_mul_variant_two_masks: if (VARIANT = "Hybrid") and (USE_TWO_MASKS = "yes") and (PIPELINED = "no") generate
-- I1 = Am * Bm
gf2_mul_1: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => BmxDI,
QxDO => I1xD);
-- I2 = Bm * Ma
gf2_mul_2: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => BmxDI,
BxDI => MaxDI,
QxDO => I2xD);
-- I3 = Am * Mb
gf2_mul_3: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => MbxDI,
QxDO => I3xD);
-- I4 = Mb * Ma
gf2_mul_4: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => MbxDI,
BxDI => MaxDI,
QxDO => I4xD);
-- purpose: Addup signals
-- type : combinational
adders_comb: process (FF1xDP, FF2xDP, FF3xDP, FF4xDP, I1xD, I2xD, I3xD,
I4xD, Mq1xDI, Mq2xDI) is
begin -- process adders_comb
FF4xDN <= I4xD xor Mq1xDI;
FF3xDN <= I3xD xor Mq2xDI;
FF2xDN <= I2xD xor Mq2xDI;
FF1xDN <= I1xD xor Mq1xDI;
-- do not use flip flop at output
MqxDP <= FF2xDP xor FF1xDP; -- this is the new mask
QmxDP <= FF4xDP xor FF3xDP; -- masked data
end process adders_comb;
end generate hybrid_mul_variant_two_masks;
-------------------------------------------------------------------
-- Hybrid Masked Multiplier, TI like with two shares, and one remasking mask
hybrid_mul_variant_one_mask: if (VARIANT = "Hybrid") and (USE_TWO_MASKS = "no") and (PIPELINED = "no") generate
-- I1 = Am * Bm
gf2_mul_1: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => BmxDI,
QxDO => I1xD);
-- I2 = Bm * Ma
gf2_mul_2: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => BmxDI,
BxDI => MaxDI,
QxDO => I2xD);
-- I3 = Am * Mb
gf2_mul_3: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => MbxDI,
QxDO => I3xD);
-- I4 = Mb * Ma
gf2_mul_4: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => MbxDI,
BxDI => MaxDI,
QxDO => I4xD);
-- purpose: Addup signals
-- type : combinational
adders_comb: process (FF1xDP, FF2xDP, FF3xDP, FF4xDP, I1xD, I2xD, I3xD,
I4xD, Mq1xDI) is
begin -- process adders_comb
FF4xDN <= I2xD xor Mq1xDI;
--FF3xDN <= I3xD;
--FF2xDN <= I2xD;
FF1xDN <= I3xD xor Mq1xDI;
--MqxDN <= FF2xDP xor FF1xDP; -- this is the new mask
--QmxDN <= FF4xDP xor FF3xDP; -- masked data
MqxDP <= I1xD xor FF1xDP; -- this is the new mask
QmxDP <= FF4xDP xor I4xD; -- masked data
end process adders_comb;
-- purpose: Register process for optional output FF
-- type : sequential
-- inputs : ClkxCI, RstxBI
-- outputs:
--register_proc_seq_opt : process (ClkxCI, RstxBI) is
--begin -- process register_proc_seq
-- if RstxBI = '0' then -- asynchronous reset (active low)
-- QmxDP <= (others => '0');
-- MqxDP <= (others => '0');
-- elsif ClkxCI'event and ClkxCI = '1' then -- rising clock edge
-- QmxDP <= QmxDN;
-- MqxDP <= MqxDN;
-- end if;
--end process register_proc_seq_opt;
end generate hybrid_mul_variant_one_mask;
-------------------------------------------------------------------
-- Classic Masked Multiplier
classic_mul_variant: if VARIANT = "Classic" generate
-- I1 = Am * Bm
gf2_mul_1: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => BmxDI,
QxDO => I1xD);
-- I2 = Bm * Ma
gf2_mul_2: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => BmxDI,
BxDI => MaxDI,
QxDO => I2xD);
-- I3 = Am * Mb
gf2_mul_3: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => AmxDI,
BxDI => MbxDI,
QxDO => I3xD);
-- I4 = Mb * Ma
gf2_mul_4: entity work.gf2_mul
generic map (
N => DATA_WIDTH)
port map (
AxDI => MbxDI,
BxDI => MaxDI,
QxDO => I4xD);
-- purpose: Addup signals
-- type : combinational
adders_comb: process (I1xD, I2xD, I3xD, I4xD, I5xD, I6xD, I7xD, Mq1xDI) is
begin -- process adders_comb
I7xD <= I4xD xor Mq1xDI;
I6xD <= I3xD xor I7xD;
I5xD <= I2xD xor I6xD;
QmxDN <= I1xD xor I5xD;
MqxDN <= Mq1xDI;
end process adders_comb;
-- purpose: Register process for optional output FF
-- type : sequential
-- inputs : ClkxCI, RstxBI
-- outputs:
register_proc_seq_opt : process (ClkxCI, RstxBI) is
begin -- process register_proc_seq
if RstxBI = '0' then -- asynchronous reset (active low)
QmxDP <= (others => '0');
MqxDP <= (others => '0');
elsif ClkxCI'event and ClkxCI = '1' then -- rising clock edge
QmxDP <= QmxDN;
MqxDP <= MqxDN;
end if;
end process register_proc_seq_opt;
end generate classic_mul_variant;
-- purpose: Register process
-- type : sequential
-- inputs : ClkxCI, RstxBI
-- outputs:
register_proc_seq: process (ClkxCI, RstxBI) is
begin -- process register_proc_seq
if RstxBI = '0' then -- asynchronous reset (active low)
FF1xDP <= (others => '0');
FF2xDP <= (others => '0');
FF3xDP <= (others => '0');
FF4xDP <= (others => '0');
elsif ClkxCI'event and ClkxCI = '1' then -- rising clock edge
FF1xDP <= FF1xDN;
FF2xDP <= FF2xDN;
FF3xDP <= FF3xDN;
FF4xDP <= FF4xDN;
end if;
end process register_proc_seq;
-- Outputs
QmxDO <= QmxDP;
MqxDO <= MqxDP;
end behavorial;