Tiran

NORX constants

Jan 1st, 2015
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. """Calculate initialisation constants of NORX
  2.  
  3. Author: Christian Heimes <[email protected]>
  4. License: CC0
  5. """
  6. from __future__ import print_function
  7. from decimal import Decimal, localcontext, getcontext
  8.  
  9. # http://www.geom.uiuc.edu/~huberty/math5337/groupe/digits.html
  10. PI = "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998"
  11.  
  12. NORX_64 = (0x243F6A8885A308D3, 0x13198A2E03707344, 0xA4093822299F31D0,
  13.            0x082EFA98EC4E6C89, 0xAE8858DC339325A1, 0x670A134EE52D7FA6,
  14.            0xC4316D80CD967541, 0xD21DFBF8B630B762, 0x375A18D261E7F892,
  15.            0x343D1F187D92285B)
  16.  
  17. NORX_32 = (0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0x254F537A,
  18.            0x38531D48, 0x839C6E83, 0xF97A3AE5, 0x8C91D88C, 0x11EAFB59)
  19.  
  20.  
  21. def approx_pi():
  22.     """Compute Pi to the current precision.
  23.  
  24.    >>> print(pi())
  25.    3.141592653589793238462643383
  26.  
  27.    https://docs.python.org/3/library/decimal.html#recipes
  28.    """
  29.     getcontext().prec += 2  # extra digits for intermediate steps
  30.     three = Decimal(3)      # substitute "three=3.0" for regular floats
  31.     lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
  32.     while s != lasts:
  33.         lasts = s
  34.         n, na = n+na, na+8
  35.         d, da = d+da, da+32
  36.         t = (t * n) / d
  37.         s += t
  38.     getcontext().prec -= 2
  39.     return +s               # unary plus applies the new precision
  40.  
  41.  
  42. def norx_pi(w=64):
  43.     with localcontext() as ctx:
  44.         ctx.prec = 80
  45.         # pi = Decimal(PI)
  46.         pi = approx_pi()
  47.         frac = pi - Decimal(3)
  48.         ft = int(frac * 2**(4*w))
  49.     return tuple((ft >> i*w) & (2**w-1) for i in (3, 2, 1, 0))
  50.  
  51.  
  52. def g(a, b, c, d, w=64):
  53.     r1, r2, r3, r4 = (8, 19, 40, 63) if w == 64 else (8, 11, 16, 31)
  54.     mask = 2**w - 1
  55.  
  56.     def rotr(x, r):
  57.         return ((x >> r) | (x << (w - r))) & mask
  58.  
  59.     def h(x, y):
  60.         return ((x ^ y) ^ ((x & y) << 1)) & mask
  61.  
  62.     a = h(a, b)
  63.     d = rotr(a ^ d, r1)
  64.     c = h(c, d)
  65.     b = rotr(b ^ c, r2)
  66.     a = h(a, b)
  67.     d = rotr(a ^ d, r3)
  68.     c = h(c, d)
  69.     b = rotr(b ^ c, r4)
  70.     return a, b, c, d
  71.  
  72.  
  73. def norx32():
  74.     norx32_0 = norx_pi(32)
  75.     norx32_1 = g(*norx32_0, w=32)
  76.     norx32_2 = g(*norx32_1, w=32)
  77.     return norx32_0 + norx32_1 + norx32_2[:2]
  78.  
  79.  
  80. def norx64():
  81.     norx64_0 = norx_pi(64)
  82.     norx64_1 = g(*norx64_0, w=64)
  83.     norx64_2 = g(*norx64_1, w=64)
  84.     return norx64_0 + norx64_1 + norx64_2[:2]
  85.  
  86.  
  87. if __name__ == "__main__":
  88.     for number in (NORX_64, norx64()):
  89.         print(" ".join("%0.16x" % n for n in number))
  90.     for number in (NORX_32, norx32()):
  91.         print(" ".join("%0.8x" % n for n in number))
Advertisement
Add Comment
Please, Sign In to add comment