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

Subversion Repositories mod_mult_exp

[/] [mod_mult_exp/] [trunk/] [rtl/] [vhdl/] [mod_mult/] [ModularMultiplierIterative.vhd] - Diff between revs 3 and 6

Only display areas with differences | Details | Blame | View Log

Rev 3 Rev 6
-----------------------------------------------------------------------
-----------------------------------------------------------------------
----                                                               ----
----                                                               ----
---- Montgomery modular multiplier and exponentiator               ----
---- Montgomery modular multiplier and exponentiator               ----
----                                                               ----
----                                                               ----
---- This file is part of the Montgomery modular multiplier        ----
---- This file is part of the Montgomery modular multiplier        ----
---- and exponentiator project                                     ----
---- and exponentiator project                                     ----
---- http://opencores.org/project,mod_mult_exp                     ----
---- http://opencores.org/project,mod_mult_exp                     ----
----                                                               ----
----                                                               ----
---- Description:                                                  ----
---- Description:                                                  ----
----   Montgomery modular multiplier main module. It combines all  ----
----   Montgomery modular multiplier main module. It combines all  ----
----   subomponents. It takes two numbers and modulus as the input ----
----   subomponents. It takes two numbers and modulus as the input ----
----   and returns the Montgomery product A*B*(R^{-1}) mod M       ----
----   and returns the Montgomery product A*B*(R^{-1}) mod M       ----
----   where R^{-1} is the modular multiplicative inverse.         ----
----   where R^{-1} is the modular multiplicative inverse.         ----
----   R*R^{-1} == 1 mod M                                         ----
----   R*R^{-1} == 1 mod M                                         ----
----   R = 2^word_length mod M                                     ----
----   R = 2^word_length mod M                                     ----
----               and word_length is the binary width of the      ----
----               and word_length is the binary width of the      ----
----               operated word (in this case 64 bit)             ----
----               operated word (in this case 32, 64 or 512 bit)  ----
---- To Do:                                                        ----
---- To Do:                                                        ----
----                                                               ----
----                                                               ----
---- Author(s):                                                    ----
---- Author(s):                                                    ----
---- - Krzysztof Gajewski, gajos@opencores.org                     ----
---- - Krzysztof Gajewski, gajos@opencores.org                     ----
----                       k.gajewski@gmail.com                    ----
----                       k.gajewski@gmail.com                    ----
----                                                               ----
----                                                               ----
-----------------------------------------------------------------------
-----------------------------------------------------------------------
----                                                               ----
----                                                               ----
---- Copyright (C) 2014 Authors and OPENCORES.ORG                  ----
---- Copyright (C) 2019 Authors and OPENCORES.ORG                  ----
----                                                               ----
----                                                               ----
---- This source file may be used and distributed without          ----
---- This source file may be used and distributed without          ----
---- restriction provided that this copyright statement is not     ----
---- restriction provided that this copyright statement is not     ----
---- removed from the file and that any derivative work contains   ----
---- removed from the file and that any derivative work contains   ----
---- the original copyright notice and the associated disclaimer.  ----
---- the original copyright notice and the associated disclaimer.  ----
----                                                               ----
----                                                               ----
---- This source file is free software; you can redistribute it    ----
---- This source file is free software; you can redistribute it    ----
---- and-or modify it under the terms of the GNU Lesser General    ----
---- and-or modify it under the terms of the GNU Lesser General    ----
---- Public License as published by the Free Software Foundation;  ----
---- Public License as published by the Free Software Foundation;  ----
---- either version 2.1 of the License, or (at your option) any    ----
---- either version 2.1 of the License, or (at your option) any    ----
---- later version.                                                ----
---- later version.                                                ----
----                                                               ----
----                                                               ----
---- This source is distributed in the hope that it will be        ----
---- This source is distributed in the hope that it will be        ----
---- useful, but WITHOUT ANY WARRANTY; without even the implied    ----
---- useful, but WITHOUT ANY WARRANTY; without even the implied    ----
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       ----
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       ----
---- PURPOSE. See the GNU Lesser General Public License for more   ----
---- PURPOSE. See the GNU Lesser General Public License for more   ----
---- details.                                                      ----
---- details.                                                      ----
----                                                               ----
----                                                               ----
---- You should have received a copy of the GNU Lesser General     ----
---- You should have received a copy of the GNU Lesser General     ----
---- Public License along with this source; if not, download it    ----
---- Public License along with this source; if not, download it    ----
---- from http://www.opencores.org/lgpl.shtml                      ----
---- from http://www.opencores.org/lgpl.shtml                      ----
----                                                               ----
----                                                               ----
-----------------------------------------------------------------------
-----------------------------------------------------------------------
library IEEE;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.NUMERIC_STD.ALL;
use IEEE.NUMERIC_STD.ALL;
use work.properties.ALL;
use work.properties.ALL;
 
 
-- Uncomment the following library declaration if using
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
--use IEEE.NUMERIC_STD.ALL;
 
 
-- Uncomment the following library declaration if instantiating
-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
-- any Xilinx primitives in this code.
--library UNISIM;
--library UNISIM;
--use UNISIM.VComponents.all;
--use UNISIM.VComponents.all;
 
 
entity ModularMultiplierIterative is
entity ModularMultiplierIterative is
    generic (
    generic (
             word_size : integer := WORD_LENGTH
             word_size : integer := WORD_LENGTH
    );
    );
    port (
    port (
        A       : in  STD_LOGIC_VECTOR(word_size - 1 downto 0);     -- multiplicand
        A       : in  STD_LOGIC_VECTOR(word_size - 1 downto 0);     -- multiplicand
                  B       : in  STD_LOGIC_VECTOR(word_size - 1 downto 0);     -- multiplier
                  B       : in  STD_LOGIC_VECTOR(word_size - 1 downto 0);     -- multiplier
                  M       : in  STD_LOGIC_VECTOR(word_size - 1 downto 0);     -- modulus
                  M       : in  STD_LOGIC_VECTOR(word_size - 1 downto 0);     -- modulus
                  start   : in  STD_LOGIC;
                  start   : in  STD_LOGIC;
                  product : out STD_LOGIC_VECTOR(word_size - 1 downto 0); -- product
                  product : out STD_LOGIC_VECTOR(word_size - 1 downto 0); -- product
                  ready   : out STD_LOGIC;
                  ready   : out STD_LOGIC;
                  clk     : in  STD_LOGIC
                  clk     : in  STD_LOGIC
    );
    );
