@noble/curves 1.9.1 → 1.9.2

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.
Files changed (189) hide show
  1. package/README.md +56 -25
  2. package/_shortw_utils.d.ts +7 -5
  3. package/_shortw_utils.d.ts.map +1 -1
  4. package/_shortw_utils.js +2 -8
  5. package/_shortw_utils.js.map +1 -1
  6. package/abstract/bls.d.ts +60 -24
  7. package/abstract/bls.d.ts.map +1 -1
  8. package/abstract/bls.js +158 -109
  9. package/abstract/bls.js.map +1 -1
  10. package/abstract/curve.d.ts +44 -9
  11. package/abstract/curve.d.ts.map +1 -1
  12. package/abstract/curve.js +86 -7
  13. package/abstract/curve.js.map +1 -1
  14. package/abstract/edwards.d.ts +112 -25
  15. package/abstract/edwards.d.ts.map +1 -1
  16. package/abstract/edwards.js +138 -102
  17. package/abstract/edwards.js.map +1 -1
  18. package/abstract/fft.d.ts +12 -10
  19. package/abstract/fft.d.ts.map +1 -1
  20. package/abstract/fft.js +12 -13
  21. package/abstract/fft.js.map +1 -1
  22. package/abstract/hash-to-curve.d.ts +25 -11
  23. package/abstract/hash-to-curve.d.ts.map +1 -1
  24. package/abstract/hash-to-curve.js +17 -14
  25. package/abstract/hash-to-curve.js.map +1 -1
  26. package/abstract/modular.d.ts +24 -11
  27. package/abstract/modular.d.ts.map +1 -1
  28. package/abstract/modular.js +49 -20
  29. package/abstract/modular.js.map +1 -1
  30. package/abstract/montgomery.d.ts +1 -1
  31. package/abstract/montgomery.d.ts.map +1 -1
  32. package/abstract/montgomery.js +5 -4
  33. package/abstract/montgomery.js.map +1 -1
  34. package/abstract/poseidon.d.ts +5 -13
  35. package/abstract/poseidon.d.ts.map +1 -1
  36. package/abstract/poseidon.js +12 -7
  37. package/abstract/poseidon.js.map +1 -1
  38. package/abstract/tower.d.ts +20 -46
  39. package/abstract/tower.d.ts.map +1 -1
  40. package/abstract/tower.js +9 -3
  41. package/abstract/tower.js.map +1 -1
  42. package/abstract/utils.d.ts +1 -115
  43. package/abstract/utils.d.ts.map +1 -1
  44. package/abstract/utils.js +17 -371
  45. package/abstract/utils.js.map +1 -1
  46. package/abstract/weierstrass.d.ts +132 -76
  47. package/abstract/weierstrass.d.ts.map +1 -1
  48. package/abstract/weierstrass.js +462 -398
  49. package/abstract/weierstrass.js.map +1 -1
  50. package/bls12-381.d.ts +2 -0
  51. package/bls12-381.d.ts.map +1 -1
  52. package/bls12-381.js +504 -466
  53. package/bls12-381.js.map +1 -1
  54. package/bn254.d.ts +2 -0
  55. package/bn254.d.ts.map +1 -1
  56. package/bn254.js +44 -32
  57. package/bn254.js.map +1 -1
  58. package/ed25519.d.ts +8 -5
  59. package/ed25519.d.ts.map +1 -1
  60. package/ed25519.js +67 -54
  61. package/ed25519.js.map +1 -1
  62. package/ed448.d.ts +10 -6
  63. package/ed448.d.ts.map +1 -1
  64. package/ed448.js +80 -57
  65. package/ed448.js.map +1 -1
  66. package/esm/_shortw_utils.d.ts +7 -5
  67. package/esm/_shortw_utils.d.ts.map +1 -1
  68. package/esm/_shortw_utils.js +2 -8
  69. package/esm/_shortw_utils.js.map +1 -1
  70. package/esm/abstract/bls.d.ts +60 -24
  71. package/esm/abstract/bls.d.ts.map +1 -1
  72. package/esm/abstract/bls.js +158 -109
  73. package/esm/abstract/bls.js.map +1 -1
  74. package/esm/abstract/curve.d.ts +44 -9
  75. package/esm/abstract/curve.d.ts.map +1 -1
  76. package/esm/abstract/curve.js +83 -8
  77. package/esm/abstract/curve.js.map +1 -1
  78. package/esm/abstract/edwards.d.ts +112 -25
  79. package/esm/abstract/edwards.d.ts.map +1 -1
  80. package/esm/abstract/edwards.js +138 -104
  81. package/esm/abstract/edwards.js.map +1 -1
  82. package/esm/abstract/fft.d.ts +12 -10
  83. package/esm/abstract/fft.d.ts.map +1 -1
  84. package/esm/abstract/fft.js +10 -11
  85. package/esm/abstract/fft.js.map +1 -1
  86. package/esm/abstract/hash-to-curve.d.ts +25 -11
  87. package/esm/abstract/hash-to-curve.d.ts.map +1 -1
  88. package/esm/abstract/hash-to-curve.js +17 -14
  89. package/esm/abstract/hash-to-curve.js.map +1 -1
  90. package/esm/abstract/modular.d.ts +24 -11
  91. package/esm/abstract/modular.d.ts.map +1 -1
  92. package/esm/abstract/modular.js +48 -19
  93. package/esm/abstract/modular.js.map +1 -1
  94. package/esm/abstract/montgomery.d.ts +1 -1
  95. package/esm/abstract/montgomery.d.ts.map +1 -1
  96. package/esm/abstract/montgomery.js +5 -4
  97. package/esm/abstract/montgomery.js.map +1 -1
  98. package/esm/abstract/poseidon.d.ts +5 -13
  99. package/esm/abstract/poseidon.d.ts.map +1 -1
  100. package/esm/abstract/poseidon.js +12 -7
  101. package/esm/abstract/poseidon.js.map +1 -1
  102. package/esm/abstract/tower.d.ts +20 -46
  103. package/esm/abstract/tower.d.ts.map +1 -1
  104. package/esm/abstract/tower.js +9 -3
  105. package/esm/abstract/tower.js.map +1 -1
  106. package/esm/abstract/utils.d.ts +1 -115
  107. package/esm/abstract/utils.d.ts.map +1 -1
  108. package/esm/abstract/utils.js +3 -344
  109. package/esm/abstract/utils.js.map +1 -1
  110. package/esm/abstract/weierstrass.d.ts +132 -76
  111. package/esm/abstract/weierstrass.d.ts.map +1 -1
  112. package/esm/abstract/weierstrass.js +460 -400
  113. package/esm/abstract/weierstrass.js.map +1 -1
  114. package/esm/bls12-381.d.ts +2 -0
  115. package/esm/bls12-381.d.ts.map +1 -1
  116. package/esm/bls12-381.js +503 -465
  117. package/esm/bls12-381.js.map +1 -1
  118. package/esm/bn254.d.ts +2 -0
  119. package/esm/bn254.d.ts.map +1 -1
  120. package/esm/bn254.js +41 -29
  121. package/esm/bn254.js.map +1 -1
  122. package/esm/ed25519.d.ts +8 -5
  123. package/esm/ed25519.d.ts.map +1 -1
  124. package/esm/ed25519.js +62 -49
  125. package/esm/ed25519.js.map +1 -1
  126. package/esm/ed448.d.ts +10 -6
  127. package/esm/ed448.d.ts.map +1 -1
  128. package/esm/ed448.js +74 -51
  129. package/esm/ed448.js.map +1 -1
  130. package/esm/misc.d.ts.map +1 -1
  131. package/esm/misc.js +31 -26
  132. package/esm/misc.js.map +1 -1
  133. package/esm/nist.d.ts +7 -16
  134. package/esm/nist.d.ts.map +1 -1
  135. package/esm/nist.js +86 -97
  136. package/esm/nist.js.map +1 -1
  137. package/esm/p256.d.ts +3 -3
  138. package/esm/p384.d.ts +3 -3
  139. package/esm/p521.d.ts +3 -3
  140. package/esm/secp256k1.d.ts +6 -6
  141. package/esm/secp256k1.d.ts.map +1 -1
  142. package/esm/secp256k1.js +43 -40
  143. package/esm/secp256k1.js.map +1 -1
  144. package/esm/utils.d.ts +96 -0
  145. package/esm/utils.d.ts.map +1 -0
  146. package/esm/utils.js +279 -0
  147. package/esm/utils.js.map +1 -0
  148. package/misc.d.ts.map +1 -1
  149. package/misc.js +35 -30
  150. package/misc.js.map +1 -1
  151. package/nist.d.ts +7 -16
  152. package/nist.d.ts.map +1 -1
  153. package/nist.js +86 -97
  154. package/nist.js.map +1 -1
  155. package/p256.d.ts +3 -3
  156. package/p384.d.ts +3 -3
  157. package/p521.d.ts +3 -3
  158. package/package.json +14 -5
  159. package/secp256k1.d.ts +6 -6
  160. package/secp256k1.d.ts.map +1 -1
  161. package/secp256k1.js +46 -43
  162. package/secp256k1.js.map +1 -1
  163. package/src/_shortw_utils.ts +5 -15
  164. package/src/abstract/bls.ts +260 -145
  165. package/src/abstract/curve.ts +115 -13
  166. package/src/abstract/edwards.ts +279 -138
  167. package/src/abstract/fft.ts +30 -19
  168. package/src/abstract/hash-to-curve.ts +51 -27
  169. package/src/abstract/modular.ts +49 -28
  170. package/src/abstract/montgomery.ts +9 -7
  171. package/src/abstract/poseidon.ts +22 -18
  172. package/src/abstract/tower.ts +36 -67
  173. package/src/abstract/utils.ts +3 -378
  174. package/src/abstract/weierstrass.ts +700 -453
  175. package/src/bls12-381.ts +540 -489
  176. package/src/bn254.ts +47 -35
  177. package/src/ed25519.ts +80 -64
  178. package/src/ed448.ts +129 -92
  179. package/src/misc.ts +39 -34
  180. package/src/nist.ts +138 -127
  181. package/src/p256.ts +3 -3
  182. package/src/p384.ts +3 -3
  183. package/src/p521.ts +3 -3
  184. package/src/secp256k1.ts +58 -46
  185. package/src/utils.ts +328 -0
  186. package/utils.d.ts +96 -0
  187. package/utils.d.ts.map +1 -0
  188. package/utils.js +313 -0
  189. package/utils.js.map +1 -0
