Skip to content

Conversation

@tarcieri
Copy link
Member

@tarcieri tarcieri commented Dec 27, 2025

For full feature parity with subtle, this adds a conditional negation trait called CtNeg.

It uses a slightly different design from subtle, supporting both in-place operation ala subtle with ct_neg_assign, while also supporting core::ops::Neg-like usage with ct_neg.

Using one trait with two methods permits a default implementation of ct_neg_assign while still letting it be overridden, which is useful for e.g. heap-allocated types. It could arguably be split into CtNeg and CtNegAssign but we would lose that default impl (and the same argument could be made for splitting up e.g. CtSelect, with the same drawbacks)

By default impls are written for the signed integer (i*) types: i8, i16, i32, i64, i128.

For full feature parity with `subtle`, this adds a conditional negation
trait called `CtNeg`.

It uses a slightly different design from `subtle`, supporting both
in-place operation ala `subtle` with `ct_neg_assign`, while also
supporting `core::ops::Neg`-like usage with `ct_neg`.

Using one trait with two methods permits a default implementation of
`ct_neg_assign` while still letting it be overridden, which is useful
for e.g. heap-allocated types. It could arguably be split into `CtNeg`
and `CtNegAssign` but we would lose that default impl (and the same
argument could be made for splitting up e.g. `CtSelect`, with the same
drawbacks)

By default impls are written for the signed integer (`i*`) types:
`i8`, `i16`, `i32`, `i64, `i128`.
@tarcieri tarcieri merged commit 1d2aff3 into master Dec 27, 2025
13 checks passed
@tarcieri tarcieri deleted the ctutils/ct_neg branch December 27, 2025 15:26
@tarcieri tarcieri mentioned this pull request Dec 27, 2025
tarcieri added a commit that referenced this pull request Dec 27, 2025
### Added
- Additional `Choice::from_u128*` constructors (#1285)
- `CtNeg` trait (#1286)

### Changed
- Renamed `Choice::from_*_nonzero` => `from_*_nz` (#1287)
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.

2 participants