end ModularMultiplierIterative;
end ModularMultiplierIterative;
 
 
architecture Behavioral of ModularMultiplierIterative is
architecture Behavioral of ModularMultiplierIterative is
 
 
-- Multiplexer
-- Multiplexer
component MontMult4inMux is
component MontMult4inMux is
    generic (
    generic (
             word_size : integer := WORD_LENGTH
             word_size : integer := WORD_LENGTH
         );
         );
    port (
    port (
             ctrl   : in  STD_LOGIC_VECTOR(1 downto 0);
             ctrl   : in  STD_LOGIC_VECTOR(1 downto 0);
             zero   : in  STD_LOGIC_VECTOR(word_size downto 0);
             zero   : in  STD_LOGIC_VECTOR(word_size downto 0);
        M      : in  STD_LOGIC_VECTOR(word_size downto 0);
        M      : in  STD_LOGIC_VECTOR(word_size downto 0);
        Y      : in  STD_LOGIC_VECTOR(word_size downto 0);
        Y      : in  STD_LOGIC_VECTOR(word_size downto 0);
        YplusM : in  STD_LOGIC_VECTOR(word_size downto 0);
        YplusM : in  STD_LOGIC_VECTOR(word_size downto 0);
                  output : out STD_LOGIC_VECTOR(word_size downto 0)
                  output : out STD_LOGIC_VECTOR(word_size downto 0)
    );
    );
