沙规格声明--例如,在SHA-224和SHA-256的情况下--常量“代表前64个素数立方体根的前32位”。这些价值观是如何被“代表”的?从素数开始,以十六进制常量结尾,生成规范中提供的所有常量的正确步骤顺序是什么?
发布于 2016-11-14 22:30:34
听起来就是这样。注意:当428a2f98解释为十六进制分数时,4/16 + 2/(16^2) + 8/(16^3) + 10/(16^4) + ...的值大约等于0.2599210496991873,所以当您将非小数部分相加时,得到的是1.2599210496991873,它是第一个素数(2)的立方根。
在python中:
In [1]: 1+sum([int(c,16)/(16.**(i+1)) for (i,c) in enumerate('428a2f98')])
Out[1]: 1.2599210496991873
In [2]: 2**(1/3.)
Out[2]: 1.2599210498948732要生成常量,可以使用以下代码创建gmpy2 python库:
from gmpy2 import mpfr, floor, next_prime
def convert_primes_cube_fractional_part_to_hex_constant(prime, hex_chars=8):
"""
Note if you want the first 8 decimal (base=10) digits of a number,
you multiply the fractional part by 10**8 and then look at the integer part
In this case we want first 8 hex digits, so multiply fractional part by 16**8
and then look at integer part (and return in hexadecimal).
"""
cube_root = mpfr(prime)**(1/mpfr(3))
frac_part = cube_root - floor(cube_root)
format_str = '%%0%dx' % hex_chars
# format_str will be '%08x' if hex_chars=8 so always emits
# 8 zero-padded hex digits
return format_str % floor(frac_part*(16**hex_chars))
def generate_n_primes(n=64):
p = 2
i = 0
while i < n:
yield p
p = next_prime(p)
i += 1在定义了可以运行的函数之后,可以通过以下步骤重新创建表:
>>> for i,p in enumerate(generate_n_primes(64)):
if i % 8 == 0:
print ""
print convert_primes_cube_fractional_part_to_hex_constant(p, hex_chars=8),
428a2f98 71374491 b5c0fbcf e9b5dba5 3956c25b 59f111f1 923f82a4 ab1c5ed5
d807aa98 12835b01 243185be 550c7dc3 72be5d74 80deb1fe 9bdc06a7 c19bf174
e49b69c1 efbe4786 0fc19dc6 240ca1cc 2de92c6f 4a7484aa 5cb0a9dc 76f988da
983e5152 a831c66d b00327c8 bf597fc7 c6e00bf3 d5a79147 06ca6351 14292967
27b70a85 2e1b2138 4d2c6dfc 53380d13 650a7354 766a0abb 81c2c92e 92722c85
a2bfe8a1 a81a664b c24b8b70 c76c51a3 d192e819 d6990624 f40e3585 106aa070
19a4c116 1e376c08 2748774c 34b0bcb5 391c0cb3 4ed8aa4a 5b9cca4f 682e6ff3
748f82ee 78a5636f 84c87814 8cc70208 90befffa a4506ceb bef9a3f7 c67178f2注意,这与NIST发布中的表完全匹配。
您可以使用以下代码生成其他SHA-512常量。注意,首先必须将gmpy2中的多重精度浮点数学从默认的53位增加到大约100位,否则,由于精度下降,最后几个六位数总是0。
>>> gmpy2.get_context().precision=100
>>> for i,p in enumerate(generate_n_primes(80)):
if i % 4 == 0:
print ""
print convert_primes_cube_fractional_part_to_hex_constant(p, hex_chars=16),https://crypto.stackexchange.com/questions/41496
复制相似问题