VHDL模2 ^ 32加法 [英] VHDL modulo 2^32 addition

查看:750
本文介绍了VHDL模2 ^ 32加法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 SHA-256 哈希函数. 我有一些这样定义的32位unsigned信号:

SIGNAL a, b : UNSIGNED (31 downto 0);

在SHA-256算法的规范内,它表示必须以2 ^ 32为模执行加法,以便在发生溢出时保留32位大小.现在,根据这个问题的答案,听起来好像已经通过VHDL中的模块化添加处理了溢出:

没有溢出处理,溢出进位只是丢失.因此,结果只是您的运算以2 ^ MAX为模的整数结果.

我有2个问题:

  1. 在我的情况下,MAX = 31是否意味着我在ab上执行的任何加法运算都将被2 ^ 31修饰?
  2. 我需要执行模2 ^ 32 的加法运算,这显然没有意义,因为我使用的是32位数字,而2 ^ 32太大了.那么这是否暗示我实际上应该和2 ^ 31 混在一起?

解决方案

unsigned(31 downto 0)很好.您引用的帖子中的2^MAX是错误,应显示为2^length. 31 downto 0的长度是32.

考虑一下,31 downto 0可以表示从0到2 ^ 32-1的数字,如果可以表示更大的数字,那么该范围的任何加法将取模2 ^ 31都没有多大意义!

我不确定我是否理解您的第二个问题,但是模2 ^ 32的加法运算结果范围为0到2 ^ 32-1. 2 ^ 32是非法的,因此可以不用未签名来表示就可以了.

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?

解决方案

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.

这篇关于VHDL模2 ^ 32加法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