end component MontMult4inMux;
end component MontMult4inMux;
 
 
-- State machine
-- State machine
component ModMultIter_SM is
component ModMultIter_SM is
    generic (
    generic (
             word_size : integer := WORD_LENGTH
             word_size : integer := WORD_LENGTH
         );
         );
    port(
    port(
        x             : in  STD_LOGIC_VECTOR(word_size - 1 downto 0);
        x             : in  STD_LOGIC_VECTOR(word_size - 1 downto 0);
                  start         : in  STD_LOGIC;
                  start         : in  STD_LOGIC;
                  clk           : in  STD_LOGIC;
                  clk           : in  STD_LOGIC;
                  s_0           : in  STD_LOGIC;
                  s_0           : in  STD_LOGIC;
                  y_0           : in  STD_LOGIC;
                  y_0           : in  STD_LOGIC;
                  ready         : out STD_LOGIC;
                  ready         : out STD_LOGIC;
                  out_reg_en    : out STD_LOGIC;
                  out_reg_en    : out STD_LOGIC;
                  mux_mult_ctrl : out STD_LOGIC;
                  mux_mult_ctrl : out STD_LOGIC;
                  mux_4in_ctrl  : out STD_LOGIC_VECTOR(1 downto 0)
                  mux_4in_ctrl  : out STD_LOGIC_VECTOR(1 downto 0)
         );
         );
