@umbra-privacy/sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +122 -0
- package/dist/addresses-Brzgurv_.d.ts +145 -0
- package/dist/addresses-D_0YAS6B.d.cts +145 -0
- package/dist/chunk-2Q75CQQJ.js +12 -0
- package/dist/chunk-2Q75CQQJ.js.map +1 -0
- package/dist/chunk-7QVYU63E.js +6 -0
- package/dist/chunk-7QVYU63E.js.map +1 -0
- package/dist/chunk-BM7N6N7E.js +1883 -0
- package/dist/chunk-BM7N6N7E.js.map +1 -0
- package/dist/chunk-GXKSUB2U.cjs +4416 -0
- package/dist/chunk-GXKSUB2U.cjs.map +1 -0
- package/dist/chunk-HOEXDXRC.cjs +792 -0
- package/dist/chunk-HOEXDXRC.cjs.map +1 -0
- package/dist/chunk-MDFSBU5W.cjs +2033 -0
- package/dist/chunk-MDFSBU5W.cjs.map +1 -0
- package/dist/chunk-MQY7HDIA.js +600 -0
- package/dist/chunk-MQY7HDIA.js.map +1 -0
- package/dist/chunk-MVKTV3FT.cjs +20 -0
- package/dist/chunk-MVKTV3FT.cjs.map +1 -0
- package/dist/chunk-PG2J6V6Y.js +4094 -0
- package/dist/chunk-PG2J6V6Y.js.map +1 -0
- package/dist/chunk-PK6SKIKE.cjs +8 -0
- package/dist/chunk-PK6SKIKE.cjs.map +1 -0
- package/dist/chunk-VEGLTTYQ.cjs +621 -0
- package/dist/chunk-VEGLTTYQ.cjs.map +1 -0
- package/dist/chunk-WVHQ46DD.js +758 -0
- package/dist/chunk-WVHQ46DD.js.map +1 -0
- package/dist/constants/index.cjs +316 -0
- package/dist/constants/index.cjs.map +1 -0
- package/dist/constants/index.d.cts +739 -0
- package/dist/constants/index.d.ts +739 -0
- package/dist/constants/index.js +193 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/cryptography-BTGC72u-.d.cts +4809 -0
- package/dist/cryptography-BTGC72u-.d.ts +4809 -0
- package/dist/errors/index.cjs +141 -0
- package/dist/errors/index.cjs.map +1 -0
- package/dist/errors/index.d.cts +1415 -0
- package/dist/errors/index.d.ts +1415 -0
- package/dist/errors/index.js +4 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index-B9pDY73x.d.ts +12933 -0
- package/dist/index-CLj_zWSD.d.ts +235 -0
- package/dist/index-CX6_pIRS.d.cts +235 -0
- package/dist/index-D33yo0qB.d.cts +12933 -0
- package/dist/index.cjs +22464 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +11694 -0
- package/dist/index.d.ts +11694 -0
- package/dist/index.js +22314 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/index.cjs +4 -0
- package/dist/interfaces/index.cjs.map +1 -0
- package/dist/interfaces/index.d.cts +8 -0
- package/dist/interfaces/index.d.ts +8 -0
- package/dist/interfaces/index.js +3 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/networks-C-orpSFW.d.ts +65 -0
- package/dist/networks-FxYERGD1.d.cts +65 -0
- package/dist/types/index.cjs +605 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +1853 -0
- package/dist/types/index.d.ts +1853 -0
- package/dist/types/index.js +4 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types-BBuELtY8.d.cts +495 -0
- package/dist/types-n-sHFcgr.d.ts +495 -0
- package/dist/utils/index.cjs +1295 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +9559 -0
- package/dist/utils/index.d.ts +9559 -0
- package/dist/utils/index.js +6 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/versions-D9PqsEvj.d.cts +173 -0
- package/dist/versions-D9PqsEvj.d.ts +173 -0
- package/package.json +151 -0
|
@@ -0,0 +1,4809 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branded Types Infrastructure
|
|
3
|
+
*
|
|
4
|
+
* Provides compile-time nominal typing for the SDK through a hierarchical branded type system.
|
|
5
|
+
* Branded types prevent accidental mixing of structurally identical values — for example,
|
|
6
|
+
* passing a `U64` where a `U128` is expected, or a `RcPlaintext` where a `RcCiphertext` is
|
|
7
|
+
* required. Because TypeScript uses structural typing, two types with the same underlying
|
|
8
|
+
* representation (e.g. both `bigint`) are otherwise interchangeable, which is a silent source
|
|
9
|
+
* of hard-to-detect bugs in cryptographic and protocol code.
|
|
10
|
+
*
|
|
11
|
+
* ## The Branded Type Pattern
|
|
12
|
+
*
|
|
13
|
+
* A branded type is formed by intersecting a base type `T` with an object carrying a unique
|
|
14
|
+
* phantom symbol property. The symbol is declared with `declare const ... : unique symbol`,
|
|
15
|
+
* which guarantees TypeScript treats it as distinct from every other symbol — the brand itself
|
|
16
|
+
* never exists at runtime and adds zero bytes to the value.
|
|
17
|
+
*
|
|
18
|
+
* ```
|
|
19
|
+
* type UserId = BrandedType<string, "UserId">;
|
|
20
|
+
* // ^^^^^^^^^^ string — the runtime representation
|
|
21
|
+
* // ^^^^^^^^ "UserId" — the phantom brand
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* ## Hierarchical Brands
|
|
25
|
+
*
|
|
26
|
+
* The module supports up to five levels of brand hierarchy through `SubBrandedType`,
|
|
27
|
+
* `SubSubBrandedType`, etc. A sub-brand inherits structural compatibility with its parent,
|
|
28
|
+
* allowing a more specific type to be used wherever the parent is accepted, but not vice versa.
|
|
29
|
+
* This mirrors subtype relationships (e.g. `U32LeBytes ⊆ LeBytes ⊆ Bytes`).
|
|
30
|
+
*
|
|
31
|
+
* ## Utility Types
|
|
32
|
+
*
|
|
33
|
+
* `UnwrapBrand` and `ExtractBrand` are provided for generic code that needs to inspect or
|
|
34
|
+
* strip the brand layer without discarding the base type information.
|
|
35
|
+
*
|
|
36
|
+
* @packageDocumentation
|
|
37
|
+
* @module common/branded
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* Unique symbol used as the primary brand property key.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Declared with `unique symbol` so TypeScript guarantees no two declarations of
|
|
44
|
+
* `BrandSymbol` can be the same symbol, making it impossible to accidentally collide
|
|
45
|
+
* with user-defined properties. The value never exists at runtime.
|
|
46
|
+
*
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
declare const BrandSymbol: unique symbol;
|
|
50
|
+
/**
|
|
51
|
+
* Unique symbol used as the sub-brand property key.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* Attached to a branded type by `SubBrandedType` to create a second phantom dimension
|
|
55
|
+
* that distinguishes sub-types from their parent without breaking parent assignability.
|
|
56
|
+
*
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
declare const SubBrandSymbol: unique symbol;
|
|
60
|
+
/**
|
|
61
|
+
* Unique symbol used as the sub-sub-brand property key.
|
|
62
|
+
*
|
|
63
|
+
* @remarks
|
|
64
|
+
* Attached by `SubSubBrandedType` to create a third phantom dimension.
|
|
65
|
+
*
|
|
66
|
+
* @internal
|
|
67
|
+
*/
|
|
68
|
+
declare const SubSubBrandSymbol: unique symbol;
|
|
69
|
+
/**
|
|
70
|
+
* Unique symbol used as the sub-sub-sub-brand property key.
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* Attached by `SubSubSubBrandedType` to create a fourth phantom dimension.
|
|
74
|
+
*
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
declare const SubSubSubBrandSymbol: unique symbol;
|
|
78
|
+
/**
|
|
79
|
+
* Unique symbol used as the sub-sub-sub-sub-brand property key.
|
|
80
|
+
*
|
|
81
|
+
* @remarks
|
|
82
|
+
* Attached by `SubSubSubSubBrandedType` to create a fifth phantom dimension.
|
|
83
|
+
*
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
declare const SubSubSubSubBrandSymbol: unique symbol;
|
|
87
|
+
/**
|
|
88
|
+
* Creates a branded (nominally typed) variant of a base type.
|
|
89
|
+
*
|
|
90
|
+
* @remarks
|
|
91
|
+
* The brand is encoded as a readonly phantom property keyed by `BrandSymbol` whose
|
|
92
|
+
* value type is the string literal `Brand`. Because `BrandSymbol` is a `unique symbol`,
|
|
93
|
+
* no two `BrandedType` declarations share the same property key unless they name the same
|
|
94
|
+
* `Brand` string. This makes cross-brand assignment a compile-time error.
|
|
95
|
+
*
|
|
96
|
+
* At runtime the intersection carries no extra data — branded values are stored and
|
|
97
|
+
* transmitted identically to their base type.
|
|
98
|
+
*
|
|
99
|
+
* Use `UnwrapBrand` to recover `Base` and `ExtractBrand` to recover `Brand` from a
|
|
100
|
+
* `BrandedType` in generic contexts.
|
|
101
|
+
*
|
|
102
|
+
* @typeParam Base - The underlying runtime type (e.g. `bigint`, `Uint8Array`, `string`).
|
|
103
|
+
* @typeParam Brand - A unique string literal that names this brand (e.g. `"U64"`, `"UserId"`).
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* // Define two distinct identifier types with the same underlying representation.
|
|
108
|
+
* type UserId = BrandedType<string, "UserId">;
|
|
109
|
+
* type PostId = BrandedType<string, "PostId">;
|
|
110
|
+
*
|
|
111
|
+
* // Values must be explicitly branded at the boundary.
|
|
112
|
+
* const userId: UserId = "user-123" as UserId;
|
|
113
|
+
* const postId: PostId = "post-456" as PostId;
|
|
114
|
+
*
|
|
115
|
+
* // TypeScript rejects accidental mixing:
|
|
116
|
+
* const invalid: PostId = userId; // Error: Type 'UserId' is not assignable to type 'PostId'.
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // Numeric brands prevent silent precision loss in the SDK.
|
|
122
|
+
* type U64 = BrandedType<bigint, "U64">;
|
|
123
|
+
* type U128 = BrandedType<bigint, "U128">;
|
|
124
|
+
*
|
|
125
|
+
* function encryptU64(value: U64): void { ... }
|
|
126
|
+
* encryptU64(myU128Value); // Compile-time error — prevents protocol bugs.
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* @public
|
|
130
|
+
*/
|
|
131
|
+
type BrandedType<Base, Brand extends string> = Base & {
|
|
132
|
+
readonly [BrandSymbol]: Brand;
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Extracts the base (unbranded) type from a `BrandedType`.
|
|
136
|
+
*
|
|
137
|
+
* @remarks
|
|
138
|
+
* Useful in generic utilities that need to operate on the underlying representation
|
|
139
|
+
* without caring about the nominal layer. For non-branded types, `T` is returned
|
|
140
|
+
* unchanged, making this safe to apply unconditionally.
|
|
141
|
+
*
|
|
142
|
+
* @typeParam T - A `BrandedType` or plain type to unwrap.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* type UserId = BrandedType<string, "UserId">;
|
|
147
|
+
* type Base = UnwrapBrand<UserId>; // string
|
|
148
|
+
*
|
|
149
|
+
* // Non-branded types pass through unchanged.
|
|
150
|
+
* type Plain = UnwrapBrand<number>; // number
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
155
|
+
type UnwrapBrand<T> = T extends BrandedType<infer Base, string> ? Base : T;
|
|
156
|
+
/**
|
|
157
|
+
* Extracts the brand string literal from a `BrandedType`.
|
|
158
|
+
*
|
|
159
|
+
* @remarks
|
|
160
|
+
* Returns `never` for types that are not `BrandedType` instances. Useful when building
|
|
161
|
+
* generic error messages or runtime assertions that need to name the expected brand.
|
|
162
|
+
*
|
|
163
|
+
* @typeParam T - A `BrandedType` from which to extract the brand name.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```typescript
|
|
167
|
+
* type UserId = BrandedType<string, "UserId">;
|
|
168
|
+
* type Name = ExtractBrand<UserId>; // "UserId"
|
|
169
|
+
*
|
|
170
|
+
* // Non-branded types yield never.
|
|
171
|
+
* type Nothing = ExtractBrand<string>; // never
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* @public
|
|
175
|
+
*/
|
|
176
|
+
type ExtractBrand<T> = T extends BrandedType<unknown, infer Brand> ? Brand : never;
|
|
177
|
+
/**
|
|
178
|
+
* Creates a sub-branded type that is a more specific variant of an existing `BrandedType`.
|
|
179
|
+
*
|
|
180
|
+
* @remarks
|
|
181
|
+
* A sub-branded type carries both the parent brand (via `[BrandSymbol]`) and its own
|
|
182
|
+
* sub-brand (via `[SubBrandSymbol]`). This means:
|
|
183
|
+
*
|
|
184
|
+
* - A `SubBrandedType<Parent, Sub>` is assignable to `Parent` (narrower types are compatible).
|
|
185
|
+
* - `Parent` is NOT assignable to `SubBrandedType<Parent, Sub>` (you cannot widen without
|
|
186
|
+
* an explicit assertion).
|
|
187
|
+
*
|
|
188
|
+
* This is used in the SDK to model byte-array hierarchies such as:
|
|
189
|
+
* `Bytes` → `LeBytes` → `U32LeBytes`.
|
|
190
|
+
*
|
|
191
|
+
* @typeParam Parent - The parent `BrandedType` to extend. Must already be a `BrandedType`.
|
|
192
|
+
* @typeParam SubBrand - A unique string literal for the sub-brand (e.g. `"LeBytes"`).
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* type Bytes = BrandedType<Uint8Array, "Bytes">;
|
|
197
|
+
* type LeBytes = SubBrandedType<Bytes, "LeBytes">;
|
|
198
|
+
* type BeBytes = SubBrandedType<Bytes, "BeBytes">;
|
|
199
|
+
*
|
|
200
|
+
* declare const le: LeBytes;
|
|
201
|
+
* declare const be: BeBytes;
|
|
202
|
+
*
|
|
203
|
+
* const asBytes: Bytes = le; // OK — LeBytes ⊆ Bytes
|
|
204
|
+
* const invalid: BeBytes = le; // Error — LeBytes is not BeBytes
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* @public
|
|
208
|
+
*/
|
|
209
|
+
type SubBrandedType<Parent extends BrandedType<unknown, string>, SubBrand extends string> = Parent & {
|
|
210
|
+
readonly [SubBrandSymbol]: SubBrand;
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Creates a sub-sub-branded type for the third level of the brand hierarchy.
|
|
214
|
+
*
|
|
215
|
+
* @remarks
|
|
216
|
+
* Inherits both the parent brand and the sub-brand, adding a third phantom dimension via
|
|
217
|
+
* `[SubSubBrandSymbol]`. A `SubSubBrandedType` is assignable to its `SubBrandedType`
|
|
218
|
+
* parent and transitively to the root `BrandedType`, but nothing in the hierarchy can
|
|
219
|
+
* be widened to a `SubSubBrandedType` without an explicit assertion.
|
|
220
|
+
*
|
|
221
|
+
* Typical usage in the SDK:
|
|
222
|
+
* `Bytes` → `LeBytes` → `U32LeBytes`
|
|
223
|
+
*
|
|
224
|
+
* @typeParam Parent - The parent `SubBrandedType` to extend.
|
|
225
|
+
* @typeParam SubSubBrand - A unique string literal for this level (e.g. `"U32LeBytes"`).
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```typescript
|
|
229
|
+
* type Bytes = BrandedType<Uint8Array, "Bytes">;
|
|
230
|
+
* type LeBytes = SubBrandedType<Bytes, "LeBytes">;
|
|
231
|
+
* type U32LeBytes = SubSubBrandedType<LeBytes, "U32LeBytes">;
|
|
232
|
+
*
|
|
233
|
+
* declare const u32: U32LeBytes;
|
|
234
|
+
* const asLe: LeBytes = u32; // OK
|
|
235
|
+
* const asBytes: Bytes = u32; // OK
|
|
236
|
+
* ```
|
|
237
|
+
*
|
|
238
|
+
* @public
|
|
239
|
+
*/
|
|
240
|
+
type SubSubBrandedType<Parent extends SubBrandedType<BrandedType<unknown, string>, string>, SubSubBrand extends string> = Parent & {
|
|
241
|
+
readonly [SubSubBrandSymbol]: SubSubBrand;
|
|
242
|
+
};
|
|
243
|
+
/**
|
|
244
|
+
* Creates a sub-sub-sub-branded type for the fourth level of the brand hierarchy.
|
|
245
|
+
*
|
|
246
|
+
* @remarks
|
|
247
|
+
* Adds a fourth phantom dimension via `[SubSubSubBrandSymbol]` on top of a
|
|
248
|
+
* `SubSubBrandedType` parent. Assignable to all ancestor levels.
|
|
249
|
+
*
|
|
250
|
+
* Typical usage in the SDK:
|
|
251
|
+
* `Bytes` → `LeBytes` → `U256LeBytes` → `Keccak256Hash`
|
|
252
|
+
*
|
|
253
|
+
* @typeParam Parent - The parent `SubSubBrandedType` to extend.
|
|
254
|
+
* @typeParam SubSubSubBrand - A unique string literal for this level (e.g. `"Keccak256Hash"`).
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* type Bytes = BrandedType<Uint8Array, "Bytes">;
|
|
259
|
+
* type LeBytes = SubBrandedType<Bytes, "LeBytes">;
|
|
260
|
+
* type U32LeBytes = SubSubBrandedType<LeBytes, "U32LeBytes">;
|
|
261
|
+
* type ValidU32LeBytes = SubSubSubBrandedType<U32LeBytes, "ValidU32LeBytes">;
|
|
262
|
+
*
|
|
263
|
+
* declare const v: ValidU32LeBytes;
|
|
264
|
+
* const asU32: U32LeBytes = v; // OK
|
|
265
|
+
* const asLe: LeBytes = v; // OK
|
|
266
|
+
* const asBytes: Bytes = v; // OK
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* @public
|
|
270
|
+
*/
|
|
271
|
+
type SubSubSubBrandedType<Parent extends SubSubBrandedType<SubBrandedType<BrandedType<unknown, string>, string>, string>, SubSubSubBrand extends string> = Parent & {
|
|
272
|
+
readonly [SubSubSubBrandSymbol]: SubSubSubBrand;
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Creates a sub-sub-sub-sub-branded type for the fifth level of the brand hierarchy.
|
|
276
|
+
*
|
|
277
|
+
* @remarks
|
|
278
|
+
* Adds a fifth phantom dimension via `[SubSubSubSubBrandSymbol]` on top of a
|
|
279
|
+
* `SubSubSubBrandedType` parent. This is the deepest level of branding supported by the
|
|
280
|
+
* SDK. Assignable to all four ancestor levels.
|
|
281
|
+
*
|
|
282
|
+
* Example chain used in the SDK for master seed derivation:
|
|
283
|
+
* `Bytes` → `LeBytes` → `U512LeBytes` → `Keccak512Hash` → `MasterSeed`
|
|
284
|
+
*
|
|
285
|
+
* @typeParam Parent - The parent `SubSubSubBrandedType` to extend.
|
|
286
|
+
* @typeParam SubSubSubSubBrand - A unique string literal for this level (e.g. `"MasterSeed"`).
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```typescript
|
|
290
|
+
* type Bytes = BrandedType<Uint8Array, "Bytes">;
|
|
291
|
+
* type LeBytes = SubBrandedType<Bytes, "LeBytes">;
|
|
292
|
+
* type U512LeBytes = SubSubBrandedType<LeBytes, "U512LeBytes">;
|
|
293
|
+
* type Keccak512Hash = SubSubSubBrandedType<U512LeBytes, "Keccak512Hash">;
|
|
294
|
+
* type MasterSeed = SubSubSubSubBrandedType<Keccak512Hash, "MasterSeed">;
|
|
295
|
+
*
|
|
296
|
+
* // MasterSeed is assignable to every ancestor level.
|
|
297
|
+
* declare const seed: MasterSeed;
|
|
298
|
+
* const asHash: Keccak512Hash = seed; // OK
|
|
299
|
+
* const asU512: U512LeBytes = seed; // OK
|
|
300
|
+
* const asLe: LeBytes = seed; // OK
|
|
301
|
+
* const asBytes: Bytes = seed; // OK
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* @public
|
|
305
|
+
*/
|
|
306
|
+
type SubSubSubSubBrandedType<Parent extends SubSubSubBrandedType<SubSubBrandedType<SubBrandedType<BrandedType<unknown, string>, string>, string>, string>, SubSubSubSubBrand extends string> = Parent & {
|
|
307
|
+
readonly [SubSubSubSubBrandSymbol]: SubSubSubSubBrand;
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Mathematics Types Module
|
|
312
|
+
*
|
|
313
|
+
* This module provides a comprehensive set of branded types and runtime assertions
|
|
314
|
+
* for mathematical primitives used throughout the SDK. These types ensure compile-time
|
|
315
|
+
* safety by preventing accidental mixing of incompatible numeric representations.
|
|
316
|
+
*
|
|
317
|
+
* ## Overview
|
|
318
|
+
*
|
|
319
|
+
* The module is organized into several categories:
|
|
320
|
+
*
|
|
321
|
+
* ### Byte Array Types
|
|
322
|
+
* - **Base types**: `LeBytes` (little-endian), `BeBytes` (big-endian)
|
|
323
|
+
* - **Sized types**: `U8LeBytes`, `U16LeBytes`, ..., `U1024LeBytes` (and BE equivalents)
|
|
324
|
+
*
|
|
325
|
+
* ### Integer Types
|
|
326
|
+
* - **Unsigned**: `U8`, `U16`, `U32`, `U64`, `U128`, `U256`, `U512`, `U1024`
|
|
327
|
+
* - **Signed**: `I8`, `I16`, `I32`, `I64`, `I128`, `I256`, `I512`, `I1024`
|
|
328
|
+
*
|
|
329
|
+
* ### Constants
|
|
330
|
+
* - Maximum values for unsigned types (`U8_MAX`, `U16_MAX`, etc.)
|
|
331
|
+
* - Min/max values for signed types (`I8_MIN`, `I8_MAX`, etc.)
|
|
332
|
+
* - Byte lengths for sized arrays (`U8_BYTE_LENGTH`, etc.)
|
|
333
|
+
*
|
|
334
|
+
* ### Assertion Functions
|
|
335
|
+
* - Runtime validation functions that throw `MathematicsAssertionError` on failure
|
|
336
|
+
* - Return branded types on success for type-safe usage
|
|
337
|
+
*
|
|
338
|
+
* ## Usage Example
|
|
339
|
+
*
|
|
340
|
+
* ```typescript
|
|
341
|
+
* import { assertU256, assertU256LeBytes, U256, U256LeBytes } from "./mathematics";
|
|
342
|
+
*
|
|
343
|
+
* // Validate and brand a bigint as U256
|
|
344
|
+
* const value: U256 = assertU256(12345n);
|
|
345
|
+
*
|
|
346
|
+
* // Validate and brand a byte array
|
|
347
|
+
* const bytes: U256LeBytes = assertU256LeBytes(new Uint8Array(32));
|
|
348
|
+
*
|
|
349
|
+
* // Type errors are caught at compile time
|
|
350
|
+
* function processU256(value: U256): void { ... }
|
|
351
|
+
* processU256(12345n); // Error: bigint is not assignable to U256
|
|
352
|
+
* processU256(assertU256(12345n)); // OK
|
|
353
|
+
* ```
|
|
354
|
+
*
|
|
355
|
+
* @module types/mathematics
|
|
356
|
+
* @see {@link ./branded} for the underlying branded type utilities
|
|
357
|
+
* @packageDocumentation
|
|
358
|
+
*/
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Generic branded byte array base type.
|
|
362
|
+
*
|
|
363
|
+
* This is the root type for all byte array types in the SDK.
|
|
364
|
+
* All specialized byte array types (LeBytes, BeBytes, X25519Bytes, etc.)
|
|
365
|
+
* derive from this type.
|
|
366
|
+
*
|
|
367
|
+
* Use this type when:
|
|
368
|
+
* - Endianness is not relevant to the operation
|
|
369
|
+
* - Endianness is handled externally
|
|
370
|
+
* - Working with raw binary data without numeric interpretation
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* ```typescript
|
|
374
|
+
* function hashBytes(data: Bytes): Bytes {
|
|
375
|
+
* // Endianness doesn't matter for hashing
|
|
376
|
+
* return sha256(data);
|
|
377
|
+
* }
|
|
378
|
+
* ```
|
|
379
|
+
*
|
|
380
|
+
* @public
|
|
381
|
+
*/
|
|
382
|
+
type Bytes = BrandedType<Uint8Array, "Bytes">;
|
|
383
|
+
/**
|
|
384
|
+
* Little-endian byte array base type.
|
|
385
|
+
*
|
|
386
|
+
* Little-endian format stores the **least significant byte first**.
|
|
387
|
+
* This is the native byte order for:
|
|
388
|
+
* - x86 and x64 processors (Intel, AMD)
|
|
389
|
+
* - ARM processors (in little-endian mode)
|
|
390
|
+
* - Most modern desktop and mobile CPUs
|
|
391
|
+
*
|
|
392
|
+
* @remarks
|
|
393
|
+
* Little-endian is often more efficient for arithmetic operations
|
|
394
|
+
* as the least significant bytes can be processed first.
|
|
395
|
+
*
|
|
396
|
+
* @example
|
|
397
|
+
* ```typescript
|
|
398
|
+
* // The 32-bit number 0x12345678 in little-endian:
|
|
399
|
+
* // Memory layout: [0x78, 0x56, 0x34, 0x12]
|
|
400
|
+
* // LSB MSB
|
|
401
|
+
* const leBytes = assertLeBytes(new Uint8Array([0x78, 0x56, 0x34, 0x12]));
|
|
402
|
+
*
|
|
403
|
+
* // Converting to number (conceptually):
|
|
404
|
+
* // 0x78 + (0x56 << 8) + (0x34 << 16) + (0x12 << 24) = 0x12345678
|
|
405
|
+
* ```
|
|
406
|
+
*
|
|
407
|
+
* @see {@link BeBytes} for big-endian representation
|
|
408
|
+
* @public
|
|
409
|
+
*/
|
|
410
|
+
type LeBytes = SubBrandedType<Bytes, "LeBytes">;
|
|
411
|
+
/**
|
|
412
|
+
* Big-endian byte array base type.
|
|
413
|
+
*
|
|
414
|
+
* Big-endian format stores the **most significant byte first**.
|
|
415
|
+
* This is used in:
|
|
416
|
+
* - Network protocols (TCP/IP) - hence "network byte order"
|
|
417
|
+
* - Many file formats (JPEG, PNG headers)
|
|
418
|
+
* - Java's DataInputStream/DataOutputStream
|
|
419
|
+
* - Most cryptographic specifications
|
|
420
|
+
*
|
|
421
|
+
* @remarks
|
|
422
|
+
* Big-endian is often more human-readable as bytes appear in the
|
|
423
|
+
* same order as the hexadecimal representation.
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* ```typescript
|
|
427
|
+
* // The 32-bit number 0x12345678 in big-endian:
|
|
428
|
+
* // Memory layout: [0x12, 0x34, 0x56, 0x78]
|
|
429
|
+
* // MSB LSB
|
|
430
|
+
* const beBytes = assertBeBytes(new Uint8Array([0x12, 0x34, 0x56, 0x78]));
|
|
431
|
+
*
|
|
432
|
+
* // The hex string "12345678" maps directly to bytes
|
|
433
|
+
* ```
|
|
434
|
+
*
|
|
435
|
+
* @see {@link LeBytes} for little-endian representation
|
|
436
|
+
* @public
|
|
437
|
+
*/
|
|
438
|
+
type BeBytes = SubBrandedType<Bytes, "BeBytes">;
|
|
439
|
+
/**
|
|
440
|
+
* 8-bit (1 byte) little-endian byte array.
|
|
441
|
+
*
|
|
442
|
+
* Represents a single byte, which is endianness-agnostic.
|
|
443
|
+
* Included for API consistency with larger sizes.
|
|
444
|
+
*
|
|
445
|
+
* @remarks
|
|
446
|
+
* - Size: 1 byte
|
|
447
|
+
* - Value range: [0, 255] (0x00 to 0xFF)
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* ```typescript
|
|
451
|
+
* const byte = assertU8LeBytes(new Uint8Array([0xFF]));
|
|
452
|
+
* // Represents the value 255
|
|
453
|
+
* ```
|
|
454
|
+
*
|
|
455
|
+
* @see {@link assertU8LeBytes}
|
|
456
|
+
* @public
|
|
457
|
+
*/
|
|
458
|
+
type U8LeBytes = SubSubBrandedType<LeBytes, "U8LeBytes">;
|
|
459
|
+
/**
|
|
460
|
+
* 16-bit (2 bytes) little-endian byte array.
|
|
461
|
+
*
|
|
462
|
+
* @remarks
|
|
463
|
+
* - Size: 2 bytes
|
|
464
|
+
* - Value range: [0, 65,535] (0x0000 to 0xFFFF)
|
|
465
|
+
*
|
|
466
|
+
* @example
|
|
467
|
+
* ```typescript
|
|
468
|
+
* // Value 0x1234 in little-endian
|
|
469
|
+
* const bytes = assertU16LeBytes(new Uint8Array([0x34, 0x12]));
|
|
470
|
+
* ```
|
|
471
|
+
*
|
|
472
|
+
* @see {@link assertU16LeBytes}
|
|
473
|
+
* @public
|
|
474
|
+
*/
|
|
475
|
+
type U16LeBytes = SubSubBrandedType<LeBytes, "U16LeBytes">;
|
|
476
|
+
/**
|
|
477
|
+
* 32-bit (4 bytes) little-endian byte array.
|
|
478
|
+
*
|
|
479
|
+
* @remarks
|
|
480
|
+
* - Size: 4 bytes
|
|
481
|
+
* - Value range: [0, 4,294,967,295] (0x00000000 to 0xFFFFFFFF)
|
|
482
|
+
* - Common use: IPv4 addresses, 32-bit hashes, timestamps
|
|
483
|
+
*
|
|
484
|
+
* @example
|
|
485
|
+
* ```typescript
|
|
486
|
+
* // Value 0x12345678 in little-endian
|
|
487
|
+
* const bytes = assertU32LeBytes(new Uint8Array([0x78, 0x56, 0x34, 0x12]));
|
|
488
|
+
* ```
|
|
489
|
+
*
|
|
490
|
+
* @see {@link assertU32LeBytes}
|
|
491
|
+
* @public
|
|
492
|
+
*/
|
|
493
|
+
type U32LeBytes = SubSubBrandedType<LeBytes, "U32LeBytes">;
|
|
494
|
+
/**
|
|
495
|
+
* 64-bit (8 bytes) little-endian byte array.
|
|
496
|
+
*
|
|
497
|
+
* @remarks
|
|
498
|
+
* - Size: 8 bytes
|
|
499
|
+
* - Value range: [0, 2^64 - 1]
|
|
500
|
+
* - Common use: Timestamps with nanosecond precision, large counters
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
* ```typescript
|
|
504
|
+
* const bytes = assertU64LeBytes(new Uint8Array(8));
|
|
505
|
+
* ```
|
|
506
|
+
*
|
|
507
|
+
* @see {@link assertU64LeBytes}
|
|
508
|
+
* @public
|
|
509
|
+
*/
|
|
510
|
+
type U64LeBytes = SubSubBrandedType<LeBytes, "U64LeBytes">;
|
|
511
|
+
/**
|
|
512
|
+
* 128-bit (16 bytes) little-endian byte array.
|
|
513
|
+
*
|
|
514
|
+
* @remarks
|
|
515
|
+
* - Size: 16 bytes
|
|
516
|
+
* - Value range: [0, 2^128 - 1]
|
|
517
|
+
* - Common use: UUIDs, AES block size
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
520
|
+
* ```typescript
|
|
521
|
+
* const bytes = assertU128LeBytes(new Uint8Array(16));
|
|
522
|
+
* ```
|
|
523
|
+
*
|
|
524
|
+
* @see {@link assertU128LeBytes}
|
|
525
|
+
* @public
|
|
526
|
+
*/
|
|
527
|
+
type U128LeBytes = SubSubBrandedType<LeBytes, "U128LeBytes">;
|
|
528
|
+
/**
|
|
529
|
+
* 256-bit (32 bytes) little-endian byte array.
|
|
530
|
+
*
|
|
531
|
+
* @remarks
|
|
532
|
+
* - Size: 32 bytes
|
|
533
|
+
* - Value range: [0, 2^256 - 1]
|
|
534
|
+
* - Common use: SHA-256 hashes, elliptic curve scalars, private keys
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* ```typescript
|
|
538
|
+
* // A 256-bit cryptographic scalar
|
|
539
|
+
* const scalar = assertU256LeBytes(new Uint8Array(32));
|
|
540
|
+
* ```
|
|
541
|
+
*
|
|
542
|
+
* @see {@link assertU256LeBytes}
|
|
543
|
+
* @public
|
|
544
|
+
*/
|
|
545
|
+
type U256LeBytes = SubSubBrandedType<LeBytes, "U256LeBytes">;
|
|
546
|
+
/**
|
|
547
|
+
* 512-bit (64 bytes) little-endian byte array.
|
|
548
|
+
*
|
|
549
|
+
* @remarks
|
|
550
|
+
* - Size: 64 bytes
|
|
551
|
+
* - Value range: [0, 2^512 - 1]
|
|
552
|
+
* - Common use: SHA-512 hashes, extended keys
|
|
553
|
+
*
|
|
554
|
+
* @example
|
|
555
|
+
* ```typescript
|
|
556
|
+
* const bytes = assertU512LeBytes(new Uint8Array(64));
|
|
557
|
+
* ```
|
|
558
|
+
*
|
|
559
|
+
* @see {@link assertU512LeBytes}
|
|
560
|
+
* @public
|
|
561
|
+
*/
|
|
562
|
+
type U512LeBytes = SubSubBrandedType<LeBytes, "U512LeBytes">;
|
|
563
|
+
/**
|
|
564
|
+
* 1024-bit (128 bytes) little-endian byte array.
|
|
565
|
+
*
|
|
566
|
+
* @remarks
|
|
567
|
+
* - Size: 128 bytes
|
|
568
|
+
* - Value range: [0, 2^1024 - 1]
|
|
569
|
+
* - Common use: RSA keys, large cryptographic values
|
|
570
|
+
*
|
|
571
|
+
* @example
|
|
572
|
+
* ```typescript
|
|
573
|
+
* const bytes = assertU1024LeBytes(new Uint8Array(128));
|
|
574
|
+
* ```
|
|
575
|
+
*
|
|
576
|
+
* @see {@link assertU1024LeBytes}
|
|
577
|
+
* @public
|
|
578
|
+
*/
|
|
579
|
+
type U1024LeBytes = SubSubBrandedType<LeBytes, "U1024LeBytes">;
|
|
580
|
+
/**
|
|
581
|
+
* 8-bit (1 byte) big-endian byte array.
|
|
582
|
+
*
|
|
583
|
+
* Represents a single byte, which is endianness-agnostic.
|
|
584
|
+
* Included for API consistency with larger sizes.
|
|
585
|
+
*
|
|
586
|
+
* @remarks
|
|
587
|
+
* - Size: 1 byte
|
|
588
|
+
* - Value range: [0, 255] (0x00 to 0xFF)
|
|
589
|
+
*
|
|
590
|
+
* @example
|
|
591
|
+
* ```typescript
|
|
592
|
+
* const byte = assertU8BeBytes(new Uint8Array([0xFF]));
|
|
593
|
+
* ```
|
|
594
|
+
*
|
|
595
|
+
* @see {@link assertU8BeBytes}
|
|
596
|
+
* @public
|
|
597
|
+
*/
|
|
598
|
+
type U8BeBytes = SubSubBrandedType<BeBytes, "U8BeBytes">;
|
|
599
|
+
/**
|
|
600
|
+
* 16-bit (2 bytes) big-endian byte array.
|
|
601
|
+
*
|
|
602
|
+
* @remarks
|
|
603
|
+
* - Size: 2 bytes
|
|
604
|
+
* - Value range: [0, 65,535]
|
|
605
|
+
* - Common use: Network port numbers, protocol headers
|
|
606
|
+
*
|
|
607
|
+
* @example
|
|
608
|
+
* ```typescript
|
|
609
|
+
* // Value 0x1234 in big-endian (network byte order)
|
|
610
|
+
* const bytes = assertU16BeBytes(new Uint8Array([0x12, 0x34]));
|
|
611
|
+
* ```
|
|
612
|
+
*
|
|
613
|
+
* @see {@link assertU16BeBytes}
|
|
614
|
+
* @public
|
|
615
|
+
*/
|
|
616
|
+
type U16BeBytes = SubSubBrandedType<BeBytes, "U16BeBytes">;
|
|
617
|
+
/**
|
|
618
|
+
* 32-bit (4 bytes) big-endian byte array.
|
|
619
|
+
*
|
|
620
|
+
* @remarks
|
|
621
|
+
* - Size: 4 bytes
|
|
622
|
+
* - Value range: [0, 4,294,967,295]
|
|
623
|
+
* - Common use: Network addresses, protocol fields
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* ```typescript
|
|
627
|
+
* // Value 0x12345678 in big-endian
|
|
628
|
+
* const bytes = assertU32BeBytes(new Uint8Array([0x12, 0x34, 0x56, 0x78]));
|
|
629
|
+
* ```
|
|
630
|
+
*
|
|
631
|
+
* @see {@link assertU32BeBytes}
|
|
632
|
+
* @public
|
|
633
|
+
*/
|
|
634
|
+
type U32BeBytes = SubSubBrandedType<BeBytes, "U32BeBytes">;
|
|
635
|
+
/**
|
|
636
|
+
* 64-bit (8 bytes) big-endian byte array.
|
|
637
|
+
*
|
|
638
|
+
* @remarks
|
|
639
|
+
* - Size: 8 bytes
|
|
640
|
+
* - Value range: [0, 2^64 - 1]
|
|
641
|
+
*
|
|
642
|
+
* @example
|
|
643
|
+
* ```typescript
|
|
644
|
+
* const bytes = assertU64BeBytes(new Uint8Array(8));
|
|
645
|
+
* ```
|
|
646
|
+
*
|
|
647
|
+
* @see {@link assertU64BeBytes}
|
|
648
|
+
* @public
|
|
649
|
+
*/
|
|
650
|
+
type U64BeBytes = SubSubBrandedType<BeBytes, "U64BeBytes">;
|
|
651
|
+
/**
|
|
652
|
+
* 128-bit (16 bytes) big-endian byte array.
|
|
653
|
+
*
|
|
654
|
+
* @remarks
|
|
655
|
+
* - Size: 16 bytes
|
|
656
|
+
* - Value range: [0, 2^128 - 1]
|
|
657
|
+
* - Common use: UUIDs in standard format, IPv6 addresses
|
|
658
|
+
*
|
|
659
|
+
* @example
|
|
660
|
+
* ```typescript
|
|
661
|
+
* const bytes = assertU128BeBytes(new Uint8Array(16));
|
|
662
|
+
* ```
|
|
663
|
+
*
|
|
664
|
+
* @see {@link assertU128BeBytes}
|
|
665
|
+
* @public
|
|
666
|
+
*/
|
|
667
|
+
type U128BeBytes = SubSubBrandedType<BeBytes, "U128BeBytes">;
|
|
668
|
+
/**
|
|
669
|
+
* 256-bit (32 bytes) big-endian byte array.
|
|
670
|
+
*
|
|
671
|
+
* @remarks
|
|
672
|
+
* - Size: 32 bytes
|
|
673
|
+
* - Value range: [0, 2^256 - 1]
|
|
674
|
+
* - Common use: Ethereum addresses (20 bytes, but often padded), field elements
|
|
675
|
+
*
|
|
676
|
+
* @example
|
|
677
|
+
* ```typescript
|
|
678
|
+
* const bytes = assertU256BeBytes(new Uint8Array(32));
|
|
679
|
+
* ```
|
|
680
|
+
*
|
|
681
|
+
* @see {@link assertU256BeBytes}
|
|
682
|
+
* @public
|
|
683
|
+
*/
|
|
684
|
+
type U256BeBytes = SubSubBrandedType<BeBytes, "U256BeBytes">;
|
|
685
|
+
/**
|
|
686
|
+
* 512-bit (64 bytes) big-endian byte array.
|
|
687
|
+
*
|
|
688
|
+
* @remarks
|
|
689
|
+
* - Size: 64 bytes
|
|
690
|
+
* - Value range: [0, 2^512 - 1]
|
|
691
|
+
*
|
|
692
|
+
* @example
|
|
693
|
+
* ```typescript
|
|
694
|
+
* const bytes = assertU512BeBytes(new Uint8Array(64));
|
|
695
|
+
* ```
|
|
696
|
+
*
|
|
697
|
+
* @see {@link assertU512BeBytes}
|
|
698
|
+
* @public
|
|
699
|
+
*/
|
|
700
|
+
type U512BeBytes = SubSubBrandedType<BeBytes, "U512BeBytes">;
|
|
701
|
+
/**
|
|
702
|
+
* 1024-bit (128 bytes) big-endian byte array.
|
|
703
|
+
*
|
|
704
|
+
* @remarks
|
|
705
|
+
* - Size: 128 bytes
|
|
706
|
+
* - Value range: [0, 2^1024 - 1]
|
|
707
|
+
*
|
|
708
|
+
* @example
|
|
709
|
+
* ```typescript
|
|
710
|
+
* const bytes = assertU1024BeBytes(new Uint8Array(128));
|
|
711
|
+
* ```
|
|
712
|
+
*
|
|
713
|
+
* @see {@link assertU1024BeBytes}
|
|
714
|
+
* @public
|
|
715
|
+
*/
|
|
716
|
+
type U1024BeBytes = SubSubBrandedType<BeBytes, "U1024BeBytes">;
|
|
717
|
+
/**
|
|
718
|
+
* Base type for all unsigned integers.
|
|
719
|
+
*
|
|
720
|
+
* Unsigned integers represent non-negative whole numbers (0, 1, 2, ...).
|
|
721
|
+
* All specific unsigned integer types (`U8`, `U16`, etc.) are sub-brands of this type.
|
|
722
|
+
*
|
|
723
|
+
* @remarks
|
|
724
|
+
* - Uses JavaScript's `BigInt` for arbitrary precision arithmetic
|
|
725
|
+
* - No upper bound at the base type level (bounds are enforced by sub-types)
|
|
726
|
+
* - Guaranteed to be non-negative (>= 0)
|
|
727
|
+
*
|
|
728
|
+
* ## Type Hierarchy
|
|
729
|
+
* ```
|
|
730
|
+
* bigint
|
|
731
|
+
* └── UnsignedInteger (branded, value >= 0)
|
|
732
|
+
* ├── U8 (0 to 255)
|
|
733
|
+
* ├── U16 (0 to 65,535)
|
|
734
|
+
* ├── U32 (0 to 4,294,967,295)
|
|
735
|
+
* └── ...
|
|
736
|
+
* ```
|
|
737
|
+
*
|
|
738
|
+
* @example
|
|
739
|
+
* ```typescript
|
|
740
|
+
* const value = assertUnsignedInteger(12345n);
|
|
741
|
+
* // value is typed as UnsignedInteger
|
|
742
|
+
*
|
|
743
|
+
* // Can be assigned to functions expecting UnsignedInteger
|
|
744
|
+
* function processUnsigned(n: UnsignedInteger): void { ... }
|
|
745
|
+
* processUnsigned(value); // OK
|
|
746
|
+
* ```
|
|
747
|
+
*
|
|
748
|
+
* @see {@link assertUnsignedInteger}
|
|
749
|
+
* @public
|
|
750
|
+
*/
|
|
751
|
+
type UnsignedInteger = BrandedType<bigint, "UnsignedInteger">;
|
|
752
|
+
/**
|
|
753
|
+
* Base type for all signed integers.
|
|
754
|
+
*
|
|
755
|
+
* Signed integers represent whole numbers including negatives (..., -2, -1, 0, 1, 2, ...).
|
|
756
|
+
* All specific signed integer types (`I8`, `I16`, etc.) are sub-brands of this type.
|
|
757
|
+
*
|
|
758
|
+
* @remarks
|
|
759
|
+
* - Uses JavaScript's `BigInt` for arbitrary precision arithmetic
|
|
760
|
+
* - No bounds at the base type level (bounds are enforced by sub-types)
|
|
761
|
+
* - Two's complement representation is assumed for negative values
|
|
762
|
+
*
|
|
763
|
+
* ## Two's Complement
|
|
764
|
+
* Two's complement is the standard way computers represent signed integers:
|
|
765
|
+
* - Positive numbers are represented normally
|
|
766
|
+
* - Negative numbers: invert all bits and add 1
|
|
767
|
+
* - Example (8-bit): -1 = 0xFF, -128 = 0x80
|
|
768
|
+
*
|
|
769
|
+
* ## Type Hierarchy
|
|
770
|
+
* ```
|
|
771
|
+
* bigint
|
|
772
|
+
* └── SignedInteger (branded)
|
|
773
|
+
* ├── I8 (-128 to 127)
|
|
774
|
+
* ├── I16 (-32,768 to 32,767)
|
|
775
|
+
* ├── I32 (-2,147,483,648 to 2,147,483,647)
|
|
776
|
+
* └── ...
|
|
777
|
+
* ```
|
|
778
|
+
*
|
|
779
|
+
* @example
|
|
780
|
+
* ```typescript
|
|
781
|
+
* const value = assertSignedInteger(-12345n);
|
|
782
|
+
* // value is typed as SignedInteger
|
|
783
|
+
* ```
|
|
784
|
+
*
|
|
785
|
+
* @see {@link assertSignedInteger}
|
|
786
|
+
* @public
|
|
787
|
+
*/
|
|
788
|
+
type SignedInteger = BrandedType<bigint, "SignedInteger">;
|
|
789
|
+
/**
|
|
790
|
+
* 8-bit unsigned integer.
|
|
791
|
+
*
|
|
792
|
+
* @remarks
|
|
793
|
+
* - Bit width: 8 bits (1 byte)
|
|
794
|
+
* - Range: [0, 255] or [0x00, 0xFF]
|
|
795
|
+
* - Max value: 2^8 - 1 = 255
|
|
796
|
+
*
|
|
797
|
+
* ## Common Uses
|
|
798
|
+
* - Single byte values
|
|
799
|
+
* - Color channel values (RGB)
|
|
800
|
+
* - Character codes (ASCII)
|
|
801
|
+
* - Small counters and flags
|
|
802
|
+
*
|
|
803
|
+
* @example
|
|
804
|
+
* ```typescript
|
|
805
|
+
* const red: U8 = assertU8(255n); // Maximum red
|
|
806
|
+
* const green: U8 = assertU8(128n); // Medium green
|
|
807
|
+
* const blue: U8 = assertU8(0n); // No blue
|
|
808
|
+
* ```
|
|
809
|
+
*
|
|
810
|
+
* @see {@link assertU8}
|
|
811
|
+
* @see {@link U8_MAX}
|
|
812
|
+
* @public
|
|
813
|
+
*/
|
|
814
|
+
type U8 = SubBrandedType<UnsignedInteger, "U8">;
|
|
815
|
+
/**
|
|
816
|
+
* 16-bit unsigned integer.
|
|
817
|
+
*
|
|
818
|
+
* @remarks
|
|
819
|
+
* - Bit width: 16 bits (2 bytes)
|
|
820
|
+
* - Range: [0, 65,535] or [0x0000, 0xFFFF]
|
|
821
|
+
* - Max value: 2^16 - 1 = 65,535
|
|
822
|
+
*
|
|
823
|
+
* ## Common Uses
|
|
824
|
+
* - Port numbers (0-65535)
|
|
825
|
+
* - Unicode code points (BMP)
|
|
826
|
+
* - Audio samples (16-bit PCM)
|
|
827
|
+
* - Small array indices
|
|
828
|
+
*
|
|
829
|
+
* @example
|
|
830
|
+
* ```typescript
|
|
831
|
+
* const port: U16 = assertU16(443n); // HTTPS port
|
|
832
|
+
* const sample: U16 = assertU16(32768n); // Audio sample
|
|
833
|
+
* ```
|
|
834
|
+
*
|
|
835
|
+
* @see {@link assertU16}
|
|
836
|
+
* @see {@link U16_MAX}
|
|
837
|
+
* @public
|
|
838
|
+
*/
|
|
839
|
+
type U16 = SubBrandedType<UnsignedInteger, "U16">;
|
|
840
|
+
/**
|
|
841
|
+
* 32-bit unsigned integer.
|
|
842
|
+
*
|
|
843
|
+
* @remarks
|
|
844
|
+
* - Bit width: 32 bits (4 bytes)
|
|
845
|
+
* - Range: [0, 4,294,967,295] or [0x00000000, 0xFFFFFFFF]
|
|
846
|
+
* - Max value: 2^32 - 1 = 4,294,967,295 (~4.3 billion)
|
|
847
|
+
*
|
|
848
|
+
* ## Common Uses
|
|
849
|
+
* - IPv4 addresses
|
|
850
|
+
* - Unix timestamps (until 2038)
|
|
851
|
+
* - Array indices
|
|
852
|
+
* - Color values (RGBA packed)
|
|
853
|
+
* - CRC32 checksums
|
|
854
|
+
*
|
|
855
|
+
* @example
|
|
856
|
+
* ```typescript
|
|
857
|
+
* const timestamp: U32 = assertU32(1704067200n); // 2024-01-01 00:00:00 UTC
|
|
858
|
+
* const ipAddress: U32 = assertU32(0x7F000001n); // 127.0.0.1
|
|
859
|
+
* ```
|
|
860
|
+
*
|
|
861
|
+
* @see {@link assertU32}
|
|
862
|
+
* @see {@link U32_MAX}
|
|
863
|
+
* @public
|
|
864
|
+
*/
|
|
865
|
+
type U32 = SubBrandedType<UnsignedInteger, "U32">;
|
|
866
|
+
/**
|
|
867
|
+
* 64-bit unsigned integer.
|
|
868
|
+
*
|
|
869
|
+
* @remarks
|
|
870
|
+
* - Bit width: 64 bits (8 bytes)
|
|
871
|
+
* - Range: [0, 18,446,744,073,709,551,615]
|
|
872
|
+
* - Max value: 2^64 - 1 (~18.4 quintillion)
|
|
873
|
+
*
|
|
874
|
+
* ## Common Uses
|
|
875
|
+
* - High-precision timestamps (nanoseconds)
|
|
876
|
+
* - File sizes
|
|
877
|
+
* - Database primary keys
|
|
878
|
+
* - Memory addresses
|
|
879
|
+
* - Large counters
|
|
880
|
+
*
|
|
881
|
+
* @example
|
|
882
|
+
* ```typescript
|
|
883
|
+
* const nanoTimestamp: U64 = assertU64(1704067200000000000n);
|
|
884
|
+
* const fileSize: U64 = assertU64(1099511627776n); // 1 TiB
|
|
885
|
+
* ```
|
|
886
|
+
*
|
|
887
|
+
* @see {@link assertU64}
|
|
888
|
+
* @see {@link U64_MAX}
|
|
889
|
+
* @public
|
|
890
|
+
*/
|
|
891
|
+
type U64 = SubBrandedType<UnsignedInteger, "U64">;
|
|
892
|
+
/**
|
|
893
|
+
* 128-bit unsigned integer.
|
|
894
|
+
*
|
|
895
|
+
* @remarks
|
|
896
|
+
* - Bit width: 128 bits (16 bytes)
|
|
897
|
+
* - Range: [0, 2^128 - 1]
|
|
898
|
+
* - Max value: ~3.4 × 10^38
|
|
899
|
+
*
|
|
900
|
+
* ## Common Uses
|
|
901
|
+
* - UUIDs (128-bit identifiers)
|
|
902
|
+
* - IPv6 addresses
|
|
903
|
+
* - Cryptographic nonces
|
|
904
|
+
* - Very large counters
|
|
905
|
+
*
|
|
906
|
+
* @example
|
|
907
|
+
* ```typescript
|
|
908
|
+
* const uuid: U128 = assertU128(0x550e8400e29b41d4a716446655440000n);
|
|
909
|
+
* ```
|
|
910
|
+
*
|
|
911
|
+
* @see {@link assertU128}
|
|
912
|
+
* @see {@link U128_MAX}
|
|
913
|
+
* @public
|
|
914
|
+
*/
|
|
915
|
+
type U128 = SubBrandedType<UnsignedInteger, "U128">;
|
|
916
|
+
/**
|
|
917
|
+
* 256-bit unsigned integer.
|
|
918
|
+
*
|
|
919
|
+
* @remarks
|
|
920
|
+
* - Bit width: 256 bits (32 bytes)
|
|
921
|
+
* - Range: [0, 2^256 - 1]
|
|
922
|
+
* - Max value: ~1.16 × 10^77
|
|
923
|
+
*
|
|
924
|
+
* ## Common Uses
|
|
925
|
+
* - Elliptic curve field elements (BN254, secp256k1)
|
|
926
|
+
* - Cryptographic scalars and private keys
|
|
927
|
+
* - SHA-256 hash outputs
|
|
928
|
+
* - Blockchain addresses and balances
|
|
929
|
+
*
|
|
930
|
+
* ## Cryptographic Significance
|
|
931
|
+
* 256 bits provides approximately 128 bits of security for symmetric
|
|
932
|
+
* operations and is the standard size for modern elliptic curves.
|
|
933
|
+
*
|
|
934
|
+
* @example
|
|
935
|
+
* ```typescript
|
|
936
|
+
* // A private key (256-bit scalar)
|
|
937
|
+
* const privateKey: U256 = assertU256(
|
|
938
|
+
* 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdefn
|
|
939
|
+
* );
|
|
940
|
+
*
|
|
941
|
+
* // A SHA-256 hash interpreted as a number
|
|
942
|
+
* const hashValue: U256 = assertU256(hashAsNumber);
|
|
943
|
+
* ```
|
|
944
|
+
*
|
|
945
|
+
* @see {@link assertU256}
|
|
946
|
+
* @see {@link U256_MAX}
|
|
947
|
+
* @public
|
|
948
|
+
*/
|
|
949
|
+
type U256 = SubBrandedType<UnsignedInteger, "U256">;
|
|
950
|
+
/**
|
|
951
|
+
* 512-bit unsigned integer.
|
|
952
|
+
*
|
|
953
|
+
* @remarks
|
|
954
|
+
* - Bit width: 512 bits (64 bytes)
|
|
955
|
+
* - Range: [0, 2^512 - 1]
|
|
956
|
+
*
|
|
957
|
+
* ## Common Uses
|
|
958
|
+
* - SHA-512 hash outputs
|
|
959
|
+
* - Extended cryptographic keys
|
|
960
|
+
* - Large field elements
|
|
961
|
+
*
|
|
962
|
+
* @example
|
|
963
|
+
* ```typescript
|
|
964
|
+
* const hash: U512 = assertU512(sha512HashValue);
|
|
965
|
+
* ```
|
|
966
|
+
*
|
|
967
|
+
* @see {@link assertU512}
|
|
968
|
+
* @see {@link U512_MAX}
|
|
969
|
+
* @public
|
|
970
|
+
*/
|
|
971
|
+
type U512 = SubBrandedType<UnsignedInteger, "U512">;
|
|
972
|
+
/**
|
|
973
|
+
* 1024-bit unsigned integer.
|
|
974
|
+
*
|
|
975
|
+
* @remarks
|
|
976
|
+
* - Bit width: 1024 bits (128 bytes)
|
|
977
|
+
* - Range: [0, 2^1024 - 1]
|
|
978
|
+
*
|
|
979
|
+
* ## Common Uses
|
|
980
|
+
* - RSA key components
|
|
981
|
+
* - Very large cryptographic values
|
|
982
|
+
* - Pairing-based cryptography
|
|
983
|
+
*
|
|
984
|
+
* @example
|
|
985
|
+
* ```typescript
|
|
986
|
+
* const rsaModulus: U1024 = assertU1024(modulusValue);
|
|
987
|
+
* ```
|
|
988
|
+
*
|
|
989
|
+
* @see {@link assertU1024}
|
|
990
|
+
* @see {@link U1024_MAX}
|
|
991
|
+
* @public
|
|
992
|
+
*/
|
|
993
|
+
type U1024 = SubBrandedType<UnsignedInteger, "U1024">;
|
|
994
|
+
/**
|
|
995
|
+
* 8-bit signed integer.
|
|
996
|
+
*
|
|
997
|
+
* @remarks
|
|
998
|
+
* - Bit width: 8 bits (1 byte)
|
|
999
|
+
* - Range: [-128, 127] or [-2^7, 2^7 - 1]
|
|
1000
|
+
* - Uses two's complement representation
|
|
1001
|
+
*
|
|
1002
|
+
* ## Two's Complement Values
|
|
1003
|
+
* - 0x00 = 0
|
|
1004
|
+
* - 0x7F = 127 (maximum positive)
|
|
1005
|
+
* - 0x80 = -128 (minimum negative)
|
|
1006
|
+
* - 0xFF = -1
|
|
1007
|
+
*
|
|
1008
|
+
* @example
|
|
1009
|
+
* ```typescript
|
|
1010
|
+
* const positive: I8 = assertI8(127n);
|
|
1011
|
+
* const negative: I8 = assertI8(-128n);
|
|
1012
|
+
* const zero: I8 = assertI8(0n);
|
|
1013
|
+
* ```
|
|
1014
|
+
*
|
|
1015
|
+
* @see {@link assertI8}
|
|
1016
|
+
* @see {@link I8_MIN}
|
|
1017
|
+
* @see {@link I8_MAX}
|
|
1018
|
+
* @public
|
|
1019
|
+
*/
|
|
1020
|
+
type I8 = SubBrandedType<SignedInteger, "I8">;
|
|
1021
|
+
/**
|
|
1022
|
+
* 16-bit signed integer.
|
|
1023
|
+
*
|
|
1024
|
+
* @remarks
|
|
1025
|
+
* - Bit width: 16 bits (2 bytes)
|
|
1026
|
+
* - Range: [-32,768, 32,767] or [-2^15, 2^15 - 1]
|
|
1027
|
+
* - Uses two's complement representation
|
|
1028
|
+
*
|
|
1029
|
+
* ## Common Uses
|
|
1030
|
+
* - Audio samples (16-bit signed PCM)
|
|
1031
|
+
* - Small offsets and deltas
|
|
1032
|
+
*
|
|
1033
|
+
* @example
|
|
1034
|
+
* ```typescript
|
|
1035
|
+
* const audioSample: I16 = assertI16(-16384n);
|
|
1036
|
+
* ```
|
|
1037
|
+
*
|
|
1038
|
+
* @see {@link assertI16}
|
|
1039
|
+
* @see {@link I16_MIN}
|
|
1040
|
+
* @see {@link I16_MAX}
|
|
1041
|
+
* @public
|
|
1042
|
+
*/
|
|
1043
|
+
type I16 = SubBrandedType<SignedInteger, "I16">;
|
|
1044
|
+
/**
|
|
1045
|
+
* 32-bit signed integer.
|
|
1046
|
+
*
|
|
1047
|
+
* @remarks
|
|
1048
|
+
* - Bit width: 32 bits (4 bytes)
|
|
1049
|
+
* - Range: [-2,147,483,648, 2,147,483,647] or [-2^31, 2^31 - 1]
|
|
1050
|
+
* - Uses two's complement representation
|
|
1051
|
+
*
|
|
1052
|
+
* ## Common Uses
|
|
1053
|
+
* - General-purpose integers
|
|
1054
|
+
* - Array indices (with negative sentinel values)
|
|
1055
|
+
* - Differences and offsets
|
|
1056
|
+
*
|
|
1057
|
+
* @example
|
|
1058
|
+
* ```typescript
|
|
1059
|
+
* const offset: I32 = assertI32(-1000n);
|
|
1060
|
+
* const index: I32 = assertI32(42n);
|
|
1061
|
+
* ```
|
|
1062
|
+
*
|
|
1063
|
+
* @see {@link assertI32}
|
|
1064
|
+
* @see {@link I32_MIN}
|
|
1065
|
+
* @see {@link I32_MAX}
|
|
1066
|
+
* @public
|
|
1067
|
+
*/
|
|
1068
|
+
type I32 = SubBrandedType<SignedInteger, "I32">;
|
|
1069
|
+
/**
|
|
1070
|
+
* 64-bit signed integer.
|
|
1071
|
+
*
|
|
1072
|
+
* @remarks
|
|
1073
|
+
* - Bit width: 64 bits (8 bytes)
|
|
1074
|
+
* - Range: [-9,223,372,036,854,775,808, 9,223,372,036,854,775,807]
|
|
1075
|
+
* - Range: [-2^63, 2^63 - 1]
|
|
1076
|
+
* - Uses two's complement representation
|
|
1077
|
+
*
|
|
1078
|
+
* ## Common Uses
|
|
1079
|
+
* - Large offsets and differences
|
|
1080
|
+
* - Signed timestamps
|
|
1081
|
+
* - Financial calculations (with appropriate scaling)
|
|
1082
|
+
*
|
|
1083
|
+
* @example
|
|
1084
|
+
* ```typescript
|
|
1085
|
+
* const difference: I64 = assertI64(-1000000000000n);
|
|
1086
|
+
* ```
|
|
1087
|
+
*
|
|
1088
|
+
* @see {@link assertI64}
|
|
1089
|
+
* @see {@link I64_MIN}
|
|
1090
|
+
* @see {@link I64_MAX}
|
|
1091
|
+
* @public
|
|
1092
|
+
*/
|
|
1093
|
+
type I64 = SubBrandedType<SignedInteger, "I64">;
|
|
1094
|
+
/**
|
|
1095
|
+
* 128-bit signed integer.
|
|
1096
|
+
*
|
|
1097
|
+
* @remarks
|
|
1098
|
+
* - Bit width: 128 bits (16 bytes)
|
|
1099
|
+
* - Range: [-2^127, 2^127 - 1]
|
|
1100
|
+
* - Uses two's complement representation
|
|
1101
|
+
*
|
|
1102
|
+
* @example
|
|
1103
|
+
* ```typescript
|
|
1104
|
+
* const value: I128 = assertI128(-170141183460469231731687303715884105728n);
|
|
1105
|
+
* ```
|
|
1106
|
+
*
|
|
1107
|
+
* @see {@link assertI128}
|
|
1108
|
+
* @see {@link I128_MIN}
|
|
1109
|
+
* @see {@link I128_MAX}
|
|
1110
|
+
* @public
|
|
1111
|
+
*/
|
|
1112
|
+
type I128 = SubBrandedType<SignedInteger, "I128">;
|
|
1113
|
+
/**
|
|
1114
|
+
* 256-bit signed integer.
|
|
1115
|
+
*
|
|
1116
|
+
* @remarks
|
|
1117
|
+
* - Bit width: 256 bits (32 bytes)
|
|
1118
|
+
* - Range: [-2^255, 2^255 - 1]
|
|
1119
|
+
* - Uses two's complement representation
|
|
1120
|
+
*
|
|
1121
|
+
* ## Common Uses
|
|
1122
|
+
* - Signed cryptographic values
|
|
1123
|
+
* - Large signed arithmetic
|
|
1124
|
+
*
|
|
1125
|
+
* @example
|
|
1126
|
+
* ```typescript
|
|
1127
|
+
* const signedScalar: I256 = assertI256(-12345678901234567890n);
|
|
1128
|
+
* ```
|
|
1129
|
+
*
|
|
1130
|
+
* @see {@link assertI256}
|
|
1131
|
+
* @see {@link I256_MIN}
|
|
1132
|
+
* @see {@link I256_MAX}
|
|
1133
|
+
* @public
|
|
1134
|
+
*/
|
|
1135
|
+
type I256 = SubBrandedType<SignedInteger, "I256">;
|
|
1136
|
+
/**
|
|
1137
|
+
* 512-bit signed integer.
|
|
1138
|
+
*
|
|
1139
|
+
* @remarks
|
|
1140
|
+
* - Bit width: 512 bits (64 bytes)
|
|
1141
|
+
* - Range: [-2^511, 2^511 - 1]
|
|
1142
|
+
* - Uses two's complement representation
|
|
1143
|
+
*
|
|
1144
|
+
* @example
|
|
1145
|
+
* ```typescript
|
|
1146
|
+
* const value: I512 = assertI512(-1n);
|
|
1147
|
+
* ```
|
|
1148
|
+
*
|
|
1149
|
+
* @see {@link assertI512}
|
|
1150
|
+
* @see {@link I512_MIN}
|
|
1151
|
+
* @see {@link I512_MAX}
|
|
1152
|
+
* @public
|
|
1153
|
+
*/
|
|
1154
|
+
type I512 = SubBrandedType<SignedInteger, "I512">;
|
|
1155
|
+
/**
|
|
1156
|
+
* 1024-bit signed integer.
|
|
1157
|
+
*
|
|
1158
|
+
* @remarks
|
|
1159
|
+
* - Bit width: 1024 bits (128 bytes)
|
|
1160
|
+
* - Range: [-2^1023, 2^1023 - 1]
|
|
1161
|
+
* - Uses two's complement representation
|
|
1162
|
+
*
|
|
1163
|
+
* @example
|
|
1164
|
+
* ```typescript
|
|
1165
|
+
* const value: I1024 = assertI1024(0n);
|
|
1166
|
+
* ```
|
|
1167
|
+
*
|
|
1168
|
+
* @see {@link assertI1024}
|
|
1169
|
+
* @see {@link I1024_MIN}
|
|
1170
|
+
* @see {@link I1024_MAX}
|
|
1171
|
+
* @public
|
|
1172
|
+
*/
|
|
1173
|
+
type I1024 = SubBrandedType<SignedInteger, "I1024">;
|
|
1174
|
+
/**
|
|
1175
|
+
* Maximum value for an 8-bit unsigned integer.
|
|
1176
|
+
*
|
|
1177
|
+
* @remarks
|
|
1178
|
+
* - Value: 255 (0xFF)
|
|
1179
|
+
* - Formula: 2^8 - 1
|
|
1180
|
+
*
|
|
1181
|
+
* @example
|
|
1182
|
+
* ```typescript
|
|
1183
|
+
* if (value > U8_MAX) {
|
|
1184
|
+
* throw new Error("Value exceeds U8 range");
|
|
1185
|
+
* }
|
|
1186
|
+
* ```
|
|
1187
|
+
*
|
|
1188
|
+
* @see {@link U8}
|
|
1189
|
+
* @public
|
|
1190
|
+
*/
|
|
1191
|
+
declare const U8_MAX = 255n;
|
|
1192
|
+
/**
|
|
1193
|
+
* Maximum value for a 16-bit unsigned integer.
|
|
1194
|
+
*
|
|
1195
|
+
* @remarks
|
|
1196
|
+
* - Value: 65,535 (0xFFFF)
|
|
1197
|
+
* - Formula: 2^16 - 1
|
|
1198
|
+
*
|
|
1199
|
+
* @example
|
|
1200
|
+
* ```typescript
|
|
1201
|
+
* if (portNumber > U16_MAX) {
|
|
1202
|
+
* throw new Error("Port number exceeds U16 range");
|
|
1203
|
+
* }
|
|
1204
|
+
* ```
|
|
1205
|
+
*
|
|
1206
|
+
* @see {@link U16}
|
|
1207
|
+
* @public
|
|
1208
|
+
*/
|
|
1209
|
+
declare const U16_MAX = 65535n;
|
|
1210
|
+
/**
|
|
1211
|
+
* Maximum value for a 32-bit unsigned integer.
|
|
1212
|
+
*
|
|
1213
|
+
* @remarks
|
|
1214
|
+
* - Value: 4,294,967,295 (0xFFFFFFFF)
|
|
1215
|
+
* - Formula: 2^32 - 1
|
|
1216
|
+
*
|
|
1217
|
+
* @example
|
|
1218
|
+
* ```typescript
|
|
1219
|
+
* if (timestamp > U32_MAX) {
|
|
1220
|
+
* throw new Error("Timestamp exceeds U32 range (Year 2038 problem)");
|
|
1221
|
+
* }
|
|
1222
|
+
* ```
|
|
1223
|
+
*
|
|
1224
|
+
* @see {@link U32}
|
|
1225
|
+
* @public
|
|
1226
|
+
*/
|
|
1227
|
+
declare const U32_MAX = 4294967295n;
|
|
1228
|
+
/**
|
|
1229
|
+
* Maximum value for a 64-bit unsigned integer.
|
|
1230
|
+
*
|
|
1231
|
+
* @remarks
|
|
1232
|
+
* - Value: 18,446,744,073,709,551,615 (0xFFFFFFFFFFFFFFFF)
|
|
1233
|
+
* - Formula: 2^64 - 1
|
|
1234
|
+
*
|
|
1235
|
+
* @example
|
|
1236
|
+
* ```typescript
|
|
1237
|
+
* if (counter > U64_MAX) {
|
|
1238
|
+
* throw new Error("Counter exceeds U64 range");
|
|
1239
|
+
* }
|
|
1240
|
+
* ```
|
|
1241
|
+
*
|
|
1242
|
+
* @see {@link U64}
|
|
1243
|
+
* @public
|
|
1244
|
+
*/
|
|
1245
|
+
declare const U64_MAX = 18446744073709551615n;
|
|
1246
|
+
/**
|
|
1247
|
+
* Maximum value for a 128-bit unsigned integer.
|
|
1248
|
+
*
|
|
1249
|
+
* @remarks
|
|
1250
|
+
* - Value: 2^128 - 1
|
|
1251
|
+
* - Approximately: 3.4 × 10^38
|
|
1252
|
+
*
|
|
1253
|
+
* @example
|
|
1254
|
+
* ```typescript
|
|
1255
|
+
* if (value > U128_MAX) {
|
|
1256
|
+
* throw new Error("Value exceeds U128 range");
|
|
1257
|
+
* }
|
|
1258
|
+
* ```
|
|
1259
|
+
*
|
|
1260
|
+
* @see {@link U128}
|
|
1261
|
+
* @public
|
|
1262
|
+
*/
|
|
1263
|
+
declare const U128_MAX: bigint;
|
|
1264
|
+
/**
|
|
1265
|
+
* Maximum value for a 256-bit unsigned integer.
|
|
1266
|
+
*
|
|
1267
|
+
* @remarks
|
|
1268
|
+
* - Value: 2^256 - 1
|
|
1269
|
+
* - Approximately: 1.16 × 10^77
|
|
1270
|
+
* - This exceeds the estimated number of atoms in the observable universe
|
|
1271
|
+
*
|
|
1272
|
+
* @example
|
|
1273
|
+
* ```typescript
|
|
1274
|
+
* // Validate a field element is within BN254 range (BN254 prime < U256_MAX)
|
|
1275
|
+
* if (fieldElement > U256_MAX) {
|
|
1276
|
+
* throw new Error("Field element exceeds U256 range");
|
|
1277
|
+
* }
|
|
1278
|
+
* ```
|
|
1279
|
+
*
|
|
1280
|
+
* @see {@link U256}
|
|
1281
|
+
* @public
|
|
1282
|
+
*/
|
|
1283
|
+
declare const U256_MAX: bigint;
|
|
1284
|
+
/**
|
|
1285
|
+
* Maximum value for a 512-bit unsigned integer.
|
|
1286
|
+
*
|
|
1287
|
+
* @remarks
|
|
1288
|
+
* - Value: 2^512 - 1
|
|
1289
|
+
*
|
|
1290
|
+
* @example
|
|
1291
|
+
* ```typescript
|
|
1292
|
+
* if (value > U512_MAX) {
|
|
1293
|
+
* throw new Error("Value exceeds U512 range");
|
|
1294
|
+
* }
|
|
1295
|
+
* ```
|
|
1296
|
+
*
|
|
1297
|
+
* @see {@link U512}
|
|
1298
|
+
* @public
|
|
1299
|
+
*/
|
|
1300
|
+
declare const U512_MAX: bigint;
|
|
1301
|
+
/**
|
|
1302
|
+
* Maximum value for a 1024-bit unsigned integer.
|
|
1303
|
+
*
|
|
1304
|
+
* @remarks
|
|
1305
|
+
* - Value: 2^1024 - 1
|
|
1306
|
+
*
|
|
1307
|
+
* @example
|
|
1308
|
+
* ```typescript
|
|
1309
|
+
* if (rsaModulus > U1024_MAX) {
|
|
1310
|
+
* throw new Error("RSA modulus exceeds U1024 range");
|
|
1311
|
+
* }
|
|
1312
|
+
* ```
|
|
1313
|
+
*
|
|
1314
|
+
* @see {@link U1024}
|
|
1315
|
+
* @public
|
|
1316
|
+
*/
|
|
1317
|
+
declare const U1024_MAX: bigint;
|
|
1318
|
+
/**
|
|
1319
|
+
* Minimum value for an 8-bit signed integer.
|
|
1320
|
+
*
|
|
1321
|
+
* @remarks
|
|
1322
|
+
* - Value: -128 (0x80 in two's complement)
|
|
1323
|
+
* - Formula: -2^7
|
|
1324
|
+
*
|
|
1325
|
+
* @see {@link I8}
|
|
1326
|
+
* @public
|
|
1327
|
+
*/
|
|
1328
|
+
declare const I8_MIN: bigint;
|
|
1329
|
+
/**
|
|
1330
|
+
* Maximum value for an 8-bit signed integer.
|
|
1331
|
+
*
|
|
1332
|
+
* @remarks
|
|
1333
|
+
* - Value: 127 (0x7F)
|
|
1334
|
+
* - Formula: 2^7 - 1
|
|
1335
|
+
*
|
|
1336
|
+
* @see {@link I8}
|
|
1337
|
+
* @public
|
|
1338
|
+
*/
|
|
1339
|
+
declare const I8_MAX: bigint;
|
|
1340
|
+
/**
|
|
1341
|
+
* Minimum value for a 16-bit signed integer.
|
|
1342
|
+
*
|
|
1343
|
+
* @remarks
|
|
1344
|
+
* - Value: -32,768
|
|
1345
|
+
* - Formula: -2^15
|
|
1346
|
+
*
|
|
1347
|
+
* @see {@link I16}
|
|
1348
|
+
* @public
|
|
1349
|
+
*/
|
|
1350
|
+
declare const I16_MIN: bigint;
|
|
1351
|
+
/**
|
|
1352
|
+
* Maximum value for a 16-bit signed integer.
|
|
1353
|
+
*
|
|
1354
|
+
* @remarks
|
|
1355
|
+
* - Value: 32,767
|
|
1356
|
+
* - Formula: 2^15 - 1
|
|
1357
|
+
*
|
|
1358
|
+
* @see {@link I16}
|
|
1359
|
+
* @public
|
|
1360
|
+
*/
|
|
1361
|
+
declare const I16_MAX: bigint;
|
|
1362
|
+
/**
|
|
1363
|
+
* Minimum value for a 32-bit signed integer.
|
|
1364
|
+
*
|
|
1365
|
+
* @remarks
|
|
1366
|
+
* - Value: -2,147,483,648
|
|
1367
|
+
* - Formula: -2^31
|
|
1368
|
+
*
|
|
1369
|
+
* @see {@link I32}
|
|
1370
|
+
* @public
|
|
1371
|
+
*/
|
|
1372
|
+
declare const I32_MIN: bigint;
|
|
1373
|
+
/**
|
|
1374
|
+
* Maximum value for a 32-bit signed integer.
|
|
1375
|
+
*
|
|
1376
|
+
* @remarks
|
|
1377
|
+
* - Value: 2,147,483,647
|
|
1378
|
+
* - Formula: 2^31 - 1
|
|
1379
|
+
*
|
|
1380
|
+
* @see {@link I32}
|
|
1381
|
+
* @public
|
|
1382
|
+
*/
|
|
1383
|
+
declare const I32_MAX: bigint;
|
|
1384
|
+
/**
|
|
1385
|
+
* Minimum value for a 64-bit signed integer.
|
|
1386
|
+
*
|
|
1387
|
+
* @remarks
|
|
1388
|
+
* - Value: -9,223,372,036,854,775,808
|
|
1389
|
+
* - Formula: -2^63
|
|
1390
|
+
*
|
|
1391
|
+
* @see {@link I64}
|
|
1392
|
+
* @public
|
|
1393
|
+
*/
|
|
1394
|
+
declare const I64_MIN: bigint;
|
|
1395
|
+
/**
|
|
1396
|
+
* Maximum value for a 64-bit signed integer.
|
|
1397
|
+
*
|
|
1398
|
+
* @remarks
|
|
1399
|
+
* - Value: 9,223,372,036,854,775,807
|
|
1400
|
+
* - Formula: 2^63 - 1
|
|
1401
|
+
*
|
|
1402
|
+
* @see {@link I64}
|
|
1403
|
+
* @public
|
|
1404
|
+
*/
|
|
1405
|
+
declare const I64_MAX: bigint;
|
|
1406
|
+
/**
|
|
1407
|
+
* Minimum value for a 128-bit signed integer.
|
|
1408
|
+
*
|
|
1409
|
+
* @remarks
|
|
1410
|
+
* - Formula: -2^127
|
|
1411
|
+
*
|
|
1412
|
+
* @see {@link I128}
|
|
1413
|
+
* @public
|
|
1414
|
+
*/
|
|
1415
|
+
declare const I128_MIN: bigint;
|
|
1416
|
+
/**
|
|
1417
|
+
* Maximum value for a 128-bit signed integer.
|
|
1418
|
+
*
|
|
1419
|
+
* @remarks
|
|
1420
|
+
* - Formula: 2^127 - 1
|
|
1421
|
+
*
|
|
1422
|
+
* @see {@link I128}
|
|
1423
|
+
* @public
|
|
1424
|
+
*/
|
|
1425
|
+
declare const I128_MAX: bigint;
|
|
1426
|
+
/**
|
|
1427
|
+
* Minimum value for a 256-bit signed integer.
|
|
1428
|
+
*
|
|
1429
|
+
* @remarks
|
|
1430
|
+
* - Formula: -2^255
|
|
1431
|
+
*
|
|
1432
|
+
* @see {@link I256}
|
|
1433
|
+
* @public
|
|
1434
|
+
*/
|
|
1435
|
+
declare const I256_MIN: bigint;
|
|
1436
|
+
/**
|
|
1437
|
+
* Maximum value for a 256-bit signed integer.
|
|
1438
|
+
*
|
|
1439
|
+
* @remarks
|
|
1440
|
+
* - Formula: 2^255 - 1
|
|
1441
|
+
*
|
|
1442
|
+
* @see {@link I256}
|
|
1443
|
+
* @public
|
|
1444
|
+
*/
|
|
1445
|
+
declare const I256_MAX: bigint;
|
|
1446
|
+
/**
|
|
1447
|
+
* Minimum value for a 512-bit signed integer.
|
|
1448
|
+
*
|
|
1449
|
+
* @remarks
|
|
1450
|
+
* - Formula: -2^511
|
|
1451
|
+
*
|
|
1452
|
+
* @see {@link I512}
|
|
1453
|
+
* @public
|
|
1454
|
+
*/
|
|
1455
|
+
declare const I512_MIN: bigint;
|
|
1456
|
+
/**
|
|
1457
|
+
* Maximum value for a 512-bit signed integer.
|
|
1458
|
+
*
|
|
1459
|
+
* @remarks
|
|
1460
|
+
* - Formula: 2^511 - 1
|
|
1461
|
+
*
|
|
1462
|
+
* @see {@link I512}
|
|
1463
|
+
* @public
|
|
1464
|
+
*/
|
|
1465
|
+
declare const I512_MAX: bigint;
|
|
1466
|
+
/**
|
|
1467
|
+
* Minimum value for a 1024-bit signed integer.
|
|
1468
|
+
*
|
|
1469
|
+
* @remarks
|
|
1470
|
+
* - Formula: -2^1023
|
|
1471
|
+
*
|
|
1472
|
+
* @see {@link I1024}
|
|
1473
|
+
* @public
|
|
1474
|
+
*/
|
|
1475
|
+
declare const I1024_MIN: bigint;
|
|
1476
|
+
/**
|
|
1477
|
+
* Maximum value for a 1024-bit signed integer.
|
|
1478
|
+
*
|
|
1479
|
+
* @remarks
|
|
1480
|
+
* - Formula: 2^1023 - 1
|
|
1481
|
+
*
|
|
1482
|
+
* @see {@link I1024}
|
|
1483
|
+
* @public
|
|
1484
|
+
*/
|
|
1485
|
+
declare const I1024_MAX: bigint;
|
|
1486
|
+
/**
|
|
1487
|
+
* Expected byte length for 8-bit values.
|
|
1488
|
+
*
|
|
1489
|
+
* @remarks Value: 1 byte
|
|
1490
|
+
*
|
|
1491
|
+
* @example
|
|
1492
|
+
* ```typescript
|
|
1493
|
+
* const bytes = new Uint8Array(U8_BYTE_LENGTH); // 1 byte
|
|
1494
|
+
* ```
|
|
1495
|
+
*
|
|
1496
|
+
* @see {@link U8LeBytes}
|
|
1497
|
+
* @see {@link U8BeBytes}
|
|
1498
|
+
* @public
|
|
1499
|
+
*/
|
|
1500
|
+
declare const U8_BYTE_LENGTH = 1;
|
|
1501
|
+
/**
|
|
1502
|
+
* Expected byte length for 16-bit values.
|
|
1503
|
+
*
|
|
1504
|
+
* @remarks Value: 2 bytes
|
|
1505
|
+
*
|
|
1506
|
+
* @example
|
|
1507
|
+
* ```typescript
|
|
1508
|
+
* const bytes = new Uint8Array(U16_BYTE_LENGTH); // 2 bytes
|
|
1509
|
+
* ```
|
|
1510
|
+
*
|
|
1511
|
+
* @see {@link U16LeBytes}
|
|
1512
|
+
* @see {@link U16BeBytes}
|
|
1513
|
+
* @public
|
|
1514
|
+
*/
|
|
1515
|
+
declare const U16_BYTE_LENGTH = 2;
|
|
1516
|
+
/**
|
|
1517
|
+
* Expected byte length for 32-bit values.
|
|
1518
|
+
*
|
|
1519
|
+
* @remarks Value: 4 bytes
|
|
1520
|
+
*
|
|
1521
|
+
* @example
|
|
1522
|
+
* ```typescript
|
|
1523
|
+
* const bytes = new Uint8Array(U32_BYTE_LENGTH); // 4 bytes
|
|
1524
|
+
* ```
|
|
1525
|
+
*
|
|
1526
|
+
* @see {@link U32LeBytes}
|
|
1527
|
+
* @see {@link U32BeBytes}
|
|
1528
|
+
* @public
|
|
1529
|
+
*/
|
|
1530
|
+
declare const U32_BYTE_LENGTH = 4;
|
|
1531
|
+
/**
|
|
1532
|
+
* Expected byte length for 64-bit values.
|
|
1533
|
+
*
|
|
1534
|
+
* @remarks Value: 8 bytes
|
|
1535
|
+
*
|
|
1536
|
+
* @example
|
|
1537
|
+
* ```typescript
|
|
1538
|
+
* const bytes = new Uint8Array(U64_BYTE_LENGTH); // 8 bytes
|
|
1539
|
+
* ```
|
|
1540
|
+
*
|
|
1541
|
+
* @see {@link U64LeBytes}
|
|
1542
|
+
* @see {@link U64BeBytes}
|
|
1543
|
+
* @public
|
|
1544
|
+
*/
|
|
1545
|
+
declare const U64_BYTE_LENGTH = 8;
|
|
1546
|
+
/**
|
|
1547
|
+
* Expected byte length for 128-bit values.
|
|
1548
|
+
*
|
|
1549
|
+
* @remarks Value: 16 bytes
|
|
1550
|
+
*
|
|
1551
|
+
* @example
|
|
1552
|
+
* ```typescript
|
|
1553
|
+
* const bytes = new Uint8Array(U128_BYTE_LENGTH); // 16 bytes
|
|
1554
|
+
* ```
|
|
1555
|
+
*
|
|
1556
|
+
* @see {@link U128LeBytes}
|
|
1557
|
+
* @see {@link U128BeBytes}
|
|
1558
|
+
* @public
|
|
1559
|
+
*/
|
|
1560
|
+
declare const U128_BYTE_LENGTH = 16;
|
|
1561
|
+
/**
|
|
1562
|
+
* Expected byte length for 256-bit values.
|
|
1563
|
+
*
|
|
1564
|
+
* @remarks Value: 32 bytes
|
|
1565
|
+
*
|
|
1566
|
+
* @example
|
|
1567
|
+
* ```typescript
|
|
1568
|
+
* // Allocate a buffer for a 256-bit cryptographic scalar
|
|
1569
|
+
* const scalarBytes = new Uint8Array(U256_BYTE_LENGTH); // 32 bytes
|
|
1570
|
+
* ```
|
|
1571
|
+
*
|
|
1572
|
+
* @see {@link U256LeBytes}
|
|
1573
|
+
* @see {@link U256BeBytes}
|
|
1574
|
+
* @public
|
|
1575
|
+
*/
|
|
1576
|
+
declare const U256_BYTE_LENGTH = 32;
|
|
1577
|
+
/**
|
|
1578
|
+
* Expected byte length for 512-bit values.
|
|
1579
|
+
*
|
|
1580
|
+
* @remarks Value: 64 bytes
|
|
1581
|
+
*
|
|
1582
|
+
* @example
|
|
1583
|
+
* ```typescript
|
|
1584
|
+
* const bytes = new Uint8Array(U512_BYTE_LENGTH); // 64 bytes
|
|
1585
|
+
* ```
|
|
1586
|
+
*
|
|
1587
|
+
* @see {@link U512LeBytes}
|
|
1588
|
+
* @see {@link U512BeBytes}
|
|
1589
|
+
* @public
|
|
1590
|
+
*/
|
|
1591
|
+
declare const U512_BYTE_LENGTH = 64;
|
|
1592
|
+
/**
|
|
1593
|
+
* Expected byte length for 1024-bit values.
|
|
1594
|
+
*
|
|
1595
|
+
* @remarks Value: 128 bytes
|
|
1596
|
+
*
|
|
1597
|
+
* @example
|
|
1598
|
+
* ```typescript
|
|
1599
|
+
* const bytes = new Uint8Array(U1024_BYTE_LENGTH); // 128 bytes
|
|
1600
|
+
* ```
|
|
1601
|
+
*
|
|
1602
|
+
* @see {@link U1024LeBytes}
|
|
1603
|
+
* @see {@link U1024BeBytes}
|
|
1604
|
+
* @public
|
|
1605
|
+
*/
|
|
1606
|
+
declare const U1024_BYTE_LENGTH = 128;
|
|
1607
|
+
/**
|
|
1608
|
+
* Error thrown when a value fails a mathematical type assertion.
|
|
1609
|
+
*
|
|
1610
|
+
* This error provides detailed information about why an assertion failed,
|
|
1611
|
+
* including the actual value, the expected type, and the constraint that
|
|
1612
|
+
* was violated.
|
|
1613
|
+
*
|
|
1614
|
+
* @remarks
|
|
1615
|
+
* All assertion functions in this module throw this error type on failure.
|
|
1616
|
+
* You can catch this specific error type to handle assertion failures
|
|
1617
|
+
* differently from other errors.
|
|
1618
|
+
*
|
|
1619
|
+
* ## Error Properties
|
|
1620
|
+
* - `value`: The actual value that failed assertion
|
|
1621
|
+
* - `expectedType`: The type that was expected (e.g., "U8", "U256LeBytes")
|
|
1622
|
+
* - `constraint`: Optional description of the constraint that was violated
|
|
1623
|
+
*
|
|
1624
|
+
* @example
|
|
1625
|
+
* ```typescript
|
|
1626
|
+
* import { assertU8, MathematicsAssertionError } from "./mathematics";
|
|
1627
|
+
*
|
|
1628
|
+
* try {
|
|
1629
|
+
* const value = assertU8(256n); // Will throw
|
|
1630
|
+
* } catch (error) {
|
|
1631
|
+
* if (error instanceof MathematicsAssertionError) {
|
|
1632
|
+
* console.error(`Assertion failed for ${error.expectedType}`);
|
|
1633
|
+
* console.error(`Value: ${error.value}`);
|
|
1634
|
+
* console.error(`Constraint: ${error.constraint}`);
|
|
1635
|
+
* // Output:
|
|
1636
|
+
* // Assertion failed for U8
|
|
1637
|
+
* // Value: 256
|
|
1638
|
+
* // Constraint: 0 <= value <= 255
|
|
1639
|
+
* }
|
|
1640
|
+
* }
|
|
1641
|
+
* ```
|
|
1642
|
+
*
|
|
1643
|
+
* @example
|
|
1644
|
+
* ```typescript
|
|
1645
|
+
* // Custom error handling
|
|
1646
|
+
* function safeParseU8(value: bigint): U8 | null {
|
|
1647
|
+
* try {
|
|
1648
|
+
* return assertU8(value);
|
|
1649
|
+
* } catch (error) {
|
|
1650
|
+
* if (error instanceof MathematicsAssertionError) {
|
|
1651
|
+
* console.warn(`Invalid U8 value: ${value}`);
|
|
1652
|
+
* return null;
|
|
1653
|
+
* }
|
|
1654
|
+
* throw error; // Re-throw unexpected errors
|
|
1655
|
+
* }
|
|
1656
|
+
* }
|
|
1657
|
+
* ```
|
|
1658
|
+
*
|
|
1659
|
+
* @sealed
|
|
1660
|
+
* @public
|
|
1661
|
+
*/
|
|
1662
|
+
declare class MathematicsAssertionError extends Error {
|
|
1663
|
+
/**
|
|
1664
|
+
* The actual value that failed the assertion.
|
|
1665
|
+
*
|
|
1666
|
+
* @remarks
|
|
1667
|
+
* This can be any type depending on what was being asserted.
|
|
1668
|
+
* For integer assertions, this will be a `bigint`.
|
|
1669
|
+
* For byte array assertions, this will be a `Uint8Array`.
|
|
1670
|
+
*
|
|
1671
|
+
* @readonly
|
|
1672
|
+
*/
|
|
1673
|
+
readonly value: unknown;
|
|
1674
|
+
/**
|
|
1675
|
+
* The type that was expected.
|
|
1676
|
+
*
|
|
1677
|
+
* @remarks
|
|
1678
|
+
* This is the branded type name (e.g., "U8", "U256", "U32LeBytes").
|
|
1679
|
+
*
|
|
1680
|
+
* @readonly
|
|
1681
|
+
*/
|
|
1682
|
+
readonly expectedType: string;
|
|
1683
|
+
/**
|
|
1684
|
+
* Description of the constraint that was violated.
|
|
1685
|
+
*
|
|
1686
|
+
* @remarks
|
|
1687
|
+
* This is `undefined` if only a type check failed (e.g., expected bigint, got string).
|
|
1688
|
+
* It contains a human-readable constraint description when a value is out of range.
|
|
1689
|
+
*
|
|
1690
|
+
* @example
|
|
1691
|
+
* - "0 <= value <= 255" for U8 range violations
|
|
1692
|
+
* - "length === 32" for U256LeBytes length violations
|
|
1693
|
+
* - "value >= 0" for negative unsigned integer attempts
|
|
1694
|
+
*
|
|
1695
|
+
* @readonly
|
|
1696
|
+
*/
|
|
1697
|
+
readonly constraint: string | undefined;
|
|
1698
|
+
/**
|
|
1699
|
+
* Creates a new MathematicsAssertionError.
|
|
1700
|
+
*
|
|
1701
|
+
* @param message - Human-readable error message
|
|
1702
|
+
* @param options - Error details
|
|
1703
|
+
* @param options.value - The value that failed assertion
|
|
1704
|
+
* @param options.expectedType - The expected type name
|
|
1705
|
+
* @param options.constraint - Optional constraint description
|
|
1706
|
+
*/
|
|
1707
|
+
constructor(message: string, options: {
|
|
1708
|
+
value: unknown;
|
|
1709
|
+
expectedType: string;
|
|
1710
|
+
constraint?: string;
|
|
1711
|
+
});
|
|
1712
|
+
}
|
|
1713
|
+
/**
|
|
1714
|
+
* Asserts that a value is a valid Uint8Array and narrows it to `Bytes`.
|
|
1715
|
+
*
|
|
1716
|
+
* This is the base assertion for all byte array types. It validates that
|
|
1717
|
+
* the input is a `Uint8Array` instance.
|
|
1718
|
+
*
|
|
1719
|
+
* @param value - The Uint8Array to assert
|
|
1720
|
+
* @throws {MathematicsAssertionError} If the value is not a Uint8Array
|
|
1721
|
+
*
|
|
1722
|
+
* @example
|
|
1723
|
+
* ```typescript
|
|
1724
|
+
* const rawBytes = new Uint8Array([0x01, 0x02, 0x03]);
|
|
1725
|
+
* assertBytes(rawBytes);
|
|
1726
|
+
* // rawBytes is now typed as Bytes
|
|
1727
|
+
* ```
|
|
1728
|
+
*
|
|
1729
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1730
|
+
* @see {@link Bytes}
|
|
1731
|
+
* @public
|
|
1732
|
+
*/
|
|
1733
|
+
declare function assertBytes(value: Uint8Array): asserts value is Bytes;
|
|
1734
|
+
/**
|
|
1735
|
+
* Asserts that a value is a valid Uint8Array and narrows it to `LeBytes`.
|
|
1736
|
+
*
|
|
1737
|
+
* This function validates that the input is a `Uint8Array` instance and
|
|
1738
|
+
* uses TypeScript's type narrowing to treat it as a little-endian byte array.
|
|
1739
|
+
* No length validation is performed.
|
|
1740
|
+
*
|
|
1741
|
+
* @param value - The Uint8Array to assert
|
|
1742
|
+
* @throws {MathematicsAssertionError} If the value is not a Uint8Array
|
|
1743
|
+
*
|
|
1744
|
+
* @remarks
|
|
1745
|
+
* This is the base assertion for little-endian byte arrays.
|
|
1746
|
+
* For sized byte arrays, use the specific assertions like `assertU32LeBytes`.
|
|
1747
|
+
*
|
|
1748
|
+
* @example
|
|
1749
|
+
* ```typescript
|
|
1750
|
+
* // Brand a byte array as little-endian
|
|
1751
|
+
* const rawBytes = new Uint8Array([0x78, 0x56, 0x34, 0x12]);
|
|
1752
|
+
* assertLeBytes(rawBytes);
|
|
1753
|
+
* // rawBytes is now typed as LeBytes
|
|
1754
|
+
*
|
|
1755
|
+
* // The branded type prevents mixing with big-endian
|
|
1756
|
+
* function processLittleEndian(bytes: LeBytes): void { ... }
|
|
1757
|
+
* processLittleEndian(rawBytes); // OK after assertion
|
|
1758
|
+
* ```
|
|
1759
|
+
*
|
|
1760
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1761
|
+
* @see {@link LeBytes}
|
|
1762
|
+
* @public
|
|
1763
|
+
*/
|
|
1764
|
+
declare function assertLeBytes(value: Uint8Array): asserts value is LeBytes;
|
|
1765
|
+
/**
|
|
1766
|
+
* Asserts that a value is a valid Uint8Array and narrows it to `BeBytes`.
|
|
1767
|
+
*
|
|
1768
|
+
* This function validates that the input is a `Uint8Array` instance and
|
|
1769
|
+
* uses TypeScript's type narrowing to treat it as a big-endian byte array.
|
|
1770
|
+
* No length validation is performed.
|
|
1771
|
+
*
|
|
1772
|
+
* @param value - The Uint8Array to assert
|
|
1773
|
+
* @throws {MathematicsAssertionError} If the value is not a Uint8Array
|
|
1774
|
+
*
|
|
1775
|
+
* @remarks
|
|
1776
|
+
* This is the base assertion for big-endian byte arrays.
|
|
1777
|
+
* For sized byte arrays, use the specific assertions like `assertU32BeBytes`.
|
|
1778
|
+
*
|
|
1779
|
+
* @example
|
|
1780
|
+
* ```typescript
|
|
1781
|
+
* // Brand a byte array as big-endian (network byte order)
|
|
1782
|
+
* const rawBytes = new Uint8Array([0x12, 0x34, 0x56, 0x78]);
|
|
1783
|
+
* assertBeBytes(rawBytes);
|
|
1784
|
+
* // rawBytes is now typed as BeBytes
|
|
1785
|
+
*
|
|
1786
|
+
* // Use for network protocol data
|
|
1787
|
+
* function sendNetworkData(bytes: BeBytes): void { ... }
|
|
1788
|
+
* sendNetworkData(rawBytes); // OK after assertion
|
|
1789
|
+
* ```
|
|
1790
|
+
*
|
|
1791
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1792
|
+
* @see {@link BeBytes}
|
|
1793
|
+
* @public
|
|
1794
|
+
*/
|
|
1795
|
+
declare function assertBeBytes(value: Uint8Array): asserts value is BeBytes;
|
|
1796
|
+
/**
|
|
1797
|
+
* Asserts that a value is a 1-byte little-endian byte array.
|
|
1798
|
+
*
|
|
1799
|
+
* @param value - The Uint8Array to assert (must be exactly 1 byte)
|
|
1800
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1801
|
+
*
|
|
1802
|
+
* @example
|
|
1803
|
+
* ```typescript
|
|
1804
|
+
* const byte = new Uint8Array([0xFF]);
|
|
1805
|
+
* assertU8LeBytes(byte);
|
|
1806
|
+
* // byte is now typed as U8LeBytes
|
|
1807
|
+
* ```
|
|
1808
|
+
*
|
|
1809
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1810
|
+
* @see {@link U8LeBytes}
|
|
1811
|
+
* @public
|
|
1812
|
+
*/
|
|
1813
|
+
declare function assertU8LeBytes(value: Uint8Array): asserts value is U8LeBytes;
|
|
1814
|
+
/**
|
|
1815
|
+
* Asserts that a value is a 2-byte little-endian byte array.
|
|
1816
|
+
*
|
|
1817
|
+
* @param value - The Uint8Array to assert (must be exactly 2 bytes)
|
|
1818
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1819
|
+
*
|
|
1820
|
+
* @example
|
|
1821
|
+
* ```typescript
|
|
1822
|
+
* // 0x1234 in little-endian
|
|
1823
|
+
* const bytes = new Uint8Array([0x34, 0x12]);
|
|
1824
|
+
* assertU16LeBytes(bytes);
|
|
1825
|
+
* // bytes is now typed as U16LeBytes
|
|
1826
|
+
* ```
|
|
1827
|
+
*
|
|
1828
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1829
|
+
* @see {@link U16LeBytes}
|
|
1830
|
+
* @public
|
|
1831
|
+
*/
|
|
1832
|
+
declare function assertU16LeBytes(value: Uint8Array): asserts value is U16LeBytes;
|
|
1833
|
+
/**
|
|
1834
|
+
* Asserts that a value is a 4-byte little-endian byte array.
|
|
1835
|
+
*
|
|
1836
|
+
* @param value - The Uint8Array to assert (must be exactly 4 bytes)
|
|
1837
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1838
|
+
*
|
|
1839
|
+
* @example
|
|
1840
|
+
* ```typescript
|
|
1841
|
+
* // 0x12345678 in little-endian
|
|
1842
|
+
* const bytes = new Uint8Array([0x78, 0x56, 0x34, 0x12]);
|
|
1843
|
+
* assertU32LeBytes(bytes);
|
|
1844
|
+
* // bytes is now typed as U32LeBytes
|
|
1845
|
+
* ```
|
|
1846
|
+
*
|
|
1847
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1848
|
+
* @see {@link U32LeBytes}
|
|
1849
|
+
* @public
|
|
1850
|
+
*/
|
|
1851
|
+
declare function assertU32LeBytes(value: Uint8Array): asserts value is U32LeBytes;
|
|
1852
|
+
/**
|
|
1853
|
+
* Asserts that a value is an 8-byte little-endian byte array.
|
|
1854
|
+
*
|
|
1855
|
+
* @param value - The Uint8Array to assert (must be exactly 8 bytes)
|
|
1856
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1857
|
+
*
|
|
1858
|
+
* @example
|
|
1859
|
+
* ```typescript
|
|
1860
|
+
* const bytes = new Uint8Array(8);
|
|
1861
|
+
* assertU64LeBytes(bytes);
|
|
1862
|
+
* // bytes is now typed as U64LeBytes
|
|
1863
|
+
* ```
|
|
1864
|
+
*
|
|
1865
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1866
|
+
* @see {@link U64LeBytes}
|
|
1867
|
+
* @public
|
|
1868
|
+
*/
|
|
1869
|
+
declare function assertU64LeBytes(value: Uint8Array): asserts value is U64LeBytes;
|
|
1870
|
+
/**
|
|
1871
|
+
* Asserts that a value is a 16-byte little-endian byte array.
|
|
1872
|
+
*
|
|
1873
|
+
* @param value - The Uint8Array to assert (must be exactly 16 bytes)
|
|
1874
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1875
|
+
*
|
|
1876
|
+
* @example
|
|
1877
|
+
* ```typescript
|
|
1878
|
+
* const bytes = new Uint8Array(16);
|
|
1879
|
+
* assertU128LeBytes(bytes);
|
|
1880
|
+
* // bytes is now typed as U128LeBytes
|
|
1881
|
+
* ```
|
|
1882
|
+
*
|
|
1883
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1884
|
+
* @see {@link U128LeBytes}
|
|
1885
|
+
* @public
|
|
1886
|
+
*/
|
|
1887
|
+
declare function assertU128LeBytes(value: Uint8Array): asserts value is U128LeBytes;
|
|
1888
|
+
/**
|
|
1889
|
+
* Asserts that a value is a 32-byte little-endian byte array.
|
|
1890
|
+
*
|
|
1891
|
+
* @param value - The Uint8Array to assert (must be exactly 32 bytes)
|
|
1892
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1893
|
+
*
|
|
1894
|
+
* @remarks
|
|
1895
|
+
* 32-byte arrays are commonly used for:
|
|
1896
|
+
* - SHA-256 hash outputs
|
|
1897
|
+
* - Elliptic curve scalars
|
|
1898
|
+
* - Private keys
|
|
1899
|
+
*
|
|
1900
|
+
* @example
|
|
1901
|
+
* ```typescript
|
|
1902
|
+
* // Create a 256-bit scalar representation
|
|
1903
|
+
* const scalar = new Uint8Array(32);
|
|
1904
|
+
* assertU256LeBytes(scalar);
|
|
1905
|
+
* // scalar is now typed as U256LeBytes
|
|
1906
|
+
* ```
|
|
1907
|
+
*
|
|
1908
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1909
|
+
* @see {@link U256LeBytes}
|
|
1910
|
+
* @public
|
|
1911
|
+
*/
|
|
1912
|
+
declare function assertU256LeBytes(value: Uint8Array): asserts value is U256LeBytes;
|
|
1913
|
+
/**
|
|
1914
|
+
* Asserts that a value is a 64-byte little-endian byte array.
|
|
1915
|
+
*
|
|
1916
|
+
* @param value - The Uint8Array to assert (must be exactly 64 bytes)
|
|
1917
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1918
|
+
*
|
|
1919
|
+
* @example
|
|
1920
|
+
* ```typescript
|
|
1921
|
+
* const bytes = new Uint8Array(64);
|
|
1922
|
+
* assertU512LeBytes(bytes);
|
|
1923
|
+
* // bytes is now typed as U512LeBytes
|
|
1924
|
+
* ```
|
|
1925
|
+
*
|
|
1926
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1927
|
+
* @see {@link U512LeBytes}
|
|
1928
|
+
* @public
|
|
1929
|
+
*/
|
|
1930
|
+
declare function assertU512LeBytes(value: Uint8Array): asserts value is U512LeBytes;
|
|
1931
|
+
/**
|
|
1932
|
+
* Asserts that a value is a 128-byte little-endian byte array.
|
|
1933
|
+
*
|
|
1934
|
+
* @param value - The Uint8Array to assert (must be exactly 128 bytes)
|
|
1935
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1936
|
+
*
|
|
1937
|
+
* @example
|
|
1938
|
+
* ```typescript
|
|
1939
|
+
* const bytes = new Uint8Array(128);
|
|
1940
|
+
* assertU1024LeBytes(bytes);
|
|
1941
|
+
* // bytes is now typed as U1024LeBytes
|
|
1942
|
+
* ```
|
|
1943
|
+
*
|
|
1944
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1945
|
+
* @see {@link U1024LeBytes}
|
|
1946
|
+
* @public
|
|
1947
|
+
*/
|
|
1948
|
+
declare function assertU1024LeBytes(value: Uint8Array): asserts value is U1024LeBytes;
|
|
1949
|
+
/**
|
|
1950
|
+
* Asserts that a value is a 1-byte big-endian byte array.
|
|
1951
|
+
*
|
|
1952
|
+
* @param value - The Uint8Array to assert (must be exactly 1 byte)
|
|
1953
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1954
|
+
*
|
|
1955
|
+
* @example
|
|
1956
|
+
* ```typescript
|
|
1957
|
+
* const byte = new Uint8Array([0xFF]);
|
|
1958
|
+
* assertU8BeBytes(byte);
|
|
1959
|
+
* // byte is now typed as U8BeBytes
|
|
1960
|
+
* ```
|
|
1961
|
+
*
|
|
1962
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1963
|
+
* @see {@link U8BeBytes}
|
|
1964
|
+
* @public
|
|
1965
|
+
*/
|
|
1966
|
+
declare function assertU8BeBytes(value: Uint8Array): asserts value is U8BeBytes;
|
|
1967
|
+
/**
|
|
1968
|
+
* Asserts that a value is a 2-byte big-endian byte array.
|
|
1969
|
+
*
|
|
1970
|
+
* @param value - The Uint8Array to assert (must be exactly 2 bytes)
|
|
1971
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1972
|
+
*
|
|
1973
|
+
* @example
|
|
1974
|
+
* ```typescript
|
|
1975
|
+
* // Network port 443 in big-endian
|
|
1976
|
+
* const portBytes = new Uint8Array([0x01, 0xBB]);
|
|
1977
|
+
* assertU16BeBytes(portBytes);
|
|
1978
|
+
* // portBytes is now typed as U16BeBytes
|
|
1979
|
+
* ```
|
|
1980
|
+
*
|
|
1981
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
1982
|
+
* @see {@link U16BeBytes}
|
|
1983
|
+
* @public
|
|
1984
|
+
*/
|
|
1985
|
+
declare function assertU16BeBytes(value: Uint8Array): asserts value is U16BeBytes;
|
|
1986
|
+
/**
|
|
1987
|
+
* Asserts that a value is a 4-byte big-endian byte array.
|
|
1988
|
+
*
|
|
1989
|
+
* @param value - The Uint8Array to assert (must be exactly 4 bytes)
|
|
1990
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
1991
|
+
*
|
|
1992
|
+
* @example
|
|
1993
|
+
* ```typescript
|
|
1994
|
+
* // IP address 127.0.0.1 in big-endian
|
|
1995
|
+
* const ipBytes = new Uint8Array([127, 0, 0, 1]);
|
|
1996
|
+
* assertU32BeBytes(ipBytes);
|
|
1997
|
+
* // ipBytes is now typed as U32BeBytes
|
|
1998
|
+
* ```
|
|
1999
|
+
*
|
|
2000
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2001
|
+
* @see {@link U32BeBytes}
|
|
2002
|
+
* @public
|
|
2003
|
+
*/
|
|
2004
|
+
declare function assertU32BeBytes(value: Uint8Array): asserts value is U32BeBytes;
|
|
2005
|
+
/**
|
|
2006
|
+
* Asserts that a value is an 8-byte big-endian byte array.
|
|
2007
|
+
*
|
|
2008
|
+
* @param value - The Uint8Array to assert (must be exactly 8 bytes)
|
|
2009
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
2010
|
+
*
|
|
2011
|
+
* @example
|
|
2012
|
+
* ```typescript
|
|
2013
|
+
* const bytes = new Uint8Array(8);
|
|
2014
|
+
* assertU64BeBytes(bytes);
|
|
2015
|
+
* // bytes is now typed as U64BeBytes
|
|
2016
|
+
* ```
|
|
2017
|
+
*
|
|
2018
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2019
|
+
* @see {@link U64BeBytes}
|
|
2020
|
+
* @public
|
|
2021
|
+
*/
|
|
2022
|
+
declare function assertU64BeBytes(value: Uint8Array): asserts value is U64BeBytes;
|
|
2023
|
+
/**
|
|
2024
|
+
* Asserts that a value is a 16-byte big-endian byte array.
|
|
2025
|
+
*
|
|
2026
|
+
* @param value - The Uint8Array to assert (must be exactly 16 bytes)
|
|
2027
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
2028
|
+
*
|
|
2029
|
+
* @example
|
|
2030
|
+
* ```typescript
|
|
2031
|
+
* // UUID bytes in big-endian format
|
|
2032
|
+
* const uuidBytes = new Uint8Array(16);
|
|
2033
|
+
* assertU128BeBytes(uuidBytes);
|
|
2034
|
+
* // uuidBytes is now typed as U128BeBytes
|
|
2035
|
+
* ```
|
|
2036
|
+
*
|
|
2037
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2038
|
+
* @see {@link U128BeBytes}
|
|
2039
|
+
* @public
|
|
2040
|
+
*/
|
|
2041
|
+
declare function assertU128BeBytes(value: Uint8Array): asserts value is U128BeBytes;
|
|
2042
|
+
/**
|
|
2043
|
+
* Asserts that a value is a 32-byte big-endian byte array.
|
|
2044
|
+
*
|
|
2045
|
+
* @param value - The Uint8Array to assert (must be exactly 32 bytes)
|
|
2046
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
2047
|
+
*
|
|
2048
|
+
* @remarks
|
|
2049
|
+
* Big-endian 256-bit arrays are common in cryptographic specifications
|
|
2050
|
+
* and blockchain protocols.
|
|
2051
|
+
*
|
|
2052
|
+
* @example
|
|
2053
|
+
* ```typescript
|
|
2054
|
+
* // Hash output in standard big-endian format
|
|
2055
|
+
* assertU256BeBytes(sha256Result);
|
|
2056
|
+
* // sha256Result is now typed as U256BeBytes
|
|
2057
|
+
* ```
|
|
2058
|
+
*
|
|
2059
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2060
|
+
* @see {@link U256BeBytes}
|
|
2061
|
+
* @public
|
|
2062
|
+
*/
|
|
2063
|
+
declare function assertU256BeBytes(value: Uint8Array): asserts value is U256BeBytes;
|
|
2064
|
+
/**
|
|
2065
|
+
* Asserts that a value is a 64-byte big-endian byte array.
|
|
2066
|
+
*
|
|
2067
|
+
* @param value - The Uint8Array to assert (must be exactly 64 bytes)
|
|
2068
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
2069
|
+
*
|
|
2070
|
+
* @example
|
|
2071
|
+
* ```typescript
|
|
2072
|
+
* const bytes = new Uint8Array(64);
|
|
2073
|
+
* assertU512BeBytes(bytes);
|
|
2074
|
+
* // bytes is now typed as U512BeBytes
|
|
2075
|
+
* ```
|
|
2076
|
+
*
|
|
2077
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2078
|
+
* @see {@link U512BeBytes}
|
|
2079
|
+
* @public
|
|
2080
|
+
*/
|
|
2081
|
+
declare function assertU512BeBytes(value: Uint8Array): asserts value is U512BeBytes;
|
|
2082
|
+
/**
|
|
2083
|
+
* Asserts that a value is a 128-byte big-endian byte array.
|
|
2084
|
+
*
|
|
2085
|
+
* @param value - The Uint8Array to assert (must be exactly 128 bytes)
|
|
2086
|
+
* @throws {MathematicsAssertionError} If not a Uint8Array or wrong length
|
|
2087
|
+
*
|
|
2088
|
+
* @example
|
|
2089
|
+
* ```typescript
|
|
2090
|
+
* const bytes = new Uint8Array(128);
|
|
2091
|
+
* assertU1024BeBytes(bytes);
|
|
2092
|
+
* // bytes is now typed as U1024BeBytes
|
|
2093
|
+
* ```
|
|
2094
|
+
*
|
|
2095
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2096
|
+
* @see {@link U1024BeBytes}
|
|
2097
|
+
* @public
|
|
2098
|
+
*/
|
|
2099
|
+
declare function assertU1024BeBytes(value: Uint8Array): asserts value is U1024BeBytes;
|
|
2100
|
+
/**
|
|
2101
|
+
* Asserts that a value is a non-negative bigint and narrows it to `UnsignedInteger`.
|
|
2102
|
+
*
|
|
2103
|
+
* This is the base assertion for all unsigned integer types.
|
|
2104
|
+
* It validates that the value is a `bigint` and is non-negative (>= 0).
|
|
2105
|
+
*
|
|
2106
|
+
* @param value - The bigint to assert (must be >= 0)
|
|
2107
|
+
* @throws {MathematicsAssertionError} If not a bigint or if negative
|
|
2108
|
+
*
|
|
2109
|
+
* @remarks
|
|
2110
|
+
* Use this when you need to ensure a value is unsigned but don't need
|
|
2111
|
+
* a specific bit-width constraint.
|
|
2112
|
+
*
|
|
2113
|
+
* @example
|
|
2114
|
+
* ```typescript
|
|
2115
|
+
* const value = 12345n;
|
|
2116
|
+
* assertUnsignedInteger(value);
|
|
2117
|
+
* // value is now typed as UnsignedInteger
|
|
2118
|
+
*
|
|
2119
|
+
* assertUnsignedInteger(-1n); // Throws: negative value
|
|
2120
|
+
* ```
|
|
2121
|
+
*
|
|
2122
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2123
|
+
* @see {@link UnsignedInteger}
|
|
2124
|
+
* @public
|
|
2125
|
+
*/
|
|
2126
|
+
declare function assertUnsignedInteger(value: bigint): asserts value is UnsignedInteger;
|
|
2127
|
+
/**
|
|
2128
|
+
* Asserts that a value is a bigint and narrows it to `SignedInteger`.
|
|
2129
|
+
*
|
|
2130
|
+
* This is the base assertion for all signed integer types.
|
|
2131
|
+
* It only validates that the value is a `bigint` (no range constraints).
|
|
2132
|
+
*
|
|
2133
|
+
* @param value - The bigint to assert
|
|
2134
|
+
* @throws {MathematicsAssertionError} If not a bigint
|
|
2135
|
+
*
|
|
2136
|
+
* @remarks
|
|
2137
|
+
* Use this when you need to ensure a value is a bigint but don't need
|
|
2138
|
+
* a specific bit-width constraint.
|
|
2139
|
+
*
|
|
2140
|
+
* @example
|
|
2141
|
+
* ```typescript
|
|
2142
|
+
* const value = -12345n;
|
|
2143
|
+
* assertSignedInteger(value);
|
|
2144
|
+
* // value is now typed as SignedInteger
|
|
2145
|
+
* ```
|
|
2146
|
+
*
|
|
2147
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2148
|
+
* @see {@link SignedInteger}
|
|
2149
|
+
* @public
|
|
2150
|
+
*/
|
|
2151
|
+
declare function assertSignedInteger(value: bigint): asserts value is SignedInteger;
|
|
2152
|
+
/**
|
|
2153
|
+
* Asserts that a value is a valid 8-bit unsigned integer (0 to 255).
|
|
2154
|
+
*
|
|
2155
|
+
* @param value - The bigint to assert (must be in range [0, 255])
|
|
2156
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2157
|
+
*
|
|
2158
|
+
* @example
|
|
2159
|
+
* ```typescript
|
|
2160
|
+
* const value = 255n;
|
|
2161
|
+
* assertU8(value);
|
|
2162
|
+
* // value is now typed as U8
|
|
2163
|
+
*
|
|
2164
|
+
* assertU8(256n); // Throws: exceeds maximum
|
|
2165
|
+
* assertU8(-1n); // Throws: below minimum
|
|
2166
|
+
* ```
|
|
2167
|
+
*
|
|
2168
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2169
|
+
* @see {@link U8}
|
|
2170
|
+
* @public
|
|
2171
|
+
*/
|
|
2172
|
+
declare function assertU8(value: bigint): asserts value is U8;
|
|
2173
|
+
/**
|
|
2174
|
+
* Asserts that a value is a valid 16-bit unsigned integer (0 to 65,535).
|
|
2175
|
+
*
|
|
2176
|
+
* @param value - The bigint to assert (must be in range [0, 65535])
|
|
2177
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2178
|
+
*
|
|
2179
|
+
* @example
|
|
2180
|
+
* ```typescript
|
|
2181
|
+
* const port = 443n;
|
|
2182
|
+
* assertU16(port);
|
|
2183
|
+
* // port is now typed as U16
|
|
2184
|
+
*
|
|
2185
|
+
* assertU16(65536n); // Throws: exceeds maximum
|
|
2186
|
+
* ```
|
|
2187
|
+
*
|
|
2188
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2189
|
+
* @see {@link U16}
|
|
2190
|
+
* @public
|
|
2191
|
+
*/
|
|
2192
|
+
declare function assertU16(value: bigint): asserts value is U16;
|
|
2193
|
+
/**
|
|
2194
|
+
* Asserts that a value is a valid 32-bit unsigned integer (0 to 4,294,967,295).
|
|
2195
|
+
*
|
|
2196
|
+
* @param value - The bigint to assert (must be in range [0, 4294967295])
|
|
2197
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2198
|
+
*
|
|
2199
|
+
* @example
|
|
2200
|
+
* ```typescript
|
|
2201
|
+
* const timestamp = 1704067200n;
|
|
2202
|
+
* assertU32(timestamp);
|
|
2203
|
+
* // timestamp is now typed as U32
|
|
2204
|
+
*
|
|
2205
|
+
* assertU32(4294967296n); // Throws: exceeds maximum (2^32)
|
|
2206
|
+
* ```
|
|
2207
|
+
*
|
|
2208
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2209
|
+
* @see {@link U32}
|
|
2210
|
+
* @public
|
|
2211
|
+
*/
|
|
2212
|
+
declare function assertU32(value: bigint): asserts value is U32;
|
|
2213
|
+
/**
|
|
2214
|
+
* Asserts that a value is a valid 64-bit unsigned integer.
|
|
2215
|
+
*
|
|
2216
|
+
* @param value - The bigint to assert (must be in range [0, 2^64 - 1])
|
|
2217
|
+
* @param name - The name of the variable being asserted, used in error messages for context.
|
|
2218
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2219
|
+
*
|
|
2220
|
+
* @defaultValue `name` defaults to `"value"`
|
|
2221
|
+
*
|
|
2222
|
+
* @example
|
|
2223
|
+
* ```typescript
|
|
2224
|
+
* const large = 18446744073709551615n;
|
|
2225
|
+
* assertU64(large, "large");
|
|
2226
|
+
* // large is now typed as U64
|
|
2227
|
+
* ```
|
|
2228
|
+
*
|
|
2229
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2230
|
+
* @see {@link U64}
|
|
2231
|
+
* @public
|
|
2232
|
+
*/
|
|
2233
|
+
declare function assertU64(value: bigint, name?: string): asserts value is U64;
|
|
2234
|
+
/**
|
|
2235
|
+
* Asserts that a value is a valid 128-bit unsigned integer.
|
|
2236
|
+
*
|
|
2237
|
+
* @param value - The bigint to assert (must be in range [0, 2^128 - 1])
|
|
2238
|
+
* @param name - The name of the variable being asserted, used in error messages for context.
|
|
2239
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2240
|
+
*
|
|
2241
|
+
* @defaultValue `name` defaults to `"value"`
|
|
2242
|
+
*
|
|
2243
|
+
* @example
|
|
2244
|
+
* ```typescript
|
|
2245
|
+
* const uuid = 0x550e8400e29b41d4a716446655440000n;
|
|
2246
|
+
* assertU128(uuid, "uuid");
|
|
2247
|
+
* // uuid is now typed as U128
|
|
2248
|
+
* ```
|
|
2249
|
+
*
|
|
2250
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2251
|
+
* @see {@link U128}
|
|
2252
|
+
* @public
|
|
2253
|
+
*/
|
|
2254
|
+
declare function assertU128(value: bigint, name?: string): asserts value is U128;
|
|
2255
|
+
/**
|
|
2256
|
+
* Asserts that a value is a valid 256-bit unsigned integer.
|
|
2257
|
+
*
|
|
2258
|
+
* @param value - The bigint to assert (must be in range [0, 2^256 - 1])
|
|
2259
|
+
* @param name - The name of the variable being asserted, used in error messages for context.
|
|
2260
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2261
|
+
*
|
|
2262
|
+
* @defaultValue `name` defaults to `"value"`
|
|
2263
|
+
*
|
|
2264
|
+
* @remarks
|
|
2265
|
+
* This is commonly used for elliptic curve operations where scalars
|
|
2266
|
+
* and field elements must be within the 256-bit range.
|
|
2267
|
+
*
|
|
2268
|
+
* @example
|
|
2269
|
+
* ```typescript
|
|
2270
|
+
* // Validate a private key is in valid range
|
|
2271
|
+
* assertU256(secretKeyBigInt, "secretKeyBigInt");
|
|
2272
|
+
* // secretKeyBigInt is now typed as U256
|
|
2273
|
+
*
|
|
2274
|
+
* // Validate a hash value
|
|
2275
|
+
* assertU256(sha256AsBigInt, "sha256AsBigInt");
|
|
2276
|
+
* // sha256AsBigInt is now typed as U256
|
|
2277
|
+
* ```
|
|
2278
|
+
*
|
|
2279
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2280
|
+
* @see {@link U256}
|
|
2281
|
+
* @public
|
|
2282
|
+
*/
|
|
2283
|
+
declare function assertU256(value: bigint, name?: string): asserts value is U256;
|
|
2284
|
+
/**
|
|
2285
|
+
* Asserts that a value is a valid 512-bit unsigned integer.
|
|
2286
|
+
*
|
|
2287
|
+
* @param value - The bigint to assert (must be in range [0, 2^512 - 1])
|
|
2288
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2289
|
+
*
|
|
2290
|
+
* @example
|
|
2291
|
+
* ```typescript
|
|
2292
|
+
* assertU512(sha512AsBigInt);
|
|
2293
|
+
* // sha512AsBigInt is now typed as U512
|
|
2294
|
+
* ```
|
|
2295
|
+
*
|
|
2296
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2297
|
+
* @see {@link U512}
|
|
2298
|
+
* @public
|
|
2299
|
+
*/
|
|
2300
|
+
declare function assertU512(value: bigint): asserts value is U512;
|
|
2301
|
+
/**
|
|
2302
|
+
* Asserts that a value is a valid 1024-bit unsigned integer.
|
|
2303
|
+
*
|
|
2304
|
+
* @param value - The bigint to assert (must be in range [0, 2^1024 - 1])
|
|
2305
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2306
|
+
*
|
|
2307
|
+
* @example
|
|
2308
|
+
* ```typescript
|
|
2309
|
+
* assertU1024(modulusBigInt);
|
|
2310
|
+
* // modulusBigInt is now typed as U1024
|
|
2311
|
+
* ```
|
|
2312
|
+
*
|
|
2313
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2314
|
+
* @see {@link U1024}
|
|
2315
|
+
* @public
|
|
2316
|
+
*/
|
|
2317
|
+
declare function assertU1024(value: bigint): asserts value is U1024;
|
|
2318
|
+
/**
|
|
2319
|
+
* Asserts that a value is a valid 8-bit signed integer (-128 to 127).
|
|
2320
|
+
*
|
|
2321
|
+
* @param value - The bigint to assert (must be in range [-128, 127])
|
|
2322
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2323
|
+
*
|
|
2324
|
+
* @example
|
|
2325
|
+
* ```typescript
|
|
2326
|
+
* const value = 127n;
|
|
2327
|
+
* assertI8(value);
|
|
2328
|
+
* // value is now typed as I8
|
|
2329
|
+
*
|
|
2330
|
+
* assertI8(128n); // Throws: exceeds maximum
|
|
2331
|
+
* assertI8(-129n); // Throws: below minimum
|
|
2332
|
+
* ```
|
|
2333
|
+
*
|
|
2334
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2335
|
+
* @see {@link I8}
|
|
2336
|
+
* @public
|
|
2337
|
+
*/
|
|
2338
|
+
declare function assertI8(value: bigint): asserts value is I8;
|
|
2339
|
+
/**
|
|
2340
|
+
* Asserts that a value is a valid 16-bit signed integer (-32,768 to 32,767).
|
|
2341
|
+
*
|
|
2342
|
+
* @param value - The bigint to assert (must be in range [-32768, 32767])
|
|
2343
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2344
|
+
*
|
|
2345
|
+
* @example
|
|
2346
|
+
* ```typescript
|
|
2347
|
+
* const audioSample = -16384n;
|
|
2348
|
+
* assertI16(audioSample);
|
|
2349
|
+
* // audioSample is now typed as I16
|
|
2350
|
+
* ```
|
|
2351
|
+
*
|
|
2352
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2353
|
+
* @see {@link I16}
|
|
2354
|
+
* @public
|
|
2355
|
+
*/
|
|
2356
|
+
declare function assertI16(value: bigint): asserts value is I16;
|
|
2357
|
+
/**
|
|
2358
|
+
* Asserts that a value is a valid 32-bit signed integer.
|
|
2359
|
+
*
|
|
2360
|
+
* @param value - The bigint to assert (must be in range [-2147483648, 2147483647])
|
|
2361
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2362
|
+
*
|
|
2363
|
+
* @example
|
|
2364
|
+
* ```typescript
|
|
2365
|
+
* const offset = -1000n;
|
|
2366
|
+
* assertI32(offset);
|
|
2367
|
+
* // offset is now typed as I32
|
|
2368
|
+
* ```
|
|
2369
|
+
*
|
|
2370
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2371
|
+
* @see {@link I32}
|
|
2372
|
+
* @public
|
|
2373
|
+
*/
|
|
2374
|
+
declare function assertI32(value: bigint): asserts value is I32;
|
|
2375
|
+
/**
|
|
2376
|
+
* Asserts that a value is a valid 64-bit signed integer.
|
|
2377
|
+
*
|
|
2378
|
+
* @param value - The bigint to assert (must be in range [-2^63, 2^63 - 1])
|
|
2379
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2380
|
+
*
|
|
2381
|
+
* @example
|
|
2382
|
+
* ```typescript
|
|
2383
|
+
* const difference = -1000000000000n;
|
|
2384
|
+
* assertI64(difference);
|
|
2385
|
+
* // difference is now typed as I64
|
|
2386
|
+
* ```
|
|
2387
|
+
*
|
|
2388
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2389
|
+
* @see {@link I64}
|
|
2390
|
+
* @public
|
|
2391
|
+
*/
|
|
2392
|
+
declare function assertI64(value: bigint): asserts value is I64;
|
|
2393
|
+
/**
|
|
2394
|
+
* Asserts that a value is a valid 128-bit signed integer.
|
|
2395
|
+
*
|
|
2396
|
+
* @param value - The bigint to assert (must be in range [-2^127, 2^127 - 1])
|
|
2397
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2398
|
+
*
|
|
2399
|
+
* @example
|
|
2400
|
+
* ```typescript
|
|
2401
|
+
* const value = -170141183460469231731687303715884105728n;
|
|
2402
|
+
* assertI128(value);
|
|
2403
|
+
* // value is now typed as I128
|
|
2404
|
+
* ```
|
|
2405
|
+
*
|
|
2406
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2407
|
+
* @see {@link I128}
|
|
2408
|
+
* @public
|
|
2409
|
+
*/
|
|
2410
|
+
declare function assertI128(value: bigint): asserts value is I128;
|
|
2411
|
+
/**
|
|
2412
|
+
* Asserts that a value is a valid 256-bit signed integer.
|
|
2413
|
+
*
|
|
2414
|
+
* @param value - The bigint to assert (must be in range [-2^255, 2^255 - 1])
|
|
2415
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2416
|
+
*
|
|
2417
|
+
* @example
|
|
2418
|
+
* ```typescript
|
|
2419
|
+
* const signedScalar = -12345678901234567890n;
|
|
2420
|
+
* assertI256(signedScalar);
|
|
2421
|
+
* // signedScalar is now typed as I256
|
|
2422
|
+
* ```
|
|
2423
|
+
*
|
|
2424
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2425
|
+
* @see {@link I256}
|
|
2426
|
+
* @public
|
|
2427
|
+
*/
|
|
2428
|
+
declare function assertI256(value: bigint): asserts value is I256;
|
|
2429
|
+
/**
|
|
2430
|
+
* Asserts that a value is a valid 512-bit signed integer.
|
|
2431
|
+
*
|
|
2432
|
+
* @param value - The bigint to assert (must be in range [-2^511, 2^511 - 1])
|
|
2433
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2434
|
+
*
|
|
2435
|
+
* @example
|
|
2436
|
+
* ```typescript
|
|
2437
|
+
* const value = -1n;
|
|
2438
|
+
* assertI512(value);
|
|
2439
|
+
* // value is now typed as I512
|
|
2440
|
+
* ```
|
|
2441
|
+
*
|
|
2442
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2443
|
+
* @see {@link I512}
|
|
2444
|
+
* @public
|
|
2445
|
+
*/
|
|
2446
|
+
declare function assertI512(value: bigint): asserts value is I512;
|
|
2447
|
+
/**
|
|
2448
|
+
* Asserts that a value is a valid 1024-bit signed integer.
|
|
2449
|
+
*
|
|
2450
|
+
* @param value - The bigint to assert (must be in range [-2^1023, 2^1023 - 1])
|
|
2451
|
+
* @throws {MathematicsAssertionError} If not a bigint or out of range
|
|
2452
|
+
*
|
|
2453
|
+
* @example
|
|
2454
|
+
* ```typescript
|
|
2455
|
+
* const value = 0n;
|
|
2456
|
+
* assertI1024(value);
|
|
2457
|
+
* // value is now typed as I1024
|
|
2458
|
+
* ```
|
|
2459
|
+
*
|
|
2460
|
+
* @returns `void` — type narrowing is applied to the argument in-place.
|
|
2461
|
+
* @see {@link I1024}
|
|
2462
|
+
* @public
|
|
2463
|
+
*/
|
|
2464
|
+
declare function assertI1024(value: bigint): asserts value is I1024;
|
|
2465
|
+
|
|
2466
|
+
/**
|
|
2467
|
+
* Cryptography Types
|
|
2468
|
+
*
|
|
2469
|
+
* This module defines branded types for cryptographic primitives used throughout the SDK.
|
|
2470
|
+
* These types provide compile-time safety for elliptic curve field elements and cipher operations.
|
|
2471
|
+
*
|
|
2472
|
+
* ## Overview
|
|
2473
|
+
*
|
|
2474
|
+
* The module includes:
|
|
2475
|
+
* - **Field Elements**: Types for elliptic curve operations (BN254, Curve25519)
|
|
2476
|
+
* - **Cipher Types**: Poseidon and Rescue cipher primitives
|
|
2477
|
+
* - **Constants**: Field primes for validation
|
|
2478
|
+
* - **Assertions**: Runtime validation functions
|
|
2479
|
+
*
|
|
2480
|
+
* @remarks
|
|
2481
|
+
* All numeric cryptographic types use TypeScript branded types (phantom type tags) to prevent
|
|
2482
|
+
* accidental mixing of values from different domains. For example, a `PoseidonKey` cannot be
|
|
2483
|
+
* passed where a `RcKey` is expected, even though both are ultimately bigints. This catches
|
|
2484
|
+
* a large class of cryptographic misuse bugs at compile time rather than runtime.
|
|
2485
|
+
*
|
|
2486
|
+
* Byte-array types (X25519, AES, Groth16 proof components) use Uint8Array sub-brands so that
|
|
2487
|
+
* the TypeScript compiler enforces correct sizes and semantics at every call site.
|
|
2488
|
+
*
|
|
2489
|
+
* @packageDocumentation
|
|
2490
|
+
* @module types/cryptography
|
|
2491
|
+
*/
|
|
2492
|
+
|
|
2493
|
+
/**
|
|
2494
|
+
* The prime field modulus for BN254 (alt_bn128) curve.
|
|
2495
|
+
*
|
|
2496
|
+
* @remarks
|
|
2497
|
+
* This is the order of the scalar field Fr for the BN254 pairing-friendly elliptic curve.
|
|
2498
|
+
* Value: 21888242871839275222246405745257275088548364400416034343698204186575808495617
|
|
2499
|
+
*
|
|
2500
|
+
* All `Bn254FieldElement` values and derived types (Poseidon keys, hashes, viewing keys)
|
|
2501
|
+
* must be strictly less than this constant.
|
|
2502
|
+
*
|
|
2503
|
+
* @see {@link Bn254FieldElement}
|
|
2504
|
+
* @see {@link assertBn254FieldElement}
|
|
2505
|
+
* @public
|
|
2506
|
+
*/
|
|
2507
|
+
declare const BN254_FIELD_PRIME = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
2508
|
+
/**
|
|
2509
|
+
* The prime field modulus for Curve25519.
|
|
2510
|
+
*
|
|
2511
|
+
* @remarks
|
|
2512
|
+
* Value: 2^255 - 19 = 57896044618658097711785492504343953926634992332820282019728792003956564819949
|
|
2513
|
+
*
|
|
2514
|
+
* All `Curve25519FieldElement` values and derived types (Rescue cipher types, RcEncryptionNonce)
|
|
2515
|
+
* must be strictly less than this constant.
|
|
2516
|
+
*
|
|
2517
|
+
* @see {@link Curve25519FieldElement}
|
|
2518
|
+
* @see {@link assertCurve25519FieldElement}
|
|
2519
|
+
* @public
|
|
2520
|
+
*/
|
|
2521
|
+
declare const CURVE25519_FIELD_PRIME: bigint;
|
|
2522
|
+
/**
|
|
2523
|
+
* Error thrown when a cryptographic type assertion fails.
|
|
2524
|
+
*
|
|
2525
|
+
* This error provides detailed information about why an assertion failed,
|
|
2526
|
+
* including the actual value, the expected type, and the constraint that
|
|
2527
|
+
* was violated.
|
|
2528
|
+
*
|
|
2529
|
+
* @remarks
|
|
2530
|
+
* Every `assertXxx` function in this module throws `CryptographyAssertionError` on failure.
|
|
2531
|
+
* Catch this specific type when you need to handle assertion failures differently from
|
|
2532
|
+
* other runtime errors (e.g., to provide user-friendly validation messages or to fall back
|
|
2533
|
+
* to a safe default).
|
|
2534
|
+
*
|
|
2535
|
+
* @example
|
|
2536
|
+
* ```typescript
|
|
2537
|
+
* import { assertBn254FieldElement, CryptographyAssertionError } from "./cryptography";
|
|
2538
|
+
*
|
|
2539
|
+
* try {
|
|
2540
|
+
* const value = assertBn254FieldElement(-1n); // Will throw
|
|
2541
|
+
* } catch (error) {
|
|
2542
|
+
* if (error instanceof CryptographyAssertionError) {
|
|
2543
|
+
* console.error(`Assertion failed for ${error.expectedType}`);
|
|
2544
|
+
* console.error(`Value: ${error.value}`);
|
|
2545
|
+
* console.error(`Constraint: ${error.constraint}`);
|
|
2546
|
+
* }
|
|
2547
|
+
* }
|
|
2548
|
+
* ```
|
|
2549
|
+
*
|
|
2550
|
+
* @sealed
|
|
2551
|
+
* @public
|
|
2552
|
+
*/
|
|
2553
|
+
declare class CryptographyAssertionError extends Error {
|
|
2554
|
+
/**
|
|
2555
|
+
* The actual value that failed the assertion.
|
|
2556
|
+
* @readonly
|
|
2557
|
+
*/
|
|
2558
|
+
readonly value: unknown;
|
|
2559
|
+
/**
|
|
2560
|
+
* The type that was expected (e.g., "Bn254FieldElement", "PoseidonKey").
|
|
2561
|
+
* @readonly
|
|
2562
|
+
*/
|
|
2563
|
+
readonly expectedType: string;
|
|
2564
|
+
/**
|
|
2565
|
+
* Description of the constraint that was violated.
|
|
2566
|
+
*
|
|
2567
|
+
* @remarks
|
|
2568
|
+
* Will be `undefined` when the failure is purely a type mismatch (e.g., received a string
|
|
2569
|
+
* instead of a bigint). Contains a human-readable range or length constraint when the value
|
|
2570
|
+
* was the correct type but out of the valid range.
|
|
2571
|
+
*
|
|
2572
|
+
* @example `"value >= 0"`, `"value < BN254_FIELD_PRIME"`, `"length === 32"`
|
|
2573
|
+
* @readonly
|
|
2574
|
+
*/
|
|
2575
|
+
readonly constraint: string | undefined;
|
|
2576
|
+
/**
|
|
2577
|
+
* Creates a new CryptographyAssertionError.
|
|
2578
|
+
*
|
|
2579
|
+
* @param message - Human-readable error message describing what went wrong
|
|
2580
|
+
* @param options - Structured error details for programmatic inspection
|
|
2581
|
+
* @param options.value - The value that failed the assertion
|
|
2582
|
+
* @param options.expectedType - Name of the branded type that was expected
|
|
2583
|
+
* @param options.constraint - Optional human-readable constraint that was violated
|
|
2584
|
+
*/
|
|
2585
|
+
constructor(message: string, options: {
|
|
2586
|
+
value: unknown;
|
|
2587
|
+
expectedType: string;
|
|
2588
|
+
constraint?: string;
|
|
2589
|
+
});
|
|
2590
|
+
}
|
|
2591
|
+
/**
|
|
2592
|
+
* Field element for the BN254 (alt_bn128) elliptic curve.
|
|
2593
|
+
*
|
|
2594
|
+
* BN254 is a pairing-friendly elliptic curve widely used in zero-knowledge proofs.
|
|
2595
|
+
* The field prime is: 21888242871839275222246405745257275088548364400416034343698204186575808495617
|
|
2596
|
+
*
|
|
2597
|
+
* @remarks
|
|
2598
|
+
* - Used in Ethereum precompiles (EIP-196, EIP-197)
|
|
2599
|
+
* - Provides ~100 bits of security
|
|
2600
|
+
* - Supports efficient pairing operations
|
|
2601
|
+
* - Valid range: [0, BN254_FIELD_PRIME - 1]
|
|
2602
|
+
* - Encoded as a TypeScript `bigint`; the sub-brand prevents mixing with Curve25519 or raw U256 values
|
|
2603
|
+
*
|
|
2604
|
+
* This is the base type for all Poseidon cipher types and all viewing key types used in the
|
|
2605
|
+
* Umbra protocol.
|
|
2606
|
+
*
|
|
2607
|
+
* @see {@link BN254_FIELD_PRIME}
|
|
2608
|
+
* @see {@link assertBn254FieldElement}
|
|
2609
|
+
* @see https://eips.ethereum.org/EIPS/eip-196
|
|
2610
|
+
* @public
|
|
2611
|
+
*/
|
|
2612
|
+
type Bn254FieldElement = SubSubBrandedType<U256, "Bn254FieldElement">;
|
|
2613
|
+
/**
|
|
2614
|
+
* Field element for the Curve25519 elliptic curve.
|
|
2615
|
+
*
|
|
2616
|
+
* Curve25519 is a Montgomery curve designed for high-speed Diffie-Hellman key exchange.
|
|
2617
|
+
* The field prime is: 2^255 - 19
|
|
2618
|
+
*
|
|
2619
|
+
* @remarks
|
|
2620
|
+
* - Provides ~128 bits of security
|
|
2621
|
+
* - Designed to be resistant to timing attacks
|
|
2622
|
+
* - Used in Ed25519 signatures and X25519 key exchange
|
|
2623
|
+
* - Valid range: [0, CURVE25519_FIELD_PRIME - 1]
|
|
2624
|
+
* - Encoded as a TypeScript `bigint`; the sub-brand prevents mixing with BN254 field values
|
|
2625
|
+
*
|
|
2626
|
+
* This is the base type for all Rescue cipher types (RcCiphertext, RcPlaintext, RcKey, RcCounter).
|
|
2627
|
+
*
|
|
2628
|
+
* @see {@link CURVE25519_FIELD_PRIME}
|
|
2629
|
+
* @see {@link assertCurve25519FieldElement}
|
|
2630
|
+
* @see https://cr.yp.to/ecdh/curve25519-20060209.pdf
|
|
2631
|
+
* @public
|
|
2632
|
+
*/
|
|
2633
|
+
type Curve25519FieldElement = SubSubBrandedType<U256, "Curve25519FieldElement">;
|
|
2634
|
+
/**
|
|
2635
|
+
* Byte length for all X25519 key exchange values (keys and shared secrets).
|
|
2636
|
+
*
|
|
2637
|
+
* @remarks
|
|
2638
|
+
* X25519 public keys, private keys, and shared secrets are all exactly 32 bytes (256 bits)
|
|
2639
|
+
* as specified in RFC 7748. This constant is used by all X25519 assertion functions.
|
|
2640
|
+
*
|
|
2641
|
+
* @see {@link assertX25519Bytes}
|
|
2642
|
+
* @see {@link assertX25519PrivateKey}
|
|
2643
|
+
* @see {@link assertX25519PublicKey}
|
|
2644
|
+
* @see {@link assertSharedSecret}
|
|
2645
|
+
* @public
|
|
2646
|
+
*/
|
|
2647
|
+
declare const X25519_BYTE_LENGTH = 32;
|
|
2648
|
+
/**
|
|
2649
|
+
* Byte array type for X25519 key exchange operations.
|
|
2650
|
+
*
|
|
2651
|
+
* X25519 is an elliptic curve Diffie-Hellman (ECDH) protocol using Curve25519.
|
|
2652
|
+
* All X25519 values (public keys, private keys, shared secrets) are exactly 32 bytes.
|
|
2653
|
+
*
|
|
2654
|
+
* @remarks
|
|
2655
|
+
* - Size: 32 bytes (256 bits)
|
|
2656
|
+
* - Used for X25519 key exchange operations
|
|
2657
|
+
* - Parallel to LeBytes/BeBytes as a sub-brand of Bytes
|
|
2658
|
+
* - Endianness-agnostic (X25519 spec defines byte order)
|
|
2659
|
+
* - This is the base type from which `X25519PrivateKey`, `X25519PublicKey`, and `SharedSecret` are derived
|
|
2660
|
+
*
|
|
2661
|
+
* @see {@link X25519_BYTE_LENGTH}
|
|
2662
|
+
* @see {@link X25519PrivateKey}
|
|
2663
|
+
* @see {@link X25519PublicKey}
|
|
2664
|
+
* @see {@link SharedSecret}
|
|
2665
|
+
* @see https://cr.yp.to/ecdh/curve25519-20060209.pdf
|
|
2666
|
+
* @see https://tools.ietf.org/html/rfc7748
|
|
2667
|
+
* @public
|
|
2668
|
+
*/
|
|
2669
|
+
type X25519Bytes = SubBrandedType<Bytes, "X25519Bytes">;
|
|
2670
|
+
/**
|
|
2671
|
+
* X25519 private key for elliptic curve Diffie-Hellman key exchange.
|
|
2672
|
+
*
|
|
2673
|
+
* A private key is a 32-byte value that should be generated using a
|
|
2674
|
+
* cryptographically secure random number generator. It is used together
|
|
2675
|
+
* with a public key to compute a shared secret.
|
|
2676
|
+
*
|
|
2677
|
+
* @remarks
|
|
2678
|
+
* - Size: 32 bytes (256 bits)
|
|
2679
|
+
* - Must be kept secret and never shared
|
|
2680
|
+
* - Should be generated using crypto.getRandomValues() or similar CSPRNG
|
|
2681
|
+
* - The X25519 algorithm applies clamping to the private key during use
|
|
2682
|
+
*
|
|
2683
|
+
* ## Type Hierarchy
|
|
2684
|
+
* ```
|
|
2685
|
+
* Bytes (base)
|
|
2686
|
+
* └── X25519Bytes (sub-brand, 32 bytes)
|
|
2687
|
+
* └── X25519PrivateKey (sub-sub-brand)
|
|
2688
|
+
* ```
|
|
2689
|
+
*
|
|
2690
|
+
* @example
|
|
2691
|
+
* ```typescript
|
|
2692
|
+
* // Generate a new private key
|
|
2693
|
+
* const rawPrivateKey = crypto.getRandomValues(new Uint8Array(32));
|
|
2694
|
+
* assertX25519PrivateKey(rawPrivateKey);
|
|
2695
|
+
* // rawPrivateKey is now typed as X25519PrivateKey
|
|
2696
|
+
*
|
|
2697
|
+
* // Derive the public key
|
|
2698
|
+
* const publicKey = x25519GetPublicKey(rawPrivateKey);
|
|
2699
|
+
* ```
|
|
2700
|
+
*
|
|
2701
|
+
* @see https://tools.ietf.org/html/rfc7748#section-5
|
|
2702
|
+
* @public
|
|
2703
|
+
*/
|
|
2704
|
+
type X25519PrivateKey = SubSubBrandedType<X25519Bytes, "X25519PrivateKey">;
|
|
2705
|
+
/**
|
|
2706
|
+
* X25519 public key for elliptic curve Diffie-Hellman key exchange.
|
|
2707
|
+
*
|
|
2708
|
+
* A public key is a 32-byte value derived from a private key. It can be
|
|
2709
|
+
* freely shared and is used by other parties to compute a shared secret.
|
|
2710
|
+
*
|
|
2711
|
+
* @remarks
|
|
2712
|
+
* - Size: 32 bytes (256 bits)
|
|
2713
|
+
* - Can be shared publicly
|
|
2714
|
+
* - Derived from a corresponding X25519PrivateKey
|
|
2715
|
+
* - Used as input to X25519 key exchange
|
|
2716
|
+
*
|
|
2717
|
+
* ## Type Hierarchy
|
|
2718
|
+
* ```
|
|
2719
|
+
* Bytes (base)
|
|
2720
|
+
* └── X25519Bytes (sub-brand, 32 bytes)
|
|
2721
|
+
* └── X25519PublicKey (sub-sub-brand)
|
|
2722
|
+
* ```
|
|
2723
|
+
*
|
|
2724
|
+
* @example
|
|
2725
|
+
* ```typescript
|
|
2726
|
+
* // Receive a public key from another party
|
|
2727
|
+
* const theirPublicKey = receivePublicKey();
|
|
2728
|
+
* assertX25519PublicKey(theirPublicKey);
|
|
2729
|
+
* // theirPublicKey is now typed as X25519PublicKey
|
|
2730
|
+
*
|
|
2731
|
+
* // Compute shared secret
|
|
2732
|
+
* const sharedSecret = x25519(myPrivateKey, theirPublicKey);
|
|
2733
|
+
* ```
|
|
2734
|
+
*
|
|
2735
|
+
* @see https://tools.ietf.org/html/rfc7748#section-5
|
|
2736
|
+
* @public
|
|
2737
|
+
*/
|
|
2738
|
+
type X25519PublicKey = SubSubBrandedType<X25519Bytes, "X25519PublicKey">;
|
|
2739
|
+
/**
|
|
2740
|
+
* Shared secret produced by X25519 key exchange.
|
|
2741
|
+
*
|
|
2742
|
+
* This type represents the output of an X25519 ECDH operation, which combines
|
|
2743
|
+
* a private key with a public key to produce a shared secret that both parties
|
|
2744
|
+
* can compute independently.
|
|
2745
|
+
*
|
|
2746
|
+
* @remarks
|
|
2747
|
+
* - Size: 32 bytes (256 bits)
|
|
2748
|
+
* - Should be used as input to a Key Derivation Function (KDF)
|
|
2749
|
+
* - Never use raw shared secrets directly as encryption keys
|
|
2750
|
+
*
|
|
2751
|
+
* ## Type Hierarchy
|
|
2752
|
+
* ```
|
|
2753
|
+
* Bytes (base)
|
|
2754
|
+
* └── X25519Bytes (sub-brand, 32 bytes)
|
|
2755
|
+
* └── SharedSecret (sub-sub-brand)
|
|
2756
|
+
* ```
|
|
2757
|
+
*
|
|
2758
|
+
* @example
|
|
2759
|
+
* ```typescript
|
|
2760
|
+
* // After performing X25519 key exchange
|
|
2761
|
+
* const rawSharedSecret = x25519(myPrivateKey, theirPublicKey);
|
|
2762
|
+
* assertSharedSecret(rawSharedSecret);
|
|
2763
|
+
* // rawSharedSecret is now typed as SharedSecret
|
|
2764
|
+
*
|
|
2765
|
+
* // Derive an encryption key from the shared secret
|
|
2766
|
+
* const encryptionKey = kdf(rawSharedSecret);
|
|
2767
|
+
* ```
|
|
2768
|
+
*
|
|
2769
|
+
* @see https://tools.ietf.org/html/rfc7748#section-6
|
|
2770
|
+
* @public
|
|
2771
|
+
*/
|
|
2772
|
+
type SharedSecret = SubSubBrandedType<X25519Bytes, "SharedSecret">;
|
|
2773
|
+
/**
|
|
2774
|
+
* Plaintext input for Poseidon cipher encryption.
|
|
2775
|
+
*
|
|
2776
|
+
* Poseidon is a cryptographic hash function and cipher optimized for
|
|
2777
|
+
* zero-knowledge proof systems. It operates natively on field elements.
|
|
2778
|
+
*
|
|
2779
|
+
* @remarks
|
|
2780
|
+
* Must be a valid BN254 field element (less than the field prime). Plaintext values that
|
|
2781
|
+
* represent token amounts or account balances are encoded as field elements before being
|
|
2782
|
+
* passed to the Poseidon cipher.
|
|
2783
|
+
*
|
|
2784
|
+
* @see {@link assertPoseidonPlaintext}
|
|
2785
|
+
* @see {@link BN254_FIELD_PRIME}
|
|
2786
|
+
* @public
|
|
2787
|
+
*/
|
|
2788
|
+
type PoseidonPlaintext = SubSubSubBrandedType<Bn254FieldElement, "PoseidonPlaintext">;
|
|
2789
|
+
/**
|
|
2790
|
+
* Hash output from the Poseidon hash function.
|
|
2791
|
+
*
|
|
2792
|
+
* The Poseidon hash function produces a single field element as output.
|
|
2793
|
+
* It is collision-resistant and designed for efficient verification in SNARKs.
|
|
2794
|
+
*
|
|
2795
|
+
* @remarks
|
|
2796
|
+
* Output is always a valid BN254 field element. Used extensively in the Umbra protocol for
|
|
2797
|
+
* UTXO commitment construction, nullifier derivation, and Merkle tree nodes.
|
|
2798
|
+
*
|
|
2799
|
+
* @see {@link assertPoseidonHash}
|
|
2800
|
+
* @public
|
|
2801
|
+
*/
|
|
2802
|
+
type PoseidonHash = SubSubSubBrandedType<Bn254FieldElement, "PoseidonHash">;
|
|
2803
|
+
/**
|
|
2804
|
+
* Encryption key for the Poseidon cipher.
|
|
2805
|
+
*
|
|
2806
|
+
* Keys should be generated using a cryptographically secure random number generator
|
|
2807
|
+
* and must be valid BN254 field elements.
|
|
2808
|
+
*
|
|
2809
|
+
* @remarks
|
|
2810
|
+
* Key security depends on uniform random sampling from the field. In the Umbra protocol,
|
|
2811
|
+
* Poseidon keys are derived deterministically from viewing keys and are scoped per-transaction.
|
|
2812
|
+
*
|
|
2813
|
+
* @see {@link assertPoseidonKey}
|
|
2814
|
+
* @public
|
|
2815
|
+
*/
|
|
2816
|
+
type PoseidonKey = SubSubSubBrandedType<Bn254FieldElement, "PoseidonKey">;
|
|
2817
|
+
/**
|
|
2818
|
+
* Ciphertext output from Poseidon cipher encryption.
|
|
2819
|
+
*
|
|
2820
|
+
* The ciphertext is a field element that can only be decrypted
|
|
2821
|
+
* with the corresponding PoseidonKey.
|
|
2822
|
+
*
|
|
2823
|
+
* @remarks
|
|
2824
|
+
* Ciphertext is indistinguishable from random field elements under the security assumptions of
|
|
2825
|
+
* Poseidon. In the Umbra protocol, Poseidon ciphertexts are stored on-chain to represent
|
|
2826
|
+
* encrypted token balances in confidential token accounts.
|
|
2827
|
+
*
|
|
2828
|
+
* @see {@link assertPoseidonCiphertext}
|
|
2829
|
+
* @see {@link PoseidonKey}
|
|
2830
|
+
* @public
|
|
2831
|
+
*/
|
|
2832
|
+
type PoseidonCiphertext = SubSubSubBrandedType<Bn254FieldElement, "PoseidonCiphertext">;
|
|
2833
|
+
/**
|
|
2834
|
+
* Counter value for the Poseidon cipher.
|
|
2835
|
+
*
|
|
2836
|
+
* Used as a nonce or counter in counter-mode encryption operations
|
|
2837
|
+
* with the Poseidon cipher. Similar to RcCounter but operates on
|
|
2838
|
+
* the BN254 field for compatibility with Poseidon's field arithmetic.
|
|
2839
|
+
*
|
|
2840
|
+
* @remarks
|
|
2841
|
+
* - Must be a valid BN254 field element (less than the field prime)
|
|
2842
|
+
* - Should be unique for each encryption operation with the same key
|
|
2843
|
+
* - Commonly used in counter-mode constructions for deterministic encryption
|
|
2844
|
+
* - Reusing a counter with the same key may compromise security
|
|
2845
|
+
*
|
|
2846
|
+
* @example
|
|
2847
|
+
* ```typescript
|
|
2848
|
+
* // Create a counter for Poseidon encryption
|
|
2849
|
+
* const counter = 0n;
|
|
2850
|
+
* assertPoseidonCounter(counter);
|
|
2851
|
+
* // counter is now typed as PoseidonCounter
|
|
2852
|
+
*
|
|
2853
|
+
* // Increment for next encryption
|
|
2854
|
+
* const nextCounter = counter + 1n;
|
|
2855
|
+
* assertPoseidonCounter(nextCounter);
|
|
2856
|
+
* ```
|
|
2857
|
+
*
|
|
2858
|
+
* @see {@link assertPoseidonCounter}
|
|
2859
|
+
* @public
|
|
2860
|
+
*/
|
|
2861
|
+
type PoseidonCounter = SubSubSubBrandedType<Bn254FieldElement, "PoseidonCounter">;
|
|
2862
|
+
/**
|
|
2863
|
+
* Keystream value generated by the Poseidon keystream generator.
|
|
2864
|
+
*
|
|
2865
|
+
* A keystream is a pseudo-random value derived from a master key and counter
|
|
2866
|
+
* using the Poseidon PRF. It is used in stream cipher operations to encrypt
|
|
2867
|
+
* plaintext data (e.g., `ciphertext = plaintext + keystream`).
|
|
2868
|
+
*
|
|
2869
|
+
* @remarks
|
|
2870
|
+
* - Must be a valid BN254 field element (less than the field prime)
|
|
2871
|
+
* - Derived deterministically from (key, counter, evaluation_point)
|
|
2872
|
+
* - Different counters produce different keystreams for the same key
|
|
2873
|
+
* - Should be used only once per encryption operation
|
|
2874
|
+
*
|
|
2875
|
+
* ## Generation Formula
|
|
2876
|
+
*
|
|
2877
|
+
* ```
|
|
2878
|
+
* keystream = Poseidon([transactionViewingKey, counter, evaluationPoint])
|
|
2879
|
+
* ```
|
|
2880
|
+
*
|
|
2881
|
+
* Where `evaluationPoint` is a domain separation constant (typically 2).
|
|
2882
|
+
*
|
|
2883
|
+
* @example
|
|
2884
|
+
* ```typescript
|
|
2885
|
+
* const keystreamGenerator = getPoseidonKeystreamGenerator();
|
|
2886
|
+
* const keystreamMap = await keystreamGenerator([0n, 1n, 2n], masterKey);
|
|
2887
|
+
*
|
|
2888
|
+
* const keystream0 = keystreamMap.get(0n); // PoseidonKeystream for counter 0
|
|
2889
|
+
* const ciphertext = (plaintext + keystream0) % BN254_FIELD_PRIME;
|
|
2890
|
+
* ```
|
|
2891
|
+
*
|
|
2892
|
+
* @see {@link assertPoseidonKeystream}
|
|
2893
|
+
* @see {@link PoseidonKey}
|
|
2894
|
+
* @see {@link PoseidonCounter}
|
|
2895
|
+
* @public
|
|
2896
|
+
*/
|
|
2897
|
+
type PoseidonKeystream = SubSubSubBrandedType<Bn254FieldElement, "PoseidonKeystream">;
|
|
2898
|
+
/**
|
|
2899
|
+
* Ciphertext output from Rescue cipher encryption.
|
|
2900
|
+
*
|
|
2901
|
+
* Rescue is an algebraic hash function and cipher designed for
|
|
2902
|
+
* zero-knowledge applications with a focus on STARK-friendliness.
|
|
2903
|
+
*
|
|
2904
|
+
* @remarks
|
|
2905
|
+
* Operates on Curve25519 field elements for compatibility with X25519 key exchange and
|
|
2906
|
+
* Ed25519 signatures. Used in the Umbra protocol for encrypting user account data that
|
|
2907
|
+
* is decryptable by the MXE (multi-party execution environment) network.
|
|
2908
|
+
*
|
|
2909
|
+
* @see {@link assertRcCiphertext}
|
|
2910
|
+
* @see {@link RcKey}
|
|
2911
|
+
* @public
|
|
2912
|
+
*/
|
|
2913
|
+
type RcCiphertext = SubSubSubBrandedType<Curve25519FieldElement, "RcCiphertext">;
|
|
2914
|
+
/**
|
|
2915
|
+
* Plaintext input for Rescue cipher encryption.
|
|
2916
|
+
*
|
|
2917
|
+
* @remarks
|
|
2918
|
+
* Must be a valid Curve25519 field element (less than 2^255 - 19). Represents the raw
|
|
2919
|
+
* data to be encrypted with the Rescue cipher before being stored as `RcCiphertext`.
|
|
2920
|
+
*
|
|
2921
|
+
* @see {@link assertRcPlaintext}
|
|
2922
|
+
* @public
|
|
2923
|
+
*/
|
|
2924
|
+
type RcPlaintext = SubSubSubBrandedType<Curve25519FieldElement, "RcPlaintext">;
|
|
2925
|
+
/**
|
|
2926
|
+
* Encryption key for the Rescue cipher.
|
|
2927
|
+
*
|
|
2928
|
+
* @remarks
|
|
2929
|
+
* Keys should be generated using a cryptographically secure random number generator.
|
|
2930
|
+
* Must be a valid Curve25519 field element (less than 2^255 - 19). In the Umbra protocol,
|
|
2931
|
+
* Rescue keys are typically derived from X25519 shared secrets via a KDF.
|
|
2932
|
+
*
|
|
2933
|
+
* @see {@link assertRcKey}
|
|
2934
|
+
* @public
|
|
2935
|
+
*/
|
|
2936
|
+
type RcKey = SubSubSubBrandedType<Curve25519FieldElement, "RcKey">;
|
|
2937
|
+
/**
|
|
2938
|
+
* Counter value for the Rescue cipher.
|
|
2939
|
+
*
|
|
2940
|
+
* Used as a nonce or counter in counter-mode encryption operations.
|
|
2941
|
+
*
|
|
2942
|
+
* @remarks
|
|
2943
|
+
* Must be a valid Curve25519 field element (less than 2^255 - 19).
|
|
2944
|
+
* Should be unique for each encryption operation with the same key.
|
|
2945
|
+
* Reusing a counter with the same key may compromise confidentiality.
|
|
2946
|
+
*
|
|
2947
|
+
* @see {@link assertRcCounter}
|
|
2948
|
+
* @public
|
|
2949
|
+
*/
|
|
2950
|
+
type RcCounter = SubSubSubBrandedType<Curve25519FieldElement, "RcCounter">;
|
|
2951
|
+
/**
|
|
2952
|
+
* Encryption nonce for the Rescue cipher.
|
|
2953
|
+
*
|
|
2954
|
+
* A nonce (number used once) is a unique value used to ensure that the same
|
|
2955
|
+
* plaintext encrypted with the same key produces different ciphertexts.
|
|
2956
|
+
*
|
|
2957
|
+
* @remarks
|
|
2958
|
+
* - Must be a valid U128 (128-bit unsigned integer, range [0, 2^128 - 1])
|
|
2959
|
+
* - Must be unique for each encryption operation with the same key
|
|
2960
|
+
* - Reusing a nonce with the same key compromises security
|
|
2961
|
+
* - Can be generated randomly or as a counter
|
|
2962
|
+
* - 128-bit nonces provide sufficient uniqueness for most applications
|
|
2963
|
+
*
|
|
2964
|
+
* ## Type Hierarchy
|
|
2965
|
+
* ```
|
|
2966
|
+
* bigint
|
|
2967
|
+
* └── UnsignedInteger (branded, value >= 0)
|
|
2968
|
+
* └── U128 (sub-brand, value < 2^128)
|
|
2969
|
+
* └── RcEncryptionNonce (sub-sub-brand)
|
|
2970
|
+
* ```
|
|
2971
|
+
*
|
|
2972
|
+
* @example
|
|
2973
|
+
* ```typescript
|
|
2974
|
+
* // Generate a random nonce
|
|
2975
|
+
* const randomNonce = generateRandomNonce();
|
|
2976
|
+
*
|
|
2977
|
+
* // Or use a counter-based nonce
|
|
2978
|
+
* const counterNonce = createRcEncryptionNonce(encryptionCounter++);
|
|
2979
|
+
* ```
|
|
2980
|
+
*
|
|
2981
|
+
* @see {@link assertRcEncryptionNonce}
|
|
2982
|
+
* @see {@link Nonce}
|
|
2983
|
+
* @public
|
|
2984
|
+
*/
|
|
2985
|
+
type RcEncryptionNonce = SubSubBrandedType<U128, "RcEncryptionNonce">;
|
|
2986
|
+
/**
|
|
2987
|
+
* Asserts that a value is a valid BN254 field element.
|
|
2988
|
+
*
|
|
2989
|
+
* Validates that the value is a `bigint` and is within the BN254 field prime.
|
|
2990
|
+
*
|
|
2991
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
2992
|
+
* @param name - The name of the variable being asserted (used in error messages)
|
|
2993
|
+
* @defaultValue `name` — `"value"`
|
|
2994
|
+
* @throws {CryptographyAssertionError} If `value` is not a bigint, is negative, or is
|
|
2995
|
+
* greater than or equal to `BN254_FIELD_PRIME`
|
|
2996
|
+
* @returns `void` — narrows the type of `value` to `Bn254FieldElement` on success
|
|
2997
|
+
*
|
|
2998
|
+
* @example
|
|
2999
|
+
* ```typescript
|
|
3000
|
+
* const value = 12345n;
|
|
3001
|
+
* assertBn254FieldElement(value, "value");
|
|
3002
|
+
* // value is now typed as Bn254FieldElement
|
|
3003
|
+
*
|
|
3004
|
+
* assertBn254FieldElement(-1n, "myValue"); // Throws: myValue is negative
|
|
3005
|
+
* assertBn254FieldElement(BN254_FIELD_PRIME, "myValue"); // Throws: myValue exceeds field prime
|
|
3006
|
+
* ```
|
|
3007
|
+
*
|
|
3008
|
+
* @public
|
|
3009
|
+
*/
|
|
3010
|
+
declare function assertBn254FieldElement(value: bigint, name?: string): asserts value is Bn254FieldElement;
|
|
3011
|
+
/**
|
|
3012
|
+
* Asserts that a value is a valid Curve25519 field element.
|
|
3013
|
+
*
|
|
3014
|
+
* Validates that the value is a `bigint` and is within the Curve25519 field prime (2^255 - 19).
|
|
3015
|
+
*
|
|
3016
|
+
* @param value - The bigint to assert (must be in range [0, CURVE25519_FIELD_PRIME - 1])
|
|
3017
|
+
* @param name - The name of the variable being asserted (used in error messages)
|
|
3018
|
+
* @defaultValue `name` — `"value"`
|
|
3019
|
+
* @throws {CryptographyAssertionError} If `value` is not a bigint, is negative, or is
|
|
3020
|
+
* greater than or equal to `CURVE25519_FIELD_PRIME`
|
|
3021
|
+
* @returns `void` — narrows the type of `value` to `Curve25519FieldElement` on success
|
|
3022
|
+
*
|
|
3023
|
+
* @example
|
|
3024
|
+
* ```typescript
|
|
3025
|
+
* const value = 12345n;
|
|
3026
|
+
* assertCurve25519FieldElement(value, "value");
|
|
3027
|
+
* // value is now typed as Curve25519FieldElement
|
|
3028
|
+
*
|
|
3029
|
+
* assertCurve25519FieldElement(-1n, "myValue"); // Throws: myValue is negative
|
|
3030
|
+
* ```
|
|
3031
|
+
*
|
|
3032
|
+
* @public
|
|
3033
|
+
*/
|
|
3034
|
+
declare function assertCurve25519FieldElement(value: bigint, name?: string): asserts value is Curve25519FieldElement;
|
|
3035
|
+
/**
|
|
3036
|
+
* Asserts that a value is a valid X25519 byte array (32 bytes).
|
|
3037
|
+
*
|
|
3038
|
+
* X25519 operations (public keys, private keys, shared secrets) all use
|
|
3039
|
+
* 32-byte values. This assertion validates that the input is a `Uint8Array`
|
|
3040
|
+
* with exactly 32 bytes.
|
|
3041
|
+
*
|
|
3042
|
+
* @param value - The Uint8Array to assert (must be exactly `X25519_BYTE_LENGTH` bytes)
|
|
3043
|
+
* @throws {CryptographyAssertionError} If the value is not a Uint8Array or not exactly 32 bytes
|
|
3044
|
+
* @returns `void` — narrows the type of `value` to `X25519Bytes` on success
|
|
3045
|
+
*
|
|
3046
|
+
* @example
|
|
3047
|
+
* ```typescript
|
|
3048
|
+
* const rawBytes = new Uint8Array(32);
|
|
3049
|
+
* assertX25519Bytes(rawBytes);
|
|
3050
|
+
* // rawBytes is now typed as X25519Bytes
|
|
3051
|
+
*
|
|
3052
|
+
* assertX25519Bytes(new Uint8Array(31)); // Throws: wrong length
|
|
3053
|
+
* ```
|
|
3054
|
+
*
|
|
3055
|
+
* @public
|
|
3056
|
+
*/
|
|
3057
|
+
declare function assertX25519Bytes(value: Uint8Array): asserts value is X25519Bytes;
|
|
3058
|
+
/**
|
|
3059
|
+
* Asserts that a value is a valid X25519 private key.
|
|
3060
|
+
*
|
|
3061
|
+
* A private key must be exactly 32 bytes. This assertion validates both
|
|
3062
|
+
* the type and the length of the input.
|
|
3063
|
+
*
|
|
3064
|
+
* @param value - The Uint8Array to assert (must be exactly `X25519_BYTE_LENGTH` bytes)
|
|
3065
|
+
* @throws {CryptographyAssertionError} If the value is not a Uint8Array or not exactly 32 bytes
|
|
3066
|
+
* @returns `void` — narrows the type of `value` to `X25519PrivateKey` on success
|
|
3067
|
+
*
|
|
3068
|
+
* @remarks
|
|
3069
|
+
* **Security Warning**: Private keys must be kept secret. Never log, transmit, or expose private
|
|
3070
|
+
* keys. Use secure storage mechanisms such as encrypted local storage or a hardware wallet.
|
|
3071
|
+
*
|
|
3072
|
+
* @example
|
|
3073
|
+
* ```typescript
|
|
3074
|
+
* // Generate a new private key
|
|
3075
|
+
* const rawKey = crypto.getRandomValues(new Uint8Array(32));
|
|
3076
|
+
* assertX25519PrivateKey(rawKey);
|
|
3077
|
+
* // rawKey is now typed as X25519PrivateKey
|
|
3078
|
+
* ```
|
|
3079
|
+
*
|
|
3080
|
+
* @public
|
|
3081
|
+
*/
|
|
3082
|
+
declare function assertX25519PrivateKey(value: Uint8Array): asserts value is X25519PrivateKey;
|
|
3083
|
+
/**
|
|
3084
|
+
* Asserts that a value is a valid X25519 public key.
|
|
3085
|
+
*
|
|
3086
|
+
* A public key must be exactly 32 bytes. This assertion validates both
|
|
3087
|
+
* the type and the length of the input.
|
|
3088
|
+
*
|
|
3089
|
+
* @param value - The Uint8Array to assert (must be exactly `X25519_BYTE_LENGTH` bytes)
|
|
3090
|
+
* @throws {CryptographyAssertionError} If the value is not a Uint8Array or not exactly 32 bytes
|
|
3091
|
+
* @returns `void` — narrows the type of `value` to `X25519PublicKey` on success
|
|
3092
|
+
*
|
|
3093
|
+
* @example
|
|
3094
|
+
* ```typescript
|
|
3095
|
+
* // Receive and validate a public key
|
|
3096
|
+
* const theirKey = receivePublicKey();
|
|
3097
|
+
* assertX25519PublicKey(theirKey);
|
|
3098
|
+
* // theirKey is now typed as X25519PublicKey
|
|
3099
|
+
*
|
|
3100
|
+
* // Use in key exchange
|
|
3101
|
+
* const sharedSecret = x25519(myPrivateKey, theirKey);
|
|
3102
|
+
* ```
|
|
3103
|
+
*
|
|
3104
|
+
* @public
|
|
3105
|
+
*/
|
|
3106
|
+
declare function assertX25519PublicKey(value: Uint8Array): asserts value is X25519PublicKey;
|
|
3107
|
+
/**
|
|
3108
|
+
* Asserts that a value is a valid X25519 shared secret.
|
|
3109
|
+
*
|
|
3110
|
+
* A shared secret is the result of an X25519 Diffie-Hellman key exchange.
|
|
3111
|
+
* It must be exactly 32 bytes. This assertion validates both the type
|
|
3112
|
+
* and the length of the input.
|
|
3113
|
+
*
|
|
3114
|
+
* @param value - The Uint8Array to assert (must be exactly `X25519_BYTE_LENGTH` bytes)
|
|
3115
|
+
* @throws {CryptographyAssertionError} If the value is not a Uint8Array or not exactly 32 bytes
|
|
3116
|
+
* @returns `void` — narrows the type of `value` to `SharedSecret` on success
|
|
3117
|
+
*
|
|
3118
|
+
* @remarks
|
|
3119
|
+
* Shared secrets should never be used directly as encryption keys.
|
|
3120
|
+
* Always use a Key Derivation Function (KDF) like HKDF to derive
|
|
3121
|
+
* encryption keys from shared secrets.
|
|
3122
|
+
*
|
|
3123
|
+
* @example
|
|
3124
|
+
* ```typescript
|
|
3125
|
+
* // After performing X25519 key exchange
|
|
3126
|
+
* const rawSecret = x25519(privateKey, publicKey);
|
|
3127
|
+
* assertSharedSecret(rawSecret);
|
|
3128
|
+
* // rawSecret is now typed as SharedSecret
|
|
3129
|
+
*
|
|
3130
|
+
* // Use with a KDF (recommended)
|
|
3131
|
+
* const encryptionKey = hkdf(rawSecret, salt, info);
|
|
3132
|
+
* ```
|
|
3133
|
+
*
|
|
3134
|
+
* @public
|
|
3135
|
+
*/
|
|
3136
|
+
declare function assertSharedSecret(value: Uint8Array): asserts value is SharedSecret;
|
|
3137
|
+
/**
|
|
3138
|
+
* Asserts that a value is a valid Poseidon plaintext.
|
|
3139
|
+
*
|
|
3140
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
3141
|
+
* @throws {CryptographyAssertionError} If not a bigint, is negative, or exceeds BN254 field prime
|
|
3142
|
+
* @returns `void` — narrows the type of `value` to `PoseidonPlaintext` on success
|
|
3143
|
+
*
|
|
3144
|
+
* @example
|
|
3145
|
+
* ```typescript
|
|
3146
|
+
* assertPoseidonPlaintext(messageAsBigInt);
|
|
3147
|
+
* // messageAsBigInt is now typed as PoseidonPlaintext
|
|
3148
|
+
* ```
|
|
3149
|
+
*
|
|
3150
|
+
* @public
|
|
3151
|
+
*/
|
|
3152
|
+
declare function assertPoseidonPlaintext(value: bigint): asserts value is PoseidonPlaintext;
|
|
3153
|
+
/**
|
|
3154
|
+
* Asserts that a value is a valid Poseidon hash.
|
|
3155
|
+
*
|
|
3156
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
3157
|
+
* @throws {CryptographyAssertionError} If not a bigint, is negative, or exceeds BN254 field prime
|
|
3158
|
+
* @returns `void` — narrows the type of `value` to `PoseidonHash` on success
|
|
3159
|
+
*
|
|
3160
|
+
* @example
|
|
3161
|
+
* ```typescript
|
|
3162
|
+
* assertPoseidonHash(hashOutputBigInt);
|
|
3163
|
+
* // hashOutputBigInt is now typed as PoseidonHash
|
|
3164
|
+
* ```
|
|
3165
|
+
*
|
|
3166
|
+
* @public
|
|
3167
|
+
*/
|
|
3168
|
+
declare function assertPoseidonHash(value: bigint): asserts value is PoseidonHash;
|
|
3169
|
+
/**
|
|
3170
|
+
* Asserts that a value is a valid Poseidon key.
|
|
3171
|
+
*
|
|
3172
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
3173
|
+
* @throws {CryptographyAssertionError} If not a bigint, is negative, or exceeds BN254 field prime
|
|
3174
|
+
* @returns `void` — narrows the type of `value` to `PoseidonKey` on success
|
|
3175
|
+
*
|
|
3176
|
+
* @example
|
|
3177
|
+
* ```typescript
|
|
3178
|
+
* assertPoseidonKey(secretKeyBigInt);
|
|
3179
|
+
* // secretKeyBigInt is now typed as PoseidonKey
|
|
3180
|
+
* ```
|
|
3181
|
+
*
|
|
3182
|
+
* @public
|
|
3183
|
+
*/
|
|
3184
|
+
declare function assertPoseidonKey(value: bigint): asserts value is PoseidonKey;
|
|
3185
|
+
/**
|
|
3186
|
+
* Asserts that a value is a valid Poseidon ciphertext.
|
|
3187
|
+
*
|
|
3188
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
3189
|
+
* @throws {CryptographyAssertionError} If not a bigint, is negative, or exceeds BN254 field prime
|
|
3190
|
+
* @returns `void` — narrows the type of `value` to `PoseidonCiphertext` on success
|
|
3191
|
+
*
|
|
3192
|
+
* @example
|
|
3193
|
+
* ```typescript
|
|
3194
|
+
* assertPoseidonCiphertext(encryptedBigInt);
|
|
3195
|
+
* // encryptedBigInt is now typed as PoseidonCiphertext
|
|
3196
|
+
* ```
|
|
3197
|
+
*
|
|
3198
|
+
* @public
|
|
3199
|
+
*/
|
|
3200
|
+
declare function assertPoseidonCiphertext(value: bigint): asserts value is PoseidonCiphertext;
|
|
3201
|
+
/**
|
|
3202
|
+
* Asserts that a value is a valid Poseidon counter.
|
|
3203
|
+
*
|
|
3204
|
+
* Validates that the value is a `bigint` and is within the BN254 field prime.
|
|
3205
|
+
*
|
|
3206
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
3207
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of range
|
|
3208
|
+
*
|
|
3209
|
+
* @remarks
|
|
3210
|
+
* - Input must be a non-negative bigint
|
|
3211
|
+
* - Value must be less than the BN254 field prime
|
|
3212
|
+
* - Counters should be unique for each encryption operation with the same key
|
|
3213
|
+
* - Commonly incremented sequentially starting from 0
|
|
3214
|
+
*
|
|
3215
|
+
* @example
|
|
3216
|
+
* ```typescript
|
|
3217
|
+
* assertPoseidonCounter(counterBigInt);
|
|
3218
|
+
* // counterBigInt is now typed as PoseidonCounter
|
|
3219
|
+
*
|
|
3220
|
+
* assertPoseidonCounter(-1n); // Throws: negative value
|
|
3221
|
+
* assertPoseidonCounter(BN254_FIELD_PRIME); // Throws: exceeds field prime
|
|
3222
|
+
* ```
|
|
3223
|
+
*
|
|
3224
|
+
* @public
|
|
3225
|
+
*/
|
|
3226
|
+
declare function assertPoseidonCounter(value: bigint): asserts value is PoseidonCounter;
|
|
3227
|
+
/**
|
|
3228
|
+
* Asserts that a value is a valid Poseidon keystream.
|
|
3229
|
+
*
|
|
3230
|
+
* Validates that the value is a `bigint` and is within the BN254 field prime.
|
|
3231
|
+
*
|
|
3232
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
3233
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of range
|
|
3234
|
+
*
|
|
3235
|
+
* @remarks
|
|
3236
|
+
* - Input must be a non-negative bigint
|
|
3237
|
+
* - Value must be less than the BN254 field prime
|
|
3238
|
+
* - Keystreams are derived from (key, counter) pairs using Poseidon PRF
|
|
3239
|
+
*
|
|
3240
|
+
* @example
|
|
3241
|
+
* ```typescript
|
|
3242
|
+
* assertPoseidonKeystream(keystreamBigInt);
|
|
3243
|
+
* // keystreamBigInt is now typed as PoseidonKeystream
|
|
3244
|
+
*
|
|
3245
|
+
* assertPoseidonKeystream(-1n); // Throws: negative value
|
|
3246
|
+
* assertPoseidonKeystream(BN254_FIELD_PRIME); // Throws: exceeds field prime
|
|
3247
|
+
* ```
|
|
3248
|
+
*
|
|
3249
|
+
* @public
|
|
3250
|
+
*/
|
|
3251
|
+
declare function assertPoseidonKeystream(value: bigint): asserts value is PoseidonKeystream;
|
|
3252
|
+
/**
|
|
3253
|
+
* Asserts that a value is a valid Rescue cipher ciphertext.
|
|
3254
|
+
*
|
|
3255
|
+
* Validates that the value is a `bigint` and is within the Curve25519 field prime.
|
|
3256
|
+
*
|
|
3257
|
+
* @param value - The bigint to assert (must be in range [0, 2^255 - 19 - 1])
|
|
3258
|
+
* @param name - The name of the variable being asserted (for error messages)
|
|
3259
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of range
|
|
3260
|
+
*
|
|
3261
|
+
* @remarks
|
|
3262
|
+
* - Input must be a non-negative bigint
|
|
3263
|
+
* - Value must be less than the Curve25519 field prime (2^255 - 19)
|
|
3264
|
+
*
|
|
3265
|
+
* @example
|
|
3266
|
+
* ```typescript
|
|
3267
|
+
* assertRcCiphertext(encryptedBigInt, "encryptedBigInt");
|
|
3268
|
+
* // encryptedBigInt is now typed as RcCiphertext
|
|
3269
|
+
*
|
|
3270
|
+
* assertRcCiphertext(-1n, "myValue"); // Throws: myValue is negative
|
|
3271
|
+
* assertRcCiphertext(CURVE25519_FIELD_PRIME, "myValue"); // Throws: myValue exceeds field prime
|
|
3272
|
+
* ```
|
|
3273
|
+
*
|
|
3274
|
+
* @public
|
|
3275
|
+
*/
|
|
3276
|
+
declare function assertRcCiphertext(value: bigint, name?: string): asserts value is RcCiphertext;
|
|
3277
|
+
/**
|
|
3278
|
+
* Asserts that a value is a valid Rescue cipher plaintext.
|
|
3279
|
+
*
|
|
3280
|
+
* Validates that the value is a `bigint` and is within the Curve25519 field prime.
|
|
3281
|
+
*
|
|
3282
|
+
* @param value - The bigint to assert (must be in range [0, 2^255 - 19 - 1])
|
|
3283
|
+
* @param name - The name of the variable being asserted (for error messages)
|
|
3284
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of range
|
|
3285
|
+
*
|
|
3286
|
+
* @remarks
|
|
3287
|
+
* - Input must be a non-negative bigint
|
|
3288
|
+
* - Value must be less than the Curve25519 field prime (2^255 - 19)
|
|
3289
|
+
*
|
|
3290
|
+
* @example
|
|
3291
|
+
* ```typescript
|
|
3292
|
+
* assertRcPlaintext(messageBigInt, "messageBigInt");
|
|
3293
|
+
* // messageBigInt is now typed as RcPlaintext
|
|
3294
|
+
*
|
|
3295
|
+
* assertRcPlaintext(-1n, "myValue"); // Throws: myValue is negative
|
|
3296
|
+
* ```
|
|
3297
|
+
*
|
|
3298
|
+
* @public
|
|
3299
|
+
*/
|
|
3300
|
+
declare function assertRcPlaintext(value: bigint, name?: string): asserts value is RcPlaintext;
|
|
3301
|
+
/**
|
|
3302
|
+
* Asserts that a value is a valid Rescue cipher key.
|
|
3303
|
+
*
|
|
3304
|
+
* Validates that the value is a `bigint` and is within the Curve25519 field prime.
|
|
3305
|
+
*
|
|
3306
|
+
* @param value - The bigint to assert (must be in range [0, 2^255 - 19 - 1])
|
|
3307
|
+
* @param name - The name of the variable being asserted (for error messages)
|
|
3308
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of range
|
|
3309
|
+
*
|
|
3310
|
+
* @remarks
|
|
3311
|
+
* - Input must be a non-negative bigint
|
|
3312
|
+
* - Value must be less than the Curve25519 field prime (2^255 - 19)
|
|
3313
|
+
* - Keys should be generated using a cryptographically secure random number generator
|
|
3314
|
+
*
|
|
3315
|
+
* @example
|
|
3316
|
+
* ```typescript
|
|
3317
|
+
* assertRcKey(secretKeyBigInt, "secretKeyBigInt");
|
|
3318
|
+
* // secretKeyBigInt is now typed as RcKey
|
|
3319
|
+
*
|
|
3320
|
+
* assertRcKey(-1n, "myKey"); // Throws: myKey is negative
|
|
3321
|
+
* ```
|
|
3322
|
+
*
|
|
3323
|
+
* @public
|
|
3324
|
+
*/
|
|
3325
|
+
declare function assertRcKey(value: bigint, name?: string): asserts value is RcKey;
|
|
3326
|
+
/**
|
|
3327
|
+
* Asserts that a value is a valid Rescue cipher counter.
|
|
3328
|
+
*
|
|
3329
|
+
* Validates that the value is a `bigint` and is within the Curve25519 field prime.
|
|
3330
|
+
*
|
|
3331
|
+
* @param value - The bigint to assert (must be in range [0, 2^255 - 19 - 1])
|
|
3332
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of range
|
|
3333
|
+
*
|
|
3334
|
+
* @remarks
|
|
3335
|
+
* - Input must be a non-negative bigint
|
|
3336
|
+
* - Value must be less than the Curve25519 field prime (2^255 - 19)
|
|
3337
|
+
* - Counters should be unique for each encryption operation with the same key
|
|
3338
|
+
*
|
|
3339
|
+
* @example
|
|
3340
|
+
* ```typescript
|
|
3341
|
+
* assertRcCounter(counterBigInt);
|
|
3342
|
+
* // counterBigInt is now typed as RcCounter
|
|
3343
|
+
*
|
|
3344
|
+
* assertRcCounter(-1n); // Throws: negative value
|
|
3345
|
+
* ```
|
|
3346
|
+
*
|
|
3347
|
+
* @public
|
|
3348
|
+
*/
|
|
3349
|
+
declare function assertRcCounter(value: bigint): asserts value is RcCounter;
|
|
3350
|
+
/**
|
|
3351
|
+
* Asserts that a value is a valid Rescue cipher encryption nonce.
|
|
3352
|
+
*
|
|
3353
|
+
* Validates that the value is a `bigint` and is within the U128 range.
|
|
3354
|
+
*
|
|
3355
|
+
* @param value - The bigint to assert (must be in range [0, 2^128 - 1])
|
|
3356
|
+
* @param name - The name of the variable being asserted (for error messages)
|
|
3357
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of range
|
|
3358
|
+
*
|
|
3359
|
+
* @remarks
|
|
3360
|
+
* - Input must be a non-negative bigint
|
|
3361
|
+
* - Value must be less than or equal to 2^128 - 1
|
|
3362
|
+
* - Nonces must be unique for each encryption operation with the same key
|
|
3363
|
+
* - Reusing a nonce with the same key compromises security
|
|
3364
|
+
*
|
|
3365
|
+
* @example
|
|
3366
|
+
* ```typescript
|
|
3367
|
+
* assertRcEncryptionNonce(nonceBigInt, "nonceBigInt");
|
|
3368
|
+
* // nonceBigInt is now typed as RcEncryptionNonce
|
|
3369
|
+
*
|
|
3370
|
+
* assertRcEncryptionNonce(-1n, "myNonce"); // Throws: myNonce is negative
|
|
3371
|
+
* assertRcEncryptionNonce(2n ** 128n, "myNonce"); // Throws: myNonce exceeds U128 max
|
|
3372
|
+
* ```
|
|
3373
|
+
*
|
|
3374
|
+
* @public
|
|
3375
|
+
*/
|
|
3376
|
+
declare function assertRcEncryptionNonce(value: bigint, name?: string): asserts value is RcEncryptionNonce;
|
|
3377
|
+
/**
|
|
3378
|
+
* Output of the Keccak-256 (SHA3-256) hash function.
|
|
3379
|
+
*
|
|
3380
|
+
* Keccak-256 is a cryptographic hash function from the SHA-3 family that produces
|
|
3381
|
+
* a 256-bit (32-byte) digest. It is widely used in blockchain applications,
|
|
3382
|
+
* notably as the primary hash function in Ethereum.
|
|
3383
|
+
*
|
|
3384
|
+
* @remarks
|
|
3385
|
+
* - Always exactly 32 bytes (256 bits)
|
|
3386
|
+
* - Stored in little-endian byte order
|
|
3387
|
+
* - Collision-resistant and preimage-resistant
|
|
3388
|
+
* - NOT the same as NIST SHA3-256 (uses different padding)
|
|
3389
|
+
*
|
|
3390
|
+
* ## Type Hierarchy
|
|
3391
|
+
* ```
|
|
3392
|
+
* Bytes (base branded type)
|
|
3393
|
+
* └── LeBytes (little-endian sub-brand)
|
|
3394
|
+
* └── U256LeBytes (256-bit sized sub-sub-brand)
|
|
3395
|
+
* └── Keccak256Hash (hash-specific sub-sub-sub-brand)
|
|
3396
|
+
* ```
|
|
3397
|
+
*
|
|
3398
|
+
* ## Use Cases
|
|
3399
|
+
* - Address derivation in Ethereum-compatible systems
|
|
3400
|
+
* - Commitment schemes
|
|
3401
|
+
* - Merkle tree construction
|
|
3402
|
+
* - General-purpose cryptographic hashing
|
|
3403
|
+
*
|
|
3404
|
+
* @example
|
|
3405
|
+
* ```typescript
|
|
3406
|
+
* import { assertKeccak256Hash, Keccak256Hash } from "./cryptography";
|
|
3407
|
+
*
|
|
3408
|
+
* const hashBytes = keccak256(data);
|
|
3409
|
+
* assertKeccak256Hash(hashBytes);
|
|
3410
|
+
* // hashBytes is now typed as Keccak256Hash
|
|
3411
|
+
* ```
|
|
3412
|
+
*
|
|
3413
|
+
* @see {@link assertKeccak256Hash}
|
|
3414
|
+
* @public
|
|
3415
|
+
*/
|
|
3416
|
+
type Keccak256Hash = SubSubSubBrandedType<U256LeBytes, "Keccak256Hash">;
|
|
3417
|
+
/**
|
|
3418
|
+
* Output of the Keccak-512 (SHA3-512) hash function.
|
|
3419
|
+
*
|
|
3420
|
+
* Keccak-512 is a cryptographic hash function from the SHA-3 family that produces
|
|
3421
|
+
* a 512-bit (64-byte) digest. It provides a higher security margin than Keccak-256
|
|
3422
|
+
* and is used when stronger collision resistance is required.
|
|
3423
|
+
*
|
|
3424
|
+
* @remarks
|
|
3425
|
+
* - Always exactly 64 bytes (512 bits)
|
|
3426
|
+
* - Stored in little-endian byte order
|
|
3427
|
+
* - Provides 256-bit security against collision attacks
|
|
3428
|
+
* - Used as the basis for master seed derivation
|
|
3429
|
+
*
|
|
3430
|
+
* ## Type Hierarchy
|
|
3431
|
+
* ```
|
|
3432
|
+
* Bytes (base branded type)
|
|
3433
|
+
* └── LeBytes (little-endian sub-brand)
|
|
3434
|
+
* └── U512LeBytes (512-bit sized sub-sub-brand)
|
|
3435
|
+
* └── Keccak512Hash (hash-specific sub-sub-sub-brand)
|
|
3436
|
+
* ```
|
|
3437
|
+
*
|
|
3438
|
+
* ## Use Cases
|
|
3439
|
+
* - Master seed generation (see {@link MasterSeed})
|
|
3440
|
+
* - High-security key derivation
|
|
3441
|
+
* - Extended hash outputs for cryptographic protocols
|
|
3442
|
+
*
|
|
3443
|
+
* @example
|
|
3444
|
+
* ```typescript
|
|
3445
|
+
* import { assertKeccak512Hash, Keccak512Hash } from "./cryptography";
|
|
3446
|
+
*
|
|
3447
|
+
* const hashBytes = keccak512(entropy);
|
|
3448
|
+
* assertKeccak512Hash(hashBytes);
|
|
3449
|
+
* // hashBytes is now typed as Keccak512Hash
|
|
3450
|
+
* ```
|
|
3451
|
+
*
|
|
3452
|
+
* @see {@link assertKeccak512Hash}
|
|
3453
|
+
* @see {@link MasterSeed}
|
|
3454
|
+
* @public
|
|
3455
|
+
*/
|
|
3456
|
+
type Keccak512Hash = SubSubSubBrandedType<U512LeBytes, "Keccak512Hash">;
|
|
3457
|
+
/**
|
|
3458
|
+
* Master seed for hierarchical key derivation.
|
|
3459
|
+
*
|
|
3460
|
+
* The master seed is the root of a hierarchical deterministic (HD) key derivation
|
|
3461
|
+
* tree. It is generated from high-entropy input (typically via Keccak-512 hashing)
|
|
3462
|
+
* and is used to derive all other keys in the system.
|
|
3463
|
+
*
|
|
3464
|
+
* @remarks
|
|
3465
|
+
* - Always exactly 64 bytes (512 bits)
|
|
3466
|
+
* - Generated from Keccak-512 hash of entropy source
|
|
3467
|
+
* - MUST be kept secret - compromise reveals all derived keys
|
|
3468
|
+
* - Should be generated from at least 256 bits of entropy
|
|
3469
|
+
* - Used to derive the master viewing key
|
|
3470
|
+
*
|
|
3471
|
+
* ## Type Hierarchy
|
|
3472
|
+
* ```
|
|
3473
|
+
* Bytes (base)
|
|
3474
|
+
* └── LeBytes
|
|
3475
|
+
* └── U512LeBytes
|
|
3476
|
+
* └── Keccak512Hash
|
|
3477
|
+
* └── MasterSeed
|
|
3478
|
+
* ```
|
|
3479
|
+
*
|
|
3480
|
+
* ## Security Considerations
|
|
3481
|
+
* - Store securely (encrypted at rest)
|
|
3482
|
+
* - Never transmit over insecure channels
|
|
3483
|
+
* - Consider using hardware security modules (HSM) for production
|
|
3484
|
+
* - Backup procedures should use secure, offline storage
|
|
3485
|
+
*
|
|
3486
|
+
* @example
|
|
3487
|
+
* ```typescript
|
|
3488
|
+
* import { assertMasterSeed, MasterSeed } from "./cryptography";
|
|
3489
|
+
*
|
|
3490
|
+
* // Generate master seed from entropy
|
|
3491
|
+
* const entropy = crypto.getRandomValues(new Uint8Array(32));
|
|
3492
|
+
* const seedBytes = keccak512(entropy);
|
|
3493
|
+
* assertMasterSeed(seedBytes);
|
|
3494
|
+
* // seedBytes is now typed as MasterSeed
|
|
3495
|
+
*
|
|
3496
|
+
* // Derive master viewing key
|
|
3497
|
+
* const masterViewingKey = deriveMasterViewingKey(seedBytes);
|
|
3498
|
+
* ```
|
|
3499
|
+
*
|
|
3500
|
+
* @see {@link assertMasterSeed}
|
|
3501
|
+
* @see {@link MasterViewingKey}
|
|
3502
|
+
* @public
|
|
3503
|
+
*/
|
|
3504
|
+
type MasterSeed = SubSubSubSubBrandedType<Keccak512Hash, "MasterSeed">;
|
|
3505
|
+
/**
|
|
3506
|
+
* Generation seed for ephemeral key derivation.
|
|
3507
|
+
*
|
|
3508
|
+
* A generation seed is used as input to derive ephemeral master seeds for
|
|
3509
|
+
* single-use key derivation scenarios. This enables forward secrecy by
|
|
3510
|
+
* allowing unique seeds to be generated for each operation.
|
|
3511
|
+
*
|
|
3512
|
+
* @remarks
|
|
3513
|
+
* - Always exactly 64 bytes (512 bits)
|
|
3514
|
+
* - Typically derived from external entropy or protocol-specific data
|
|
3515
|
+
* - Used as input to ephemeral master seed generation
|
|
3516
|
+
* - Different from MasterSeed in that it's meant as an input, not a derived root
|
|
3517
|
+
*
|
|
3518
|
+
* ## Type Hierarchy
|
|
3519
|
+
* ```
|
|
3520
|
+
* Bytes (base)
|
|
3521
|
+
* └── LeBytes
|
|
3522
|
+
* └── U512LeBytes
|
|
3523
|
+
* └── Keccak512Hash
|
|
3524
|
+
* └── GenerationSeed
|
|
3525
|
+
* ```
|
|
3526
|
+
*
|
|
3527
|
+
* ## Use Cases
|
|
3528
|
+
* - Ephemeral key generation with deterministic input
|
|
3529
|
+
* - Protocol-specific seed derivation
|
|
3530
|
+
* - Forward secrecy implementations
|
|
3531
|
+
*
|
|
3532
|
+
* @example
|
|
3533
|
+
* ```typescript
|
|
3534
|
+
* import { assertGenerationSeed, GenerationSeed } from "./cryptography";
|
|
3535
|
+
*
|
|
3536
|
+
* // Create generation seed from protocol data
|
|
3537
|
+
* const seedBytes = keccak512(protocolData);
|
|
3538
|
+
* assertGenerationSeed(seedBytes);
|
|
3539
|
+
* // seedBytes is now typed as GenerationSeed
|
|
3540
|
+
*
|
|
3541
|
+
* // Use to generate ephemeral master seed
|
|
3542
|
+
* const ephemeralSeed = generateEphemeralMasterSeed(seedBytes);
|
|
3543
|
+
* ```
|
|
3544
|
+
*
|
|
3545
|
+
* @see {@link assertGenerationSeed}
|
|
3546
|
+
* @public
|
|
3547
|
+
*/
|
|
3548
|
+
type GenerationSeed = SubSubSubSubBrandedType<Keccak512Hash, "GenerationSeed">;
|
|
3549
|
+
/**
|
|
3550
|
+
* Master viewing key for privacy-preserving protocols.
|
|
3551
|
+
*
|
|
3552
|
+
* The master viewing key is derived from the master seed and serves as the root
|
|
3553
|
+
* of the viewing key derivation tree. It can be used to derive time-scoped viewing keys
|
|
3554
|
+
* (yearly, monthly, daily, etc.) for selective disclosure of transaction history.
|
|
3555
|
+
*
|
|
3556
|
+
* @remarks
|
|
3557
|
+
* - Element of the BN254 scalar field (< field prime)
|
|
3558
|
+
* - Derived deterministically from the master seed
|
|
3559
|
+
* - Allows viewing ALL transactions without spending capability
|
|
3560
|
+
* - Can be shared to grant full viewing access
|
|
3561
|
+
*
|
|
3562
|
+
* ## Type Hierarchy
|
|
3563
|
+
* All viewing keys are siblings under Bn254FieldElement (NOT subbrands of each other):
|
|
3564
|
+
* ```
|
|
3565
|
+
* bigint (base)
|
|
3566
|
+
* └── U256 (SubBrandedType)
|
|
3567
|
+
* └── Bn254FieldElement (SubSubBrandedType)
|
|
3568
|
+
* ├── MasterViewingKey (SubSubSubBrandedType)
|
|
3569
|
+
* ├── YearlyViewingKey (SubSubSubBrandedType)
|
|
3570
|
+
* ├── MonthlyViewingKey (SubSubSubBrandedType)
|
|
3571
|
+
* ├── DailyViewingKey (SubSubSubBrandedType)
|
|
3572
|
+
* ├── HourlyViewingKey (SubSubSubBrandedType)
|
|
3573
|
+
* ├── MinuteViewingKey (SubSubSubBrandedType)
|
|
3574
|
+
* └── SecondViewingKey (SubSubSubBrandedType)
|
|
3575
|
+
* ```
|
|
3576
|
+
*
|
|
3577
|
+
* ## Derivation Hierarchy (NOT type hierarchy)
|
|
3578
|
+
* The following shows how keys are DERIVED, not their type relationships:
|
|
3579
|
+
* ```
|
|
3580
|
+
* MasterSeed
|
|
3581
|
+
* └── MasterViewingKey (views all time)
|
|
3582
|
+
* ├── YearlyViewingKey (views specific year)
|
|
3583
|
+
* ├── MonthlyViewingKey (views specific month)
|
|
3584
|
+
* ├── DailyViewingKey (views specific day)
|
|
3585
|
+
* ├── HourlyViewingKey (views specific hour)
|
|
3586
|
+
* ├── MinuteViewingKey (views specific minute)
|
|
3587
|
+
* └── SecondViewingKey (views specific second)
|
|
3588
|
+
* ```
|
|
3589
|
+
*
|
|
3590
|
+
* @example
|
|
3591
|
+
* ```typescript
|
|
3592
|
+
* import { MasterViewingKey, assertMasterViewingKey } from "./cryptography";
|
|
3593
|
+
*
|
|
3594
|
+
* const mvk = deriveMasterViewingKey(masterSeed);
|
|
3595
|
+
* assertMasterViewingKey(mvk);
|
|
3596
|
+
*
|
|
3597
|
+
* // Grant viewing access to auditor
|
|
3598
|
+
* shareViewingKey(auditor, mvk); // Full access
|
|
3599
|
+
* ```
|
|
3600
|
+
*
|
|
3601
|
+
* @see {@link assertMasterViewingKey}
|
|
3602
|
+
* @see {@link YearlyViewingKey}
|
|
3603
|
+
* @see {@link MintViewingKey}
|
|
3604
|
+
* @public
|
|
3605
|
+
*/
|
|
3606
|
+
type MasterViewingKey = SubSubSubBrandedType<Bn254FieldElement, "MasterViewingKey">;
|
|
3607
|
+
/**
|
|
3608
|
+
* Yearly viewing key for time-scoped transaction viewing.
|
|
3609
|
+
*
|
|
3610
|
+
* A yearly viewing key is derived from the master viewing key and grants
|
|
3611
|
+
* viewing access to all transactions within a specific calendar year.
|
|
3612
|
+
* This enables selective disclosure for annual audits or tax reporting.
|
|
3613
|
+
*
|
|
3614
|
+
* @remarks
|
|
3615
|
+
* - Element of the BN254 scalar field
|
|
3616
|
+
* - Derived from master viewing key + year identifier
|
|
3617
|
+
* - Can view all transactions in the specified year
|
|
3618
|
+
* - Cannot view transactions from other years
|
|
3619
|
+
* - Can derive monthly, daily, and finer-grained keys
|
|
3620
|
+
*
|
|
3621
|
+
* ## Use Cases
|
|
3622
|
+
* - Annual financial audits
|
|
3623
|
+
* - Tax reporting requirements
|
|
3624
|
+
* - Yearly compliance reviews
|
|
3625
|
+
* - Historical transaction analysis for a specific year
|
|
3626
|
+
*
|
|
3627
|
+
* @example
|
|
3628
|
+
* ```typescript
|
|
3629
|
+
* import { YearlyViewingKey, assertYearlyViewingKey } from "./cryptography";
|
|
3630
|
+
*
|
|
3631
|
+
* const yearKey = deriveYearlyViewingKey(masterViewingKey, 2024);
|
|
3632
|
+
* assertYearlyViewingKey(yearKey);
|
|
3633
|
+
*
|
|
3634
|
+
* // Share with tax authority for 2024 audit
|
|
3635
|
+
* shareViewingKey(taxAuthority, yearKey);
|
|
3636
|
+
* ```
|
|
3637
|
+
*
|
|
3638
|
+
* @see {@link assertYearlyViewingKey}
|
|
3639
|
+
* @see {@link MonthlyViewingKey}
|
|
3640
|
+
* @public
|
|
3641
|
+
*/
|
|
3642
|
+
type YearlyViewingKey = SubSubSubBrandedType<Bn254FieldElement, "YearlyViewingKey">;
|
|
3643
|
+
/**
|
|
3644
|
+
* Monthly viewing key for time-scoped transaction viewing.
|
|
3645
|
+
*
|
|
3646
|
+
* A monthly viewing key grants viewing access to all transactions within
|
|
3647
|
+
* a specific calendar month. It is derived from the yearly viewing key
|
|
3648
|
+
* and provides more granular access control than yearly keys.
|
|
3649
|
+
*
|
|
3650
|
+
* @remarks
|
|
3651
|
+
* - Element of the BN254 scalar field
|
|
3652
|
+
* - Derived from yearly viewing key + month identifier
|
|
3653
|
+
* - Can view all transactions in the specified month
|
|
3654
|
+
* - Cannot view transactions from other months
|
|
3655
|
+
* - Can derive daily and finer-grained keys
|
|
3656
|
+
*
|
|
3657
|
+
* ## Use Cases
|
|
3658
|
+
* - Monthly financial reconciliation
|
|
3659
|
+
* - Periodic audit sampling
|
|
3660
|
+
* - Subscription billing verification
|
|
3661
|
+
* - Monthly expense reporting
|
|
3662
|
+
*
|
|
3663
|
+
* @example
|
|
3664
|
+
* ```typescript
|
|
3665
|
+
* import { MonthlyViewingKey, assertMonthlyViewingKey } from "./cryptography";
|
|
3666
|
+
*
|
|
3667
|
+
* const monthKey = deriveMonthlyViewingKey(yearlyKey, 6); // June
|
|
3668
|
+
* assertMonthlyViewingKey(monthKey);
|
|
3669
|
+
*
|
|
3670
|
+
* // Share with accountant for monthly review
|
|
3671
|
+
* shareViewingKey(accountant, monthKey);
|
|
3672
|
+
* ```
|
|
3673
|
+
*
|
|
3674
|
+
* @see {@link assertMonthlyViewingKey}
|
|
3675
|
+
* @see {@link DailyViewingKey}
|
|
3676
|
+
* @public
|
|
3677
|
+
*/
|
|
3678
|
+
type MonthlyViewingKey = SubSubSubBrandedType<Bn254FieldElement, "MonthlyViewingKey">;
|
|
3679
|
+
/**
|
|
3680
|
+
* Daily viewing key for time-scoped transaction viewing.
|
|
3681
|
+
*
|
|
3682
|
+
* A daily viewing key grants viewing access to all transactions within
|
|
3683
|
+
* a specific calendar day. It provides fine-grained access control for
|
|
3684
|
+
* scenarios requiring day-level precision.
|
|
3685
|
+
*
|
|
3686
|
+
* @remarks
|
|
3687
|
+
* - Element of the BN254 scalar field
|
|
3688
|
+
* - Derived from monthly viewing key + day identifier
|
|
3689
|
+
* - Can view all transactions on the specified day
|
|
3690
|
+
* - Cannot view transactions from other days
|
|
3691
|
+
* - Can derive hourly and finer-grained keys
|
|
3692
|
+
*
|
|
3693
|
+
* ## Use Cases
|
|
3694
|
+
* - Daily settlement verification
|
|
3695
|
+
* - Incident investigation for specific dates
|
|
3696
|
+
* - Daily trading activity review
|
|
3697
|
+
* - Point-in-time compliance checks
|
|
3698
|
+
*
|
|
3699
|
+
* @example
|
|
3700
|
+
* ```typescript
|
|
3701
|
+
* import { DailyViewingKey, assertDailyViewingKey } from "./cryptography";
|
|
3702
|
+
*
|
|
3703
|
+
* const dayKey = deriveDailyViewingKey(monthlyKey, 15); // 15th of month
|
|
3704
|
+
* assertDailyViewingKey(dayKey);
|
|
3705
|
+
*
|
|
3706
|
+
* // Share for investigating a specific date
|
|
3707
|
+
* shareViewingKey(investigator, dayKey);
|
|
3708
|
+
* ```
|
|
3709
|
+
*
|
|
3710
|
+
* @see {@link assertDailyViewingKey}
|
|
3711
|
+
* @see {@link HourlyViewingKey}
|
|
3712
|
+
* @public
|
|
3713
|
+
*/
|
|
3714
|
+
type DailyViewingKey = SubSubSubBrandedType<Bn254FieldElement, "DailyViewingKey">;
|
|
3715
|
+
/**
|
|
3716
|
+
* Hourly viewing key for time-scoped transaction viewing.
|
|
3717
|
+
*
|
|
3718
|
+
* An hourly viewing key grants viewing access to all transactions within
|
|
3719
|
+
* a specific hour. This provides very fine-grained access control for
|
|
3720
|
+
* high-frequency trading scenarios or detailed forensic analysis.
|
|
3721
|
+
*
|
|
3722
|
+
* @remarks
|
|
3723
|
+
* - Element of the BN254 scalar field
|
|
3724
|
+
* - Derived from daily viewing key + hour identifier (0-23)
|
|
3725
|
+
* - Can view all transactions in the specified hour
|
|
3726
|
+
* - Cannot view transactions from other hours
|
|
3727
|
+
* - Can derive minute and second-level keys
|
|
3728
|
+
*
|
|
3729
|
+
* ## Use Cases
|
|
3730
|
+
* - High-frequency trading audits
|
|
3731
|
+
* - Incident response for specific time windows
|
|
3732
|
+
* - Market manipulation investigation
|
|
3733
|
+
* - Detailed activity timeline construction
|
|
3734
|
+
*
|
|
3735
|
+
* @example
|
|
3736
|
+
* ```typescript
|
|
3737
|
+
* import { HourlyViewingKey, assertHourlyViewingKey } from "./cryptography";
|
|
3738
|
+
*
|
|
3739
|
+
* const hourKey = deriveHourlyViewingKey(dailyKey, 14); // 2 PM
|
|
3740
|
+
* assertHourlyViewingKey(hourKey);
|
|
3741
|
+
* ```
|
|
3742
|
+
*
|
|
3743
|
+
* @see {@link assertHourlyViewingKey}
|
|
3744
|
+
* @see {@link MinuteViewingKey}
|
|
3745
|
+
* @public
|
|
3746
|
+
*/
|
|
3747
|
+
type HourlyViewingKey = SubSubSubBrandedType<Bn254FieldElement, "HourlyViewingKey">;
|
|
3748
|
+
/**
|
|
3749
|
+
* Minute viewing key for time-scoped transaction viewing.
|
|
3750
|
+
*
|
|
3751
|
+
* A minute viewing key grants viewing access to all transactions within
|
|
3752
|
+
* a specific minute. This provides extremely fine-grained access control
|
|
3753
|
+
* for forensic analysis or real-time monitoring scenarios.
|
|
3754
|
+
*
|
|
3755
|
+
* @remarks
|
|
3756
|
+
* - Element of the BN254 scalar field
|
|
3757
|
+
* - Derived from hourly viewing key + minute identifier (0-59)
|
|
3758
|
+
* - Can view all transactions in the specified minute
|
|
3759
|
+
* - Cannot view transactions from other minutes
|
|
3760
|
+
* - Can derive second-level keys for ultimate precision
|
|
3761
|
+
*
|
|
3762
|
+
* ## Use Cases
|
|
3763
|
+
* - Real-time fraud detection analysis
|
|
3764
|
+
* - Precise transaction timing verification
|
|
3765
|
+
* - Forensic timeline reconstruction
|
|
3766
|
+
* - Flash loan attack investigation
|
|
3767
|
+
*
|
|
3768
|
+
* @example
|
|
3769
|
+
* ```typescript
|
|
3770
|
+
* import { MinuteViewingKey, assertMinuteViewingKey } from "./cryptography";
|
|
3771
|
+
*
|
|
3772
|
+
* const minuteKey = deriveMinuteViewingKey(hourlyKey, 30); // :30 minute
|
|
3773
|
+
* assertMinuteViewingKey(minuteKey);
|
|
3774
|
+
* ```
|
|
3775
|
+
*
|
|
3776
|
+
* @see {@link assertMinuteViewingKey}
|
|
3777
|
+
* @see {@link SecondViewingKey}
|
|
3778
|
+
* @public
|
|
3779
|
+
*/
|
|
3780
|
+
type MinuteViewingKey = SubSubSubBrandedType<Bn254FieldElement, "MinuteViewingKey">;
|
|
3781
|
+
/**
|
|
3782
|
+
* Second viewing key for time-scoped transaction viewing.
|
|
3783
|
+
*
|
|
3784
|
+
* A second viewing key grants viewing access to all transactions within
|
|
3785
|
+
* a specific second. This is the finest granularity of viewing keys and
|
|
3786
|
+
* is primarily used for forensic analysis of specific transactions.
|
|
3787
|
+
*
|
|
3788
|
+
* @remarks
|
|
3789
|
+
* - Element of the BN254 scalar field
|
|
3790
|
+
* - Derived from minute viewing key + second identifier (0-59)
|
|
3791
|
+
* - Can view all transactions in the specified second
|
|
3792
|
+
* - Cannot view transactions from other seconds
|
|
3793
|
+
* - Finest granularity in the viewing key hierarchy
|
|
3794
|
+
*
|
|
3795
|
+
* ## Use Cases
|
|
3796
|
+
* - Precise transaction attribution
|
|
3797
|
+
* - MEV (Maximal Extractable Value) analysis
|
|
3798
|
+
* - Front-running detection
|
|
3799
|
+
* - Block-level transaction ordering analysis
|
|
3800
|
+
*
|
|
3801
|
+
* @example
|
|
3802
|
+
* ```typescript
|
|
3803
|
+
* import { SecondViewingKey, assertSecondViewingKey } from "./cryptography";
|
|
3804
|
+
*
|
|
3805
|
+
* const secondKey = deriveSecondViewingKey(minuteKey, 45); // :45 second
|
|
3806
|
+
* assertSecondViewingKey(secondKey);
|
|
3807
|
+
* ```
|
|
3808
|
+
*
|
|
3809
|
+
* @see {@link assertSecondViewingKey}
|
|
3810
|
+
* @public
|
|
3811
|
+
*/
|
|
3812
|
+
type SecondViewingKey = SubSubSubBrandedType<Bn254FieldElement, "SecondViewingKey">;
|
|
3813
|
+
/**
|
|
3814
|
+
* Mint viewing key for token-specific transaction viewing.
|
|
3815
|
+
*
|
|
3816
|
+
* A mint viewing key grants viewing access to all transactions involving
|
|
3817
|
+
* a specific token (mint address). This allows selective disclosure of
|
|
3818
|
+
* activity for a particular asset without revealing transactions of other tokens.
|
|
3819
|
+
*
|
|
3820
|
+
* @remarks
|
|
3821
|
+
* - Element of the BN254 scalar field
|
|
3822
|
+
* - Derived from master viewing key + mint address
|
|
3823
|
+
* - Can view all transactions involving the specified mint
|
|
3824
|
+
* - Cannot view transactions involving other mints
|
|
3825
|
+
* - Used as base for time-scoped viewing keys for a specific mint
|
|
3826
|
+
*
|
|
3827
|
+
* ## Derivation
|
|
3828
|
+
*
|
|
3829
|
+
* The mint viewing key is derived using Poseidon hash:
|
|
3830
|
+
* ```
|
|
3831
|
+
* h0 = Poseidon(MVK, mintAddressLow, mintAddressHigh)
|
|
3832
|
+
* ```
|
|
3833
|
+
*
|
|
3834
|
+
* Where:
|
|
3835
|
+
* - MVK is the master viewing key
|
|
3836
|
+
* - mintAddressLow is bytes 0-15 of the mint address (little-endian U128)
|
|
3837
|
+
* - mintAddressHigh is bytes 16-31 of the mint address (little-endian U128)
|
|
3838
|
+
*
|
|
3839
|
+
* ## Use Cases
|
|
3840
|
+
* - Token-specific compliance reporting
|
|
3841
|
+
* - Asset-specific audit trails
|
|
3842
|
+
* - Selective disclosure for specific tokens
|
|
3843
|
+
* - Token holder activity analysis
|
|
3844
|
+
*
|
|
3845
|
+
* @example
|
|
3846
|
+
* ```typescript
|
|
3847
|
+
* import { MintViewingKey, assertMintViewingKey } from "./cryptography";
|
|
3848
|
+
*
|
|
3849
|
+
* const mintKey = deriveMintViewingKey(masterViewingKey, usdcMint);
|
|
3850
|
+
* assertMintViewingKey(mintKey);
|
|
3851
|
+
*
|
|
3852
|
+
* // Share with auditor for USDC-specific review
|
|
3853
|
+
* shareViewingKey(auditor, mintKey);
|
|
3854
|
+
* ```
|
|
3855
|
+
*
|
|
3856
|
+
* @see {@link assertMintViewingKey}
|
|
3857
|
+
* @public
|
|
3858
|
+
*/
|
|
3859
|
+
type MintViewingKey = SubSubSubBrandedType<Bn254FieldElement, "MintViewingKey">;
|
|
3860
|
+
/**
|
|
3861
|
+
* An X25519 key pair consisting of a private key and its corresponding public key.
|
|
3862
|
+
*
|
|
3863
|
+
* X25519 is an elliptic curve Diffie-Hellman (ECDH) protocol using Curve25519.
|
|
3864
|
+
* A keypair consists of:
|
|
3865
|
+
* - A 32-byte private key (random or derived)
|
|
3866
|
+
* - A 32-byte public key (computed from the private key)
|
|
3867
|
+
*
|
|
3868
|
+
* @remarks
|
|
3869
|
+
* ## Key Generation
|
|
3870
|
+
* - Private key: 32 random bytes (optionally clamped per X25519 spec)
|
|
3871
|
+
* - Public key: Scalar multiplication of private key with base point
|
|
3872
|
+
*
|
|
3873
|
+
* ## Security Properties
|
|
3874
|
+
* - Private key MUST be kept secret
|
|
3875
|
+
* - Public key can be freely shared
|
|
3876
|
+
* - Shared secret is computed as: `X25519(myPrivate, theirPublic)`
|
|
3877
|
+
* - Key exchange is symmetric: both parties compute the same shared secret
|
|
3878
|
+
*
|
|
3879
|
+
* ## Use Cases
|
|
3880
|
+
* - End-to-end encryption key establishment
|
|
3881
|
+
* - Ephemeral key exchange for forward secrecy
|
|
3882
|
+
* - Diffie-Hellman key agreement in protocols like Noise, Signal
|
|
3883
|
+
* - Deriving symmetric encryption keys from asymmetric exchange
|
|
3884
|
+
*
|
|
3885
|
+
* @example
|
|
3886
|
+
* ```typescript
|
|
3887
|
+
* import { X25519Keypair, generateX25519Keypair } from "./cryptography";
|
|
3888
|
+
*
|
|
3889
|
+
* // Generate a new keypair
|
|
3890
|
+
* const keypair: X25519Keypair = await generateX25519Keypair();
|
|
3891
|
+
*
|
|
3892
|
+
* // Share public key with peer
|
|
3893
|
+
* sendPublicKey(peer, keypair.publicKey);
|
|
3894
|
+
*
|
|
3895
|
+
* // Compute shared secret with peer's public key
|
|
3896
|
+
* const sharedSecret = x25519(keypair.privateKey, peerPublicKey);
|
|
3897
|
+
* ```
|
|
3898
|
+
*
|
|
3899
|
+
* @example
|
|
3900
|
+
* ```typescript
|
|
3901
|
+
* // Ephemeral key exchange for forward secrecy
|
|
3902
|
+
* const ephemeralKeypair: X25519Keypair = await generateX25519Keypair();
|
|
3903
|
+
*
|
|
3904
|
+
* // Use ephemeral keypair for this session only
|
|
3905
|
+
* const sessionKey = deriveSessionKey(
|
|
3906
|
+
* x25519(ephemeralKeypair.privateKey, recipientPublicKey)
|
|
3907
|
+
* );
|
|
3908
|
+
*
|
|
3909
|
+
* // Discard private key after use for forward secrecy
|
|
3910
|
+
* ```
|
|
3911
|
+
*
|
|
3912
|
+
* @public
|
|
3913
|
+
*/
|
|
3914
|
+
interface X25519Keypair {
|
|
3915
|
+
/**
|
|
3916
|
+
* The X25519 private key (32 bytes).
|
|
3917
|
+
*
|
|
3918
|
+
* This key MUST be kept secret. It is used to:
|
|
3919
|
+
* - Compute shared secrets with other parties' public keys
|
|
3920
|
+
* - Derive the corresponding public key
|
|
3921
|
+
*
|
|
3922
|
+
* @remarks
|
|
3923
|
+
* The private key should be generated from a cryptographically secure
|
|
3924
|
+
* random source. The X25519 algorithm applies clamping during use.
|
|
3925
|
+
*/
|
|
3926
|
+
readonly privateKey: X25519PrivateKey;
|
|
3927
|
+
/**
|
|
3928
|
+
* The X25519 public key (32 bytes).
|
|
3929
|
+
*
|
|
3930
|
+
* This key can be freely shared. It is:
|
|
3931
|
+
* - Computed from the private key via scalar multiplication
|
|
3932
|
+
* - Used by other parties to compute shared secrets
|
|
3933
|
+
* - Safe to transmit over insecure channels
|
|
3934
|
+
*/
|
|
3935
|
+
readonly publicKey: X25519PublicKey;
|
|
3936
|
+
}
|
|
3937
|
+
/**
|
|
3938
|
+
* Asserts that a value is a valid Keccak-256 hash output.
|
|
3939
|
+
*
|
|
3940
|
+
* Validates that the value is a 32-byte Uint8Array.
|
|
3941
|
+
*
|
|
3942
|
+
* @param value - The Uint8Array to assert (must be exactly `U256_BYTE_LENGTH` = 32 bytes)
|
|
3943
|
+
* @throws {CryptographyAssertionError} If not a Uint8Array or not exactly 32 bytes
|
|
3944
|
+
* @returns `void` — narrows the type of `value` to `Keccak256Hash` on success
|
|
3945
|
+
*
|
|
3946
|
+
* @example
|
|
3947
|
+
* ```typescript
|
|
3948
|
+
* const hashBytes = keccak256(data);
|
|
3949
|
+
* assertKeccak256Hash(hashBytes);
|
|
3950
|
+
* // hashBytes is now typed as Keccak256Hash
|
|
3951
|
+
* ```
|
|
3952
|
+
*
|
|
3953
|
+
* @public
|
|
3954
|
+
*/
|
|
3955
|
+
declare function assertKeccak256Hash(value: Uint8Array): asserts value is Keccak256Hash;
|
|
3956
|
+
/**
|
|
3957
|
+
* Asserts that a value is a valid Keccak-512 hash output.
|
|
3958
|
+
*
|
|
3959
|
+
* Validates that the value is a 64-byte Uint8Array.
|
|
3960
|
+
*
|
|
3961
|
+
* @param value - The Uint8Array to assert (must be exactly `U512_BYTE_LENGTH` = 64 bytes)
|
|
3962
|
+
* @throws {CryptographyAssertionError} If not a Uint8Array or not exactly 64 bytes
|
|
3963
|
+
* @returns `void` — narrows the type of `value` to `Keccak512Hash` on success
|
|
3964
|
+
*
|
|
3965
|
+
* @example
|
|
3966
|
+
* ```typescript
|
|
3967
|
+
* const hashBytes = keccak512(data);
|
|
3968
|
+
* assertKeccak512Hash(hashBytes);
|
|
3969
|
+
* // hashBytes is now typed as Keccak512Hash
|
|
3970
|
+
* ```
|
|
3971
|
+
*
|
|
3972
|
+
* @public
|
|
3973
|
+
*/
|
|
3974
|
+
declare function assertKeccak512Hash(value: Uint8Array): asserts value is Keccak512Hash;
|
|
3975
|
+
/**
|
|
3976
|
+
* Asserts that a value is a valid master seed.
|
|
3977
|
+
*
|
|
3978
|
+
* Validates that the value is a 64-byte Uint8Array (Keccak-512 hash output).
|
|
3979
|
+
*
|
|
3980
|
+
* @param value - The Uint8Array to assert (must be exactly `U512_BYTE_LENGTH` = 64 bytes)
|
|
3981
|
+
* @throws {CryptographyAssertionError} If not a Uint8Array or not exactly 64 bytes
|
|
3982
|
+
* @returns `void` — narrows the type of `value` to `MasterSeed` on success
|
|
3983
|
+
*
|
|
3984
|
+
* @example
|
|
3985
|
+
* ```typescript
|
|
3986
|
+
* const seedBytes = keccak512(entropy);
|
|
3987
|
+
* assertMasterSeed(seedBytes);
|
|
3988
|
+
* // seedBytes is now typed as MasterSeed
|
|
3989
|
+
* ```
|
|
3990
|
+
*
|
|
3991
|
+
* @public
|
|
3992
|
+
*/
|
|
3993
|
+
declare function assertMasterSeed(value: Uint8Array): asserts value is MasterSeed;
|
|
3994
|
+
/**
|
|
3995
|
+
* Asserts that a value is a valid generation seed.
|
|
3996
|
+
*
|
|
3997
|
+
* Validates that the value is a 64-byte Uint8Array (Keccak-512 hash output).
|
|
3998
|
+
*
|
|
3999
|
+
* @param value - The Uint8Array to assert (must be exactly `U512_BYTE_LENGTH` = 64 bytes)
|
|
4000
|
+
* @throws {CryptographyAssertionError} If not a Uint8Array or not exactly 64 bytes
|
|
4001
|
+
* @returns `void` — narrows the type of `value` to `GenerationSeed` on success
|
|
4002
|
+
*
|
|
4003
|
+
* @example
|
|
4004
|
+
* ```typescript
|
|
4005
|
+
* const seedBytes = keccak512(protocolData);
|
|
4006
|
+
* assertGenerationSeed(seedBytes);
|
|
4007
|
+
* // seedBytes is now typed as GenerationSeed
|
|
4008
|
+
* ```
|
|
4009
|
+
*
|
|
4010
|
+
* @public
|
|
4011
|
+
*/
|
|
4012
|
+
declare function assertGenerationSeed(value: Uint8Array): asserts value is GenerationSeed;
|
|
4013
|
+
/**
|
|
4014
|
+
* Asserts that a value is a valid master viewing key.
|
|
4015
|
+
*
|
|
4016
|
+
* Validates that the value is a bigint within the BN254 field.
|
|
4017
|
+
*
|
|
4018
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
4019
|
+
* @throws {CryptographyAssertionError} If not a bigint, is negative, or exceeds BN254 field prime
|
|
4020
|
+
* @returns `void` — narrows the type of `value` to `MasterViewingKey` on success
|
|
4021
|
+
*
|
|
4022
|
+
* @example
|
|
4023
|
+
* ```typescript
|
|
4024
|
+
* const mvk = deriveMasterViewingKey(masterSeed);
|
|
4025
|
+
* assertMasterViewingKey(mvk);
|
|
4026
|
+
* // mvk is now typed as MasterViewingKey
|
|
4027
|
+
* ```
|
|
4028
|
+
*
|
|
4029
|
+
* @public
|
|
4030
|
+
*/
|
|
4031
|
+
declare function assertMasterViewingKey(value: bigint): asserts value is MasterViewingKey;
|
|
4032
|
+
/**
|
|
4033
|
+
* Asserts that a value is a valid yearly viewing key.
|
|
4034
|
+
*
|
|
4035
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
4036
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of the BN254 field range
|
|
4037
|
+
* @returns `void` — narrows the type of `value` to `YearlyViewingKey` on success
|
|
4038
|
+
*
|
|
4039
|
+
* @example
|
|
4040
|
+
* ```typescript
|
|
4041
|
+
* const key = deriveYearlyViewingKey(masterViewingKey, 2024n);
|
|
4042
|
+
* assertYearlyViewingKey(key);
|
|
4043
|
+
* // key is now typed as YearlyViewingKey
|
|
4044
|
+
* ```
|
|
4045
|
+
*
|
|
4046
|
+
* @public
|
|
4047
|
+
*/
|
|
4048
|
+
declare function assertYearlyViewingKey(value: bigint): asserts value is YearlyViewingKey;
|
|
4049
|
+
/**
|
|
4050
|
+
* Asserts that a value is a valid monthly viewing key.
|
|
4051
|
+
*
|
|
4052
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
4053
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of the BN254 field range
|
|
4054
|
+
* @returns `void` — narrows the type of `value` to `MonthlyViewingKey` on success
|
|
4055
|
+
*
|
|
4056
|
+
* @example
|
|
4057
|
+
* ```typescript
|
|
4058
|
+
* const key = deriveMonthlyViewingKey(yearlyViewingKey, 6n);
|
|
4059
|
+
* assertMonthlyViewingKey(key);
|
|
4060
|
+
* // key is now typed as MonthlyViewingKey
|
|
4061
|
+
* ```
|
|
4062
|
+
*
|
|
4063
|
+
* @public
|
|
4064
|
+
*/
|
|
4065
|
+
declare function assertMonthlyViewingKey(value: bigint): asserts value is MonthlyViewingKey;
|
|
4066
|
+
/**
|
|
4067
|
+
* Asserts that a value is a valid daily viewing key.
|
|
4068
|
+
*
|
|
4069
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
4070
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of the BN254 field range
|
|
4071
|
+
* @returns `void` — narrows the type of `value` to `DailyViewingKey` on success
|
|
4072
|
+
*
|
|
4073
|
+
* @example
|
|
4074
|
+
* ```typescript
|
|
4075
|
+
* const key = deriveDailyViewingKey(monthlyViewingKey, 15n);
|
|
4076
|
+
* assertDailyViewingKey(key);
|
|
4077
|
+
* // key is now typed as DailyViewingKey
|
|
4078
|
+
* ```
|
|
4079
|
+
*
|
|
4080
|
+
* @public
|
|
4081
|
+
*/
|
|
4082
|
+
declare function assertDailyViewingKey(value: bigint): asserts value is DailyViewingKey;
|
|
4083
|
+
/**
|
|
4084
|
+
* Asserts that a value is a valid hourly viewing key.
|
|
4085
|
+
*
|
|
4086
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
4087
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of the BN254 field range
|
|
4088
|
+
* @returns `void` — narrows the type of `value` to `HourlyViewingKey` on success
|
|
4089
|
+
*
|
|
4090
|
+
* @example
|
|
4091
|
+
* ```typescript
|
|
4092
|
+
* const key = deriveHourlyViewingKey(dailyViewingKey, 14n);
|
|
4093
|
+
* assertHourlyViewingKey(key);
|
|
4094
|
+
* // key is now typed as HourlyViewingKey
|
|
4095
|
+
* ```
|
|
4096
|
+
*
|
|
4097
|
+
* @public
|
|
4098
|
+
*/
|
|
4099
|
+
declare function assertHourlyViewingKey(value: bigint): asserts value is HourlyViewingKey;
|
|
4100
|
+
/**
|
|
4101
|
+
* Asserts that a value is a valid minute viewing key.
|
|
4102
|
+
*
|
|
4103
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
4104
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of the BN254 field range
|
|
4105
|
+
* @returns `void` — narrows the type of `value` to `MinuteViewingKey` on success
|
|
4106
|
+
*
|
|
4107
|
+
* @example
|
|
4108
|
+
* ```typescript
|
|
4109
|
+
* const key = deriveMinuteViewingKey(hourlyViewingKey, 30n);
|
|
4110
|
+
* assertMinuteViewingKey(key);
|
|
4111
|
+
* // key is now typed as MinuteViewingKey
|
|
4112
|
+
* ```
|
|
4113
|
+
*
|
|
4114
|
+
* @public
|
|
4115
|
+
*/
|
|
4116
|
+
declare function assertMinuteViewingKey(value: bigint): asserts value is MinuteViewingKey;
|
|
4117
|
+
/**
|
|
4118
|
+
* Asserts that a value is a valid second viewing key.
|
|
4119
|
+
*
|
|
4120
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
4121
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of the BN254 field range
|
|
4122
|
+
* @returns `void` — narrows the type of `value` to `SecondViewingKey` on success
|
|
4123
|
+
*
|
|
4124
|
+
* @example
|
|
4125
|
+
* ```typescript
|
|
4126
|
+
* const key = deriveSecondViewingKey(minuteViewingKey, 45n);
|
|
4127
|
+
* assertSecondViewingKey(key);
|
|
4128
|
+
* // key is now typed as SecondViewingKey
|
|
4129
|
+
* ```
|
|
4130
|
+
*
|
|
4131
|
+
* @public
|
|
4132
|
+
*/
|
|
4133
|
+
declare function assertSecondViewingKey(value: bigint): asserts value is SecondViewingKey;
|
|
4134
|
+
/**
|
|
4135
|
+
* Asserts that a value is a valid mint viewing key.
|
|
4136
|
+
*
|
|
4137
|
+
* @param value - The bigint to assert (must be in range [0, BN254_FIELD_PRIME - 1])
|
|
4138
|
+
* @throws {CryptographyAssertionError} If not a bigint or out of the BN254 field range
|
|
4139
|
+
* @returns `void` — narrows the type of `value` to `MintViewingKey` on success
|
|
4140
|
+
*
|
|
4141
|
+
* @example
|
|
4142
|
+
* ```typescript
|
|
4143
|
+
* const key = deriveMintViewingKey(masterViewingKey, usdcMintAddress);
|
|
4144
|
+
* assertMintViewingKey(key);
|
|
4145
|
+
* // key is now typed as MintViewingKey
|
|
4146
|
+
* ```
|
|
4147
|
+
*
|
|
4148
|
+
* @public
|
|
4149
|
+
*/
|
|
4150
|
+
declare function assertMintViewingKey(value: bigint): asserts value is MintViewingKey;
|
|
4151
|
+
/**
|
|
4152
|
+
* Asserts that a value is a valid X25519 keypair.
|
|
4153
|
+
*
|
|
4154
|
+
* Validates that the value is an object with valid `privateKey` and `publicKey` fields,
|
|
4155
|
+
* each being a 32-byte Uint8Array.
|
|
4156
|
+
*
|
|
4157
|
+
* @param value - The object to assert (must have `privateKey` and `publicKey` Uint8Array fields)
|
|
4158
|
+
* @throws {CryptographyAssertionError} If the object is missing required fields or either
|
|
4159
|
+
* field is not a valid 32-byte Uint8Array
|
|
4160
|
+
* @returns `void` — narrows the type of `value` to `X25519Keypair` on success
|
|
4161
|
+
*
|
|
4162
|
+
* @example
|
|
4163
|
+
* ```typescript
|
|
4164
|
+
* const keypair = { privateKey: privKey, publicKey: pubKey };
|
|
4165
|
+
* assertX25519Keypair(keypair);
|
|
4166
|
+
* // keypair is now typed as X25519Keypair
|
|
4167
|
+
* ```
|
|
4168
|
+
*
|
|
4169
|
+
* @public
|
|
4170
|
+
*/
|
|
4171
|
+
declare function assertX25519Keypair(value: {
|
|
4172
|
+
privateKey: Uint8Array;
|
|
4173
|
+
publicKey: Uint8Array;
|
|
4174
|
+
}): asserts value is X25519Keypair;
|
|
4175
|
+
/**
|
|
4176
|
+
* A single Base85 limb value.
|
|
4177
|
+
*
|
|
4178
|
+
* Base85 limbs are used to represent 256-bit values as 3 limbs, where each
|
|
4179
|
+
* limb fits within 85 bits (though stored as U128 for convenience).
|
|
4180
|
+
* This representation is commonly used in ZK circuits to handle large
|
|
4181
|
+
* field elements efficiently.
|
|
4182
|
+
*
|
|
4183
|
+
* @remarks
|
|
4184
|
+
* - Each limb must be less than 2^85
|
|
4185
|
+
* - Used in Poseidon hash inputs for ZK proof generation
|
|
4186
|
+
* - Three Base85 limbs can represent a full 256-bit value
|
|
4187
|
+
*
|
|
4188
|
+
* ## Type Hierarchy
|
|
4189
|
+
* ```
|
|
4190
|
+
* bigint
|
|
4191
|
+
* └── UnsignedInteger
|
|
4192
|
+
* └── U128
|
|
4193
|
+
* └── Base85Limb
|
|
4194
|
+
* ```
|
|
4195
|
+
*
|
|
4196
|
+
* @example
|
|
4197
|
+
* ```typescript
|
|
4198
|
+
* const limb = assertBase85Limb(12345n);
|
|
4199
|
+
* // limb is now typed as Base85Limb
|
|
4200
|
+
* ```
|
|
4201
|
+
*
|
|
4202
|
+
* @see {@link assertBase85Limb}
|
|
4203
|
+
* @see {@link Base85LimbTuple}
|
|
4204
|
+
* @see {@link BASE85_LIMB_MAX}
|
|
4205
|
+
* @public
|
|
4206
|
+
*/
|
|
4207
|
+
type Base85Limb = SubSubBrandedType<U128, "Base85Limb">;
|
|
4208
|
+
/**
|
|
4209
|
+
* Maximum value for a Base85 limb.
|
|
4210
|
+
*
|
|
4211
|
+
* @remarks
|
|
4212
|
+
* Value: 2^85 - 1 = 38685626227668133590597631999999999999999999999999999999n (approximately 3.87e25)
|
|
4213
|
+
*
|
|
4214
|
+
* All `Base85Limb` values must be less than or equal to this constant.
|
|
4215
|
+
*
|
|
4216
|
+
* @see {@link Base85Limb}
|
|
4217
|
+
* @see {@link assertBase85Limb}
|
|
4218
|
+
* @public
|
|
4219
|
+
*/
|
|
4220
|
+
declare const BASE85_LIMB_MAX: bigint;
|
|
4221
|
+
/**
|
|
4222
|
+
* A tuple of three Base85 limbs representing a 256-bit value.
|
|
4223
|
+
*
|
|
4224
|
+
* This interface represents a 256-bit value split into three limbs:
|
|
4225
|
+
* - `low`: bits 0-84 (least significant)
|
|
4226
|
+
* - `middle`: bits 85-169
|
|
4227
|
+
* - `high`: bits 170-255 (most significant)
|
|
4228
|
+
*
|
|
4229
|
+
* @remarks
|
|
4230
|
+
* The total bit capacity is 3 * 85 = 255 bits, which is sufficient
|
|
4231
|
+
* for most 256-bit field elements used in cryptography.
|
|
4232
|
+
*
|
|
4233
|
+
* ## Use Cases
|
|
4234
|
+
* - Poseidon hash inputs in ZK circuits
|
|
4235
|
+
* - Field element representations for proof generation
|
|
4236
|
+
* - Efficient arithmetic in constrained environments
|
|
4237
|
+
*
|
|
4238
|
+
* @example
|
|
4239
|
+
* ```typescript
|
|
4240
|
+
* const limbs: Base85LimbTuple = {
|
|
4241
|
+
* low: assertBase85Limb(lowBits),
|
|
4242
|
+
* middle: assertBase85Limb(middleBits),
|
|
4243
|
+
* high: assertBase85Limb(highBits),
|
|
4244
|
+
* };
|
|
4245
|
+
* ```
|
|
4246
|
+
*
|
|
4247
|
+
* @see {@link Base85Limb}
|
|
4248
|
+
* @public
|
|
4249
|
+
*/
|
|
4250
|
+
interface Base85LimbTuple {
|
|
4251
|
+
/**
|
|
4252
|
+
* The low 85 bits (bits 0-84).
|
|
4253
|
+
*/
|
|
4254
|
+
readonly low: Base85Limb;
|
|
4255
|
+
/**
|
|
4256
|
+
* The middle 85 bits (bits 85-169).
|
|
4257
|
+
*/
|
|
4258
|
+
readonly middle: Base85Limb;
|
|
4259
|
+
/**
|
|
4260
|
+
* The high bits (bits 170-255).
|
|
4261
|
+
*/
|
|
4262
|
+
readonly high: Base85Limb;
|
|
4263
|
+
}
|
|
4264
|
+
/**
|
|
4265
|
+
* Asserts that a value is a valid Base85 limb.
|
|
4266
|
+
*
|
|
4267
|
+
* Validates that the value is a `bigint` and is within the Base85 range [0, 2^85 - 1].
|
|
4268
|
+
*
|
|
4269
|
+
* @param value - The bigint to assert (must be in range [0, BASE85_LIMB_MAX])
|
|
4270
|
+
* @param name - The name of the variable being asserted (used in error messages)
|
|
4271
|
+
* @defaultValue `name` — `"value"`
|
|
4272
|
+
* @throws {CryptographyAssertionError} If not a bigint, is negative, or exceeds `BASE85_LIMB_MAX`
|
|
4273
|
+
* @returns `void` — narrows the type of `value` to `Base85Limb` on success
|
|
4274
|
+
*
|
|
4275
|
+
* @example
|
|
4276
|
+
* ```typescript
|
|
4277
|
+
* const value = 12345n;
|
|
4278
|
+
* assertBase85Limb(value, "value");
|
|
4279
|
+
* // value is now typed as Base85Limb
|
|
4280
|
+
* ```
|
|
4281
|
+
*
|
|
4282
|
+
* @public
|
|
4283
|
+
*/
|
|
4284
|
+
declare function assertBase85Limb(value: bigint, name?: string): asserts value is Base85Limb;
|
|
4285
|
+
/**
|
|
4286
|
+
* Byte length for G1 points in a Groth16 proof (64 bytes = 2 x 32-byte field elements).
|
|
4287
|
+
*
|
|
4288
|
+
* @remarks
|
|
4289
|
+
* Each G1 point on BN254 is represented as (x, y) where both coordinates are 32-byte
|
|
4290
|
+
* big-endian integers. Used for the `a` and `c` components of a Groth16 proof.
|
|
4291
|
+
*
|
|
4292
|
+
* @see {@link Groth16ProofA}
|
|
4293
|
+
* @see {@link Groth16ProofC}
|
|
4294
|
+
* @public
|
|
4295
|
+
*/
|
|
4296
|
+
declare const GROTH16_G1_BYTE_LENGTH = 64;
|
|
4297
|
+
/**
|
|
4298
|
+
* Byte length for G2 points in a Groth16 proof (128 bytes = 4 x 32-byte field elements).
|
|
4299
|
+
*
|
|
4300
|
+
* @remarks
|
|
4301
|
+
* G2 points on BN254 are in the extension field Fq2, so each coordinate is a pair of
|
|
4302
|
+
* 32-byte field elements: (x0, x1) and (y0, y1). Used for the `b` component of a Groth16 proof.
|
|
4303
|
+
*
|
|
4304
|
+
* @see {@link Groth16ProofB}
|
|
4305
|
+
* @public
|
|
4306
|
+
*/
|
|
4307
|
+
declare const GROTH16_G2_BYTE_LENGTH = 128;
|
|
4308
|
+
/**
|
|
4309
|
+
* The A component of a Groth16 proof (G1 point), serialized as 64 bytes.
|
|
4310
|
+
*
|
|
4311
|
+
* This is an elliptic curve point on the G1 group of the BN254 curve,
|
|
4312
|
+
* serialized as 64 bytes: [x (32 bytes), y (32 bytes)].
|
|
4313
|
+
*
|
|
4314
|
+
* @remarks
|
|
4315
|
+
* - Size: 64 bytes (2 field elements x 32 bytes each)
|
|
4316
|
+
* - Format: [x, y] where each coordinate is 32 bytes big-endian
|
|
4317
|
+
* - Represents a point on the G1 curve
|
|
4318
|
+
* - Part of the zkSNARK proof tuple (A, B, C)
|
|
4319
|
+
*
|
|
4320
|
+
* @see {@link assertGroth16ProofA}
|
|
4321
|
+
* @see {@link Groth16Proof}
|
|
4322
|
+
* @public
|
|
4323
|
+
*/
|
|
4324
|
+
type Groth16ProofA = SubBrandedType<Bytes, "Groth16ProofA">;
|
|
4325
|
+
/**
|
|
4326
|
+
* The B component of a Groth16 proof (G2 point), serialized as 128 bytes.
|
|
4327
|
+
*
|
|
4328
|
+
* This is an elliptic curve point on the G2 group of the BN254 curve,
|
|
4329
|
+
* serialized as 128 bytes: [x0 (32), x1 (32), y0 (32), y1 (32)].
|
|
4330
|
+
*
|
|
4331
|
+
* @remarks
|
|
4332
|
+
* - Size: 128 bytes (4 field elements x 32 bytes each)
|
|
4333
|
+
* - Format: [x0, x1, y0, y1] where each component is 32 bytes big-endian
|
|
4334
|
+
* - Represents a point on the G2 curve (extension field Fq2)
|
|
4335
|
+
* - The x-coordinate is (x0, x1) and y-coordinate is (y0, y1)
|
|
4336
|
+
*
|
|
4337
|
+
* @see {@link assertGroth16ProofB}
|
|
4338
|
+
* @see {@link Groth16Proof}
|
|
4339
|
+
* @public
|
|
4340
|
+
*/
|
|
4341
|
+
type Groth16ProofB = SubBrandedType<Bytes, "Groth16ProofB">;
|
|
4342
|
+
/**
|
|
4343
|
+
* The C component of a Groth16 proof (G1 point), serialized as 64 bytes.
|
|
4344
|
+
*
|
|
4345
|
+
* This is an elliptic curve point on the G1 group of the BN254 curve,
|
|
4346
|
+
* serialized as 64 bytes: [x (32 bytes), y (32 bytes)].
|
|
4347
|
+
*
|
|
4348
|
+
* @remarks
|
|
4349
|
+
* - Size: 64 bytes (2 field elements x 32 bytes each)
|
|
4350
|
+
* - Format: [x, y] where each coordinate is 32 bytes big-endian
|
|
4351
|
+
* - Represents a point on the G1 curve
|
|
4352
|
+
* - Part of the zkSNARK proof tuple (A, B, C)
|
|
4353
|
+
*
|
|
4354
|
+
* @see {@link assertGroth16ProofC}
|
|
4355
|
+
* @see {@link Groth16Proof}
|
|
4356
|
+
* @public
|
|
4357
|
+
*/
|
|
4358
|
+
type Groth16ProofC = SubBrandedType<Bytes, "Groth16ProofC">;
|
|
4359
|
+
/**
|
|
4360
|
+
* Asserts that a value is a valid Groth16ProofA (64 bytes).
|
|
4361
|
+
*
|
|
4362
|
+
* @param value - The Uint8Array to assert (must be exactly `GROTH16_G1_BYTE_LENGTH` = 64 bytes)
|
|
4363
|
+
* @param name - Optional name used in error messages for better diagnostics
|
|
4364
|
+
* @throws {TypeError} If the value is not a Uint8Array or not exactly 64 bytes
|
|
4365
|
+
* @returns `void` — narrows the type of `value` to `Groth16ProofA` on success
|
|
4366
|
+
*
|
|
4367
|
+
* @example
|
|
4368
|
+
* ```typescript
|
|
4369
|
+
* const proofA = new Uint8Array(64);
|
|
4370
|
+
* assertGroth16ProofA(proofA);
|
|
4371
|
+
* // proofA is now typed as Groth16ProofA
|
|
4372
|
+
* ```
|
|
4373
|
+
*
|
|
4374
|
+
* @public
|
|
4375
|
+
*/
|
|
4376
|
+
declare function assertGroth16ProofA(value: Uint8Array, name?: string): asserts value is Groth16ProofA;
|
|
4377
|
+
/**
|
|
4378
|
+
* Asserts that a value is a valid Groth16ProofB (128 bytes).
|
|
4379
|
+
*
|
|
4380
|
+
* @param value - The Uint8Array to assert (must be exactly `GROTH16_G2_BYTE_LENGTH` = 128 bytes)
|
|
4381
|
+
* @param name - Optional name used in error messages for better diagnostics
|
|
4382
|
+
* @throws {TypeError} If the value is not a Uint8Array or not exactly 128 bytes
|
|
4383
|
+
* @returns `void` — narrows the type of `value` to `Groth16ProofB` on success
|
|
4384
|
+
*
|
|
4385
|
+
* @example
|
|
4386
|
+
* ```typescript
|
|
4387
|
+
* const proofB = new Uint8Array(128);
|
|
4388
|
+
* assertGroth16ProofB(proofB);
|
|
4389
|
+
* // proofB is now typed as Groth16ProofB
|
|
4390
|
+
* ```
|
|
4391
|
+
*
|
|
4392
|
+
* @public
|
|
4393
|
+
*/
|
|
4394
|
+
declare function assertGroth16ProofB(value: Uint8Array, name?: string): asserts value is Groth16ProofB;
|
|
4395
|
+
/**
|
|
4396
|
+
* Asserts that a value is a valid Groth16ProofC (64 bytes).
|
|
4397
|
+
*
|
|
4398
|
+
* @param value - The Uint8Array to assert (must be exactly `GROTH16_G1_BYTE_LENGTH` = 64 bytes)
|
|
4399
|
+
* @param name - Optional name used in error messages for better diagnostics
|
|
4400
|
+
* @throws {TypeError} If the value is not a Uint8Array or not exactly 64 bytes
|
|
4401
|
+
* @returns `void` — narrows the type of `value` to `Groth16ProofC` on success
|
|
4402
|
+
*
|
|
4403
|
+
* @example
|
|
4404
|
+
* ```typescript
|
|
4405
|
+
* const proofC = new Uint8Array(64);
|
|
4406
|
+
* assertGroth16ProofC(proofC);
|
|
4407
|
+
* // proofC is now typed as Groth16ProofC
|
|
4408
|
+
* ```
|
|
4409
|
+
*
|
|
4410
|
+
* @public
|
|
4411
|
+
*/
|
|
4412
|
+
declare function assertGroth16ProofC(value: Uint8Array, name?: string): asserts value is Groth16ProofC;
|
|
4413
|
+
/**
|
|
4414
|
+
* A complete Groth16 zero-knowledge proof.
|
|
4415
|
+
*
|
|
4416
|
+
* Groth16 is a pairing-based zkSNARK proof system that produces compact proofs
|
|
4417
|
+
* consisting of three elliptic curve points: A (G1), B (G2), and C (G1).
|
|
4418
|
+
*
|
|
4419
|
+
* @remarks
|
|
4420
|
+
* ## Proof Structure (256 bytes total)
|
|
4421
|
+
* - `a`: G1 point (64 bytes)
|
|
4422
|
+
* - `b`: G2 point (128 bytes)
|
|
4423
|
+
* - `c`: G1 point (64 bytes)
|
|
4424
|
+
*
|
|
4425
|
+
* ## Security Properties
|
|
4426
|
+
* - Proof size is constant regardless of circuit size
|
|
4427
|
+
* - Verification is efficient (constant time)
|
|
4428
|
+
* - Requires trusted setup (circuit-specific)
|
|
4429
|
+
*
|
|
4430
|
+
* ## Use Cases
|
|
4431
|
+
* - User registration proofs
|
|
4432
|
+
* - Transfer validity proofs
|
|
4433
|
+
* - Balance update proofs
|
|
4434
|
+
*
|
|
4435
|
+
* @example
|
|
4436
|
+
* ```typescript
|
|
4437
|
+
* const proofA = new Uint8Array(64);
|
|
4438
|
+
* const proofB = new Uint8Array(128);
|
|
4439
|
+
* const proofC = new Uint8Array(64);
|
|
4440
|
+
* assertGroth16ProofA(proofA);
|
|
4441
|
+
* assertGroth16ProofB(proofB);
|
|
4442
|
+
* assertGroth16ProofC(proofC);
|
|
4443
|
+
*
|
|
4444
|
+
* const proof: Groth16Proof = {
|
|
4445
|
+
* a: proofA,
|
|
4446
|
+
* b: proofB,
|
|
4447
|
+
* c: proofC,
|
|
4448
|
+
* };
|
|
4449
|
+
* ```
|
|
4450
|
+
*
|
|
4451
|
+
* @see {@link Groth16ProofA}
|
|
4452
|
+
* @see {@link Groth16ProofB}
|
|
4453
|
+
* @see {@link Groth16ProofC}
|
|
4454
|
+
* @see {@link ZkProofBytes}
|
|
4455
|
+
* @public
|
|
4456
|
+
*/
|
|
4457
|
+
interface Groth16Proof {
|
|
4458
|
+
/**
|
|
4459
|
+
* The A component of the proof (G1 point, 64 bytes).
|
|
4460
|
+
*/
|
|
4461
|
+
readonly a: Groth16ProofA;
|
|
4462
|
+
/**
|
|
4463
|
+
* The B component of the proof (G2 point, 128 bytes).
|
|
4464
|
+
*/
|
|
4465
|
+
readonly b: Groth16ProofB;
|
|
4466
|
+
/**
|
|
4467
|
+
* The C component of the proof (G1 point, 64 bytes).
|
|
4468
|
+
*/
|
|
4469
|
+
readonly c: Groth16ProofC;
|
|
4470
|
+
}
|
|
4471
|
+
/**
|
|
4472
|
+
* A cryptographic nonce for encryption operations.
|
|
4473
|
+
*
|
|
4474
|
+
* This is an alias for `RcEncryptionNonce` and represents a 128-bit value
|
|
4475
|
+
* used to ensure unique encryption outputs.
|
|
4476
|
+
*
|
|
4477
|
+
* @remarks
|
|
4478
|
+
* - Must be unique for each encryption with the same key
|
|
4479
|
+
* - Reusing a nonce compromises security
|
|
4480
|
+
* - Can be generated randomly or as a counter
|
|
4481
|
+
*
|
|
4482
|
+
* @example
|
|
4483
|
+
* ```typescript
|
|
4484
|
+
* const nonce: Nonce = generateRandomNonce();
|
|
4485
|
+
* ```
|
|
4486
|
+
*
|
|
4487
|
+
* @see {@link RcEncryptionNonce}
|
|
4488
|
+
* @public
|
|
4489
|
+
*/
|
|
4490
|
+
type Nonce = RcEncryptionNonce;
|
|
4491
|
+
/**
|
|
4492
|
+
* Expected byte length for serialized Groth16 proofs.
|
|
4493
|
+
*
|
|
4494
|
+
* @remarks
|
|
4495
|
+
* A Groth16 proof consists of:
|
|
4496
|
+
* - A (G1): 2 * 32 = 64 bytes
|
|
4497
|
+
* - B (G2): 4 * 32 = 128 bytes
|
|
4498
|
+
* - C (G1): 2 * 32 = 64 bytes
|
|
4499
|
+
* Total: 256 bytes
|
|
4500
|
+
*
|
|
4501
|
+
* @see {@link ZkProofBytes}
|
|
4502
|
+
* @see {@link assertZkProofBytes}
|
|
4503
|
+
* @public
|
|
4504
|
+
*/
|
|
4505
|
+
declare const ZK_PROOF_BYTE_LENGTH = 256;
|
|
4506
|
+
/**
|
|
4507
|
+
* Serialized zero-knowledge proof bytes.
|
|
4508
|
+
*
|
|
4509
|
+
* This type represents a Groth16 proof serialized to a byte array,
|
|
4510
|
+
* suitable for transmission over the network or storage on-chain.
|
|
4511
|
+
*
|
|
4512
|
+
* @remarks
|
|
4513
|
+
* - Size: 256 bytes (8 field elements * 32 bytes each)
|
|
4514
|
+
* - Format: [A.x, A.y, B.x0, B.x1, B.y0, B.y1, C.x, C.y]
|
|
4515
|
+
* - Each field element is encoded as 32 bytes (big-endian)
|
|
4516
|
+
*
|
|
4517
|
+
* @example
|
|
4518
|
+
* ```typescript
|
|
4519
|
+
* const proofBytes = serializeGroth16Proof(proof);
|
|
4520
|
+
* assertZkProofBytes(proofBytes);
|
|
4521
|
+
* // proofBytes is now typed as ZkProofBytes
|
|
4522
|
+
* ```
|
|
4523
|
+
*
|
|
4524
|
+
* @see {@link assertZkProofBytes}
|
|
4525
|
+
* @see {@link ZK_PROOF_BYTE_LENGTH}
|
|
4526
|
+
* @public
|
|
4527
|
+
*/
|
|
4528
|
+
type ZkProofBytes = SubBrandedType<Bytes, "ZkProofBytes">;
|
|
4529
|
+
/**
|
|
4530
|
+
* Asserts that a value is a valid serialized ZK proof.
|
|
4531
|
+
*
|
|
4532
|
+
* Validates that the value is a 256-byte Uint8Array.
|
|
4533
|
+
*
|
|
4534
|
+
* @param value - The Uint8Array to assert (must be exactly `ZK_PROOF_BYTE_LENGTH` = 256 bytes)
|
|
4535
|
+
* @throws {CryptographyAssertionError} If not a Uint8Array or not exactly 256 bytes
|
|
4536
|
+
* @returns `void` — narrows the type of `value` to `ZkProofBytes` on success
|
|
4537
|
+
*
|
|
4538
|
+
* @example
|
|
4539
|
+
* ```typescript
|
|
4540
|
+
* const proofBytes = new Uint8Array(256);
|
|
4541
|
+
* assertZkProofBytes(proofBytes);
|
|
4542
|
+
* // proofBytes is now typed as ZkProofBytes
|
|
4543
|
+
* ```
|
|
4544
|
+
*
|
|
4545
|
+
* @public
|
|
4546
|
+
*/
|
|
4547
|
+
declare function assertZkProofBytes(value: Uint8Array): asserts value is ZkProofBytes;
|
|
4548
|
+
/**
|
|
4549
|
+
* Byte length for AES-256 keys.
|
|
4550
|
+
*
|
|
4551
|
+
* @remarks
|
|
4552
|
+
* AES-256 requires exactly 32 bytes (256 bits) for the encryption key.
|
|
4553
|
+
*
|
|
4554
|
+
* @see {@link AesKey}
|
|
4555
|
+
* @see {@link assertAesKey}
|
|
4556
|
+
* @public
|
|
4557
|
+
*/
|
|
4558
|
+
declare const AES_KEY_LENGTH = 32;
|
|
4559
|
+
/**
|
|
4560
|
+
* Byte length for AES-GCM initialization vectors.
|
|
4561
|
+
*
|
|
4562
|
+
* @remarks
|
|
4563
|
+
* AES-GCM uses a 12-byte (96-bit) IV as recommended by NIST SP 800-38D.
|
|
4564
|
+
* Each encryption operation must use a unique IV to maintain security.
|
|
4565
|
+
*
|
|
4566
|
+
* @see {@link AES_METADATA_OVERHEAD}
|
|
4567
|
+
* @public
|
|
4568
|
+
*/
|
|
4569
|
+
declare const AES_IV_LENGTH = 12;
|
|
4570
|
+
/**
|
|
4571
|
+
* Byte length for AES-GCM authentication tags.
|
|
4572
|
+
*
|
|
4573
|
+
* @remarks
|
|
4574
|
+
* AES-GCM produces a 16-byte (128-bit) authentication tag for integrity and
|
|
4575
|
+
* authenticity verification. The tag is stored alongside the ciphertext.
|
|
4576
|
+
*
|
|
4577
|
+
* @see {@link AES_METADATA_OVERHEAD}
|
|
4578
|
+
* @public
|
|
4579
|
+
*/
|
|
4580
|
+
declare const AES_AUTH_TAG_LENGTH = 16;
|
|
4581
|
+
/**
|
|
4582
|
+
* Total overhead for AES ciphertext metadata (IV + AuthTag).
|
|
4583
|
+
*
|
|
4584
|
+
* @remarks
|
|
4585
|
+
* The combined ciphertext format is: [IV (12 bytes) | AuthTag (16 bytes) | Ciphertext (N bytes)]
|
|
4586
|
+
* Total overhead: 12 + 16 = 28 bytes. Any `AesCiphertextWithMetadata` must be at least this many bytes.
|
|
4587
|
+
*
|
|
4588
|
+
* @see {@link AesCiphertextWithMetadata}
|
|
4589
|
+
* @see {@link assertAesCiphertextWithMetadata}
|
|
4590
|
+
* @public
|
|
4591
|
+
*/
|
|
4592
|
+
declare const AES_METADATA_OVERHEAD: number;
|
|
4593
|
+
/**
|
|
4594
|
+
* AES-256 encryption key.
|
|
4595
|
+
*
|
|
4596
|
+
* A 32-byte (256-bit) key used for AES-256-GCM encryption. These keys are
|
|
4597
|
+
* derived from master seeds (for ephemeral unlocker) or X25519 shared secrets
|
|
4598
|
+
* (for receiver unlocker).
|
|
4599
|
+
*
|
|
4600
|
+
* @remarks
|
|
4601
|
+
* - Size: 32 bytes exactly
|
|
4602
|
+
* - Must be kept secret and never transmitted in plaintext
|
|
4603
|
+
* - Should be derived using a secure KDF
|
|
4604
|
+
*
|
|
4605
|
+
* ## Type Hierarchy
|
|
4606
|
+
* ```
|
|
4607
|
+
* Bytes (base)
|
|
4608
|
+
* └── AesKey (sub-sub-brand, 32 bytes)
|
|
4609
|
+
* ```
|
|
4610
|
+
*
|
|
4611
|
+
* @example
|
|
4612
|
+
* ```typescript
|
|
4613
|
+
* const key = deriveAesKey(masterSeed, generationIndex);
|
|
4614
|
+
* assertAesKey(key);
|
|
4615
|
+
* // key is now typed as AesKey
|
|
4616
|
+
* ```
|
|
4617
|
+
*
|
|
4618
|
+
* @see {@link AES_KEY_LENGTH}
|
|
4619
|
+
* @see {@link assertAesKey}
|
|
4620
|
+
* @public
|
|
4621
|
+
*/
|
|
4622
|
+
type AesKey = SubBrandedType<Bytes, "AesKey">;
|
|
4623
|
+
/**
|
|
4624
|
+
* AES plaintext input for encryption.
|
|
4625
|
+
*
|
|
4626
|
+
* Variable-length byte array to be encrypted with AES-256-GCM.
|
|
4627
|
+
* For UTXO recovery, this contains: amount (8), modified generation index (16),
|
|
4628
|
+
* destination address (32), and domain separator hash (12) = 68 bytes total.
|
|
4629
|
+
*
|
|
4630
|
+
* @remarks
|
|
4631
|
+
* - Size: Variable (any length)
|
|
4632
|
+
* - Must be a valid Uint8Array
|
|
4633
|
+
* - Content structure depends on the encryption context
|
|
4634
|
+
*
|
|
4635
|
+
* ## Type Hierarchy
|
|
4636
|
+
* ```
|
|
4637
|
+
* Bytes (base)
|
|
4638
|
+
* └── AesPlaintext (sub-brand, variable length)
|
|
4639
|
+
* ```
|
|
4640
|
+
*
|
|
4641
|
+
* @example
|
|
4642
|
+
* ```typescript
|
|
4643
|
+
* const plaintext = buildRecoveryPlaintext(amount, genIndex, destination);
|
|
4644
|
+
* assertAesPlaintext(plaintext);
|
|
4645
|
+
* const ciphertext = await aesEncryptor(key, plaintext);
|
|
4646
|
+
* ```
|
|
4647
|
+
*
|
|
4648
|
+
* @see {@link assertAesPlaintext}
|
|
4649
|
+
* @see {@link AesCiphertextWithMetadata}
|
|
4650
|
+
* @public
|
|
4651
|
+
*/
|
|
4652
|
+
type AesPlaintext = SubBrandedType<Bytes, "AesPlaintext">;
|
|
4653
|
+
/**
|
|
4654
|
+
* AES-GCM ciphertext with embedded metadata.
|
|
4655
|
+
*
|
|
4656
|
+
* A combined format that includes the IV, authentication tag, and encrypted data
|
|
4657
|
+
* in a single byte array. This format is self-contained for decryption.
|
|
4658
|
+
*
|
|
4659
|
+
* ## Layout (28 + N bytes)
|
|
4660
|
+
* ```
|
|
4661
|
+
* [IV (12 bytes) | AuthTag (16 bytes) | Ciphertext (N bytes)]
|
|
4662
|
+
* ```
|
|
4663
|
+
*
|
|
4664
|
+
* @remarks
|
|
4665
|
+
* - Minimum size: 28 bytes (IV + AuthTag with empty plaintext)
|
|
4666
|
+
* - IV is randomly generated per encryption
|
|
4667
|
+
* - AuthTag provides authenticity and integrity verification
|
|
4668
|
+
* - Ciphertext length equals plaintext length
|
|
4669
|
+
*
|
|
4670
|
+
* ## Type Hierarchy
|
|
4671
|
+
* ```
|
|
4672
|
+
* Bytes (base)
|
|
4673
|
+
* └── AesCiphertextWithMetadata (sub-brand, 28+ bytes)
|
|
4674
|
+
* ```
|
|
4675
|
+
*
|
|
4676
|
+
* @example
|
|
4677
|
+
* ```typescript
|
|
4678
|
+
* const ciphertext = await aesEncryptor(key, plaintext);
|
|
4679
|
+
* assertAesCiphertextWithMetadata(ciphertext);
|
|
4680
|
+
*
|
|
4681
|
+
* // Extract components if needed:
|
|
4682
|
+
* const iv = ciphertext.slice(0, 12);
|
|
4683
|
+
* const authTag = ciphertext.slice(12, 28);
|
|
4684
|
+
* const encrypted = ciphertext.slice(28);
|
|
4685
|
+
* ```
|
|
4686
|
+
*
|
|
4687
|
+
* @see {@link assertAesCiphertextWithMetadata}
|
|
4688
|
+
* @see {@link AES_METADATA_OVERHEAD}
|
|
4689
|
+
* @public
|
|
4690
|
+
*/
|
|
4691
|
+
type AesCiphertextWithMetadata = SubBrandedType<Bytes, "AesCiphertextWithMetadata">;
|
|
4692
|
+
/**
|
|
4693
|
+
* Asserts that a value is a valid AES-256 key.
|
|
4694
|
+
*
|
|
4695
|
+
* Validates that the value is a 32-byte Uint8Array.
|
|
4696
|
+
*
|
|
4697
|
+
* @param value - The Uint8Array to assert (must be exactly `AES_KEY_LENGTH` = 32 bytes)
|
|
4698
|
+
* @throws {CryptographyAssertionError} If not a Uint8Array or not exactly 32 bytes
|
|
4699
|
+
* @returns `void` — narrows the type of `value` to `AesKey` on success
|
|
4700
|
+
*
|
|
4701
|
+
* @example
|
|
4702
|
+
* ```typescript
|
|
4703
|
+
* const keyBytes = deriveKeyFromSeed(seed);
|
|
4704
|
+
* assertAesKey(keyBytes);
|
|
4705
|
+
* // keyBytes is now typed as AesKey
|
|
4706
|
+
* ```
|
|
4707
|
+
*
|
|
4708
|
+
* @public
|
|
4709
|
+
*/
|
|
4710
|
+
declare function assertAesKey(value: Uint8Array): asserts value is AesKey;
|
|
4711
|
+
/**
|
|
4712
|
+
* Asserts that a value is a valid AES plaintext.
|
|
4713
|
+
*
|
|
4714
|
+
* Validates that the value is a Uint8Array. Plaintext can be any length.
|
|
4715
|
+
*
|
|
4716
|
+
* @param value - The Uint8Array to assert (any non-null Uint8Array is valid)
|
|
4717
|
+
* @throws {CryptographyAssertionError} If not a Uint8Array
|
|
4718
|
+
* @returns `void` — narrows the type of `value` to `AesPlaintext` on success
|
|
4719
|
+
*
|
|
4720
|
+
* @example
|
|
4721
|
+
* ```typescript
|
|
4722
|
+
* const data = new Uint8Array([1, 2, 3, 4]);
|
|
4723
|
+
* assertAesPlaintext(data);
|
|
4724
|
+
* // data is now typed as AesPlaintext
|
|
4725
|
+
* ```
|
|
4726
|
+
*
|
|
4727
|
+
* @public
|
|
4728
|
+
*/
|
|
4729
|
+
declare function assertAesPlaintext(value: Uint8Array): asserts value is AesPlaintext;
|
|
4730
|
+
/**
|
|
4731
|
+
* Asserts that a value is a valid AES ciphertext with metadata.
|
|
4732
|
+
*
|
|
4733
|
+
* Validates that the value is a Uint8Array with at least 28 bytes
|
|
4734
|
+
* (12-byte IV + 16-byte AuthTag + 0 or more bytes of ciphertext).
|
|
4735
|
+
*
|
|
4736
|
+
* @param value - The Uint8Array to assert (must be at least `AES_METADATA_OVERHEAD` = 28 bytes)
|
|
4737
|
+
* @throws {CryptographyAssertionError} If not a Uint8Array or fewer than 28 bytes
|
|
4738
|
+
* @returns `void` — narrows the type of `value` to `AesCiphertextWithMetadata` on success
|
|
4739
|
+
*
|
|
4740
|
+
* @example
|
|
4741
|
+
* ```typescript
|
|
4742
|
+
* const encrypted = await aesEncrypt(key, plaintext);
|
|
4743
|
+
* assertAesCiphertextWithMetadata(encrypted);
|
|
4744
|
+
* // encrypted is now typed as AesCiphertextWithMetadata
|
|
4745
|
+
* ```
|
|
4746
|
+
*
|
|
4747
|
+
* @public
|
|
4748
|
+
*/
|
|
4749
|
+
declare function assertAesCiphertextWithMetadata(value: Uint8Array): asserts value is AesCiphertextWithMetadata;
|
|
4750
|
+
/**
|
|
4751
|
+
* Byte length of optional data fields used in on-chain instructions.
|
|
4752
|
+
*
|
|
4753
|
+
* @remarks
|
|
4754
|
+
* Optional data fields in Arcium/Umbra protocol instructions are always 32 bytes.
|
|
4755
|
+
*
|
|
4756
|
+
* @internal
|
|
4757
|
+
*/
|
|
4758
|
+
declare const OPTIONAL_DATA_BYTE_LENGTH = 32;
|
|
4759
|
+
/**
|
|
4760
|
+
* A 32-byte array for optional data fields in on-chain instructions.
|
|
4761
|
+
*
|
|
4762
|
+
* @remarks
|
|
4763
|
+
* Optional data fields are used in Arcium/Umbra protocol instructions
|
|
4764
|
+
* for passing additional metadata or future-proofing the protocol.
|
|
4765
|
+
*
|
|
4766
|
+
* ## Properties
|
|
4767
|
+
* - Size: 32 bytes exactly
|
|
4768
|
+
* - Content: Arbitrary bytes (meaning depends on context)
|
|
4769
|
+
* - Default: All zeros if not used
|
|
4770
|
+
*
|
|
4771
|
+
* @example
|
|
4772
|
+
* ```typescript
|
|
4773
|
+
* const optionalData = new Uint8Array(32);
|
|
4774
|
+
* assertOptionalData32(optionalData);
|
|
4775
|
+
* // optionalData is now typed as OptionalData32
|
|
4776
|
+
* ```
|
|
4777
|
+
*
|
|
4778
|
+
* @see {@link assertOptionalData32}
|
|
4779
|
+
* @see {@link OPTIONAL_DATA_BYTE_LENGTH}
|
|
4780
|
+
* @public
|
|
4781
|
+
*/
|
|
4782
|
+
type OptionalData32 = SubBrandedType<Bytes, "OptionalData32">;
|
|
4783
|
+
/**
|
|
4784
|
+
* Asserts that a value is a valid 32-byte optional data array.
|
|
4785
|
+
*
|
|
4786
|
+
* Validates that the value is a Uint8Array with exactly 32 bytes.
|
|
4787
|
+
*
|
|
4788
|
+
* @param value - The Uint8Array to assert (must be exactly 32 bytes)
|
|
4789
|
+
* @param name - Optional name for error messages
|
|
4790
|
+
* @throws {CryptographyAssertionError} If not a Uint8Array or not 32 bytes
|
|
4791
|
+
*
|
|
4792
|
+
* @example
|
|
4793
|
+
* ```typescript
|
|
4794
|
+
* const data = new Uint8Array(32);
|
|
4795
|
+
* assertOptionalData32(data);
|
|
4796
|
+
* // data is now typed as OptionalData32
|
|
4797
|
+
* ```
|
|
4798
|
+
*
|
|
4799
|
+
* @example
|
|
4800
|
+
* ```typescript
|
|
4801
|
+
* // With custom name for better error messages
|
|
4802
|
+
* assertOptionalData32(data, "accountInitOptionalData");
|
|
4803
|
+
* ```
|
|
4804
|
+
*
|
|
4805
|
+
* @public
|
|
4806
|
+
*/
|
|
4807
|
+
declare function assertOptionalData32(value: Uint8Array, name?: string): asserts value is OptionalData32;
|
|
4808
|
+
|
|
4809
|
+
export { type X25519PrivateKey as $, type U16LeBytes as A, type BrandedType as B, type Curve25519FieldElement as C, type U256 as D, type U256BeBytes as E, type U256LeBytes as F, type U32 as G, type U32BeBytes as H, type I1024 as I, type U32LeBytes as J, type U512 as K, type LeBytes as L, type U512BeBytes as M, type U512LeBytes as N, type U64 as O, type PoseidonCiphertext as P, type U64BeBytes as Q, type RcCiphertext as R, type SubBrandedType as S, type U64LeBytes as T, type U128 as U, type U8 as V, type U8BeBytes as W, type X25519PublicKey as X, type U8LeBytes as Y, type UnsignedInteger as Z, type X25519Bytes as _, type BeBytes as a, U256_BYTE_LENGTH as a$, type Base85LimbTuple as a0, type Base85Limb as a1, type SubSubBrandedType as a2, type MasterSeed as a3, type Nonce as a4, type OptionalData32 as a5, type AesKey as a6, type AesCiphertextWithMetadata as a7, type AesPlaintext as a8, type PoseidonCounter as a9, I1024_MIN as aA, I128_MAX as aB, I128_MIN as aC, I16_MAX as aD, I16_MIN as aE, I256_MAX as aF, I256_MIN as aG, I32_MAX as aH, I32_MIN as aI, I512_MAX as aJ, I512_MIN as aK, I64_MAX as aL, I64_MIN as aM, I8_MAX as aN, I8_MIN as aO, type Keccak256Hash as aP, type Keccak512Hash as aQ, MathematicsAssertionError as aR, OPTIONAL_DATA_BYTE_LENGTH as aS, type SubSubSubBrandedType as aT, type SubSubSubSubBrandedType as aU, U1024_BYTE_LENGTH as aV, U1024_MAX as aW, U128_BYTE_LENGTH as aX, U128_MAX as aY, U16_BYTE_LENGTH as aZ, U16_MAX as a_, type PoseidonKeystream as aa, type MasterViewingKey as ab, type SecondViewingKey as ac, type DailyViewingKey as ad, type HourlyViewingKey as ae, type MintViewingKey as af, type MinuteViewingKey as ag, type MonthlyViewingKey as ah, type YearlyViewingKey as ai, type Groth16ProofA as aj, type Groth16ProofB as ak, type Groth16ProofC as al, AES_AUTH_TAG_LENGTH as am, AES_IV_LENGTH as an, AES_KEY_LENGTH as ao, AES_METADATA_OVERHEAD as ap, BASE85_LIMB_MAX as aq, BN254_FIELD_PRIME as ar, CURVE25519_FIELD_PRIME as as, CryptographyAssertionError as at, type ExtractBrand as au, GROTH16_G1_BYTE_LENGTH as av, GROTH16_G2_BYTE_LENGTH as aw, type GenerationSeed as ax, type Groth16Proof as ay, I1024_MAX as az, type Bn254FieldElement as b, assertU128BeBytes as b$, U256_MAX as b0, U32_BYTE_LENGTH as b1, U32_MAX as b2, U512_BYTE_LENGTH as b3, U512_MAX as b4, U64_BYTE_LENGTH as b5, U64_MAX as b6, U8_BYTE_LENGTH as b7, U8_MAX as b8, type UnwrapBrand as b9, assertKeccak256Hash as bA, assertKeccak512Hash as bB, assertLeBytes as bC, assertMasterSeed as bD, assertMasterViewingKey as bE, assertMintViewingKey as bF, assertMinuteViewingKey as bG, assertMonthlyViewingKey as bH, assertOptionalData32 as bI, assertPoseidonCiphertext as bJ, assertPoseidonCounter as bK, assertPoseidonHash as bL, assertPoseidonKey as bM, assertPoseidonKeystream as bN, assertPoseidonPlaintext as bO, assertRcCiphertext as bP, assertRcCounter as bQ, assertRcEncryptionNonce as bR, assertRcKey as bS, assertRcPlaintext as bT, assertSecondViewingKey as bU, assertSharedSecret as bV, assertSignedInteger as bW, assertU1024 as bX, assertU1024BeBytes as bY, assertU1024LeBytes as bZ, assertU128 as b_, type X25519Keypair as ba, X25519_BYTE_LENGTH as bb, ZK_PROOF_BYTE_LENGTH as bc, type ZkProofBytes as bd, assertAesCiphertextWithMetadata as be, assertAesKey as bf, assertAesPlaintext as bg, assertBase85Limb as bh, assertBeBytes as bi, assertBn254FieldElement as bj, assertBytes as bk, assertCurve25519FieldElement as bl, assertDailyViewingKey as bm, assertGenerationSeed as bn, assertGroth16ProofA as bo, assertGroth16ProofB as bp, assertGroth16ProofC as bq, assertHourlyViewingKey as br, assertI1024 as bs, assertI128 as bt, assertI16 as bu, assertI256 as bv, assertI32 as bw, assertI512 as bx, assertI64 as by, assertI8 as bz, type Bytes as c, assertU128LeBytes as c0, assertU16 as c1, assertU16BeBytes as c2, assertU16LeBytes as c3, assertU256 as c4, assertU256BeBytes as c5, assertU256LeBytes as c6, assertU32 as c7, assertU32BeBytes as c8, assertU32LeBytes as c9, assertU512 as ca, assertU512BeBytes as cb, assertU512LeBytes as cc, assertU64 as cd, assertU64BeBytes as ce, assertU64LeBytes as cf, assertU8 as cg, assertU8BeBytes as ch, assertU8LeBytes as ci, assertUnsignedInteger as cj, assertX25519Bytes as ck, assertX25519Keypair as cl, assertX25519PrivateKey as cm, assertX25519PublicKey as cn, assertYearlyViewingKey as co, assertZkProofBytes as cp, type I128 as d, type I16 as e, type I256 as f, type I32 as g, type I512 as h, type I64 as i, type I8 as j, type PoseidonHash as k, type PoseidonKey as l, type PoseidonPlaintext as m, type RcCounter as n, type RcEncryptionNonce as o, type RcKey as p, type RcPlaintext as q, type SharedSecret as r, type SignedInteger as s, type U1024 as t, type U1024BeBytes as u, type U1024LeBytes as v, type U128BeBytes as w, type U128LeBytes as x, type U16 as y, type U16BeBytes as z };
|