1 |
6 |
gajos |
-----------------------------------------------------------------------
|
2 |
|
|
---- ----
|
3 |
|
|
---- Montgomery modular multiplier and exponentiator ----
|
4 |
|
|
---- ----
|
5 |
|
|
---- This file is part of the Montgomery modular multiplier ----
|
6 |
|
|
---- and exponentiator project ----
|
7 |
|
|
---- http://opencores.org/project,mod_mult_exp ----
|
8 |
|
|
---- ----
|
9 |
|
|
---- Description: ----
|
10 |
|
|
---- This module is example implementation of the Montgomery ----
|
11 |
|
|
---- modular exponentiator combined with the RS-232 communication----
|
12 |
|
|
---- with PC. All related to the communication logic was ----
|
13 |
|
|
---- inclueded here. Input data are retrieved by serial input ----
|
14 |
|
|
---- and converted into parallel data by the shift registers ----
|
15 |
|
|
---- After exponentiation in similar way parallel data are ----
|
16 |
|
|
---- converted into serial data. For the communication, the ----
|
17 |
|
|
---- RS232RefComp module made by Digilent was used and slightly ----
|
18 |
|
|
---- modified (increased data transfer speed to 115 200 bps). ----
|
19 |
|
|
---- ----
|
20 |
|
|
---- To Do: ----
|
21 |
|
|
---- ----
|
22 |
|
|
---- Author(s): ----
|
23 |
|
|
---- - Krzysztof Gajewski, gajos@opencores.org ----
|
24 |
|
|
---- k.gajewski@gmail.com ----
|
25 |
|
|
---- ----
|
26 |
|
|
-----------------------------------------------------------------------
|
27 |
|
|
---- ----
|
28 |
|
|
---- Copyright (C) 2019 Authors and OPENCORES.ORG ----
|
29 |
|
|
---- ----
|
30 |
|
|
---- This source file may be used and distributed without ----
|
31 |
|
|
---- restriction provided that this copyright statement is not ----
|
32 |
|
|
---- removed from the file and that any derivative work contains ----
|
33 |
|
|
---- the original copyright notice and the associated disclaimer. ----
|
34 |
|
|
---- ----
|
35 |
|
|
---- This source file is free software; you can redistribute it ----
|
36 |
|
|
---- and-or modify it under the terms of the GNU Lesser General ----
|
37 |
|
|
---- Public License as published by the Free Software Foundation; ----
|
38 |
|
|
---- either version 2.1 of the License, or (at your option) any ----
|
39 |
|
|
---- later version. ----
|
40 |
|
|
---- ----
|
41 |
|
|
---- This source is distributed in the hope that it will be ----
|
42 |
|
|
---- useful, but WITHOUT ANY WARRANTY; without even the implied ----
|
43 |
|
|
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ----
|
44 |
|
|
---- PURPOSE. See the GNU Lesser General Public License for more ----
|
45 |
|
|
---- details. ----
|
46 |
|
|
---- ----
|
47 |
|
|
---- You should have received a copy of the GNU Lesser General ----
|
48 |
|
|
---- Public License along with this source; if not, download it ----
|
49 |
|
|
---- from http://www.opencores.org/lgpl.shtml ----
|
50 |
|
|
---- ----
|
51 |
|
|
-----------------------------------------------------------------------
|
52 |
|
|
library IEEE;
|
53 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
54 |
|
|
use work.properties.ALL;
|
55 |
|
|
|
56 |
|
|
-- Uncomment the following library declaration if using
|
57 |
|
|
-- arithmetic functions with Signed or Unsigned values
|
58 |
|
|
--use IEEE.NUMERIC_STD.ALL;
|
59 |
|
|
|
60 |
|
|
-- Uncomment the following library declaration if instantiating
|
61 |
|
|
-- any Xilinx primitives in this code.
|
62 |
|
|
--library UNISIM;
|
63 |
|
|
--use UNISIM.VComponents.all;
|
64 |
|
|
|
65 |
|
|
-- Definition of the component
|
66 |
|
|
entity ModExpComm is
|
67 |
|
|
generic (word_size : integer := WORD_LENGTH);
|
68 |
|
|
port (
|
69 |
|
|
DATA_RXD : in STD_LOGIC;
|
70 |
|
|
CLK : in STD_LOGIC;
|
71 |
|
|
RESET : in STD_LOGIC;
|
72 |
|
|
DATA_TXD : out STD_LOGIC
|
73 |
|
|
);
|
74 |
|
|
end ModExpComm;
|
75 |
|
|
|
76 |
|
|
architecture Behavioral of ModExpComm is
|
77 |
|
|
|
78 |
|
|
-- This is DCM component generated by the ISE. It was used due
|
79 |
|
|
-- to maximum clock speed available in the S3EBOARD served by
|
80 |
|
|
-- the Digilent (50 MHz) it is used in order to decrease speed
|
81 |
|
|
-- of exponentiator core. RS232 is working with 50 MHz and the
|
82 |
|
|
-- rest part of the core is working with 10 MHz. This is due
|
83 |
|
|
-- to timing estimation of ISE.
|
84 |
|
|
component dcms is
|
85 |
|
|
port (
|
86 |
|
|
CLKIN_IN : in STD_LOGIC;
|
87 |
|
|
CLKDV_OUT : out STD_LOGIC;
|
88 |
|
|
CLK0_OUT : out STD_LOGIC
|
89 |
|
|
);
|
90 |
|
|
end component dcms;
|
91 |
|
|
|
92 |
|
|
-- Montgomery modular exponentiator
|
93 |
|
|
component ModExp is
|
94 |
|
|
generic (
|
95 |
|
|
word_size : integer := WORD_LENGTH;
|
96 |
|
|
word_binary : integer := WORD_INTEGER
|
97 |
|
|
);
|
98 |
|
|
Port (
|
99 |
|
|
input : in STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
100 |
|
|
ctrl : in STD_LOGIC_VECTOR(2 downto 0);
|
101 |
|
|
clk : in STD_LOGIC;
|
102 |
|
|
reset : in STD_LOGIC;
|
103 |
|
|
data_in_ready : in STD_LOGIC;
|
104 |
|
|
ready : out STD_LOGIC;
|
105 |
|
|
output : out STD_LOGIC_VECTOR(word_size - 1 downto 0)
|
106 |
|
|
);
|
107 |
|
|
end component ModExp;
|
108 |
|
|
|
109 |
|
|
-- RS232 component made by the Digilent
|
110 |
|
|
-- all checking was ignored but for communication
|
111 |
|
|
-- odd parity is used
|
112 |
|
|
component Rs232RefComp is
|
113 |
|
|
port (
|
114 |
|
|
TXD : out STD_LOGIC := '1';
|
115 |
|
|
RXD : in STD_LOGIC;
|
116 |
|
|
CLK : in STD_LOGIC; --Master Clock
|
117 |
|
|
DBIN : in STD_LOGIC_VECTOR(7 downto 0); --Data Bus in
|
118 |
|
|
DBOUT : out STD_LOGIC_VECTOR(7 downto 0); --Data Bus out
|
119 |
|
|
RDA : inout STD_LOGIC; --Read Data Available
|
120 |
|
|
TBE : inout STD_LOGIC := '1'; --Transfer Bus Empty
|
121 |
|
|
RD : in STD_LOGIC; --Read Strobe
|
122 |
|
|
WR : in STD_LOGIC; --Write Strobe
|
123 |
|
|
PE : out STD_LOGIC; --Parity Error Flag
|
124 |
|
|
FE : out STD_LOGIC; --Frame Error Flag
|
125 |
|
|
OE : out STD_LOGIC; --Overwrite Error Flag
|
126 |
|
|
RST : in STD_LOGIC := '0' --Master Reset
|
127 |
|
|
);
|
128 |
|
|
end component Rs232RefComp;
|
129 |
|
|
|
130 |
|
|
-- Register for storing control word for ModExpComm component
|
131 |
|
|
component Reg is
|
132 |
|
|
generic(word_size : integer := 8);
|
133 |
|
|
port(
|
134 |
|
|
input : in STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
135 |
|
|
output : out STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
136 |
|
|
enable : in STD_LOGIC;
|
137 |
|
|
clk : in STD_LOGIC;
|
138 |
|
|
reset : in STD_LOGIC
|
139 |
|
|
);
|
140 |
|
|
end component Reg;
|
141 |
|
|
|
142 |
|
|
---- Shift registers for input and output data for the modular exponentiator
|
143 |
|
|
component ShiftReg is
|
144 |
|
|
generic (
|
145 |
|
|
length_1 : integer := BYTE;
|
146 |
|
|
length_2 : integer := WORD_LENGTH;
|
147 |
|
|
internal_data : integer := WORD_LENGTH
|
148 |
|
|
);
|
149 |
|
|
port (
|
150 |
|
|
input : in STD_LOGIC_VECTOR(length_1 - 1 downto 0);
|
151 |
|
|
output : out STD_LOGIC_VECTOR(length_2 - 1 downto 0);
|
152 |
|
|
en : in STD_LOGIC;
|
153 |
|
|
shift : in STD_LOGIC;
|
154 |
|
|
clk : in STD_LOGIC;
|
155 |
|
|
reset : in STD_LOGIC
|
156 |
|
|
);
|
157 |
|
|
end component ShiftReg;
|
158 |
|
|
|
159 |
|
|
---- some 'help' mux at the output of the component
|
160 |
|
|
component AsyncMux is
|
161 |
|
|
generic(
|
162 |
|
|
word_size : integer := WORD_LENGTH
|
163 |
|
|
);
|
164 |
|
|
port(
|
165 |
|
|
input0 : in STD_LOGIC_VECTOR(word_size downto 0);
|
166 |
|
|
input1 : in STD_LOGIC_VECTOR(word_size downto 0);
|
167 |
|
|
ctrl : in STD_LOGIC;
|
168 |
|
|
output : out STD_LOGIC_VECTOR(word_size downto 0)
|
169 |
|
|
);
|
170 |
|
|
end component AsyncMux;
|
171 |
|
|
|
172 |
|
|
---- State machine
|
173 |
|
|
component ModExpDataCtrlSM is
|
174 |
|
|
port(
|
175 |
|
|
clk : in STD_LOGIC;
|
176 |
|
|
reset : in STD_LOGIC;
|
177 |
|
|
RDAsig : in STD_LOGIC;
|
178 |
|
|
TBEsig : in STD_LOGIC;
|
179 |
|
|
RDsig : out STD_LOGIC;
|
180 |
|
|
WRsig : out STD_LOGIC;
|
181 |
|
|
data_in_ready : out STD_LOGIC;
|
182 |
|
|
readySig : in STD_LOGIC;
|
183 |
|
|
modExpCtrlRegEn : out STD_LOGIC;
|
184 |
|
|
dataToModExpEn : out STD_LOGIC;
|
185 |
|
|
dataToModExpShift : out STD_LOGIC;
|
186 |
|
|
dataFromModExpEn : out STD_LOGIC;
|
187 |
|
|
dataFromModExpShift : out STD_LOGIC;
|
188 |
|
|
muxCtrl : out STD_LOGIC;
|
189 |
|
|
opcodes : in STD_LOGIC_VECTOR(2 downto 0);
|
190 |
|
|
controlStateOut : out STD_LOGIC_VECTOR(2 downto 0)
|
191 |
|
|
);
|
192 |
|
|
end component ModExpDataCtrlSM;
|
193 |
|
|
|
194 |
|
|
-- All signals needed in the implementation
|
195 |
|
|
signal clk_div : STD_LOGIC;
|
196 |
|
|
signal clk_0 : STD_LOGIC;
|
197 |
|
|
|
198 |
|
|
signal dataTXD : STD_LOGIC_VECTOR(7 downto 0);
|
199 |
|
|
signal dataRXD : STD_LOGIC_VECTOR(7 downto 0);
|
200 |
|
|
signal RDAsig : STD_LOGIC;
|
201 |
|
|
signal TBEsig : STD_LOGIC;
|
202 |
|
|
signal RDsig : STD_LOGIC;
|
203 |
|
|
signal WRsig : STD_LOGIC;
|
204 |
|
|
signal PEsig : STD_LOGIC;
|
205 |
|
|
signal FEsig : STD_LOGIC;
|
206 |
|
|
signal OEsig : STD_LOGIC;
|
207 |
|
|
|
208 |
|
|
signal modExpInput : STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
209 |
|
|
signal modExpCtrl : STD_LOGIC_VECTOR(2 downto 0);
|
210 |
|
|
signal modExpCtrlRegEn : STD_LOGIC;
|
211 |
|
|
signal data_in_ready : STD_LOGIC;
|
212 |
|
|
signal readySig : STD_LOGIC;
|
213 |
|
|
signal modExpOutput : STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
214 |
|
|
|
215 |
|
|
signal dataToModExpEn : STD_LOGIC;
|
216 |
|
|
signal dataToModExpShift : STD_LOGIC;
|
217 |
|
|
|
218 |
|
|
signal dataFromModExpEn : STD_LOGIC;
|
219 |
|
|
signal dataFromModExpShift : STD_LOGIC;
|
220 |
|
|
|
221 |
|
|
signal inputToMux : STD_LOGIC_VECTOR(BYTE - 1 downto 0);
|
222 |
|
|
signal controlStateOut : STD_LOGIC_VECTOR(2 downto 0);
|
223 |
|
|
signal muxCtrl : STD_LOGIC;
|
224 |
|
|
|
225 |
|
|
signal ctrl_zero : STD_LOGIC_VECTOR(4 downto 0) := "00000";
|
226 |
|
|
signal control_state_to_out : STD_LOGIC_VECTOR(7 downto 0);
|
227 |
|
|
|
228 |
|
|
begin
|
229 |
|
|
-- Architecture definition
|
230 |
|
|
ctrl_zero <= (others => '0');
|
231 |
|
|
control_state_to_out <= controlStateOut & ctrl_zero;
|
232 |
|
|
|
233 |
|
|
-- DCM
|
234 |
|
|
dcm_module : dcms
|
235 |
|
|
port map(
|
236 |
|
|
CLKIN_IN => CLK,
|
237 |
|
|
CLKDV_OUT => clk_div, --clk_null,
|
238 |
|
|
CLK0_OUT => clk_0
|
239 |
|
|
);
|
240 |
|
|
|
241 |
|
|
-- RS232 component
|
242 |
|
|
serialPort : Rs232RefComp
|
243 |
|
|
port map (
|
244 |
|
|
TXD => DATA_TXD,
|
245 |
|
|
RXD => DATA_RXD,
|
246 |
|
|
CLK => clk_0,
|
247 |
|
|
DBIN => dataTXD,
|
248 |
|
|
DBOUT => dataRXD,
|
249 |
|
|
RDA => RDAsig, --Read Data Available
|
250 |
|
|
TBE => TBEsig, --Transfer Bus Empty
|
251 |
|
|
RD => RDsig, --Read Strobe
|
252 |
|
|
WR => WRsig, --Write Strobe
|
253 |
|
|
PE => PEsig, --Parity Error Flag
|
254 |
|
|
FE => FEsig, --Frame Error Flag
|
255 |
|
|
OE => OEsig, --Overwrite Error Flag
|
256 |
|
|
RST => RESET --Master Reset
|
257 |
|
|
);
|
258 |
|
|
|
259 |
|
|
-- Shift register at input of the modular exponentiator
|
260 |
|
|
-- (convert data from 8 bit to 32 bit, 64 bit, 512 bit, etc.)
|
261 |
|
|
modExpCompIn : ShiftReg
|
262 |
|
|
generic map(
|
263 |
|
|
length_1 => BYTE,
|
264 |
|
|
length_2 => WORD_LENGTH,
|
265 |
|
|
internal_data => WORD_LENGTH
|
266 |
|
|
)
|
267 |
|
|
port map (
|
268 |
|
|
input => dataRXD,
|
269 |
|
|
output => modExpInput,
|
270 |
|
|
en => dataToModExpEn,
|
271 |
|
|
shift => dataToModExpShift,
|
272 |
|
|
clk => CLK_div,
|
273 |
|
|
reset => RESET
|
274 |
|
|
);
|
275 |
|
|
|
276 |
|
|
-- Control register
|
277 |
|
|
modCtrl : Reg
|
278 |
|
|
generic map(
|
279 |
|
|
word_size => 3
|
280 |
|
|
)
|
281 |
|
|
port map (
|
282 |
|
|
input => dataRXD(2 downto 0),
|
283 |
|
|
output => modExpCtrl,
|
284 |
|
|
enable => modExpCtrlRegEn,
|
285 |
|
|
clk => CLK_div,
|
286 |
|
|
reset => RESET
|
287 |
|
|
);
|
288 |
|
|
|
289 |
|
|
-- Modular exponentiator component
|
290 |
|
|
ModExpComp : ModExp
|
291 |
|
|
port map (
|
292 |
|
|
input => modExpInput,
|
293 |
|
|
ctrl => modExpCtrl,
|
294 |
|
|
clk => CLK_div,
|
295 |
|
|
reset => RESET,
|
296 |
|
|
data_in_ready => data_in_ready,
|
297 |
|
|
ready => readySig,
|
298 |
|
|
output => modExpOutput
|
299 |
|
|
);
|
300 |
|
|
|
301 |
|
|
-- Shift register at output of the modular exponentiator
|
302 |
|
|
-- (convert data from 32 bit, 64 bit, 512 bit, etc. to 8 bit)
|
303 |
|
|
dataFromModExpComponent : ShiftReg
|
304 |
|
|
generic map(
|
305 |
|
|
length_1 => WORD_LENGTH,
|
306 |
|
|
length_2 => BYTE,
|
307 |
|
|
internal_data => WORD_LENGTH
|
308 |
|
|
)
|
309 |
|
|
port map (
|
310 |
|
|
input => modExpOutput,
|
311 |
|
|
output => inputToMux,
|
312 |
|
|
en => dataFromModExpEn,
|
313 |
|
|
shift => dataFromModExpShift,
|
314 |
|
|
clk => CLK_div,
|
315 |
|
|
reset => RESET
|
316 |
|
|
);
|
317 |
|
|
|
318 |
|
|
-- Multiplexer at the output of the component
|
319 |
|
|
outMux : AsyncMux
|
320 |
|
|
generic map(
|
321 |
|
|
word_size => BYTE - 1
|
322 |
|
|
)
|
323 |
|
|
port map(
|
324 |
|
|
input0 => inputToMux,
|
325 |
|
|
input1 => control_state_to_out,
|
326 |
|
|
ctrl => muxCtrl,
|
327 |
|
|
output => dataTXD
|
328 |
|
|
);
|
329 |
|
|
|
330 |
|
|
-- State machine
|
331 |
|
|
stateMachine : ModExpDataCtrlSM
|
332 |
|
|
port map(
|
333 |
|
|
clk => CLK_div,
|
334 |
|
|
reset => RESET,
|
335 |
|
|
RDAsig => RDAsig,
|
336 |
|
|
TBEsig => TBEsig,
|
337 |
|
|
RDsig => RDsig,
|
338 |
|
|
WRsig => WRsig,
|
339 |
|
|
data_in_ready => data_in_ready,
|
340 |
|
|
readySig => readySig,
|
341 |
|
|
modExpCtrlRegEn => modExpCtrlRegEn,
|
342 |
|
|
dataToModExpEn => dataToModExpEn,
|
343 |
|
|
dataToModExpShift => dataToModExpShift,
|
344 |
|
|
dataFromModExpEn => dataFromModExpEn,
|
345 |
|
|
dataFromModExpShift => dataFromModExpShift,
|
346 |
|
|
muxCtrl => muxCtrl,
|
347 |
|
|
opcodes => dataRXD(2 downto 0),
|
348 |
|
|
controlStateOut => controlStateOut
|
349 |
|
|
);
|
350 |
|
|
|
351 |
|
|
end Behavioral;
|