@@ -38,10 +38,11 @@ import { abytes, asciiToBytes, bytesToNumberLE, equalBytes } from './utils.ts';
38
38
// Finite field 2n**448n - 2n**224n - 1n
39
39
// Subgroup order
40
40
// 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
41
- const ed448_CURVE : EdwardsOpts = {
42
- p : BigInt (
43
- '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
44
- ) ,
41
+ const ed448_CURVE_p = BigInt (
42
+ '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
43
+ ) ;
44
+ const ed448_CURVE : EdwardsOpts = /* @__PURE__ */ ( ( ) => ( {
45
+ p : ed448_CURVE_p ,
45
46
n : BigInt (
46
47
'0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3'
47
48
) ,
@@ -56,36 +57,37 @@ const ed448_CURVE: EdwardsOpts = {
56
57
Gy : BigInt (
57
58
'0x693f46716eb6bc248876203756c9c7624bea73736ca3984087789c1e05a0c2d73ad3ff1ce67c39c4fdbd132c4ed7c8ad9808795bf230fa14'
58
59
) ,
59
- } ;
60
+ } ) ) ( ) ;
60
61
61
62
// E448 NIST curve is identical to edwards448, except for:
62
63
// d = 39082/39081
63
64
// Gx = 3/2
64
- const E448_CURVE : EdwardsOpts = Object . assign ( { } , ed448_CURVE , {
65
- d : BigInt (
66
- '0xd78b4bdc7f0daf19f24f38c29373a2ccad46157242a50f37809b1da3412a12e79ccc9c81264cfe9ad080997058fb61c4243cc32dbaa156b9'
67
- ) ,
68
- Gx : BigInt (
69
- '0x79a70b2b70400553ae7c9df416c792c61128751ac92969240c25a07d728bdc93e21f7787ed6972249de732f38496cd11698713093e9c04fc'
70
- ) ,
71
- Gy : BigInt (
72
- '0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000001'
73
- ) ,
74
- } ) ;
65
+ const E448_CURVE : EdwardsOpts = /* @__PURE__ */ ( ( ) =>
66
+ Object . assign ( { } , ed448_CURVE , {
67
+ d : BigInt (
68
+ '0xd78b4bdc7f0daf19f24f38c29373a2ccad46157242a50f37809b1da3412a12e79ccc9c81264cfe9ad080997058fb61c4243cc32dbaa156b9'
69
+ ) ,
70
+ Gx : BigInt (
71
+ '0x79a70b2b70400553ae7c9df416c792c61128751ac92969240c25a07d728bdc93e21f7787ed6972249de732f38496cd11698713093e9c04fc'
72
+ ) ,
73
+ Gy : BigInt (
74
+ '0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000001'
75
+ ) ,
76
+ } ) ) ( ) ;
75
77
76
78
const shake256_114 = /* @__PURE__ */ wrapConstructor ( ( ) => shake256 . create ( { dkLen : 114 } ) ) ;
77
79
const shake256_64 = /* @__PURE__ */ wrapConstructor ( ( ) => shake256 . create ( { dkLen : 64 } ) ) ;
78
80
79
81
// prettier-ignore
80
- const _1n = BigInt ( 1 ) , _2n = BigInt ( 2 ) , _3n = BigInt ( 3 ) , _4n = BigInt ( 4 ) , _11n = BigInt ( 11 ) ;
82
+ const _1n = BigInt ( 1 ) , _2n = BigInt ( 2 ) , _3n = BigInt ( 3 ) , _4n = /* @__PURE__ */ BigInt ( 4 ) , _11n = BigInt ( 11 ) ;
81
83
// prettier-ignore
82
84
const _22n = BigInt ( 22 ) , _44n = BigInt ( 44 ) , _88n = BigInt ( 88 ) , _223n = BigInt ( 223 ) ;
83
85
84
86
// powPminus3div4 calculates z = x^k mod p, where k = (p-3)/4.
85
87
// Used for efficient square root calculation.
86
88
// ((P-3)/4).toString(2) would produce bits [223x 1, 0, 222x 1]
87
89
function ed448_pow_Pminus3div4 ( x : bigint ) : bigint {
88
- const P = ed448_CURVE . p ;
90
+ const P = ed448_CURVE_p ;
89
91
const b2 = ( x * x * x ) % P ;
90
92
const b3 = ( b2 * b2 * x ) % P ;
91
93
const b6 = ( pow2 ( b3 , _3n , P ) * b3 ) % P ;
@@ -114,7 +116,7 @@ function adjustScalarBytes(bytes: Uint8Array): Uint8Array {
114
116
// Constant-time ratio of u to v. Allows to combine inversion and square root u/√v.
115
117
// Uses algo from RFC8032 5.1.3.
116
118
function uvRatio ( u : bigint , v : bigint ) : { isValid : boolean ; value : bigint } {
117
- const P = ed448_CURVE . p ;
119
+ const P = ed448_CURVE_p ;
118
120
// https://www.rfc-editor.org/rfc/rfc8032#section-5.2.3
119
121
// To compute the square root of (u/v), the first step is to compute the
120
122
// candidate root x = (u/v)^((p+1)/4). This can be done using the
@@ -137,10 +139,10 @@ function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
137
139
// The value fits in 448 bits, but we use 456-bit (57-byte) elements because of bitflags.
138
140
// - ed25519 fits in 255 bits, allowing using last 1 byte for specifying bit flag of point negation.
139
141
// - ed448 fits in 448 bits. We can't use last 1 byte: we can only use a bit 224 in the middle.
140
- const Fp = /* @__PURE__ */ ( ( ) => Field ( ed448_CURVE . p , { BITS : 456 , isLE : true } ) ) ( ) ;
142
+ const Fp = /* @__PURE__ */ ( ( ) => Field ( ed448_CURVE_p , { BITS : 456 , isLE : true } ) ) ( ) ;
141
143
const Fn = /* @__PURE__ */ ( ( ) => Field ( ed448_CURVE . n , { BITS : 456 , isLE : true } ) ) ( ) ;
142
144
// decaf448 uses 448-bit (56-byte) keys
143
- const Fp448 = /* @__PURE__ */ ( ( ) => Field ( ed448_CURVE . p , { BITS : 448 , isLE : true } ) ) ( ) ;
145
+ const Fp448 = /* @__PURE__ */ ( ( ) => Field ( ed448_CURVE_p , { BITS : 448 , isLE : true } ) ) ( ) ;
144
146
const Fn448 = /* @__PURE__ */ ( ( ) => Field ( ed448_CURVE . n , { BITS : 448 , isLE : true } ) ) ( ) ;
145
147
146
148
// SHAKE256(dom4(phflag,context)||x, 114)
@@ -154,7 +156,7 @@ function dom4(data: Uint8Array, ctx: Uint8Array, phflag: boolean) {
154
156
) ;
155
157
}
156
158
const ed448_eddsa_opts = { adjustScalarBytes, domain : dom4 } ;
157
- const ed448_Point = edwards ( ed448_CURVE , { Fp, Fn, uvRatio } ) ;
159
+ const ed448_Point = /* @__PURE__ */ edwards ( ed448_CURVE , { Fp, Fn, uvRatio } ) ;
158
160
159
161
/**
160
162
* ed448 EdDSA curve and methods.
@@ -168,20 +170,21 @@ const ed448_Point = edwards(ed448_CURVE, { Fp, Fn, uvRatio });
168
170
* const isValid = ed448.verify(sig, msg, publicKey);
169
171
* ```
170
172
*/
171
- export const ed448 : EdDSA = eddsa ( ed448_Point , shake256_114 , ed448_eddsa_opts ) ;
173
+ export const ed448 : EdDSA = /* @__PURE__ */ eddsa ( ed448_Point , shake256_114 , ed448_eddsa_opts ) ;
172
174
173
175
// There is no ed448ctx, since ed448 supports ctx by default
174
176
/** Prehashed version of ed448. See {@link ed448} */
175
- export const ed448ph : EdDSA = /* @__PURE__ */ eddsa ( ed448_Point , shake256_114 , {
176
- ...ed448_eddsa_opts ,
177
- prehash : shake256_64 ,
178
- } ) ;
177
+ export const ed448ph : EdDSA = /* @__PURE__ */ ( ( ) =>
178
+ eddsa ( ed448_Point , shake256_114 , {
179
+ ...ed448_eddsa_opts ,
180
+ prehash : shake256_64 ,
181
+ } ) ) ( ) ;
179
182
180
183
/**
181
184
* E448 (NIST) != edwards448 used in ed448.
182
185
* E448 is birationally equivalent to edwards448.
183
186
*/
184
- export const E448 : EdwardsPointCons = edwards ( E448_CURVE ) ;
187
+ export const E448 : EdwardsPointCons = /* @__PURE__ */ edwards ( E448_CURVE ) ;
185
188
186
189
/**
187
190
* ECDH using curve448 aka x448.
@@ -195,7 +198,7 @@ export const E448: EdwardsPointCons = edwards(E448_CURVE);
195
198
* ```
196
199
*/
197
200
export const x448 : MontgomeryECDH = /* @__PURE__ */ ( ( ) => {
198
- const P = ed448_CURVE . p ;
201
+ const P = ed448_CURVE_p ;
199
202
return montgomery ( {
200
203
P,
201
204
type : 'x448' ,
@@ -209,7 +212,7 @@ export const x448: MontgomeryECDH = /* @__PURE__ */ (() => {
209
212
} ) ( ) ;
210
213
211
214
// Hash To Curve Elligator2 Map
212
- const ELL2_C1 = /* @__PURE__ */ ( ( ) => ( Fp . ORDER - BigInt ( 3 ) ) / BigInt ( 4 ) ) ( ) ; // 1. c1 = (q - 3) / 4 # Integer arithmetic
215
+ const ELL2_C1 = /* @__PURE__ */ ( ( ) => ( ed448_CURVE_p - BigInt ( 3 ) ) / BigInt ( 4 ) ) ( ) ; // 1. c1 = (q - 3) / 4 # Integer arithmetic
213
216
const ELL2_J = /* @__PURE__ */ BigInt ( 156326 ) ;
214
217
215
218
function map_to_curve_elligator2_curve448 ( u : bigint ) {
@@ -290,7 +293,7 @@ export const ed448_hasher: H2CHasher<EdwardsPointCons> = /* @__PURE__ */ (() =>
290
293
createHasher ( ed448_Point , ( scalars : bigint [ ] ) => map_to_curve_elligator2_edwards448 ( scalars [ 0 ] ) , {
291
294
DST : 'edwards448_XOF:SHAKE256_ELL2_RO_' ,
292
295
encodeDST : 'edwards448_XOF:SHAKE256_ELL2_NU_' ,
293
- p : Fp . ORDER ,
296
+ p : ed448_CURVE_p ,
294
297
m : 1 ,
295
298
k : 224 ,
296
299
expand : 'xof' ,
@@ -318,9 +321,8 @@ const invertSqrt = (number: bigint) => uvRatio(_1n, number);
318
321
* and [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-element-derivation-2).
319
322
*/
320
323
function calcElligatorDecafMap ( r0 : bigint ) : EdwardsPoint {
321
- const { d } = ed448_CURVE ;
322
- const P = Fp . ORDER ;
323
- const mod = ( n : bigint ) => Fp . create ( n ) ;
324
+ const { d, p : P } = ed448_CURVE ;
325
+ const mod = ( n : bigint ) => Fp448 . create ( n ) ;
324
326
325
327
const r = mod ( - ( r0 * r0 ) ) ; // 1
326
328
const u0 = mod ( d * ( r - _1n ) ) ; // 2
@@ -386,8 +388,7 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
386
388
387
389
static fromBytes ( bytes : Uint8Array ) : _DecafPoint {
388
390
abytes ( bytes , 56 ) ;
389
- const { d } = ed448_CURVE ;
390
- const P = Fp . ORDER ;
391
+ const { d, p : P } = ed448_CURVE ;
391
392
const mod = ( n : bigint ) => Fp448 . create ( n ) ;
392
393
const s = Fp448 . fromBytes ( bytes ) ;
393
394
@@ -429,8 +430,8 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
429
430
*/
430
431
toBytes ( ) : Uint8Array {
431
432
const { X, Z, T } = this . ep ;
432
- const P = Fp . ORDER ;
433
- const mod = ( n : bigint ) => Fp . create ( n ) ;
433
+ const P = ed448_CURVE . p ;
434
+ const mod = ( n : bigint ) => Fp448 . create ( n ) ;
434
435
const u1 = mod ( mod ( X + T ) * mod ( X - T ) ) ; // 1
435
436
const x2 = mod ( X * X ) ;
436
437
const { value : invsqrt } = invertSqrt ( mod ( u1 * ONE_MINUS_D * x2 ) ) ; // 2
@@ -451,7 +452,7 @@ class _DecafPoint extends PrimeEdwardsPoint<_DecafPoint> {
451
452
const { X : X1 , Y : Y1 } = this . ep ;
452
453
const { X : X2 , Y : Y2 } = other . ep ;
453
454
// (x1 * y2 == y1 * x2)
454
- return Fp . create ( X1 * Y2 ) === Fp . create ( Y1 * X2 ) ;
455
+ return Fp448 . create ( X1 * Y2 ) === Fp448 . create ( Y1 * X2 ) ;
455
456
}
456
457
457
458
is0 ( ) : boolean {
0 commit comments