Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to handle large numbers in VHDL

Status
Not open for further replies.

malikkhaled

Junior Member level 1
Junior Member level 1
Joined
Jan 14, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
sweden
Visit site
Activity points
1,447
hi,

i am doing encryption and decryption and my algorithm inputs sizes is 256 bits. in test bench i am getting an error like "555677895559 exceeds maximum integer value ", although i didn't declare it as an integer.
constant P: std_logic_vector(255 downto 0):=x"2523648240000001ba344d80000000086121000000000013a700000000000013";
i declare P as constant and perform A+B mod P and A-B mod P based on Cin. any suggestion or comment from your's side?
 

hi,

i am doing encryption and decryption and my algorithm inputs sizes is 256 bits. in test bench i am getting an error like "555677895559 exceeds maximum integer value ", although i didn't declare it as an integer.
constant P: std_logic_vector(255 downto 0):=x"2523648240000001ba344d80000000086121000000000013a700000000000013";
i declare P as constant and perform A+B mod P and A-B mod P based on Cin. any suggestion or comment from your's side?

Using VHDL integers breaks down at 32 bits, since you need something larger you should be using 'signed' and 'unsigned' types from the ieee.numeric_std library. Vector length of these types is virtually unlimited (i.e. you can have on the approximately 2^32 bits in the vector as opposed to just 32 bits that represents integers).

Kevin Jennings
 

i am doing encryption and decryption and my algorithm inputs sizes is 256 bits. in test bench i am getting an error like "555677895559 exceeds maximum integer value ", although i didn't declare it as an integer.

Sounds like you're doing a conversion somewhere....
 

Using VHDL integers breaks down at 32 bits, since you need something larger you should be using 'signed' and 'unsigned' types from the ieee.numeric_std library. Vector length of these types is virtually unlimited (i.e. you can have on the approximately 2^32 bits in the vector as opposed to just 32 bits that represents integers).

Kevin Jennings

yes i am using unsigned types from ieee.numeric_std library. in test bench i used a conversion function to convert decimal into binary like
"a<= conv_std_logic_vector (1157777,256)", i think the problem could be here i need your suggestion.
 

well for a start, conv_std_logic_vector is not in the numeric_std library, so you must be using a non-standard library.
Second, you cannot convert integers to large arrays like that. You'll have to do it in 32 bit chunks and concatenate them or slice the ranges:

Code:
--concatenate
a <=   x"00000000" --255 downto 224
     & x"00000000" --223 downto 192 
      ...etc
     & std_logic_vector( to_signed(1157777, 32) );

--slices
a <=  (31 downto 0 => std_logic_vector( to_signed( 1157777, 32) ), others => '0');
 

This function can convert decimal integer constants of any size (written as a string) to "unsigned":


Code VHDL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
 
function decimal_string_to_unsigned(decimal_string: string; wanted_bitwidth: positive) return unsigned is
  variable tmp_unsigned: unsigned(wanted_bitwidth+3 downto 0) := (others => '0'); -- 4 extra bits to detect overflow
  variable character_value: integer;
begin
  for string_pos in decimal_string'range loop
    case decimal_string(string_pos) is
      when '0' => character_value := 0;
      when '1' => character_value := 1;
      when '2' => character_value := 2;
      when '3' => character_value := 3;
      when '4' => character_value := 4;
      when '5' => character_value := 5;
      when '6' => character_value := 6;
      when '7' => character_value := 7;
      when '8' => character_value := 8;
      when '9' => character_value := 9;
      when others => report("Illegal number") severity failure;
    end case;
    tmp_unsigned := tmp_unsigned(wanted_bitwidth-1 downto 0) * to_unsigned(10, 4);
    tmp_unsigned := tmp_unsigned + character_value;
    if tmp_unsigned(wanted_bitwidth+3 downto wanted_bitwidth) /= "0000" then
      report("Too large number") severity failure;
    end if;
  end loop;
  return tmp_unsigned(wanted_bitwidth-1 downto 0);
end decimal_string_to_unsigned;



It can be used like this:

Code VHDL - [expand]
1
2
a <= decimal_string_to_unsigned("1157777", 256)
a <= decimal_string_to_unsigned("12345678901234567890", 256)



It is a slightly modified version of the function I posted in this thread:

https://www.edaboard.com/threads/247443/
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top