package/src/ed448.ts CHANGED
@@ -7,17 +7,29 @@
7
7
  * @module
8
8
  */
9
9
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
10
- import { shake256 } from '@noble/hashes/sha3';
11
- import { concatBytes, randomBytes, utf8ToBytes, wrapConstructor } from '@noble/hashes/utils';
10
+ import { shake256 } from '@noble/hashes/sha3.js';
11
+ import {
12
+ abytes,
13
+ concatBytes,
14
+ utf8ToBytes,
15
+ createHasher as wrapConstructor,
16
+ } from '@noble/hashes/utils.js';
12
17
  import type { AffinePoint, Group } from './abstract/curve.ts';
13
18
  import { pippenger } from './abstract/curve.ts';
14
- import { type CurveFn, type ExtPointType, twistedEdwards } from './abstract/edwards.ts';
19
+ import {
20
+ type CurveFn,
21
+ edwards,
22
+ type EdwardsOpts,
23
+ type ExtPointConstructor,
24
+ type ExtPointType,
25
+ twistedEdwards,
26
+ } from './abstract/edwards.ts';
15
27
  import {
16
28
  createHasher,
17
29
  expand_message_xof,
18
- type Hasher,
30
+ type H2CHasher,
31
+ type H2CMethod,
19
32
  type htfBasicOpts,
20
- type HTFMethod,
21
33
  } from './abstract/hash-to-curve.ts';
