Skip to content

Conversation

@xavierleroy
Copy link
Contributor

Converting between big integers and byte sequences is important e.g. for cryptosystems based on number theory. The existing Z.to_bits and Z.of_bits conversion functions are useful but a bit limited: they use little-endian representation while big-endian is more common for crypto protocols, and there's no way to control the size of the output of Z.to_bits, while many protocols fix the length in advance.

This PR implements Z.to_bytes and Z.of_bytes conversion functions that let the user choose endianness, the size of the output, and whether signed numbers should be accommodated (using two's complement representation). The functions are modeled after the int.to_bytes and int.from_bytes Python standard library functions.

@antoinemine
Copy link
Collaborator

That looks great, thanks!

In Python, byteorder seems to be optional and defaults to big-endian.
I like that, if not specified, the length is automatically set. Would it be useful to export length computation to the user in a dedicated function (e.g., if the length needs to be checked or rounded by the user before conversion)?

Also, happy New Year!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants