subtle icon indicating copy to clipboard operation
subtle copied to clipboard

reduce boilerplate implementing comparisons for user-defined types

Open cosmicexplorer opened this issue 2 years ago • 0 comments

Problem

In signalapp/libsignal#469, we discussed having to hand-roll a constant-time comparison function for a public key with a slice of bytes and an enum tag. After seeing #78 where we implement ConstantTimeEq for slices, I realized we could extend this method of iterated constant-time computation to make it more fluent to implement comparison operations for structs with multiple fields.

Proposed Solution

  1. Introduce IteratedOperation and IteratedEq to modularize the approach used in the existing ConstantTimeEq impl for slices.
    • Add a doctest demonstrating how to apply this to user structs with multiple fields.
  2. Develop a novel method to calculate ConstantTimeGreater over a collection of elements as LexicographicIteratedGreater.
    • Implement ConstantTimeGreater for slices using LexicographicIteratedGreater.

Result

  • It is now much nicer to support constant-time comparisons with custom structs, and doctests are provided to demonstrate this.
  • ConstantTimeOrd will be implemented automatically for slices if/when #98 is merged.

cosmicexplorer avatar Dec 31 '22 19:12 cosmicexplorer