22
34
  import { Field, FpInvertBatch, isNegativeLE, mod, pow2 } from './abstract/modular.ts';
23
35
  import { montgomery, type CurveFn as XCurveFn } from './abstract/montgomery.ts';
@@ -28,13 +40,53 @@ import {
28
40
  equalBytes,
29
41
  type Hex,
30
42
  numberToBytesLE,
31
- } from './abstract/utils.ts';
43
+ } from './utils.ts';
44
+
45
+ // a = 1n
46
+ // d = Fp.neg(39081n)
47
+ // Finite field 2n**448n - 2n**224n - 1n
48
+ // Subgroup order
49
+ // 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
50
+ const ed448_CURVE: EdwardsOpts = {
51
+ p: BigInt(
52
+ '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
53
+ ),
54
+ n: BigInt(
55
+ '0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffff7cca23e9c44edb49aed63690216cc2728dc58f552378c292ab5844f3'
56
+ ),
57
+ h: BigInt(4),
58
+ a: BigInt(1),
59
+ d: BigInt(
60
+ '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffffffffffffffffffffffffffffffffffffffffffffffff6756'
61
+ ),
62
+ Gx: BigInt(
63
+ '0x4f1970c66bed0ded221d15a622bf36da9e146570470f1767ea6de324a3d3a46412ae1af72ab66511433b80e18b00938e2626a82bc70cc05e'
64
+ ),
65
+ Gy: BigInt(
66
+ '0x693f46716eb6bc248876203756c9c7624bea73736ca3984087789c1e05a0c2d73ad3ff1ce67c39c4fdbd132c4ed7c8ad9808795bf230fa14'
67
+ ),
68
+ };
69
+
70
+ // E448 != Edwards448 used in ed448
71
+ // E448 is defined by NIST
72
+ // It's birationally equivalent to edwards448
73
+ // d = 39082/39081
74
+ // Gx = 3/2
75
+ const E448_CURVE: EdwardsOpts = Object.assign({}, ed448_CURVE, {
76
+ d: BigInt(
77
+ '0xd78b4bdc7f0daf19f24f38c29373a2ccad46157242a50f37809b1da3412a12e79ccc9c81264cfe9ad080997058fb61c4243cc32dbaa156b9'
78
+ ),
79
+ Gx: BigInt(
80
+ '0x79a70b2b70400553ae7c9df416c792c61128751ac92969240c25a07d728bdc93e21f7787ed6972249de732f38496cd11698713093e9c04fc'
81
+ ),
82
+ Gy: BigInt(
83
+ '0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000001'
84
+ ),
85
+ });
86
+ export const E448: ExtPointConstructor = edwards(E448_CURVE);
32
87
 
