Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement lexicographical ordering for slices of arbitrary types #116

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

conduition
Copy link

@conduition conduition commented Oct 18, 2023

This generalizes the implementation of ConstantTimeEq for [T] to also support ConstantTimeGreater and ConstantTimeLess. I haven't touched the implementation of ConstantTimeEq for [T] as the standalone implementation is more efficient than the multi-purpose code i've added here. However in principle the execution of the code is very similar.

I added a utility function ct_slice_lex_cmp(x, y) which produces a cmp::Ordering in time proportional to min(x.len(), y.len()). I chose this approach rather than implementing ConstantTimeGreater directly, because it allows us to also implement ConstantTimeLess without invoking both ct_eq and ct_gt, which would perform up to twice as many loop iterations over both slices.

Reasoning

I wrote this PR because I found a need in my project for constant time comparison on fixed-size arrays of bytes (secret data), beyond simple equality checking. Specifically, I needed to check if an elliptic curve secret scalar value represented as [u8; 32] was larger than the curve order (some fixed [u8; 32] constant).

In non-constant time operations, one could simply do x >= y. I wrote ct_slice_lex_cmp to fulfill this duty and realized it might be handy upstream here.

PS those formatting changes in test/mod.rs were automatically applied by cargo fmt. I can revert commit ca90794 if you'd prefer to keep that code formatted as it was before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant