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
Supporting CtOption::map for non-Default types? #63
This is especially problematic for types which require some constant-time validation of the input in their public constructors, say with the following API.
Hmm, regardless of the Default bound, that code really looks like it should be using Option<T> or Result<T,E>, because if the input is invalid, the computation can't sensibly continue.
One thing that might work to relax the Default bound would be to unconditionally apply the closure to the CtOption's T value, but I haven't thought it through yet...
If #94 were addressed which would unlock using combinators like CtOption::map with heap-allocated types (which we need for the rsa and dsa crates), the use of Default in map would become a problem, because our T values can vary in precision (i.e. number of limbs), and the Default impl can't match precision to an existing value of T except by chance.
Unconditionally applying the closure to T would address this problem and prevent a spurious heap allocation (i.e. getting the Default) every time map is called.
It's also problematic to use Default because it's typically a "zero value" which may not make sense given the mathematical operation being applied in the closure, e.g. it could be a divisor and division-by-zero could potentially cause a panic, precluding the use of map in cases where it would work if the closure were unilaterally applied to the contained value instead of Default.
I'm going to add my voice here that Default is a bad trait to begin with; if we force types to have a Default impl in order to make them interoperable with subtle, this also means that the Default impl is available in other places, which could cause the default value to inadvertently be constructed and incorrectly interpreted as valid data. This is a general type safety problem; Default just shouldn't exist, and forcing users to implement it is detrimental to the overall safety of systems.
Activity
hdevalence commentedon Dec 5, 2019
Hmm, regardless of the
Defaultbound, that code really looks like it should be usingOption<T>orResult<T,E>, because if the input is invalid, the computation can't sensibly continue.hdevalence commentedon Dec 5, 2019
One thing that might work to relax the
Defaultbound would be to unconditionally apply the closure to theCtOption'sTvalue, but I haven't thought it through yet...tarcieri commentedon Nov 28, 2023
If #94 were addressed which would unlock using combinators like
CtOption::mapwith heap-allocated types (which we need for thersaanddsacrates), the use ofDefaultinmapwould become a problem, because ourTvalues can vary in precision (i.e. number of limbs), and theDefaultimpl can't match precision to an existing value ofTexcept by chance.Unconditionally applying the closure to
Twould address this problem and prevent a spurious heap allocation (i.e. getting theDefault) every timemapis called.It's also problematic to use
Defaultbecause it's typically a "zero value" which may not make sense given the mathematical operation being applied in the closure, e.g. it could be a divisor and division-by-zero could potentially cause a panic, precluding the use ofmapin cases where it would work if the closure were unilaterally applied to the contained value instead ofDefault.Add `ConstantTimeSelect` and `ConstantTimeClone` traits
Add `ConstantTimeSelect` and `ConstantTimeClone` traits
tarcieri commentedon Nov 29, 2023
I have proposed removing this bound in #118, although I worry it's a breaking change.
An non-breaking alternative would be to add new methods which do the same thing but don't have the bound.
nuttycom commentedon Feb 15, 2024
I'm going to add my voice here that
Defaultis a bad trait to begin with; if we force types to have aDefaultimpl in order to make them interoperable withsubtle, this also means that theDefaultimpl is available in other places, which could cause the default value to inadvertently be constructed and incorrectly interpreted as valid data. This is a general type safety problem;Defaultjust shouldn't exist, and forcing users to implement it is detrimental to the overall safety of systems.[WIP] subtle: breaking changes for a hypothetical v3.0