问题
I am working on a VHDL implementation of the SHA-256 hash function.
I have some 32-bit unsigned
signals defined as such:
SIGNAL a, b : UNSIGNED (31 downto 0);
Within the specifications of the SHA-256 algorithm, it says addition must be performed modulo 2^32 in order to retain the 32-bit size in case of an overflow. Now, according to the answer to this question, it sounds like overflow is already handled with modular addition in VHDL:
There is no overflow handling, the overflow carry is simply lost. Thus the result is simply the integer result of your operation modulo 2^MAX.
I have 2 questions:
- In my case,
MAX = 31
so does that mean that any addition operation I perform ona
andb
will be modded with 2^31? - I need to perform addition modulo 2^32 which obviously doesn't make sense since I am working with 32-bit numbers and 2^32 is one bit too large. So is it somehow implied that I should actually be modding with 2^31?
回答1:
You are fine with unsigned(31 downto 0)
. The 2^MAX
in the post you reference is an error and should read 2^length
. The length of 31 downto 0
is 32.
Think about it, 31 downto 0
can represent numbers from 0 to 2^32-1, it wouldn't make much sense if any addition of that range would be modulo 2^31 if you can represent larger numbers!
I'm not sure I understand your second question, but addition modulo 2^32 yields results in the range of 0 to 2^32-1. 2^32 is illegal, thus it's quite fine that you can't represent it with your unsigned.
来源:https://stackoverflow.com/questions/30609749/vhdl-modulo-232-addition