OpenCores
URL https://opencores.org/ocsvn/BasicRSA/BasicRSA/trunk

Subversion Repositories BasicRSA

[/] [BasicRSA/] [trunk/] [rtl/] [vhdl/] [modmult.vhd] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 srmcqueen
----------------------------------------------------------------------
2
----                                                                                                                                                                    ----
3
---- Modular Multiplier                                                                                                                 ----
4
---- RSA Public Key Cryptography IP Core                                                                        ----
5
----                                                                                                                                                                    ----
6
---- This file is part of the BasicRSA project                                                  ----
7
---- http://www.opencores.org/                                                                                          ----
8
----                                                                                                                                                                    ----
9
---- To Do:                                                                                                                                             ----
10
---- - Speed and efficiency improvements                                                                        ----
11
---- - Possible revisions for good engineering/coding practices ----
12
----                                                                                                                                                                    ----
13
---- Author(s):                                                                                                                                         ----
14
---- - Steven R. McQueen, srmcqueen@opencores.org                                               ----
15
----                                                                                                                                                                    ----
16
----------------------------------------------------------------------
17
----                                                                                                                                                                    ----
18
---- Copyright (C) 2003 Steven R. McQueen                                                       ----
19
----                                                                                                                                                                    ----
20
---- This source file may be used and distributed without                       ----
21
---- restriction provided that this copyright statement is not  ----
22
---- removed from the file and that any derivative work contains        ----
23
---- the original copyright notice and the associated disclaimer. ----
24
----                                                                                                                                                                    ----
25
---- This source file is free software; you can redistribute it         ----
26
---- and/or modify it under the terms of the GNU Lesser General         ----
27
---- Public License as published by the Free Software Foundation; ----
28
---- either version 2.1 of the License, or (at your option) any         ----
29
---- later version.                                                                                                                             ----
30
----                                                                                                                                                                    ----
31
---- This source is distributed in the hope that it will be             ----
32
---- useful, but WITHOUT ANY WARRANTY; without even the implied         ----
33
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ----
34
---- PURPOSE. See the GNU Lesser General Public License for more        ----
35
---- details.                                                                                                                                           ----
36
----                                                                                                                                                                    ----
37
---- You should have received a copy of the GNU Lesser General  ----
38
---- Public License along with this source; if not, download it         ----
39
---- from http://www.opencores.org/lgpl.shtml                                                   ----
40
----                                                                                                                                                                    ----
41
----------------------------------------------------------------------
42
--
43
-- CVS Revision History
44
--
45
-- $Log: not supported by cvs2svn $
46
--
47
 
48
-- This module implements the modular multiplier for the RSA Public Key Cypher. It expects 
49
-- to receive a multiplicand on th MPAND bus, a multiplier on the MPLIER bus, and a modulus
50
-- on the MODULUS bus. The multiplier and multiplicand must have a value less than the modulus.
51
--
52
-- A Shift-and-Add algorithm is used in this module. For each bit of the multiplier, the
53
-- multiplicand value is shifted. For each '1' bit of the multiplier, the shifted multiplicand
54
-- value is added       to the product. To ensure that the product is always expressed as a remainder
55
-- two subtractions are performed on the product, P2 = P1-modulus, and P3 = P1-(2*modulus).
56
-- The high-order bits of these results are used to determine whether P sould be copied from
57
-- P1, P2, or P3. 
58
--
59
-- The operation ends when all '1' bits in the multiplier have been used.
60
--
61
-- Comments, questions and suggestions may be directed to the author at srmcqueen@mcqueentech.com.
62
 
63
 
64
library IEEE;
65
use IEEE.STD_LOGIC_1164.ALL;
66
use IEEE.STD_LOGIC_ARITH.ALL;
67
use IEEE.STD_LOGIC_UNSIGNED.ALL;
68
 
