Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. ###### Regular Signed Digit ####
  2. # Based on:
  3. # "Exponent recoding and regular exponentiation algorithms"
  4. # by M. Joye and M. Tunstall. Africacrypt 2003.
  5. # Also appears in:
  6. # "Selecting Elliptic Curves for Cryptography: An Efficiency
  7. # and Security Analysis", by Bos, Costello, Longa, Naehrig.
  8. def signed(n,w,t):
  9. k = []
  10. for i in range(t):
  11. ki = (n%2**w)-2**(w-1)
  12. k += [ki]
  13. n = (n-ki)>>(w-1)
  14. k += [n]
  15. return k
  16.  
  17. # SignedDigit obtains the signed-digit recoding of n and returns a list L of
  18. # digits such that n = sum( L[i]*2^(w-1) ), and each L[i] is an odd number
  19. # in the set {±1, ±3, ..., ±2^(w-1)-1}. The third parameter ensures that the
  20. # output has ceil(l/(w-1)) digits.
  21. # Restrictions:
  22. # - n > 0.
  23. # - 1 < w < 32.
  24. # - l >= bit length of n.
  25. def eval(K,w):
  26. return sum([ ki*2**(i*(w-1)) for i,ki in enumerate(K) ])
  27.  
  28.  
  29. ## This script finds scalars k that triggers the requirement of complete
  30. # addition formula at Step 19.
  31. locat = lambda x: [ i for i,k in enumerate(x) if k]
  32. for w in range(2,20):
  33. T = [2*i+1 for i in range(0,2**(w-2))]
  34. LT = [ signed(r-2*ti,w,384/(w-1)) for ti in T ]
  35. test = [ 2**(w-1)*eval(L[1:],w) == r-ki for L,ki in zip(LT,T) ]
  36. idx = locat(test)
  37. if idx != [] :
  38. print("w: {0}".format(w))
  39. for id in idx:
  40. k0 = eval(LT[id],w)
  41. k1 = r-2*T[id]
  42. assert(k0==k1)
  43. assert(k0<r)
  44. assert(is_odd(r-2*T[id]))
  45. print("ki: {0}".format(T[id]))
  46. print("k = 0x{0:x}".format(2*T[id]))
  47. print("k = r - {0} = 0x{1:x}".format(2*T[id],(k0)))
  48.  
  49. # EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement