Add conversion functions Z.to_bytes and Z.of_bytes#167
Add conversion functions Z.to_bytes and Z.of_bytes#167xavierleroy merged 1 commit intoocaml:masterfrom
Z.to_bytes and Z.of_bytes#167Conversation
|
That looks great, thanks! In Python, byteorder seems to be optional and defaults to big-endian. Also, happy New Year! |
|
Thanks for the feedback, and happy New Year !
Right. It's true that big-endian seems much more common esp. in cryptographic protocols, so I thought making it the default. But then it clashes a bit with the use of little-endian in
Yes, I think it's useful as a canonical representation of a
I don't feel a clear need at this point. The use cases I ran into while playing with cryptography use fixed lengths and you're guaranteed that the encoded integer will fit this length (because it's taken modulo q for appropriate q and length). |
Converting between big integers and byte sequences is important e.g. for cryptosystems based on number theory. The existing
Z.to_bitsandZ.of_bitsconversion 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 ofZ.to_bits, while many protocols fix the length in advance.This PR implements
Z.to_bytesandZ.of_bytesconversion 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 theint.to_bytesandint.from_bytesPython standard library functions.