crypto-bigint icon indicating copy to clipboard operation
crypto-bigint copied to clipboard

Add support for Binary and Decimal encodings

Open andrewwhitehead opened this issue 2 years ago • 1 comments

Decimal serialization (outside of write_decimal_bytes_vartime) is currently only supported when the alloc flag is enabled, as the encoded length is potentially unbounded for Uint<L> and digits are produced in reverse. In practice anything up to U8192 can be encoded without allocating. Note that unlike the other encodings, decimal encoding is not constant-time.

Unresolved questions:

  • Add a trait for encoding numbers as decimal, to match FromDecimal? This is a bit tricky as it would require the alloc feature (or gain additional methods when the feature is enabled).
  • Implement Error for ParseDecimalError? This would require a std flag.
  • Add an error type for encoding errors?

andrewwhitehead avatar Jun 01 '23 21:06 andrewwhitehead

Sorry for taking awhile to review this. The implementation is somewhat complex which makes it difficult, but I also have some initial thoughts.

The existing support for hex encoding is implemented in a constant-timeish manner, avoiding branches on the inputs in favor of arithmetic operations.

This implementation appears to branch on the input in several places. That's okay, but the corresponding functions should be marked as *_vartime to reflect that, per this crate's constant-time-by-default policy.

tarcieri avatar Aug 07 '23 00:08 tarcieri

Closing as stale

tarcieri avatar May 07 '24 13:05 tarcieri