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 |
|
|
---- This is TestBench for the Montgomery modular exponentiator ----
|
11 |
|
|
---- with the 512 bit width. ----
|
12 |
|
|
---- It takes four nubers - base, power, modulus and Montgomery ----
|
13 |
|
|
---- residuum (2^(2*word_length) mod N) as the input and results ----
|
14 |
|
|
---- the modular 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 |
|
|
ENTITY ModExp512bitTB IS
|
58 |
|
|
END ModExp512bitTB;
|
59 |
|
|
|
60 |
|
|
ARCHITECTURE behavior OF ModExp512bitTB IS
|
61 |
|
|
|
62 |
|
|
-- Component Declaration for the Unit Under Test (UUT)
|
63 |
|
|
|
64 |
|
|
COMPONENT ModExp
|
65 |
|
|
PORT(
|
66 |
|
|
input : in STD_LOGIC_VECTOR(511 downto 0);
|
67 |
|
|
ctrl : in STD_LOGIC_VECTOR(2 downto 0);
|
68 |
|
|
clk : in STD_LOGIC;
|
69 |
|
|
reset : in STD_LOGIC;
|
70 |
|
|
data_in_ready : in STD_LOGIC;
|
71 |
|
|
ready : out STD_LOGIC;
|
72 |
|
|
output : out STD_LOGIC_VECTOR(511 downto 0)
|
73 |
|
|
);
|
74 |
|
|
END COMPONENT;
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
--Inputs
|
78 |
|
|
signal input : STD_LOGIC_VECTOR(511 downto 0) := (others => '0');
|
79 |
|
|
signal ctrl : STD_LOGIC_VECTOR(2 downto 0) := (others => '0');
|
80 |
|
|
signal clk : STD_LOGIC := '0';
|
81 |
|
|
signal reset : STD_LOGIC := '0';
|
82 |
|
|
signal data_in_ready : STD_LOGIC := '0';
|
83 |
|
|
|
84 |
|
|
--Outputs
|
85 |
|
|
signal ready : STD_LOGIC;
|
86 |
|
|
signal output : STD_LOGIC_VECTOR(511 downto 0);
|
87 |
|
|
|
88 |
|
|
-- Clock period definitions
|
89 |
|
|
constant clk_period : time := 10 ns;
|
90 |
|
|
|
91 |
|
|
BEGIN
|
92 |
|
|
|
93 |
|
|
-- Instantiate the Unit Under Test (UUT)
|
94 |
|
|
uut: ModExp PORT MAP (
|
95 |
|
|
input => input,
|
96 |
|
|
ctrl => ctrl,
|
97 |
|
|
clk => clk,
|
98 |
|
|
reset => reset,
|
99 |
|
|
data_in_ready => data_in_ready,
|
100 |
|
|
ready => ready,
|
101 |
|
|
output => output
|
102 |
|
|
);
|
103 |
|
|
|
104 |
|
|
-- Clock process definitions
|
105 |
|
|
clk_process :process
|
106 |
|
|
begin
|
107 |
|
|
clk <= '1';
|
108 |
|
|
wait for clk_period/2;
|
109 |
|
|
clk <= '0';
|
110 |
|
|
wait for clk_period/2;
|
111 |
|
|
end process;
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
-- Stimulus process
|
115 |
|
|
stim_proc: process
|
116 |
|
|
begin
|
117 |
|
|
reset <= '1';
|
118 |
|
|
wait for 100 ns;
|
119 |
|
|
reset <= '0';
|
120 |
|
|
wait for clk_period*10;
|
121 |
|
|
|
122 |
|
|
---- Preparation for test case 1 -----------------
|
123 |
|
|
-- base = 409173825987017733751648542997566029938148046617392981389751408119740010106823408957031501223019018303621410623709446515603337041483208280918267736985 in decimal
|
124 |
|
|
-- = 0x1ffffffffffffffffffff003031300d060960864801650304020105000420f75db0d45d3189d910fc5d782745578c59481accf6f7cbf5e79bdecbe5233399 in hexadecimal
|
125 |
|
|
-- exponent = 4991398326204141236652697335767169457643189913066361675852469427068576791337775798287514344957972397666876518042551243608843475377858636774161719825165098 in decimal
|
126 |
|
|
-- = 0x5f4d7261a28d1e9c9a45059eb0ce9122f6840ec7878d2d2a87057fb15db61eac7a37af6b0cb80f0001870b2a29e350f7b052cc89f1c7fbed07926640d6926b2a in hexhexadecimal
|
127 |
|
|
-- modulus = 7630362531884975956392615644472323592768112181489355162005628253173318027895577525003064336256778044210380071348425604079063304117213210643679811834656203 in decimal
|
128 |
|
|
-- = 0x91b06f65a203bebb1cfa1b065cb2142e3771d113024a902f0829be8effe539ff6caa7c4b7f87e1913481e8c4f88a3f3e27a853179119aa029fe00e4c45a6b5cb in hexhexadecimal
|
129 |
|
|
-- expected_result = 1030188469358454649940099943953262093153216946958355916901057176262906329079894663437512624898962713254938994365603039233579679436863344699542897702118673 in decimal,
|
130 |
|
|
-- in hex 13ab74d318c919ec6faa10bea70211d4a981e7c31fc5205a8bb28e754ea59bcdd7459d6880758653918e72376c061177fdd51e72bece6815aa24001bda6ea511
|
131 |
|
|
-- power_mod(
|
132 |
|
|
-- 409173825987017733751648542997566029938148046617392981389751408119740010106823408957031501223019018303621410623709446515603337041483208280918267736985,
|
133 |
|
|
-- 4991398326204141236652697335767169457643189913066361675852469427068576791337775798287514344957972397666876518042551243608843475377858636774161719825165098,
|
134 |
|
|
-- 7630362531884975956392615644472323592768112181489355162005628253173318027895577525003064336256778044210380071348425604079063304117213210643679811834656203
|
135 |
|
|
-- ) =
|
136 |
|
|
-- = 1030188469358454649940099943953262093153216946958355916901057176262906329079894663437512624898962713254938994365603039233579679436863344699542897702118673
|
137 |
|
|
-- = 13ab74d318c919ec6faa10bea70211d4a981e7c31fc5205a8bb28e754ea59bcdd7459d6880758653918e72376c061177fdd51e72bece6815aa24001bda6ea511 in hexadecimal
|
138 |
|
|
-- where 1398454690893823236632472980512935706632382980363069616905016603014572888067778885889245016848922097099694154000460402372958600055088633374563202044624216 is the residuum
|
139 |
|
|
--------------------------------------------------
|
140 |
|
|
|
141 |
|
|
data_in_ready <= '1';
|
142 |
|
|
ctrl <= mn_read_base;
|
143 |
|
|
input <= x"0001ffffffffffffffffffff003031300d060960864801650304020105000420f75db0d45d3189d910fc5d782745578c59481accf6f7cbf5e79bdecbe5233399";
|
144 |
|
|
wait for clk_period*2;
|
145 |
|
|
|
146 |
|
|
ctrl <= mn_read_modulus;
|
147 |
|
|
input <= x"91b06f65a203bebb1cfa1b065cb2142e3771d113024a902f0829be8effe539ff6caa7c4b7f87e1913481e8c4f88a3f3e27a853179119aa029fe00e4c45a6b5cb";
|
148 |
|
|
wait for clk_period*2;
|
149 |
|
|
|
150 |
|
|
ctrl <= mn_read_exponent;
|
151 |
|
|
input <= x"5f4d7261a28d1e9c9a45059eb0ce9122f6840ec7878d2d2a87057fb15db61eac7a37af6b0cb80f0001870b2a29e350f7b052cc89f1c7fbed07926640d6926b2a";
|
152 |
|
|
wait for clk_period*2;
|
153 |
|
|
|
154 |
|
|
ctrl <= mn_read_residuum;
|
155 |
|
|
input <= "00011010101100111000000100001111010111001110011000001010110100001111011110111000011110111011111111011111011001111011010101010101010110010011001000010110100100100000010000100101001111011100001101111011001011010100011101100100011100101001110000101011100100101110111100101110011111101000111100101010100010000100011111010101111000100111101101011011000010111010011011000100000101001000111011010110110100001111001100001110001111111110000000011001010101111000101010101000110111011000110110110000000100010111110101011000";
|
156 |
|
|
wait for clk_period*2;
|
157 |
|
|
|
158 |
|
|
ctrl <= mn_count_power;
|
159 |
|
|
|
160 |
|
|
report "Please wait. It may take up ro few minutes..." severity note;
|
161 |
|
|
|
162 |
|
|
wait until ready = '1' and clk = '0';
|
163 |
|
|
|
164 |
|
|
if output /= x"13ab74d318c919ec6faa10bea70211d4a981e7c31fc5205a8bb28e754ea59bcdd7459d6880758653918e72376c061177fdd51e72bece6815aa24001bda6ea511" then
|
165 |
|
|
report "RESULT MISMATCH! Test case 1 failed" severity ERROR;
|
166 |
|
|
assert false severity failure;
|
167 |
|
|
else
|
168 |
|
|
report "Test case 1 successful" severity note;
|
169 |
|
|
end if;
|
170 |
|
|
|
171 |
|
|
ctrl <= mn_show_result;
|
172 |
|
|
wait for clk_period*10;
|
173 |
|
|
|
174 |
|
|
ctrl <= mn_prepare_for_data;
|
175 |
|
|
wait for clk_period*2;
|
176 |
|
|
|
177 |
|
|
---- Preparation for test case 2 -----------------
|
178 |
|
|
-- base = 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589431 in decimal
|
179 |
|
|
-- = 0x100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000037 in hexadecimal
|
180 |
|
|
-- exponent = 622376668989630299558359971768444342820680304013329676986135064534413345603604938346345762083389451304101819605682193623416033951320823027994905238921170 in decimal
|
181 |
|
|
-- = 0xbe21d214053f66c3e101fd875b531ecaccca3befca14d989ae2ffe4d6bbf1a3df0c694dc4c83af61ee3cf7c7bc97c9d6844d5d1fe428105082910c637c55fd2 in hexhexadecimal
|
182 |
|
|
-- modulus = 3351951982485649274893506249551461531869841455148098344430890360930446855046914914263767984168972974033957028381338463851007479808527777429670210341401251 in decimal
|
183 |
|
|
-- = 0x400000000000000000000000000000000000000000000000000000000302929200000000000000000000000000000000000000000000000000005af3fbdb72a3 in hexhexadecimal
|
184 |
|
|
-- expected_result = 1135574785903187283000914738069914842639275616893687122668359807022003618585980215260939798952644749528921700342000274265548842002316414917974647561961683 in decimal,
|
185 |
|
|
-- in hex 15ae92ed25cdbb29458414ad1a28fa35f5bfc311d7e1efedba753e48ccee1e9ff1d160714449bf6f85a0e3fe0784548b3c461ac5fbf28b7a1c3c83f4dff6c0d3
|
186 |
|
|
-- power_mod(
|
187 |
|
|
-- 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589431,
|
188 |
|
|
-- 622376668989630299558359971768444342820680304013329676986135064534413345603604938346345762083389451304101819605682193623416033951320823027994905238921170,
|
189 |
|
|
-- 3351951982485649274893506249551461531869841455148098344430890360930446855046914914263767984168972974033957028381338463851007479808527777429670210341401251
|
190 |
|
|
-- ) =
|
191 |
|
|
-- = 1135574785903187283000914738069914842639275616893687122668359807022003618585980215260939798952644749528921700342000274265548842002316414917974647561961683
|
192 |
|
|
-- = 15ae92ed25cdbb29458414ad1a28fa35f5bfc311d7e1efedba753e48ccee1e9ff1d160714449bf6f85a0e3fe0784548b3c461ac5fbf28b7a1c3c83f4dff6c0d3 in hexadecimal
|
193 |
|
|
-- where 3351951982485649274893506249551461531869841455148097408724357100071878499222574108103974817495155088879387961281773763412796138005544310585710276679277619 is the residuum
|
194 |
|
|
--------------------------------------------------
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
ctrl <= mn_read_base;
|
198 |
|
|
input <= x"00100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000037";
|
199 |
|
|
wait for clk_period*2;
|
200 |
|
|
|
201 |
|
|
ctrl <= mn_read_modulus;
|
202 |
|
|
input <= x"400000000000000000000000000000000000000000000000000000000302929200000000000000000000000000000000000000000000000000005af3fbdb72a3";
|
203 |
|
|
wait for clk_period*2;
|
204 |
|
|
|
205 |
|
|
ctrl <= mn_read_exponent;
|
206 |
|
|
input <= x"0be21d214053f66c3e101fd875b531ecaccca3befca14d989ae2ffe4d6bbf1a3df0c694dc4c83af61ee3cf7c7bc97c9d6844d5d1fe428105082910c637c55fd2";
|
207 |
|
|
wait for clk_period*2;
|
208 |
|
|
|
209 |
|
|
ctrl <= mn_read_residuum;
|
210 |
|
|
input <= "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001010100001100110001111100000111011101010001011101111101110101100000111111010100011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100110100000000001000000110110110010000010111000001100010111100011000101010110010110110111001110000110011";
|
211 |
|
|
wait for clk_period*2;
|
212 |
|
|
|
213 |
|
|
ctrl <= mn_count_power;
|
214 |
|
|
|
215 |
|
|
report "Please wait. It may take up ro few minutes..." severity note;
|
216 |
|
|
|
217 |
|
|
wait until ready = '1' and clk = '0';
|
218 |
|
|
|
219 |
|
|
if output /= x"15ae92ed25cdbb29458414ad1a28fa35f5bfc311d7e1efedba753e48ccee1e9ff1d160714449bf6f85a0e3fe0784548b3c461ac5fbf28b7a1c3c83f4dff6c0d3" then
|
220 |
|
|
report "RESULT MISMATCH! Test case 2 failed" severity ERROR;
|
221 |
|
|
assert false severity failure;
|
222 |
|
|
else
|
223 |
|
|
report "Test case 2 successful" severity note;
|
224 |
|
|
end if;
|
225 |
|
|
|
226 |
|
|
ctrl <= mn_show_result;
|
227 |
|
|
wait for clk_period*10;
|
228 |
|
|
|
229 |
|
|
ctrl <= mn_prepare_for_data;
|
230 |
|
|
wait for clk_period*2;
|
231 |
|
|
|
232 |
|
|
assert false severity failure;
|
233 |
|
|
end process;
|
234 |
|
|
|
235 |
|
|
END;
|