end component ModMultIter_SM;
end component ModMultIter_SM;
 
 
-- Signals
-- Signals
signal Mi              : STD_LOGIC_VECTOR(word_size downto 0);
signal Mi              : STD_LOGIC_VECTOR(word_size downto 0);
signal Yi              : STD_LOGIC_VECTOR(word_size downto 0);
signal Yi              : STD_LOGIC_VECTOR(word_size downto 0);
signal sumYM           : STD_LOGIC_VECTOR(word_size downto 0);
signal sumYM           : STD_LOGIC_VECTOR(word_size downto 0);
signal zero_sig        : STD_LOGIC_VECTOR(word_size downto 0) := (others => '0');
signal zero_sig        : STD_LOGIC_VECTOR(word_size downto 0) := (others => '0');
signal four_in_mux_out : STD_LOGIC_VECTOR(word_size downto 0);
signal four_in_mux_out : STD_LOGIC_VECTOR(word_size downto 0);
 
 
signal mux_4in_ctrl_sig  : STD_LOGIC_VECTOR(1 downto 0);
signal mux_4in_ctrl_sig  : STD_LOGIC_VECTOR(1 downto 0);
signal mult_mux_ctrl_sig : STD_LOGIC;
signal mult_mux_ctrl_sig : STD_LOGIC;
 
 
signal mult_mux_out     : STD_LOGIC_VECTOR(word_size downto 0);
signal mult_mux_out     : STD_LOGIC_VECTOR(word_size downto 0);
signal out_reg_sig      : STD_LOGIC_VECTOR(word_size downto 0);
signal out_reg_sig      : STD_LOGIC_VECTOR(word_size downto 0);
signal product_sig      : STD_LOGIC_VECTOR(word_size downto 0);
signal product_sig      : STD_LOGIC_VECTOR(word_size downto 0);
signal out_en           : STD_LOGIC;
signal out_en           : STD_LOGIC;
 
 
signal sum_mult_out     : STD_LOGIC_VECTOR(word_size + 1 downto 0);
signal sum_mult_out     : STD_LOGIC_VECTOR(word_size + 1 downto 0);
signal sum_div_2        : STD_LOGIC_VECTOR(word_size downto 0);
signal sum_div_2        : STD_LOGIC_VECTOR(word_size downto 0);
 
 
begin
begin
     zero_sig <= (others => '0'); -- '0'
     zero_sig <= (others => '0'); -- '0'
         -- 'widening' to store the intermediate steps
         -- 'widening' to store the intermediate steps
         Mi <= '0' & M;
         Mi <= '0' & M;
         Yi <= '0' & B;
         Yi <= '0' & B;
 
 
         -- Operations needed to compute the Montgomery multiplications
         -- Operations needed to compute the Montgomery multiplications
         sum_div_2 <= sum_mult_out(word_size + 1 downto 1);
         sum_div_2 <= sum_mult_out(word_size + 1 downto 1);
         sum_mult_out <= ('0' & four_in_mux_out) + ('0' & mult_mux_out);
         sum_mult_out <= ('0' & four_in_mux_out) + ('0' & mult_mux_out);
         sumYM <= ('0' & B) + ('0' & M);
         sumYM <= ('0' & B) + ('0' & M);
 
 
         -- Multiplexer component
         -- Multiplexer component
         four_in_mux : MontMult4inMux port map(
         four_in_mux : MontMult4inMux port map(
             ctrl => mux_4in_ctrl_sig, zero => zero_sig, M => Mi, Y => Yi,
             ctrl => mux_4in_ctrl_sig, zero => zero_sig, M => Mi, Y => Yi,
        YplusM => sumYM, output => four_in_mux_out
        YplusM => sumYM, output => four_in_mux_out
         );
         );
 
 
         -- Two input asynchronuos multiplexer for output 'not clear' code due to
         -- Two input asynchronuos multiplexer for output 'not clear' code due to
         -- 'historical works'
         -- 'historical works'
         mult_mux_out <= (others => '0') when (mult_mux_ctrl_sig = '0') else
         mult_mux_out <= (others => '0') when (mult_mux_ctrl_sig = '0') else
                   out_reg_sig;
                   out_reg_sig;
 
 
         -- State machine
         -- State machine
         state_machine : ModMultIter_SM port map(
         state_machine : ModMultIter_SM port map(
        x => A,
        x => A,
                  start => start,
                  start => start,
                  clk => clk,
                  clk => clk,
                  s_0 => out_reg_sig(0),
                  s_0 => out_reg_sig(0),
                  y_0 => B(0),
                  y_0 => B(0),
                  ready => ready,
                  ready => ready,
                  out_reg_en => out_en,
                  out_reg_en => out_en,
                  mux_mult_ctrl => mult_mux_ctrl_sig,
                  mux_mult_ctrl => mult_mux_ctrl_sig,
                  mux_4in_ctrl => mux_4in_ctrl_sig
                  mux_4in_ctrl => mux_4in_ctrl_sig
         );
         );
 
 
             -- Register like structure for signal synchronous work
             -- Register like structure for signal synchronous work
                 clock : process(clk, start)
                 clock : process(clk, start)
             begin
             begin
                      if (clk = '1' and clk'Event) then
                      if (clk = '1' and clk'Event) then
                                    if (start = '0') then
                                    if (start = '0') then
                                        out_reg_sig <= (others => '0');
                                        out_reg_sig <= (others => '0');
                                         elsif out_en = '1' then
                                         elsif out_en = '1' then
                                                  out_reg_sig <= sum_div_2;
                                                  out_reg_sig <= sum_div_2;
                                        end if;
                                        end if;
                           end if;
                           end if;
                  end process clock;
                  end process clock;
 
 
                  -- One additional 'subtract' component which was added after
                  -- One additional 'subtract' component which was added after
                  -- first experiments with Montgomery multiplication. It was
                  -- first experiments with Montgomery multiplication. It was
                  -- observed that sometimes intermediate step can be higher 
                  -- observed that sometimes intermediate step can be higher 
                  -- than modulus. In this situation 'M' substraction is 
                  -- than modulus. In this situation 'M' substraction is 
                  -- compulsory
                  -- compulsory
                  product_proc : process(clk, Mi, out_reg_sig)
                  product_proc : process(clk, Mi, out_reg_sig)
                  begin
                  begin
                                if(out_reg_sig < ("0" & Mi)) then
                                if(out_reg_sig < ("0" & Mi)) then
                                         product_sig <= out_reg_sig;
                                         product_sig <= out_reg_sig;
                                else
                                else
                                         product_sig <= out_reg_sig - Mi;
                                         product_sig <= out_reg_sig - Mi;
                                end if;
                                end if;
                  end process product_proc;
                  end process product_proc;
                  product <= product_sig(word_size - 1 downto 0);
                  product <= product_sig(word_size - 1 downto 0);
 
 
 
 

powered by: WebSVN 2.1.0

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