69
--  Uncomment the following lines to use the declarations that are
70
--  provided for instantiating Xilinx primitive components.
71
--library UNISIM;
72
--use UNISIM.VComponents.all;
73
 
74
entity modmult is
75
        Generic (MPWID: integer := 32);
76
    Port ( mpand : in std_logic_vector(MPWID-1 downto 0);
77
           mplier : in std_logic_vector(MPWID-1 downto 0);
78
           modulus : in std_logic_vector(MPWID-1 downto 0);
79
           product : out std_logic_vector(MPWID-1 downto 0);
80
           clk : in std_logic;
81
                          ds : in std_logic;
82
                          reset : in std_logic;
83
                          ready : out std_logic);
84
end modmult;
85
 
86
architecture modmult1 of modmult is
87
 
88
signal mpreg: std_logic_vector(MPWID-1 downto 0);
89
signal mcreg, mcreg1, mcreg2: std_logic_vector(MPWID+1 downto 0);
90
signal modreg1, modreg2: std_logic_vector(MPWID+1 downto 0);
91
signal prodreg, prodreg1, prodreg2, prodreg3, prodreg4: std_logic_vector(MPWID+1 downto 0);
92
 
93
--signal count: integer;
94
signal modstate: std_logic_vector(1 downto 0);
95
signal first: std_logic;
96
 
97
begin
98
 
99
        -- final result...
100
        product <= prodreg4(MPWID-1 downto 0);
101
 
102
        -- add shifted value if place bit is '1', copy original if place bit is '0'
103
        with mpreg(0) select
104
                prodreg1 <= prodreg + mcreg when '1',
105
                                                prodreg when others;
106
 
107
        -- subtract modulus and subtract modulus * 2.
108
        prodreg2 <= prodreg1 - modreg1;
109
        prodreg3 <= prodreg1 - modreg2;
110
 
111
        -- negative results mean that we subtracted too much...
112
        modstate <= prodreg3(mpwid+1) & prodreg2(mpwid+1);
113
 
114
        -- select the correct modular result and copy it....
115
        with modstate select
116
                prodreg4 <= prodreg1 when "11",
117
                                                prodreg2 when "10",
118
                                                prodreg3 when others;
119
 
120
        -- meanwhile, subtract the modulus from the shifted multiplicand...
121
        mcreg1 <= mcreg - modreg1;
122
 
123
        -- select the correct modular value and copy it.
124
        with mcreg1(MPWID) select
125
                mcreg2 <= mcreg when '1',
126
                                         mcreg1 when others;
127
 
128
        ready <= first;
129
 
130
        combine: process (clk, first, ds, mpreg, reset) is
131
 
132
        begin
133
 
134
                if reset = '1' then
135
                        first <= '1';
136
                elsif rising_edge(clk) then
137
                        if first = '1' then
138
                        -- First time through, set up registers to start multiplication procedure
139
                        -- Input values are sampled only once
140
                                if ds = '1' then
141
                                        mpreg <= mplier;
142
                                        mcreg <= "00" & mpand;
143
                                        modreg1 <= "00" & modulus;
144
                                        modreg2 <= '0' & modulus & '0';
145
                                        prodreg <= (others => '0');
146
                                        first <= '0';
147
                                end if;
148
                        else
149
                        -- when all bits have been shifted out of the multiplicand, operation is over
150
                        -- Note: this leads to at least one waste cycle per multiplication
151
                                if mpreg = 0 then
152
                                        first <= '1';
153
                                else
154
                                -- shift the multiplicand left one bit
155
                                        mcreg <= mcreg2(MPWID downto 0) & '0';
156
                                -- shift the multiplier right one bit
157
                                        mpreg <= '0' & mpreg(MPWID-1 downto 1);
158
                                -- copy intermediate product
159
                                        prodreg <= prodreg4;
160
                                end if;
161
                        end if;
162
                end if;
163
 
164
        end process combine;
165
 
166
end modmult1;

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.