33
88
  const shake256_114 = /* @__PURE__ */ wrapConstructor(() => shake256.create({ dkLen: 114 }));
34
89
  const shake256_64 = /* @__PURE__ */ wrapConstructor(() => shake256.create({ dkLen: 64 }));
35
- const ed448P = BigInt(
36
- '726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018365439'
37
- );
38
90
 
39
91
  // prettier-ignore
40
92
  const _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _4n = BigInt(4), _11n = BigInt(11);
@@ -45,7 +97,7 @@ const _22n = BigInt(22), _44n = BigInt(44), _88n = BigInt(88), _223n = BigInt(22
45
97
  // Used for efficient square root calculation.
46
98
  // ((P-3)/4).toString(2) would produce bits [223x 1, 0, 222x 1]
47
99
  function ed448_pow_Pminus3div4(x: bigint): bigint {
48
- const P = ed448P;
100
+ const P = ed448_CURVE.p;
49
101
  const b2 = (x * x * x) % P;
50
102
  const b3 = (b2 * b2 * x) % P;
51
103
  const b6 = (pow2(b3, _3n, P) * b3) % P;
@@ -75,7 +127,7 @@ function adjustScalarBytes(bytes: Uint8Array): Uint8Array {
75
127
  // Constant-time ratio of u to v. Allows to combine inversion and square root u/√v.
76
128
  // Uses algo from RFC8032 5.1.3.
77
129
  function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
78
- const P = ed448P;
130
+ const P = ed448_CURVE.p;
79
131
  // https://www.rfc-editor.org/rfc/rfc8032#section-5.2.3
80
132
  // To compute the square root of (u/v), the first step is to compute the
81
133
  // candidate root x = (u/v)^((p+1)/4). This can be done using the
@@ -95,48 +147,27 @@ function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
95
147
  }
96
148
 
97
149
  // Finite field 2n**448n - 2n**224n - 1n
98
- const Fp = /* @__PURE__ */ (() => Field(ed448P, 456, true))();
99
-
100
- const ED448_DEF = /* @__PURE__ */ (() =>
101
- ({
102
- // Param: a
103
- a: BigInt(1),
104
- // -39081 a.k.a. Fp.neg(39081)
105
- d: BigInt(
106
- '726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018326358'
107
- ),
108
- // Finite field 2n**448n - 2n**224n - 1n
109
- Fp,
110
- // Subgroup order
111
- // 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
112
- n: BigInt(
113
- '181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779'
114
- ),
115
- // RFC 7748 has 56-byte keys, RFC 8032 has 57-byte keys
116
- nBitLength: 456,
117
- h: BigInt(4),
118
- Gx: BigInt(
119
- '224580040295924300187604334099896036246789641632564134246125461686950415467406032909029192869357953282578032075146446173674602635247710'
120
- ),
121
- Gy: BigInt(
122
- '298819210078481492676017930443930673437544040154080242095928241372331506189835876003536878655418784733982303233503462500531545062832660'
123
- ),
124
- // SHAKE256(dom4(phflag,context)||x, 114)
125
- hash: shake256_114,
126
- randomBytes,
127
- adjustScalarBytes,
128
- // dom4
129
- domain: (data: Uint8Array, ctx: Uint8Array, phflag: boolean) => {
130
- if (ctx.length > 255) throw new Error('context must be smaller than 255, got: ' + ctx.length);
131
- return concatBytes(
132
- utf8ToBytes('SigEd448'),
133
- new Uint8Array([phflag ? 1 : 0, ctx.length]),
134
- ctx,
135
- data
136
- );
137
- },
138
- uvRatio,
139
- }) as const)();
150
+ const Fp = /* @__PURE__ */ (() => Field(ed448_CURVE.p, 456, true))();
151
+ // RFC 7748 has 56-byte keys, RFC 8032 has 57-byte keys
152
+ // SHAKE256(dom4(phflag,context)||x, 114)
153
+ const ED448_DEF = /* @__PURE__ */ (() => ({
154
+ ...ed448_CURVE,
155
+ Fp,
156
+ nBitLength: 456,
157
+ hash: shake256_114,
158
+ adjustScalarBytes,
159
+ // dom4
160
+ domain: (data: Uint8Array, ctx: Uint8Array, phflag: boolean) => {
161
+ if (ctx.length > 255) throw new Error('context must be smaller than 255, got: ' + ctx.length);
162
+ return concatBytes(
163
+ utf8ToBytes('SigEd448'),
164
+ new Uint8Array([phflag ? 1 : 0, ctx.length]),
165
+ ctx,
166
+ data
167
+ );
168
+ },
169
+ uvRatio,
170
+ }))();
140
171
 
141
172
  /**
142
173
  * ed448 EdDSA curve and methods.
@@ -161,19 +192,19 @@ export const ed448ph: CurveFn = /* @__PURE__ */ (() =>
161
192
  * x448 has 56-byte keys as per RFC 7748, while
162
193
  * ed448 has 57-byte keys as per RFC 8032.
163
194
  */
164
- export const x448: XCurveFn = /* @__PURE__ */ (() =>
165
- montgomery({
166
- P: ed448P,
195
+ export const x448: XCurveFn = /* @__PURE__ */ (() => {
196
+ const P = ed448_CURVE.p;
197
+ return montgomery({
198
+ P,
167
199
  type: 'x448',
168
200
  powPminus2: (x: bigint): bigint => {
169
- const P = ed448P;
170
201
  const Pminus3div4 = ed448_pow_Pminus3div4(x);
171
202
  const Pminus3 = pow2(Pminus3div4, _2n, P);
172
203
  return mod(Pminus3 * x, P); // Pminus3 * x = Pminus2
173
204
  },
174
205
  adjustScalarBytes,
175
- randomBytes,
176
- }))();
206
+ });
207
+ })();
177
208
 
178
209
  /**
179
210
  * Converts edwards448 public key to x448 public key. Uses formula:
@@ -184,7 +215,8 @@ export const x448: XCurveFn = /* @__PURE__ */ (() =>
184
215
  * x448.getSharedSecret(edwardsToMontgomery(aPub), edwardsToMontgomery(someonesPub))
185
216
  */
186
217
  export function edwardsToMontgomeryPub(edwardsPub: string | Uint8Array): Uint8Array {
187
- const { y } = ed448.ExtendedPoint.fromHex(edwardsPub);
218
+ const bpub = ensureBytes('pub', edwardsPub);
219
+ const { y } = ed448.Point.fromHex(bpub);
188
220
  const _1n = BigInt(1);
189
221
  return Fp.toBytes(Fp.create((y - _1n) * Fp.inv(y + _1n)));
190
222
  }
@@ -269,22 +301,18 @@ function map_to_curve_elligator2_edwards448(u: bigint) {
269
301
  return { x: Fp.mul(xEn, inv[0]), y: Fp.mul(yEn, inv[1]) }; // 38. return (xEn, xEd, yEn, yEd)
270
302
  }
271
303
 
272
- export const ed448_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
273
- createHasher(
274
- ed448.ExtendedPoint,
275
- (scalars: bigint[]) => map_to_curve_elligator2_edwards448(scalars[0]),
276
- {
277
- DST: 'edwards448_XOF:SHAKE256_ELL2_RO_',
278
- encodeDST: 'edwards448_XOF:SHAKE256_ELL2_NU_',
279
- p: Fp.ORDER,
280
- m: 1,
281
- k: 224,
282
- expand: 'xof',
283
- hash: shake256,
284
- }
285
- ))();
286
- export const hashToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() => ed448_hasher.hashToCurve)();
287
- export const encodeToCurve: HTFMethod<bigint> = /* @__PURE__ */ (() =>
304
+ export const ed448_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
305
+ createHasher(ed448.Point, (scalars: bigint[]) => map_to_curve_elligator2_edwards448(scalars[0]), {
306
+ DST: 'edwards448_XOF:SHAKE256_ELL2_RO_',
307
+ encodeDST: 'edwards448_XOF:SHAKE256_ELL2_NU_',
308
+ p: Fp.ORDER,
309
+ m: 1,
310
+ k: 224,
311
+ expand: 'xof',
312
+ hash: shake256,
313
+ }))();
314
+ export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => ed448_hasher.hashToCurve)();
315
+ export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
288
316
  ed448_hasher.encodeToCurve)();
