dedis / kyber

Advanced crypto library for the Go language
Other
641 stars 168 forks source link

Bytes/SetBytes endianness control #172

Open bford opened 7 years ago

bford commented 7 years ago

mod.Int has a limited degree of support for "endianness control"; i.e., serving clients who might want little-endian byte-slices as opposed to the default big-endian. However, two issues with this:

Thus, I propose that we move the ByteOrder, BigEndian, and LittleEndian names from the group/mod package to the top level, and define the Scalar.Bytes and Scalar.SetBytes to take a byteOrder parameter directly. Just make this standard functionality for all Scalars (of which there currently aren't that many different implementations anyway).

Another, related bit of API inconsistency comes from the fact that mod.Int also has LittleEndian() and BigEndian() methods, which act like Bytes() for the appropriate endianness but also take 'min' and 'max' parameters defining the minimum and maximum output length. I think this has proven to be pretty useful functionality as well - e.g., sometimes you just want the minimum-length slice that can represent an integer, sometimes you want a slice exactly some length. So for further API consolidation, I suggest we eliminate the separate BigEndian() and LittleEndian() methods from mod.Int and instead make Scalar.Bytes() take three paramters: min, max, and byteOrder.

nikkolasg commented 7 years ago

I think this has proven to be pretty useful functionality as well

Do you have specific examples on that ? From my 2 years experience using/developing this library, I never used it explicitely. I'm always requiring a specific bitlength so min = 32 and max = 32.

I'm OK with the suggestion of moving the endianness as a parameters of the SetBytes and Bytes functions. I'm not sure with overloading them with a min and max parameter...