VHDL modulo 2^32 addition

≡放荡痞女 提交于 2019-12-13 00:29:11

问题


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:

  1. In my case, MAX = 31 so does that mean that any addition operation I perform on a and b will be modded with 2^31?
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!