1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Personality typestate for [`FixedUInt`](crate::FixedUInt).
//!
//! The personality is a zero-size marker generic on [`FixedUInt`] that selects
//! which implementations of operation primitives are picked at
//! monomorphization. Two personalities ship with the crate:
//!
//! - [`Nct`] (default): standard "non-constant-time" implementation.
//! - [`Ct`]: constant-time implementation. Slower bodies whose timing is
//! independent of operand values, defensible against an adversarial
//! optimizer. Used by signing paths and any code handling secret values.
//!
//! ## Const-eval dispatch via `match P::TAG`
//!
//! Composite `const fn` methods that need to dispatch on personality can
//! `match` on the [`PersonalityTag`] associated constant:
//!
//! ```ignore
//! const fn dispatched<P: Personality>(x: u32) -> u32 {
//! match P::TAG {
//! PersonalityTag::Nct => fast_body(x),
//! PersonalityTag::Ct => ct_body(x),
//! }
//! }
//! ```
//!
//! Trait *method* calls (e.g. `P::widening_mul(x, y)`) require unstable
//! `const_trait_impl` in const contexts and are not used by this crate. Tag
//! dispatch works on stable today and composes through multi-level generic
//! `const fn` chains without loss of const-eval.
use PhantomData;
/// Marker trait for personalities. Sealed — implementations live in this
/// crate. Downstream code uses the [`Nct`] and [`Ct`] types directly or
/// writes `match P::TAG` dispatch using [`PersonalityTag`].
/// Compile-time tag identifying the active personality. Returned by
/// [`Personality::TAG`] and used as the discriminant for `match P::TAG`
/// dispatch in `const fn` bodies.
/// Non-constant-time marker. Default personality for [`FixedUInt`].
;
/// Constant-time marker.
///
/// Selects constant-time implementations of operation primitives — bodies
/// whose execution time and memory access pattern do not depend on operand
/// values. Appropriate for code that handles secret values (signing,
/// scalar multiplication on secret scalars, Montgomery multiplication on
/// secret operands).
///
/// Calls to `subtle::ConditionallySelectable`, `ConstantTimeEq`, and
/// related traits are only available for [`FixedUInt`]<_, _, Ct>` — wrong-
/// variant calls become compile errors, not silent NCT execution.
///
/// [`FixedUInt`]: crate::FixedUInt
;
/// PhantomData helper for storing a personality marker as a zero-size field.
/// Use as `_p: PersonalityMarker<P>` in struct definitions.
pub type PersonalityMarker<P> = ;