| 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 |
|
|
---- Montgomery modular multiplier main module. It combines all ----
|
| 11 |
|
|
---- subomponents. It takes two numbers and modulus as the input ----
|
| 12 |
|
|
---- and returns the Montgomery product A*B*(R^{-1}) mod M ----
|
| 13 |
|
|
---- where R^{-1} is the modular multiplicative inverse. ----
|
| 14 |
|
|
---- R*R^{-1} == 1 mod M ----
|
| 15 |
|
|
---- R = 2^word_length mod M ----
|
| 16 |
|
|
---- and word_length is the binary width of the ----
|
| 17 |
6 |
gajos |
---- operated word (in this case 32, 64 or 512 bit) ----
|
| 18 |
3 |
gajos |
---- To Do: ----
|
| 19 |
|
|
---- ----
|
| 20 |
|
|
---- Author(s): ----
|
| 21 |
|
|
---- - Krzysztof Gajewski, gajos@opencores.org ----
|
| 22 |
|
|
---- k.gajewski@gmail.com ----
|
| 23 |
|
|
---- ----
|
| 24 |
|
|
-----------------------------------------------------------------------
|
| 25 |
|
|
---- ----
|
| 26 |
6 |
gajos |
---- Copyright (C) 2019 Authors and OPENCORES.ORG ----
|
| 27 |
3 |
gajos |
---- ----
|
| 28 |
|
|
---- This source file may be used and distributed without ----
|
| 29 |
|
|
---- restriction provided that this copyright statement is not ----
|
| 30 |
|
|
---- removed from the file and that any derivative work contains ----
|
| 31 |
|
|
---- the original copyright notice and the associated disclaimer. ----
|
| 32 |
|
|
---- ----
|
| 33 |
|
|
---- This source file is free software; you can redistribute it ----
|
| 34 |
|
|
---- and-or modify it under the terms of the GNU Lesser General ----
|
| 35 |
|
|
---- Public License as published by the Free Software Foundation; ----
|
| 36 |
|
|
---- either version 2.1 of the License, or (at your option) any ----
|
| 37 |
|
|
---- later version. ----
|
| 38 |
|
|
---- ----
|
| 39 |
|
|
---- This source is distributed in the hope that it will be ----
|
| 40 |
|
|
---- useful, but WITHOUT ANY WARRANTY; without even the implied ----
|
| 41 |
|
|
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ----
|
| 42 |
|
|
---- PURPOSE. See the GNU Lesser General Public License for more ----
|
| 43 |
|
|
---- details. ----
|
| 44 |
|
|
---- ----
|
| 45 |
|
|
---- You should have received a copy of the GNU Lesser General ----
|
| 46 |
|
|
---- Public License along with this source; if not, download it ----
|
| 47 |
|
|
---- from http://www.opencores.org/lgpl.shtml ----
|
| 48 |
|
|
---- ----
|
| 49 |
|
|
-----------------------------------------------------------------------
|
| 50 |
|
|
library IEEE;
|
| 51 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
| 52 |
|
|
use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
| 53 |
|
|
use IEEE.NUMERIC_STD.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 |
|
|
entity ModularMultiplierIterative is
|
| 66 |
|
|
generic (
|
| 67 |
|
|
word_size : integer := WORD_LENGTH
|
| 68 |
|
|
);
|
| 69 |
|
|
port (
|
| 70 |
|
|
A : in STD_LOGIC_VECTOR(word_size - 1 downto 0); -- multiplicand
|
| 71 |
|
|
B : in STD_LOGIC_VECTOR(word_size - 1 downto 0); -- multiplier
|
| 72 |
|
|
M : in STD_LOGIC_VECTOR(word_size - 1 downto 0); -- modulus
|
| 73 |
|
|
start : in STD_LOGIC;
|
| 74 |
|
|
product : out STD_LOGIC_VECTOR(word_size - 1 downto 0); -- product
|
| 75 |
|
|
ready : out STD_LOGIC;
|
| 76 |
|
|
clk : in STD_LOGIC
|
| 77 |
|
|
);
|
| 78 |
|
|
end ModularMultiplierIterative;
|
| 79 |
|
|
|
| 80 |
|
|
architecture Behavioral of ModularMultiplierIterative is
|
| 81 |
|
|
|
| 82 |
|
|
-- Multiplexer
|
| 83 |
|
|
component MontMult4inMux is
|
| 84 |
|
|
generic (
|
| 85 |
|
|
word_size : integer := WORD_LENGTH
|
| 86 |
|
|
);
|
| 87 |
|
|
port (
|
| 88 |
|
|
ctrl : in STD_LOGIC_VECTOR(1 downto 0);
|
| 89 |
|
|
zero : in STD_LOGIC_VECTOR(word_size downto 0);
|
| 90 |
|
|
M : in STD_LOGIC_VECTOR(word_size downto 0);
|
| 91 |
|
|
Y : in STD_LOGIC_VECTOR(word_size downto 0);
|
| 92 |
|
|
YplusM : in STD_LOGIC_VECTOR(word_size downto 0);
|
| 93 |
|
|
output : out STD_LOGIC_VECTOR(word_size downto 0)
|
| 94 |
|
|
);
|
| 95 |
|
|
end component MontMult4inMux;
|
| 96 |
|
|
|
| 97 |
|
|
-- State machine
|
| 98 |
|
|
component ModMultIter_SM is
|
| 99 |
|
|
generic (
|
| 100 |
|
|
word_size : integer := WORD_LENGTH
|
| 101 |
|
|
);
|
| 102 |
|
|
port(
|
| 103 |
|
|
x : in STD_LOGIC_VECTOR(word_size - 1 downto 0);
|
| 104 |
|
|
start : in STD_LOGIC;
|
| 105 |
|
|
clk : in STD_LOGIC;
|
| 106 |
|
|
s_0 : in STD_LOGIC;
|
| 107 |
|
|
y_0 : in STD_LOGIC;
|
| 108 |
|
|
ready : out STD_LOGIC;
|
| 109 |
|
|
out_reg_en : out STD_LOGIC;
|
| 110 |
|
|
mux_mult_ctrl : out STD_LOGIC;
|
| 111 |
|
|
mux_4in_ctrl : out STD_LOGIC_VECTOR(1 downto 0)
|
| 112 |
|
|
);
|
| 113 |
|
|
end component ModMultIter_SM;
|
| 114 |
|
|
|
| 115 |
|
|
-- Signals
|
| 116 |
|
|
signal Mi : STD_LOGIC_VECTOR(word_size downto 0);
|
| 117 |
|
|
signal Yi : STD_LOGIC_VECTOR(word_size downto 0);
|
| 118 |
|
|
signal sumYM : STD_LOGIC_VECTOR(word_size downto 0);
|
| 119 |
|
|
signal zero_sig : STD_LOGIC_VECTOR(word_size downto 0) := (others => '0');
|
| 120 |
|
|
signal four_in_mux_out : STD_LOGIC_VECTOR(word_size downto 0);
|
| 121 |
|
|
|
| 122 |
|
|
signal mux_4in_ctrl_sig : STD_LOGIC_VECTOR(1 downto 0);
|
| 123 |
|
|
signal mult_mux_ctrl_sig : STD_LOGIC;
|
| 124 |
|
|
|
| 125 |
|
|
signal mult_mux_out : STD_LOGIC_VECTOR(word_size downto 0);
|
| 126 |
|
|
signal out_reg_sig : STD_LOGIC_VECTOR(word_size downto 0);
|
| 127 |
|
|
signal product_sig : STD_LOGIC_VECTOR(word_size downto 0);
|
| 128 |
|
|
signal out_en : STD_LOGIC;
|
| 129 |
|
|
|
| 130 |
|
|
signal sum_mult_out : STD_LOGIC_VECTOR(word_size + 1 downto 0);
|
| 131 |
|
|
signal sum_div_2 : STD_LOGIC_VECTOR(word_size downto 0);
|
| 132 |
|
|
|
| 133 |
|
|
begin
|
| 134 |
|
|
zero_sig <= (others => '0'); -- '0'
|
| 135 |
|
|
-- 'widening' to store the intermediate steps
|
| 136 |
|
|
Mi <= '0' & M;
|
| 137 |
|
|
Yi <= '0' & B;
|
| 138 |
|
|
|
| 139 |
|
|
-- Operations needed to compute the Montgomery multiplications
|
| 140 |
|
|
sum_div_2 <= sum_mult_out(word_size + 1 downto 1);
|
| 141 |
|
|
sum_mult_out <= ('0' & four_in_mux_out) + ('0' & mult_mux_out);
|
| 142 |
|
|
sumYM <= ('0' & B) + ('0' & M);
|
| 143 |
|
|
|
| 144 |
|
|
-- Multiplexer component
|
| 145 |
|
|
four_in_mux : MontMult4inMux port map(
|
| 146 |
|
|
ctrl => mux_4in_ctrl_sig, zero => zero_sig, M => Mi, Y => Yi,
|
| 147 |
|
|
YplusM => sumYM, output => four_in_mux_out
|
| 148 |
|
|
);
|
| 149 |
|
|
|
| 150 |
|
|
-- Two input asynchronuos multiplexer for output 'not clear' code due to
|
| 151 |
|
|
-- 'historical works'
|
| 152 |
|
|
mult_mux_out <= (others => '0') when (mult_mux_ctrl_sig = '0') else
|
| 153 |
|
|
out_reg_sig;
|
| 154 |
|
|
|
| 155 |
|
|
-- State machine
|
| 156 |
|
|
state_machine : ModMultIter_SM port map(
|
| 157 |
|
|
x => A,
|
| 158 |
|
|
start => start,
|
| 159 |
|
|
clk => clk,
|
| 160 |
|
|
s_0 => out_reg_sig(0),
|
| 161 |
|
|
y_0 => B(0),
|
| 162 |
|
|
ready => ready,
|
| 163 |
|
|
out_reg_en => out_en,
|
| 164 |
|
|
mux_mult_ctrl => mult_mux_ctrl_sig,
|
| 165 |
|
|
mux_4in_ctrl => mux_4in_ctrl_sig
|
| 166 |
|
|
);
|
| 167 |
|
|
|
| 168 |
|
|
-- Register like structure for signal synchronous work
|
| 169 |
|
|
clock : process(clk, start)
|
| 170 |
|
|
begin
|
| 171 |
|
|
if (clk = '1' and clk'Event) then
|
| 172 |
|
|
if (start = '0') then
|
| 173 |
|
|
out_reg_sig <= (others => '0');
|
| 174 |
|
|
elsif out_en = '1' then
|
| 175 |
|
|
out_reg_sig <= sum_div_2;
|
| 176 |
|
|
end if;
|
| 177 |
|
|
end if;
|
| 178 |
|
|
end process clock;
|
| 179 |
|
|
|
| 180 |
|
|
-- One additional 'subtract' component which was added after
|
| 181 |
|
|
-- first experiments with Montgomery multiplication. It was
|
| 182 |
|
|
-- observed that sometimes intermediate step can be higher
|
| 183 |
|
|
-- than modulus. In this situation 'M' substraction is
|
| 184 |
|
|
-- compulsory
|
| 185 |
|
|
product_proc : process(clk, Mi, out_reg_sig)
|
| 186 |
|
|
begin
|
| 187 |
|
|
if(out_reg_sig < ("0" & Mi)) then
|
| 188 |
|
|
product_sig <= out_reg_sig;
|
| 189 |
|
|
else
|
| 190 |
|
|
product_sig <= out_reg_sig - Mi;
|
| 191 |
|
|
end if;
|
| 192 |
|
|
end process product_proc;
|
| 193 |
|
|
product <= product_sig(word_size - 1 downto 0);
|
| 194 |
|
|
|
| 195 |
|
|
end Behavioral;
|