| 1 |
5 |
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 |
|
|
---- Montgomery modular exponentiator main module. It combines ----
|
| 11 |
|
|
---- all subomponents. It takes four numbers as the input: ----
|
| 12 |
|
|
---- base, power, modulus and Montgomery residuum ----
|
| 13 |
|
|
---- (2^(2*word_length) mod N) and results the modular ----
|
| 14 |
|
|
---- exponentiation A^B mod M. ----
|
| 15 |
|
|
---- In fact input data are read through one input controlled by ----
|
| 16 |
|
|
---- the ctrl input. ----
|
| 17 |
|
|
---- To Do: ----
|
| 18 |
|
|
---- ----
|
| 19 |
|
|
---- Author(s): ----
|
| 20 |
|
|
---- - Krzysztof Gajewski, gajos@opencores.org ----
|
| 21 |
|
|
---- k.gajewski@gmail.com ----
|
| 22 |
|
|
---- ----
|
| 23 |
|
|
-----------------------------------------------------------------------
|
| 24 |
|
|
---- ----
|
| 25 |
|
|
---- Copyright (C) 2014 Authors and OPENCORES.ORG ----
|
| 26 |
|
|
---- ----
|
| 27 |
|
|
---- This source file may be used and distributed without ----
|
| 28 |
|
|
---- restriction provided that this copyright statement is not ----
|
| 29 |
|
|
---- removed from the file and that any derivative work contains ----
|
| 30 |
|
|
---- the original copyright notice and the associated disclaimer. ----
|
| 31 |
|
|
---- ----
|
| 32 |
|
|
---- This source file is free software; you can redistribute it ----
|
| 33 |
|
|
---- and-or modify it under the terms of the GNU Lesser General ----
|
| 34 |
|
|
---- Public License as published by the Free Software Foundation; ----
|
| 35 |
|
|
---- either version 2.1 of the License, or (at your option) any ----
|
| 36 |
|
|
---- later version. ----
|
| 37 |
|
|
---- ----
|
| 38 |
|
|
---- This source is distributed in the hope that it will be ----
|
| 39 |
|
|
---- useful, but WITHOUT ANY WARRANTY; without even the implied ----
|
| 40 |
|
|
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ----
|
| 41 |
|
|
---- PURPOSE. See the GNU Lesser General Public License for more ----
|
| 42 |
|
|
---- details. ----
|
| 43 |
|
|
---- ----
|
| 44 |
|
|
---- You should have received a copy of the GNU Lesser General ----
|
| 45 |
|
|
---- Public License along with this source; if not, download it ----
|
| 46 |
|
|
---- from http://www.opencores.org/lgpl.shtml ----
|
| 47 |
|
|
---- ----
|
| 48 |
|
|
-----------------------------------------------------------------------
|
| 49 |
|
|
library IEEE;
|
| 50 |
|
|
use work.properties.ALL;
|
| 51 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
| 52 |
|
|
|
| 53 |
|
|
-- Uncomment the following library declaration if using
|
| 54 |
|
|
-- arithmetic functions with Signed or Unsigned values
|
| 55 |
|
|
--use IEEE.NUMERIC_STD.ALL;
|
| 56 |
|
|
|
| 57 |
|
|
-- Uncomment the following library declaration if instantiating
|
| 58 |
|
|
-- any Xilinx primitives in this code.
|
| 59 |
|
|
--library UNISIM;
|
| 60 |
|
|
--use UNISIM.VComponents.all;
|
| 61 |
|
|
|
| 62 |
|
|
entity ModExp is
|
| 63 |
|
|
generic (
|
| 64 |
|
|
word_size : integer := WORD_LENGTH;
|
| 65 |
|
|
word_binary : integer := WORD_INTEGER
|
| 66 |
|
|
);
|
| 67 |
|
|
Port (
|
| 68 |
|
|
input : in STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 69 |
|
|
ctrl : in STD_LOGIC_VECTOR(2 downto 0);
|
| 70 |
|
|
clk : in STD_LOGIC;
|
| 71 |
|
|
reset : in STD_LOGIC;
|
| 72 |
|
|
data_in_ready : in STD_LOGIC;
|
| 73 |
|
|
ready : out STD_LOGIC;
|
| 74 |
|
|
output : out STD_LOGIC_VECTOR(word_size - 1 downto 0)
|
| 75 |
|
|
);
|
| 76 |
|
|
end ModExp;
|
| 77 |
|
|
|
| 78 |
|
|
architecture Behavioral of ModExp is
|
| 79 |
|
|
|
| 80 |
|
|
-- Montgomery modular multiplier component
|
| 81 |
|
|
component ModularMultiplierIterative is
|
| 82 |
|
|
generic (
|
| 83 |
|
|
word_size : integer := WORD_LENGTH
|
| 84 |
|
|
);
|
| 85 |
|
|
port (
|
| 86 |
|
|
A : in STD_LOGIC_VECTOR(word_size - 1 downto 0); -- multiplicand
|
| 87 |
|
|
B : in STD_LOGIC_VECTOR(word_size - 1 downto 0); -- multiplier
|
| 88 |
|
|
M : in STD_LOGIC_VECTOR(word_size - 1 downto 0); -- modulus
|
| 89 |
|
|
start : in STD_LOGIC;
|
| 90 |
|
|
product : out STD_LOGIC_VECTOR(word_size - 1 downto 0); -- product
|
| 91 |
|
|
ready : out STD_LOGIC;
|
| 92 |
|
|
clk : in STD_LOGIC
|
| 93 |
|
|
);
|
| 94 |
|
|
end component ModularMultiplierIterative;
|
| 95 |
|
|
|
| 96 |
|
|
-- Block memory component generated through ISE
|
| 97 |
|
|
-- It is used like multiple cell register
|
| 98 |
|
|
COMPONENT blockMemory
|
| 99 |
|
|
PORT (
|
| 100 |
|
|
clka : in STD_LOGIC;
|
| 101 |
|
|
rsta : in STD_LOGIC;
|
| 102 |
|
|
wea : in STD_LOGIC_VECTOR(0 DOWNTO 0);
|
| 103 |
|
|
addra : in STD_LOGIC_VECTOR(3 DOWNTO 0);
|
| 104 |
|
|
dina : in STD_LOGIC_VECTOR(word_size - 1 DOWNTO 0);
|
| 105 |
|
|
douta : out STD_LOGIC_VECTOR(word_size - 1 DOWNTO 0)
|
| 106 |
|
|
);
|
| 107 |
|
|
END COMPONENT;
|
| 108 |
|
|
|
| 109 |
|
|
-- Register
|
| 110 |
|
|
component Reg is
|
| 111 |
|
|
generic(
|
| 112 |
|
|
word_size : integer := WORD_LENGTH
|
| 113 |
|
|
);
|
| 114 |
|
|
port(
|
| 115 |
|
|
input : in STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 116 |
|
|
output : out STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 117 |
|
|
enable : in STD_LOGIC;
|
| 118 |
|
|
clk : in STD_LOGIC;
|
| 119 |
|
|
reset : in STD_LOGIC
|
| 120 |
|
|
);
|
| 121 |
|
|
end component Reg;
|
| 122 |
|
|
|
| 123 |
|
|
-- Multiplexer
|
| 124 |
|
|
component MontMult4inMux is
|
| 125 |
|
|
generic (
|
| 126 |
|
|
word_size : integer := WORD_LENGTH - 1
|
| 127 |
|
|
);
|
| 128 |
|
|
port (
|
| 129 |
|
|
ctrl : in STD_LOGIC_VECTOR(1 downto 0);
|
| 130 |
|
|
zero : in STD_LOGIC_VECTOR(word_size downto 0);
|
| 131 |
|
|
M : in STD_LOGIC_VECTOR(word_size downto 0);
|
| 132 |
|
|
Y : in STD_LOGIC_VECTOR(word_size downto 0);
|
| 133 |
|
|
YplusM : in STD_LOGIC_VECTOR(word_size downto 0);
|
| 134 |
|
|
output : out STD_LOGIC_VECTOR(word_size downto 0)
|
| 135 |
|
|
);
|
| 136 |
|
|
end component MontMult4inMux;
|
| 137 |
|
|
|
| 138 |
|
|
-- State machine
|
| 139 |
|
|
component ModExpSM is
|
| 140 |
|
|
generic(
|
| 141 |
|
|
word_size : integer := WORD_LENGTH;
|
| 142 |
|
|
word_binary : integer := WORD_INTEGER
|
| 143 |
|
|
);
|
| 144 |
|
|
port (
|
| 145 |
|
|
data_in_ready : in STD_LOGIC;
|
| 146 |
|
|
clk : in STD_LOGIC;
|
| 147 |
|
|
exp_ctrl : in STD_LOGIC_VECTOR(2 downto 0);
|
| 148 |
|
|
reset : in STD_LOGIC;
|
| 149 |
|
|
in_mux_control : out STD_LOGIC_VECTOR(1 downto 0);
|
| 150 |
|
|
-- finalizer end status
|
| 151 |
|
|
ready : out STD_LOGIC;
|
| 152 |
|
|
-- control for multiplier
|
| 153 |
|
|
modMultStart : out STD_LOGIC;
|
| 154 |
|
|
modMultReady : in STD_LOGIC;
|
| 155 |
|
|
-- control for memory and registers
|
| 156 |
|
|
addr_dataA : out STD_LOGIC_VECTOR(3 downto 0);
|
| 157 |
|
|
addr_dataB : out STD_LOGIC_VECTOR(3 downto 0);
|
| 158 |
|
|
regData_EnA : out STD_LOGIC_VECTOR(0 downto 0);
|
| 159 |
|
|
regData_EnB : out STD_LOGIC_VECTOR(0 downto 0);
|
| 160 |
|
|
regData_EnC : out STD_LOGIC;
|
| 161 |
|
|
regData_EnExponent : out STD_LOGIC;
|
| 162 |
|
|
ExponentData : in STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 163 |
|
|
memory_reset : out STD_LOGIC
|
| 164 |
|
|
);
|
| 165 |
|
|
end component ModExpSM;
|
| 166 |
|
|
|
| 167 |
|
|
-- data registers signals
|
| 168 |
|
|
signal addr_dataA : STD_LOGIC_VECTOR(3 downto 0);
|
| 169 |
|
|
signal addr_dataB : STD_LOGIC_VECTOR(3 downto 0);
|
| 170 |
|
|
|
| 171 |
|
|
signal memDataLoadA : STD_LOGIC_VECTOR(0 downto 0);
|
| 172 |
|
|
signal memDataLoadB : STD_LOGIC_VECTOR(0 downto 0);
|
| 173 |
|
|
signal memDataLoadC : STD_LOGIC;
|
| 174 |
|
|
signal memDataLoadExponent : STD_LOGIC;
|
| 175 |
|
|
|
| 176 |
|
|
signal memDataA : STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 177 |
|
|
signal memDataB : STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 178 |
|
|
signal memDataC : STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 179 |
|
|
signal memDataExponent : STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 180 |
|
|
signal memoryIn : STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 181 |
|
|
|
| 182 |
|
|
signal in_mux_control : STD_LOGIC_VECTOR(1 downto 0);
|
| 183 |
|
|
|
| 184 |
|
|
-- signal for multiplier
|
| 185 |
|
|
signal multStart : STD_LOGIC;
|
| 186 |
|
|
signal multReady : STD_LOGIC;
|
| 187 |
|
|
signal modMultToBuffer : STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 188 |
|
|
|
| 189 |
|
|
signal zero : STD_LOGIC_VECTOR(word_size - 1 downto 0) := (others => '0');
|
| 190 |
|
|
signal one : STD_LOGIC_VECTOR(word_size - 1 downto 0) := (0 => '1', others => '0');
|
| 191 |
|
|
|
| 192 |
|
|
signal memory_reset : STD_LOGIC;
|
| 193 |
|
|
|
| 194 |
|
|
begin
|
| 195 |
|
|
-- connections between components
|
| 196 |
|
|
zero <= (others => '0');
|
| 197 |
|
|
one <= (0 => '1', others => '0');
|
| 198 |
|
|
|
| 199 |
|
|
-- Montgomery modular multiplier component
|
| 200 |
|
|
modMult : ModularMultiplierIterative
|
| 201 |
|
|
port map (
|
| 202 |
|
|
A => memDataA,
|
| 203 |
|
|
B => memDataB,
|
| 204 |
|
|
M => memDataC,
|
| 205 |
|
|
start => multStart,
|
| 206 |
|
|
product => modMultToBuffer,
|
| 207 |
|
|
ready => multReady,
|
| 208 |
|
|
clk => clk
|
| 209 |
|
|
);
|
| 210 |
|
|
|
| 211 |
|
|
-- Multiplexer
|
| 212 |
|
|
mux : MontMult4inMux
|
| 213 |
|
|
port map (
|
| 214 |
|
|
ctrl => in_mux_control,
|
| 215 |
|
|
zero => zero,
|
| 216 |
|
|
M => one,
|
| 217 |
|
|
Y => modMultToBuffer,
|
| 218 |
|
|
YplusM => input,
|
| 219 |
|
|
output => memoryIn
|
| 220 |
|
|
);
|
| 221 |
|
|
|
| 222 |
|
|
-- Block memory for the first input of the multiplier
|
| 223 |
|
|
memoryA : blockMemory
|
| 224 |
|
|
port map (
|
| 225 |
|
|
clka => clk,
|
| 226 |
|
|
rsta => memory_reset,
|
| 227 |
|
|
wea => memDataLoadA,
|
| 228 |
|
|
addra => addr_dataA,
|
| 229 |
|
|
dina => memoryIn,
|
| 230 |
|
|
douta => memDataA
|
| 231 |
|
|
);
|
| 232 |
|
|
|
| 233 |
|
|
-- Block memory for the second input of the multiplier
|
| 234 |
|
|
memoryB : blockMemory
|
| 235 |
|
|
port map (
|
| 236 |
|
|
clka => clk,
|
| 237 |
|
|
rsta => memory_reset,
|
| 238 |
|
|
wea => memDataLoadB,
|
| 239 |
|
|
addra => addr_dataB,
|
| 240 |
|
|
dina => memoryIn,
|
| 241 |
|
|
douta => memDataB
|
| 242 |
|
|
);
|
| 243 |
|
|
|
| 244 |
|
|
-- Register for the modulus for the multiplier
|
| 245 |
|
|
memoryModulus : Reg
|
| 246 |
|
|
port map (
|
| 247 |
|
|
input => memoryIn,
|
| 248 |
|
|
output => memDataC,
|
| 249 |
|
|
enable => memDataLoadC,
|
| 250 |
|
|
clk => clk,
|
| 251 |
|
|
reset => memory_reset
|
| 252 |
|
|
);
|
| 253 |
|
|
|
| 254 |
|
|
-- Register for the exponent - it feeds also the state machine for the control of the exponentiation process
|
| 255 |
|
|
memoryExponent : Reg
|
| 256 |
|
|
port map (
|
| 257 |
|
|
input => memoryIn,
|
| 258 |
|
|
output => memDataExponent,
|
| 259 |
|
|
enable => memDataLoadExponent,
|
| 260 |
|
|
clk => clk,
|
| 261 |
|
|
reset => memory_reset
|
| 262 |
|
|
);
|
| 263 |
|
|
|
| 264 |
|
|
-- State machine of the Montgomery modular exponentiator
|
| 265 |
|
|
stateMachine : ModExpSM
|
| 266 |
|
|
port map(
|
| 267 |
|
|
data_in_ready => data_in_ready,
|
| 268 |
|
|
clk => clk,
|
| 269 |
|
|
exp_ctrl => ctrl,
|
| 270 |
|
|
reset => reset,
|
| 271 |
|
|
in_mux_control => in_mux_control,
|
| 272 |
|
|
ready => ready,
|
| 273 |
|
|
modMultStart => multStart,
|
| 274 |
|
|
modMultReady => multReady,
|
| 275 |
|
|
addr_dataA => addr_dataA,
|
| 276 |
|
|
addr_dataB => addr_dataB,
|
| 277 |
|
|
regData_EnA => memDataLoadA,
|
| 278 |
|
|
regData_EnB => memDataLoadB,
|
| 279 |
|
|
regData_EnC => memDataLoadC,
|
| 280 |
|
|
regData_EnExponent => memDataLoadExponent,
|
| 281 |
|
|
ExponentData => memDataExponent,
|
| 282 |
|
|
memory_reset => memory_reset
|
| 283 |
|
|
);
|
| 284 |
|
|
|
| 285 |
|
|
output <= memDataA;
|
| 286 |
|
|
|
| 287 |
|
|
end Behavioral;
|