frost_util.sage 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Utility functions for the FROST module.
  2. from hashlib import sha256
  3. def scalar_to_bytes(a):
  4. int_repr = ZZ(a)
  5. byte_repr = int(int_repr).to_bytes(32, byteorder="big")
  6. return byte_repr
  7. def point_to_bytes(P):
  8. if P.is_zero():
  9. return b"\x00"
  10. x_bytes = int(ZZ(P[0])).to_bytes(32, byteorder="big")
  11. y_bytes = int(ZZ(P[1])).to_bytes(32, byteorder="big")
  12. return x_bytes + y_bytes
  13. def hash_domain(domain, *args):
  14. concat = domain.encode() + b"".join(str(arg).encode() for arg in args)
  15. return int(sha256(concat).hexdigest(), 16)
  16. def H1(*args):
  17. return hash_domain("H1", *args)
  18. def H2(*args):
  19. return hash_domain("H2", *args)
  20. def H3(*args):
  21. return hash_domain("H3", *args)
  22. def H4(*args):
  23. return hash_domain("H4", *args)
  24. def H5(*args):
  25. return hash_domain("H5", *args)
  26. # Serialize the group commitment list
  27. def encode_group_commitment_list(commit_list):
  28. encoded_group_commitment = b""
  29. for (ident, hiding_nonce_commit, binding_nonce_commit) in commit_list:
  30. enc_commit = scalar_to_bytes(ident) \
  31. + point_to_bytes(hiding_nonce_commit) \
  32. + point_to_bytes(binding_nonce_commit)
  33. encoded_group_commitment = encoded_group_commitment + enc_commit
  34. return encoded_group_commitment