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 |
4 |
gajos |
---- with the 32 bit width. ----
|
12 |
3 |
gajos |
---- 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 |
4 |
gajos |
---- operated word (in this case 32 bit) ----
|
19 |
3 |
gajos |
---- 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 ModularMultiplierIterative32bitTB IS
|
59 |
|
|
END ModularMultiplierIterative32bitTB;
|
60 |
|
|
|
61 |
|
|
ARCHITECTURE behavior OF ModularMultiplierIterative32bitTB IS
|
62 |
|
|
|
63 |
|
|
-- Component Declaration for the Unit Under Test (UUT)
|
64 |
|
|
|
65 |
|
|
COMPONENT ModularMultiplierIterative
|
66 |
|
|
PORT(
|
67 |
|
|
A : IN STD_LOGIC_VECTOR(31 downto 0);
|
68 |
|
|
B : IN STD_LOGIC_VECTOR(31 downto 0);
|
69 |
|
|
M : IN STD_LOGIC_VECTOR(31 downto 0);
|
70 |
|
|
start : IN STD_LOGIC;
|
71 |
|
|
product : OUT STD_LOGIC_VECTOR(31 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(31 downto 0) := (others => '0');
|
80 |
|
|
signal B : STD_LOGIC_VECTOR(31 downto 0) := (others => '0');
|
81 |
|
|
signal M : STD_LOGIC_VECTOR(31 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(31 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 = 1073741827 in decimal
|
125 |
|
|
-- B = 1876543287 in decimal
|
126 |
|
|
-- M = 2147483659 in decimal
|
127 |
|
|
-- expected_result = 1075674849379283795 in decimal, in hex 66e4624e
|
128 |
|
|
-- mod(1073741827*1876543287*1659419191, 2147483659) = 1726243406
|
129 |
|
|
-- where 2703402148733296366 is the inverse modulus
|
130 |
|
|
--------------------------------------------------
|
131 |
|
|
|
132 |
|
|
start <= '1';
|
133 |
|
|
-- A = 1073741827 in decimal
|
134 |
|
|
A <= "01000000000000000000000000000011";
|
135 |
|
|
-- B = 1876543210987 in decimal
|
136 |
|
|
B <= "01101111110110011100011100110111";
|
137 |
|
|
-- M = 2147483659 in decimal
|
138 |
|
|
M <= "10000000000000000000000000001011";
|
139 |
|
|
|
140 |
|
|
--wait for 80*clk_period;
|
141 |
|
|
wait until ready = '1' and clk = '0';
|
142 |
|
|
|
143 |
|
|
if product /= x"66e4624e" then
|
144 |
|
|
report "RESULT MISMATCH! Test case 1 failed" severity ERROR;
|
145 |
|
|
assert false severity failure;
|
146 |
|
|
else
|
147 |
|
|
report "Test case 1 successful" severity note;
|
148 |
|
|
end if;
|
149 |
|
|
|
150 |
|
|
start <= '0';
|
151 |
|
|
|
152 |
|
|
---- Preparation for test case 2 -----------------
|
153 |
|
|
-- A = 1073741826 in decimal
|
154 |
|
|
-- B = 1876543286 in decimal
|
155 |
|
|
-- M = 2147483659 in decimal
|
156 |
|
|
-- expected_result = 1075674849379283795 in decimal, in hex 66e4624e
|
157 |
|
|
-- mod(1073741826*1876543286*1659419191, 2147483659) = 1567508594
|
158 |
|
|
-- where 1659419191 is the inverse modulus
|
159 |
|
|
--------------------------------------------------
|
160 |
|
|
|
161 |
|
|
-- A = 1073741826 in decimal
|
162 |
|
|
A <= "01000000000000000000000000000010";
|
163 |
|
|
-- B = 1876543210986 in decimal
|
164 |
|
|
B <= "01101111110110011100011100110110";
|
165 |
|
|
-- M = 2147483659 in decimal
|
166 |
|
|
M <= "10000000000000000000000000001011";
|
167 |
|
|
wait for clk_period;
|
168 |
|
|
start <= '1';
|
169 |
|
|
--wait for 80*clk_period;
|
170 |
|
|
wait until ready = '1' and clk = '0';
|
171 |
|
|
|
172 |
|
|
if product /= x"5d6e4872" then
|
173 |
|
|
report "RESULT MISMATCH! Test case 2 failed" severity ERROR;
|
174 |
|
|
assert false severity failure;
|
175 |
|
|
else
|
176 |
|
|
report "Test case 2 successful" severity note;
|
177 |
|
|
end if;
|
178 |
|
|
|
179 |
|
|
assert false severity failure;
|
180 |
|
|
end process;
|
181 |
|
|
|
182 |
|
|
END;
|