289
317
 
290
318
  function adecafp(other: unknown) {
@@ -309,8 +337,7 @@ const invertSqrt = (number: bigint) => uvRatio(_1n, number);
309
337
  const MAX_448B = /* @__PURE__ */ BigInt(
310
338
  '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
311
339
  );
312
- const bytes448ToNumberLE = (bytes: Uint8Array) =>
313
- ed448.CURVE.Fp.create(bytesToNumberLE(bytes) & MAX_448B);
340
+ const bytes448ToNumberLE = (bytes: Uint8Array) => Fp.create(bytesToNumberLE(bytes) & MAX_448B);
314
341
 
315
342
  type ExtendedPoint = ExtPointType;
316
343
 
@@ -321,8 +348,8 @@ type ExtendedPoint = ExtPointType;
321
348
  */
322
349
  function calcElligatorDecafMap(r0: bigint): ExtendedPoint {
323
350
  const { d } = ed448.CURVE;
324
- const P = ed448.CURVE.Fp.ORDER;
325
- const mod = ed448.CURVE.Fp.create;
351
+ const P = Fp.ORDER;
352
+ const mod = Fp.create;
326
353
 
327
354
  const r = mod(-(r0 * r0)); // 1
328
355
  const u0 = mod(d * (r - _1n)); // 2
@@ -345,7 +372,7 @@ function calcElligatorDecafMap(r0: bigint): ExtendedPoint {
345
372
  const W1 = mod(s2 + _1n); // 9
346
373
  const W2 = mod(s2 - _1n); // 10
347
374
  const W3 = mod(v_prime * s * (r - _1n) * ONE_MINUS_TWO_D + sgn); // 11
348
- return new ed448.ExtendedPoint(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
375
+ return new ed448.Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
349
376
  }
350
377
 
351
378
  /**
@@ -366,7 +393,7 @@ class DcfPoint implements Group<DcfPoint> {
366
393
  }
367
394
 
368
395
  static fromAffine(ap: AffinePoint<bigint>): DcfPoint {
369
- return new DcfPoint(ed448.ExtendedPoint.fromAffine(ap));
396
+ return new DcfPoint(ed448.Point.fromAffine(ap));
370
397
  }
371
398
 
372
399
  /**
@@ -386,6 +413,11 @@ class DcfPoint implements Group<DcfPoint> {
386
413
  return new DcfPoint(R1.add(R2));
387
414
  }
388
415
 
416
+ static fromBytes(bytes: Uint8Array): DcfPoint {
417
+ abytes(bytes);
418
+ return this.fromHex(bytes);
419
+ }
420
+
389
421
  /**
390
422
  * Converts decaf-encoded string to decaf point.
391
423
  * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode-2).
@@ -394,8 +426,8 @@ class DcfPoint implements Group<DcfPoint> {
394
426
  static fromHex(hex: Hex): DcfPoint {
395
427
  hex = ensureBytes('decafHex', hex, 56);
396
428
  const { d } = ed448.CURVE;
397
- const P = ed448.CURVE.Fp.ORDER;
398
- const mod = ed448.CURVE.Fp.create;
429
+ const P = Fp.ORDER;
430
+ const mod = Fp.create;
399
431
  const emsg = 'DecafPoint.fromHex: the hex is not valid encoding of DecafPoint';
400
432
  const s = bytes448ToNumberLE(hex);
401
433
 
@@ -418,7 +450,7 @@ class DcfPoint implements Group<DcfPoint> {
418
450
  const t = mod(x * y); // 8
419
451
 
420
452
  if (!isValid) throw new Error(emsg);
421
- return new DcfPoint(new ed448.ExtendedPoint(x, y, _1n, t));
453
+ return new DcfPoint(new ed448.Point(x, y, _1n, t));
422
454
  }
423
455
 
424
456
  static msm(points: DcfPoint[], scalars: bigint[]): DcfPoint {
@@ -430,10 +462,10 @@ class DcfPoint implements Group<DcfPoint> {
430
462
  * Encodes decaf point to Uint8Array.
431
463
  * Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode-2).
432
464
  */
433
- toRawBytes(): Uint8Array {
465
+ toBytes(): Uint8Array {
434
466
  let { ex: x, ey: _y, ez: z, et: t } = this.ep;
435
- const P = ed448.CURVE.Fp.ORDER;
436
- const mod = ed448.CURVE.Fp.create;
467
+ const P = Fp.ORDER;
468
+ const mod = Fp.create;
437
469
 
438
470
  const u1 = mod(mod(x + t) * mod(x - t)); // 1
439
471
  const x2 = mod(x * x);
@@ -450,8 +482,13 @@ class DcfPoint implements Group<DcfPoint> {
450
482
  return numberToBytesLE(s, 56);
451
483
  }
452
484
 
485
+ /** @deprecated use `toBytes` */
486
+ toRawBytes(): Uint8Array {
487
+ return this.toBytes();
488
+ }
489
+
453
490
  toHex(): string {
454
- return bytesToHex(this.toRawBytes());
491
+ return bytesToHex(this.toBytes());
455
492
  }
456
493
 
457
494
  toString(): string {
@@ -466,7 +503,7 @@ class DcfPoint implements Group<DcfPoint> {
466
503
  adecafp(other);
467
504
  const { ex: X1, ey: Y1 } = this.ep;
468
505
  const { ex: X2, ey: Y2 } = other.ep;
469
- const mod = ed448.CURVE.Fp.create;
506
+ const mod = Fp.create;
470
507
  // (x1 * y2 == y1 * x2)
471
508
  return mod(X1 * Y2) === mod(Y1 * X2);
472
509
  }
@@ -505,8 +542,8 @@ class DcfPoint implements Group<DcfPoint> {
505
542
  export const DecafPoint: typeof DcfPoint = /* @__PURE__ */ (() => {
506
543
  // decaf448 base point is ed448 base x 2
507
544
  // https://github.com/dalek-cryptography/curve25519-dalek/blob/59837c6ecff02b77b9d5ff84dbc239d0cf33ef90/vendor/ristretto.sage#L699
508
- if (!DcfPoint.BASE) DcfPoint.BASE = new DcfPoint(ed448.ExtendedPoint.BASE).multiply(_2n);
509
- if (!DcfPoint.ZERO) DcfPoint.ZERO = new DcfPoint(ed448.ExtendedPoint.ZERO);
545
+ if (!DcfPoint.BASE) DcfPoint.BASE = new DcfPoint(ed448.Point.BASE).multiply(_2n);
546
+ if (!DcfPoint.ZERO) DcfPoint.ZERO = new DcfPoint(ed448.Point.ZERO);
510
547
  return DcfPoint;
511
548
  })();
512
549
 
package/src/misc.ts CHANGED
@@ -4,52 +4,57 @@
4
4
  * @module
5
5
  */
6
6
  /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
7
- import { blake256 } from '@noble/hashes/blake1';
8
- import { blake2s } from '@noble/hashes/blake2';
9
- import { sha256, sha512 } from '@noble/hashes/sha2';
10
- import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
11
- import { getHash } from './_shortw_utils.ts';
12
- import { type CurveFn, type ExtPointType, twistedEdwards } from './abstract/edwards.ts';
7
+ import { blake256 } from '@noble/hashes/blake1.js';
8
+ import { blake2s } from '@noble/hashes/blake2.js';
9
+ import { sha256, sha512 } from '@noble/hashes/sha2.js';
10
+ import { concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';
11
+ import {
12
+ twistedEdwards,
13
+ type CurveFn,
14
+ type EdwardsOpts,
15
+ type ExtPointType,
16
+ } from './abstract/edwards.ts';
13
17
  import { Field, mod } from './abstract/modular.ts';
14
- import { type CurveFn as WCurveFn, weierstrass } from './abstract/weierstrass.ts';
18
+ import { weierstrass, type CurveFn as WCurveFn } from './abstract/weierstrass.ts';
19
+ import { bls12_381_Fr } from './bls12-381.ts';
20
+ import { bn254_Fr } from './bn254.ts';
15
21
 
16
22
  // Jubjub curves have 𝔽p over scalar fields of other curves. They are friendly to ZK proofs.
17
23
  // jubjub Fp = bls n. babyjubjub Fp = bn254 n.
18
24
  // verify manually, check bls12-381.ts and bn254.ts.
19
25
  // https://neuromancer.sk/std/other/JubJub
20
26
 
21
- const bls12_381_Fr = Field(
22
- BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')
23
- );
24
- const bn254_Fr = Field(
25
- BigInt('21888242871839275222246405745257275088548364400416034343698204186575808495617')
26
- );
27
-
28
- /** Curve over scalar field of bls12-381. jubjub Fp = bls n */
29
- export const jubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
30
- a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
31
- d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
32
- Fp: bls12_381_Fr,
27
+ const jubjub_CURVE: EdwardsOpts = {
28
+ p: bls12_381_Fr.ORDER,
33
29
  n: BigInt('0xe7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7'),
34
30
  h: BigInt(8),
31
+ a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
32
+ d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
35
33
  Gx: BigInt('0x11dafe5d23e1218086a365b99fbf3d3be72f6afd7d1f72623e6b071492d1122b'),
36
34
  Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
35
+ };
36
+ /** Curve over scalar field of bls12-381. jubjub Fp = bls n */
37
+ export const jubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
38
+ ...jubjub_CURVE,
39
+ Fp: bls12_381_Fr,
37
40
  hash: sha512,
38
- randomBytes,
39
- } as const);
41
+ });
40
42
 
43
+ const babyjubjub_CURVE: EdwardsOpts = {
44
+ p: bn254_Fr.ORDER,
45
+ n: BigInt('0x30644e72e131a029b85045b68181585d59f76dc1c90770533b94bee1c9093788'),
46
+ h: BigInt(8),
47
+ a: BigInt('168700'),
48
+ d: BigInt('168696'),
49
+ Gx: BigInt('0x23343e3445b673d38bcba38f25645adb494b1255b1162bb40f41a59f4d4b45e'),
50
+ Gy: BigInt('0xc19139cb84c680a6e14116da06056174a0cfa121e6e5c2450f87d64fc000001'),
51
+ };
41
52
  /** Curve over scalar field of bn254. babyjubjub Fp = bn254 n */
42
53
  export const babyjubjub: CurveFn = /* @__PURE__ */ twistedEdwards({
43
- a: BigInt(168700),
44
- d: BigInt(168696),
54
+ ...babyjubjub_CURVE,
45
55
  Fp: bn254_Fr,
46
- n: BigInt('21888242871839275222246405745257275088614511777268538073601725287587578984328'),
47
- h: BigInt(8),
48
- Gx: BigInt('995203441582195749578291179787384436505546430278305826713579947235728471134'),
49
- Gy: BigInt('5472060717959818805561601436314318772137091100104008585924551046643952123905'),
50
56
  hash: blake256,
51
- randomBytes,
52
- } as const);
57
+ });
53
58
 
54
59
  const jubjub_gh_first_block = utf8ToBytes(
55
60
  '096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0'
@@ -61,10 +66,10 @@ export function jubjub_groupHash(tag: Uint8Array, personalization: Uint8Array):
61
66
  h.update(jubjub_gh_first_block);
62
67
  h.update(tag);
63
68
  // NOTE: returns ExtendedPoint, in case it will be multiplied later
64
- let p = jubjub.ExtendedPoint.fromHex(h.digest());
69
+ let p = jubjub.Point.fromHex(h.digest());
65
70
  // NOTE: cannot replace with isSmallOrder, returns Point*8
66
71
  p = p.multiply(jubjub.CURVE.h);
67
- if (p.equals(jubjub.ExtendedPoint.ZERO)) throw new Error('Point has small order');
72
+ if (p.equals(jubjub.Point.ZERO)) throw new Error('Point has small order');
68
73
  return p;
69
74
  }
70
75
 
@@ -72,7 +77,7 @@ export function jubjub_groupHash(tag: Uint8Array, personalization: Uint8Array):
72
77
  // It operates over public data:
73
78
  // const G_SPEND = jubjub.findGroupHash(Uint8Array.of(), utf8ToBytes('Item_G_'));
74
79
  export function jubjub_findGroupHash(m: Uint8Array, personalization: Uint8Array): ExtPointType {
75
- const tag = concatBytes(m, new Uint8Array([0]));
80
+ const tag = concatBytes(m, Uint8Array.of(0));
76
81
  const hashes = [];
77
82
  for (let i = 0; i < 256; i++) {
78
83
  tag[tag.length - 1] = i;
@@ -105,7 +110,7 @@ export const pallas: WCurveFn = weierstrass({
105
110
  Gx: mod(BigInt(-1), pasta_p),
106
111
  Gy: BigInt(2),
107
112
  h: BigInt(1),
108
- ...getHash(sha256),
113
+ hash: sha256,
109
114
  });
110
115
  /**
111
116
  * https://neuromancer.sk/std/other/Vesta
@@ -119,5 +124,5 @@ export const vesta: WCurveFn = weierstrass({
119
124
  Gx: mod(BigInt(-1), pasta_q),
120
125
  Gy: BigInt(2),
121
126
  h: BigInt(1),
122
- ...getHash(sha256),
127
+ hash: sha256,
123
128
  });