You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support ConditionallySelectable for types that are not Copy #94
It's unclear to me what the rationale for the Copy bound even is.
Looking at how subtle uses it, AFAICT it's only used by conditional_swap. Questions of breaking changes aside, it looks like the bound could potentially be moved to that method only.
Copy can also appear in conditional_select, like in this code that implements ConditionallySelectable for all fixed-size arrays.
If I understand correctly, Henry's stated reasoning is that allowing Clone types gives people too much leeway in their impls, allowing them to possibly do a clone() whose timing leaks bits about the data.
I'm not opposed to this reasoning. I just wonder if there's an escape hatch we could make that allows you to do this if you're very careful.
If I understand correctly, Henry's #56 is that allowing Clone types gives people too much leeway in their impls, allowing them to possibly do a clone() whose timing leaks bits about the data.
That is a reasonable concern. Copy should always be constant-time, whereas Clone admits a sharp edge.
However, I do also sympathize with the concern that it may be unwise to impl Copy on very large types. Personally I tend to use the size of an x86 cache line (64-bytes) as my personal rule of thumb for a boundary after which one should consider if types are too large to impl Copy.
Agreed. A not-great idea: make a trait SafeConditionallySelectable: Copy and then make trait ConditionallySelectable unconstrainted. That way people can still write generics relying on ConditionallySelectable. And it'd be the case that SafeConditionallySelectable is always ConditionallySelectable. Downsides: this changes the meanings of pre-existing terms, doesn't remove the footgun, and requires a major version release.
A big drawback of the Copy bound is it makes it impossible to implement ConditionallySelectable for heap allocated types, e.g. a heap-backed big integer.
However, changing it seems like a breaking change due to removing the supertrait bound on Copy.
Currently anything that's ConditionallySelectable can be assumed to be Copy via that supertrait bound. Removing it might require changing bounds in existing code to ConditionallySelectable + Copy.
It's the kind of thing that makes me wish we had something like Crater to see if it would break any real-world code. I think there's a real chance it might.
It seems like the only way to support non-Copy types without breaking changes would be to define a new trait and use a blanket impl for backwards compatibility. Something like:
I've been attempting to make use of subtle with heap-allocated BoxedInteger/BoxedResidue types, with the goal of using those to implement the rsa and dsa crates.
I can attest the Copy bound becomes a major impediment in these cases, because without ConditionallySelectable the CtOption combinators don't work, which IMO is one of subtle's best features for writing constant-time code.
I can try to open a PR with what I've outlined above, which in theory should be a backwards compatible change as current ConditionallySelectable users can leverage the new trait via the blanket impl (though it should likely be deprecated).
CtOption will need to be changed to use the new trait, which again shouldn't be breaking due to the blanket impl. #63 will also likely need to be addressed for my use cases to work, as we need to ensure fixed-but-dynamic precisions are consistent throughout, and Default will return a value with a number of limbs smaller than e.g. an RSA modulus.
Activity
tarcieri commentedon Jul 21, 2022
It's unclear to me what the rationale for the
Copybound even is.Looking at how
subtleuses it, AFAICT it's only used byconditional_swap. Questions of breaking changes aside, it looks like the bound could potentially be moved to that method only.rozbb commentedon Oct 11, 2022
Copycan also appear inconditional_select, like in this code that implementsConditionallySelectablefor all fixed-size arrays.If I understand correctly, Henry's stated reasoning is that allowing
Clonetypes gives people too much leeway in their impls, allowing them to possibly do aclone()whose timing leaks bits about the data.I'm not opposed to this reasoning. I just wonder if there's an escape hatch we could make that allows you to do this if you're very careful.
tarcieri commentedon Oct 11, 2022
The
Copybound could potentially be moved to that impl, e.g.That is a reasonable concern.
Copyshould always be constant-time, whereasCloneadmits a sharp edge.However, I do also sympathize with the concern that it may be unwise to impl
Copyon very large types. Personally I tend to use the size of an x86 cache line (64-bytes) as my personal rule of thumb for a boundary after which one should consider if types are too large to implCopy.rozbb commentedon Oct 11, 2022
Agreed. A not-great idea: make a
trait SafeConditionallySelectable: Copyand then maketrait ConditionallySelectableunconstrainted. That way people can still write generics relying onConditionallySelectable. And it'd be the case thatSafeConditionallySelectableis alwaysConditionallySelectable. Downsides: this changes the meanings of pre-existing terms, doesn't remove the footgun, and requires a major version release.tarcieri commentedon Oct 11, 2022
Another option besides splitting the
ConditionallySelectabletrait would be adding a marker trait like:...and then changing
ConditionallySelectableto be bound onCtClone.CtClonewould be a marker trait which means "I assert myCloneimpl is constant-time".rozbb commentedon Oct 11, 2022
Oh, that's pretty nice. I'd be fine with that.
sonti-lurker commentedon Apr 4, 2023
I think a similar argument applies to
CtOption::map(andand_then,or_else).The code has a comment:
which is obviously only true if the called closure runs in constant-time but there is nothing enforcing that.
tarcieri commentedon Apr 27, 2023
A big drawback of the
Copybound is it makes it impossible to implementConditionallySelectablefor heap allocated types, e.g. a heap-backed big integer.tarcieri commentedon Apr 27, 2023
However, changing it seems like a breaking change due to removing the supertrait bound on
Copy.Currently anything that's
ConditionallySelectablecan be assumed to beCopyvia that supertrait bound. Removing it might require changing bounds in existing code toConditionallySelectable + Copy.It's the kind of thing that makes me wish we had something like Crater to see if it would break any real-world code. I think there's a real chance it might.
tarcieri commentedon Apr 27, 2023
It seems like the only way to support non-
Copytypes without breaking changes would be to define a new trait and use a blanket impl for backwards compatibility. Something like:...then
ConditionallySelectablecould potentially be deprecated.Add `BoxedUint::conditional_select`
Add `BoxedUint::conditional_select` (#329)
tarcieri commentedon Nov 28, 2023
I've been attempting to make use of
subtlewith heap-allocatedBoxedInteger/BoxedResiduetypes, with the goal of using those to implement thersaanddsacrates.I can attest the
Copybound becomes a major impediment in these cases, because withoutConditionallySelectabletheCtOptioncombinators don't work, which IMO is one ofsubtle's best features for writing constant-time code.I can try to open a PR with what I've outlined above, which in theory should be a backwards compatible change as current
ConditionallySelectableusers can leverage the new trait via the blanket impl (though it should likely be deprecated).CtOptionwill need to be changed to use the new trait, which again shouldn't be breaking due to the blanket impl. #63 will also likely need to be addressed for my use cases to work, as we need to ensure fixed-but-dynamic precisions are consistent throughout, andDefaultwill return a value with a number of limbs smaller than e.g. an RSA modulus.BoxedUint: add `cond_map` and `cond_and_then`
BoxedUint: add `cond_map` and `cond_and_then` (#352)
Add `ConstantTimeSelect` and `ConstantTimeClone` traits
tarcieri commentedon Nov 29, 2023
I went ahead and impl'd my aforementioned
ConstantTimeSelecttrait idea here: #118Add `ConstantTimeSelect` and `ConstantTimeClone` traits
[WIP] subtle: breaking changes for a hypothetical v3.0
Change `ConditionallySelectable` supertrait
Add `BoxedUint::conditional_select` (#329)
BoxedUint: add `cond_map` and `cond_and_then` (#352)
Add `BoxedUint::conditional_select` (#329)
BoxedUint: add `cond_map` and `cond_and_then` (#352)
Add `BoxedUint::conditional_select` (#329)
BoxedUint: add `cond_map` and `cond_and_then` (#352)
Add `BoxedUint::conditional_select` (#329)
BoxedUint: add `cond_map` and `cond_and_then` (#352)