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 TestBench for the Montgomery modular multiplier ----
|
11 |
|
|
---- with the 512 bit width. ----
|
12 |
|
|
---- it takes two nubers and modulus as the input and results ----
|
13 |
|
|
---- the Montgomery product A*B*(R^{-1}) mod M ----
|
14 |
|
|
---- where R^{-1} is the modular multiplicative inverse. ----
|
15 |
|
|
---- R*R^{-1} == 1 mod M ----
|
16 |
|
|
---- R = 2^word_length mod M ----
|
17 |
|
|
---- and word_length is the binary width of the ----
|
18 |
|
|
---- operated word (in this case 512 bit) ----
|
19 |
|
|
---- To Do: ----
|
20 |
|
|
---- ----
|
21 |
|
|
---- Author(s): ----
|
22 |
|
|
---- - Krzysztof Gajewski, gajos@opencores.org ----
|
23 |
|
|
---- k.gajewski@gmail.com ----
|
24 |
|
|
---- ----
|
25 |
|
|
-----------------------------------------------------------------------
|
26 |
|
|
---- ----
|
27 |
|
|
---- Copyright (C) 2014 Authors and OPENCORES.ORG ----
|
28 |
|
|
---- ----
|
29 |
|
|
---- This source file may be used and distributed without ----
|
30 |
|
|
---- restriction provided that this copyright statement is not ----
|
31 |
|
|
---- removed from the file and that any derivative work contains ----
|
32 |
|
|
---- the original copyright notice and the associated disclaimer. ----
|
33 |
|
|
---- ----
|
34 |
|
|
---- This source file is free software; you can redistribute it ----
|
35 |
|
|
---- and-or modify it under the terms of the GNU Lesser General ----
|
36 |
|
|
---- Public License as published by the Free Software Foundation; ----
|
37 |
|
|
---- either version 2.1 of the License, or (at your option) any ----
|
38 |
|
|
---- later version. ----
|
39 |
|
|
---- ----
|
40 |
|
|
---- This source is distributed in the hope that it will be ----
|
41 |
|
|
---- useful, but WITHOUT ANY WARRANTY; without even the implied ----
|
42 |
|
|
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ----
|
43 |
|
|
---- PURPOSE. See the GNU Lesser General Public License for more ----
|
44 |
|
|
---- details. ----
|
45 |
|
|
---- ----
|
46 |
|
|
---- You should have received a copy of the GNU Lesser General ----
|
47 |
|
|
---- Public License along with this source; if not, download it ----
|
48 |
|
|
---- from http://www.opencores.org/lgpl.shtml ----
|
49 |
|
|
---- ----
|
50 |
|
|
-----------------------------------------------------------------------
|
51 |
|
|
LIBRARY ieee;
|
52 |
|
|
USE ieee.std_logic_1164.ALL;
|
53 |
|
|
|
54 |
|
|
-- Uncomment the following library declaration if using
|
55 |
|
|
-- arithmetic functions with Signed or Unsigned values
|
56 |
|
|
--USE ieee.numeric_std.ALL;
|
57 |
|
|
|
58 |
|
|
ENTITY ModularMultiplierIterative512bitTB IS
|
59 |
|
|
END ModularMultiplierIterative512bitTB;
|
60 |
|
|
|
61 |
|
|
ARCHITECTURE behavior OF ModularMultiplierIterative512bitTB IS
|
62 |
|
|
|
63 |
|
|
-- Component Declaration for the Unit Under Test (UUT)
|
64 |
|
|
|
65 |
|
|
COMPONENT ModularMultiplierIterative
|
66 |
|
|
PORT(
|
67 |
|
|
A : in STD_LOGIC_VECTOR(511 downto 0);
|
68 |
|
|
B : in STD_LOGIC_VECTOR(511 downto 0);
|
69 |
|
|
M : in STD_LOGIC_VECTOR(511 downto 0);
|
70 |
|
|
start : in STD_LOGIC;
|
71 |
|
|
product : out STD_LOGIC_VECTOR(511 downto 0);
|
72 |
|
|
ready : out STD_LOGIC;
|
73 |
|
|
clk : in STD_LOGIC
|
74 |
|
|
);
|
75 |
|
|
END COMPONENT;
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
--Inputs
|
79 |
|
|
signal A : STD_LOGIC_VECTOR(511 downto 0) := (others => '0');
|
80 |
|
|
signal B : STD_LOGIC_VECTOR(511 downto 0) := (others => '0');
|
81 |
|
|
signal M : STD_LOGIC_VECTOR(511 downto 0) := (others => '0');
|
82 |
|
|
signal start : STD_LOGIC := '0';
|
83 |
|
|
signal clk : STD_LOGIC := '0';
|
84 |
|
|
|
85 |
|
|
--Outputs
|
86 |
|
|
signal product : STD_LOGIC_VECTOR(511 downto 0);
|
87 |
|
|
signal ready : STD_LOGIC;
|
88 |
|
|
|
89 |
|
|
-- Clock period definitions
|
90 |
|
|
constant clk_period : time := 10 ns;
|
91 |
|
|
|
92 |
|
|
BEGIN
|
93 |
|
|
|
94 |
|
|
-- Instantiate the Unit Under Test (UUT)
|
95 |
|
|
uut: ModularMultiplierIterative PORT MAP (
|
96 |
|
|
A => A,
|
97 |
|
|
B => B,
|
98 |
|
|
M => M,
|
99 |
|
|
start => start,
|
100 |
|
|
product => product,
|
101 |
|
|
ready => ready,
|
102 |
|
|
clk => clk
|
103 |
|
|
);
|
104 |
|
|
|
105 |
|
|
-- Clock process definitions
|
106 |
|
|
clk_process :process
|
107 |
|
|
begin
|
108 |
|
|
clk <= '0';
|
109 |
|
|
wait for clk_period/2;
|
110 |
|
|
clk <= '1';
|
111 |
|
|
wait for clk_period/2;
|
112 |
|
|
end process;
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
-- Stimulus process
|
116 |
|
|
stim_proc: process
|
117 |
|
|
begin
|
118 |
|
|
-- hold reset state for 100 ns.
|
119 |
|
|
|
120 |
|
|
start <= '0';
|
121 |
|
|
wait for 100 ns;
|
122 |
|
|
|
123 |
|
|
---- Preparation for test case 1 -----------------
|
124 |
|
|
-- A = 1135574785903187283000914738069914842639275616893687122668359807022003618585980215260939798952644749528921700342000274265548842002316414917974647561961683 in decimal
|
125 |
|
|
-- B = 97927786390663519429528993360368267006249228136794892056090651513080073109454331808866772457049032741774590681339704155886317906072752116837364369820881 in decimal
|
126 |
|
|
-- M = 3351951982485649274893506249551461531869841455148098344430890360930446855046914914263767984168972974033957028381338463851007479808527777429670210341401251 in decimal
|
127 |
|
|
-- expected_result = 2228133496571818711622350692880669459929128102839647013792122413518929533298354919965858625663488002993791315812426542313874032336596139553001249634708855 in decimal,
|
128 |
|
|
-- in hex 2a8ae3c12ae96d6babce2e342ec7beeff5754a14e7c8e6057eeebf6dc1cb12145e26e97c874f8e05cfa6fcaf83240f90d2fd21b3f41016b74607c143e49eed77
|
129 |
|
|
-- mod(
|
130 |
|
|
-- 1135574785903187283000914738069914842639275616893687122668359807022003618585980215260939798952644749528921700342000274265548842002316414917974647561961683 *
|
131 |
|
|
-- 97927786390663519429528993360368267006249228136794892056090651513080073109454331808866772457049032741774590681339704155886317906072752116837364369820881 *
|
132 |
|
|
-- 2591367877621154684380773880291249237701602230100736077754314629198930824379666744084279080961590867282481555124997788427853751639203524473059719065731751 ,
|
133 |
|
|
-- 3351951982485649274893506249551461531869841455148098344430890360930446855046914914263767984168972974033957028381338463851007479808527777429670210341401251 ) =
|
134 |
|
|
-- = 2228133496571818711622350692880669459929128102839647013792122413518929533298354919965858625663488002993791315812426542313874032336596139553001249634708855
|
135 |
|
|
-- where 2591367877621154684380773880291249237701602230100736077754314629198930824379666744084279080961590867282481555124997788427853751639203524473059719065731751 is the inverse modulus
|
136 |
|
|
--------------------------------------------------
|
137 |
|
|
|
138 |
|
|
start <= '1';
|
139 |
|
|
-- A = 1135574785903187283000914738069914842639275616893687122668359807022003618585980215260939798952644749528921700342000274265548842002316414917974647561961683 in decimal
|
140 |
|
|
A <= "00010101101011101001001011101101001001011100110110111011001010010100010110000100000101001010110100011010001010001111101000110101111101011011111111000011000100011101011111100001111011111110110110111010011101010011111001001000110011001110111000011110100111111111000111010001011000000111000101000100010010011011111101101111100001011010000011100011111111100000011110000100010101001000101100111100010001100001101011000101111110111111001010001011011110100001110000111100100000111111010011011111111101101100000011010011";
|
141 |
|
|
-- B = 97927786390663519429528993360368267006249228136794892056090651513080073109454331808866772457049032741774590681339704155886317906072752116837364369820881 in decimal
|
142 |
|
|
B <= "00000001110111101010100100111010100000100100111111101001100100111011001111000010101011111001001001110011011010010100101000100010110011011101111000010011100100101000011010000110110010101101101100000111101000001111010101000110100001100011101110100011100111101100000001000110010110111001110111111110101000001110001000011001000001000000111100000001100110000100011100010011101110010100111110010111110001000110111010010010101101001111110000111001110000100111111111100011011101100000011110100100100000011000110011010001";
|
143 |
|
|
-- M = 3351951982485649274893506249551461531869841455148098344430890360930446855046914914263767984168972974033957028381338463851007479808527777429670210341401251 in decimal
|
144 |
|
|
M <= "01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000001010010010100100100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010110101111001111111011110110110111001010100011";
|
145 |
|
|
|
146 |
|
|
--wait for 600*clk_period;
|
147 |
|
|
-- Result = 2228133496571818711622350692880669459929128102839647013792122413518929533298354919965858625663488002993791315812426542313874032336596139553001249634708855 in decimal
|
148 |
|
|
|
149 |
|
|
wait until ready = '1' and clk = '0';
|
150 |
|
|
|
151 |
|
|
if product /= x"2a8ae3c12ae96d6babce2e342ec7beeff5754a14e7c8e6057eeebf6dc1cb12145e26e97c874f8e05cfa6fcaf83240f90d2fd21b3f41016b74607c143e49eed77" then
|
152 |
|
|
report "RESULT MISMATCH! Test case 1 failed" severity ERROR;
|
153 |
|
|
assert false severity failure;
|
154 |
|
|
else
|
155 |
|
|
report "Test case 1 successful" severity note;
|
156 |
|
|
end if;
|
157 |
|
|
|
158 |
|
|
start <= '0';
|
159 |
|
|
|
160 |
|
|
---- Preparation for test case 2 -----------------
|
161 |
|
|
-- A = 3351951982485649274893506249551461531869841455148098344430890360930441007518386744200468574541725856922507964546621512713438470702986642486608412251521039 in decimal
|
162 |
|
|
-- B = 97927786390663519429528993360368267006249228136794892056090651513080073109454331808866772457049032741774590681339704155886317906072752116837364369820881 in decimal
|
163 |
|
|
-- M = 6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042159 in decimal
|
164 |
|
|
-- expected_result = 5770539552593938046267215339235143056108840937616962443047031076129629580294766891795665005337423591502330655021878623252853392851503861478061794255888635 in decimal,
|
165 |
|
|
-- in hex 6e2dcf4e2226cb7a14afa007b0bafdf50d573776681c0cca8d7ff56515076baffd05eaa8ee73d63874a1df6d13e2bbc0aeb6dcd21d8ee10613df1e2e5e02e0fb
|
166 |
|
|
-- mod(
|
167 |
|
|
-- 3351951982485649274893506249551461531869841455148098344430890360930441007518386744200468574541725856922507964546621512713438470702986642486608412251521039 *
|
168 |
|
|
-- 97927786390663519429528993360368267006249228136794892056090651513080073109454331808866772457049032741774590681339704155886317906072752116837364369820881 *
|
169 |
|
|
-- 6311333012067573859934619875281580722169341118251824810685189958869028563705791257098179568281267604475713194506701767181158922314632507024334758203314465 ,
|
170 |
|
|
-- 6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042159 ) =
|
171 |
|
|
-- = 5770539552593938046267215339235143056108840937616962443047031076129629580294766891795665005337423591502330655021878623252853392851503861478061794255888635
|
172 |
|
|
-- where 6311333012067573859934619875281580722169341118251824810685189958869028563705791257098179568281267604475713194506701767181158922314632507024334758203314465 is the inverse modulus
|
173 |
|
|
--------------------------------------------------
|
174 |
|
|
|
175 |
|
|
-- A = 3351951982485649274893506249551461531869841455148098344430890360930441007518386744200468574541725856922507964546621512713438470702986642486608412251521039 in decimal
|
176 |
|
|
A <= "01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111";
|
177 |
|
|
-- B = 97927786390663519429528993360368267006249228136794892056090651513080073109454331808866772457049032741774590681339704155886317906072752116837364369820881 in decimal
|
178 |
|
|
B <= "00000001110111101010100100111010100000100100111111101001100100111011001111000010101011111001001001110011011010010100101000100010110011011101111000010011100100101000011010000110110010101101101100000111101000001111010101000110100001100011101110100011100111101100000001000110010110111001110111111110101000001110001000011001000001000000111100000001100110000100011100010011101110010100111110010111110001000110111010010010101101001111110000111001110000100111111111100011011101100000011110100100100000011000110011010001";
|
179 |
|
|
-- M = 6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042159 in decimal
|
180 |
|
|
M <= "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001101111";
|
181 |
|
|
wait for clk_period;
|
182 |
|
|
-- Result = 1075674849379283795 in decimal
|
183 |
|
|
start <= '1';
|
184 |
|
|
|
185 |
|
|
--wait for 600*clk_period;
|
186 |
|
|
wait until ready = '1' and clk = '0';
|
187 |
|
|
if product /= x"6e2dcf4e2226cb7a14afa007b0bafdf50d573776681c0cca8d7ff56515076baffd05eaa8ee73d63874a1df6d13e2bbc0aeb6dcd21d8ee10613df1e2e5e02e0fb" then
|
188 |
|
|
report "RESULT MISMATCH! Test case 2 failed" severity ERROR;
|
189 |
|
|
assert false severity failure;
|
190 |
|
|
else
|
191 |
|
|
report "Test case 2 successful" severity note;
|
192 |
|
|
end if;
|
193 |
|
|
|
194 |
|
|
assert false severity failure;
|
195 |
|
|
end process;
|
196 |
|
|
|
197 |
|
|
END;
|