1 |
3 |
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 is state machine for the modular multiplier it consists----
|
11 |
|
|
---- of three states, NOP the preparation stage, CALCULATE_START ----
|
12 |
|
|
---- for the modular multiply and STOP for the presentation ----
|
13 |
|
|
---- result. ----
|
14 |
|
|
---- ----
|
15 |
|
|
---- To Do: ----
|
16 |
|
|
---- ----
|
17 |
|
|
---- Author(s): ----
|
18 |
|
|
---- - Krzysztof Gajewski, gajos@opencores.org ----
|
19 |
|
|
---- k.gajewski@gmail.com ----
|
20 |
|
|
---- ----
|
21 |
|
|
-----------------------------------------------------------------------
|
22 |
|
|
---- ----
|
23 |
|
|
---- Copyright (C) 2014 Authors and OPENCORES.ORG ----
|
24 |
|
|
---- ----
|
25 |
|
|
---- This source file may be used and distributed without ----
|
26 |
|
|
---- restriction provided that this copyright statement is not ----
|
27 |
|
|
---- removed from the file and that any derivative work contains ----
|
28 |
|
|
---- the original copyright notice and the associated disclaimer. ----
|
29 |
|
|
---- ----
|
30 |
|
|
---- This source file is free software; you can redistribute it ----
|
31 |
|
|
---- and-or modify it under the terms of the GNU Lesser General ----
|
32 |
|
|
---- Public License as published by the Free Software Foundation; ----
|
33 |
|
|
---- either version 2.1 of the License, or (at your option) any ----
|
34 |
|
|
---- later version. ----
|
35 |
|
|
---- ----
|
36 |
|
|
---- This source is distributed in the hope that it will be ----
|
37 |
|
|
---- useful, but WITHOUT ANY WARRANTY; without even the implied ----
|
38 |
|
|
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ----
|
39 |
|
|
---- PURPOSE. See the GNU Lesser General Public License for more ----
|
40 |
|
|
---- details. ----
|
41 |
|
|
---- ----
|
42 |
|
|
---- You should have received a copy of the GNU Lesser General ----
|
43 |
|
|
---- Public License along with this source; if not, download it ----
|
44 |
|
|
---- from http://www.opencores.org/lgpl.shtml ----
|
45 |
|
|
---- ----
|
46 |
|
|
-----------------------------------------------------------------------
|
47 |
|
|
library IEEE;
|
48 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
49 |
|
|
use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
50 |
|
|
use work.properties.ALL;
|
51 |
|
|
|
52 |
|
|
-- Uncomment the following library declaration if using
|
53 |
|
|
-- arithmetic functions with Signed or Unsigned values
|
54 |
|
|
--use IEEE.NUMERIC_STD.ALL;
|
55 |
|
|
|
56 |
|
|
-- Uncomment the following library declaration if instantiating
|
57 |
|
|
-- any Xilinx primitives in this code.
|
58 |
|
|
--library UNISIM;
|
59 |
|
|
--use UNISIM.VComponents.all;
|
60 |
|
|
|
61 |
|
|
entity ModMultIter_SM is
|
62 |
|
|
generic (
|
63 |
|
|
word_size : integer := WORD_LENGTH;
|
64 |
|
|
word_binary : integer := WORD_INTEGER
|
65 |
|
|
);
|
66 |
|
|
port(
|
67 |
|
|
x : in STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
68 |
|
|
start : in STD_LOGIC;
|
69 |
|
|
clk : in STD_LOGIC;
|
70 |
|
|
s_0 : in STD_LOGIC;
|
71 |
|
|
y_0 : in STD_LOGIC;
|
72 |
|
|
ready : out STD_LOGIC;
|
73 |
|
|
out_reg_en : out STD_LOGIC;
|
74 |
|
|
mux_mult_ctrl : out STD_LOGIC;
|
75 |
|
|
mux_4in_ctrl : out STD_LOGIC_VECTOR(1 downto 0)
|
76 |
|
|
);
|
77 |
|
|
end ModMultIter_SM;
|
78 |
|
|
|
79 |
|
|
architecture Behavioral of ModMultIter_SM is
|
80 |
|
|
|
81 |
|
|
signal state : multiplier_states := NOP;
|
82 |
|
|
signal next_state : multiplier_states := NOP;
|
83 |
|
|
signal position_counter : STD_LOGIC_VECTOR(word_binary downto 0) := (others => '0');
|
84 |
|
|
signal shift_reg : STD_LOGIC_VECTOR(word_size - 1 downto 0) := (others => '0');
|
85 |
|
|
|
86 |
|
|
signal q : STD_LOGIC;
|
87 |
|
|
|
88 |
|
|
begin
|
89 |
|
|
q <= (shift_reg(0) and y_0) xor s_0;
|
90 |
|
|
mux_4in_ctrl <= shift_reg(0) & q;
|
91 |
|
|
|
92 |
|
|
SM : process(state, start, position_counter)
|
93 |
|
|
begin
|
94 |
|
|
case state is
|
95 |
|
|
-- Prepare for the Montgomery iterations
|
96 |
|
|
when NOP =>
|
97 |
|
|
ready <= '0';
|
98 |
|
|
if (start = '1') then
|
99 |
|
|
next_state <= CALCULATE_START;
|
100 |
|
|
out_reg_en <= '1';
|
101 |
|
|
mux_mult_ctrl <= '1';
|
102 |
|
|
else
|
103 |
|
|
out_reg_en <= '0';
|
104 |
|
|
mux_mult_ctrl <= '0';
|
105 |
|
|
next_state <= NOP;
|
106 |
|
|
end if;
|
107 |
|
|
-- State for the calculations of the Montgomery iterations
|
108 |
|
|
when CALCULATE_START =>
|
109 |
|
|
mux_mult_ctrl <= '1';
|
110 |
|
|
ready <= '0';
|
111 |
|
|
-- End of iterations (counter contains the 'word_size' number)
|
112 |
|
|
if (position_counter = (word_size - 1)) then
|
113 |
|
|
out_reg_en <= '0';
|
114 |
|
|
next_state <= STOP;
|
115 |
|
|
-- Calculation process
|
116 |
|
|
else
|
117 |
|
|
out_reg_en <= '1';
|
118 |
|
|
next_state <= CALCULATE_START;
|
119 |
|
|
end if;
|
120 |
|
|
-- End of the calculations
|
121 |
|
|
when STOP =>
|
122 |
|
|
ready <= '1';
|
123 |
|
|
mux_mult_ctrl <= '1';
|
124 |
|
|
out_reg_en <= '0';
|
125 |
|
|
if (start = '1') then
|
126 |
|
|
next_state <= STOP;
|
127 |
|
|
else
|
128 |
|
|
next_state <= NOP;
|
129 |
|
|
end if;
|
130 |
|
|
end case;
|
131 |
|
|
end process SM;
|
132 |
|
|
|
133 |
|
|
-- Shift register enabling proper calculations of the all Montgomery iterations
|
134 |
|
|
shift : process (clk, state)
|
135 |
|
|
begin
|
136 |
|
|
if (clk = '0' and clk'Event) then
|
137 |
|
|
if (state = CALCULATE_START) then
|
138 |
|
|
shift_reg <= shift_reg(0) & shift_reg(word_size - 1 downto 1);
|
139 |
|
|
else
|
140 |
|
|
shift_reg <= x;
|
141 |
|
|
end if;
|
142 |
|
|
end if;
|
143 |
|
|
end process shift;
|
144 |
|
|
|
145 |
|
|
-- Process for the state change between each clock tick
|
146 |
|
|
state_control : process (clk, start)
|
147 |
|
|
begin
|
148 |
|
|
if (start = '0') then
|
149 |
|
|
state <= NOP;
|
150 |
|
|
elsif (clk = '1' and clk'Event) then
|
151 |
|
|
state <= next_state;
|
152 |
|
|
end if;
|
153 |
|
|
end process state_control;
|
154 |
|
|
|
155 |
|
|
-- Counter for controlling the number of the montgomery iterations during counting
|
156 |
|
|
couner_modifier : process (clk)
|
157 |
|
|
begin
|
158 |
|
|
if (clk = '1' and clk'Event) then
|
159 |
|
|
if (state = CALCULATE_START) then
|
160 |
|
|
position_counter <= position_counter + 1;
|
161 |
|
|
else
|
162 |
|
|
position_counter <= (others => '0');
|
163 |
|
|
end if;
|
164 |
|
|
end if;
|
165 |
|
|
end process couner_modifier;
|
166 |
|
|
end Behavioral;
|