subtle
subtle copied to clipboard
reduce boilerplate implementing comparisons for user-defined types
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
- Introduce
IteratedOperation
andIteratedEq
to modularize the approach used in the existingConstantTimeEq
impl for slices.- Add a doctest demonstrating how to apply this to user structs with multiple fields.
- Develop a novel method to calculate
ConstantTimeGreater
over a collection of elements asLexicographicIteratedGreater
.- Implement
ConstantTimeGreater
for slices usingLexicographicIteratedGreater
.
- Implement
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.