@noble/curves 0.6.4 → 0.7.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.
Files changed (172) hide show
  1. package/README.md +429 -281
  2. package/{lib/_shortw_utils.d.ts → _shortw_utils.d.ts} +1 -0
  3. package/_shortw_utils.d.ts.map +1 -0
  4. package/{lib/_shortw_utils.js → _shortw_utils.js} +2 -0
  5. package/_shortw_utils.js.map +1 -0
  6. package/{lib/abstract → abstract}/bls.d.ts +4 -9
  7. package/abstract/bls.d.ts.map +1 -0
  8. package/{lib/abstract → abstract}/bls.js +12 -25
  9. package/abstract/bls.js.map +1 -0
  10. package/{lib/abstract → abstract}/curve.d.ts +1 -0
  11. package/abstract/curve.d.ts.map +1 -0
  12. package/{lib/abstract → abstract}/curve.js +1 -0
  13. package/abstract/curve.js.map +1 -0
  14. package/{lib/abstract → abstract}/edwards.d.ts +1 -0
  15. package/abstract/edwards.d.ts.map +1 -0
  16. package/{lib/abstract → abstract}/edwards.js +9 -15
  17. package/abstract/edwards.js.map +1 -0
  18. package/{lib/abstract → abstract}/hash-to-curve.d.ts +5 -5
  19. package/abstract/hash-to-curve.d.ts.map +1 -0
  20. package/{lib/abstract → abstract}/hash-to-curve.js +41 -38
  21. package/abstract/hash-to-curve.js.map +1 -0
  22. package/{lib/abstract → abstract}/modular.d.ts +1 -0
  23. package/abstract/modular.d.ts.map +1 -0
  24. package/{lib/abstract → abstract}/modular.js +2 -1
  25. package/abstract/modular.js.map +1 -0
  26. package/{lib/abstract → abstract}/montgomery.d.ts +1 -0
  27. package/abstract/montgomery.d.ts.map +1 -0
  28. package/{lib/abstract → abstract}/montgomery.js +3 -2
  29. package/abstract/montgomery.js.map +1 -0
  30. package/{lib/abstract → abstract}/poseidon.d.ts +1 -0
  31. package/abstract/poseidon.d.ts.map +1 -0
  32. package/{lib/abstract → abstract}/poseidon.js +1 -0
  33. package/abstract/poseidon.js.map +1 -0
  34. package/{lib/abstract → abstract}/utils.d.ts +12 -1
  35. package/abstract/utils.d.ts.map +1 -0
  36. package/{lib/abstract → abstract}/utils.js +96 -10
  37. package/abstract/utils.js.map +1 -0
  38. package/{lib/abstract → abstract}/weierstrass.d.ts +4 -3
  39. package/abstract/weierstrass.d.ts.map +1 -0
  40. package/{lib/abstract → abstract}/weierstrass.js +45 -91
  41. package/abstract/weierstrass.js.map +1 -0
  42. package/{lib/bls12-381.d.ts → bls12-381.d.ts} +1 -0
  43. package/bls12-381.d.ts.map +1 -0
  44. package/{lib/bls12-381.js → bls12-381.js} +41 -7
  45. package/bls12-381.js.map +1 -0
  46. package/{lib/bn.d.ts → bn.d.ts} +1 -0
  47. package/bn.d.ts.map +1 -0
  48. package/{lib/bn.js → bn.js} +1 -0
  49. package/bn.js.map +1 -0
  50. package/{lib/ed25519.d.ts → ed25519.d.ts} +2 -1
  51. package/ed25519.d.ts.map +1 -0
  52. package/{lib/ed25519.js → ed25519.js} +4 -3
  53. package/ed25519.js.map +1 -0
  54. package/{lib/ed448.d.ts → ed448.d.ts} +2 -1
  55. package/ed448.d.ts.map +1 -0
  56. package/{lib/ed448.js → ed448.js} +2 -1
  57. package/ed448.js.map +1 -0
  58. package/{lib/esm → esm}/_shortw_utils.js +2 -0
  59. package/esm/_shortw_utils.js.map +1 -0
  60. package/{lib/esm → esm}/abstract/bls.js +13 -26
  61. package/esm/abstract/bls.js.map +1 -0
  62. package/{lib/esm → esm}/abstract/curve.js +1 -0
  63. package/esm/abstract/curve.js.map +1 -0
  64. package/{lib/esm → esm}/abstract/edwards.js +9 -15
  65. package/esm/abstract/edwards.js.map +1 -0
  66. package/{lib/esm → esm}/abstract/hash-to-curve.js +40 -36
  67. package/esm/abstract/hash-to-curve.js.map +1 -0
  68. package/{lib/esm → esm}/abstract/modular.js +2 -1
  69. package/esm/abstract/modular.js.map +1 -0
  70. package/{lib/esm → esm}/abstract/montgomery.js +3 -2
  71. package/esm/abstract/montgomery.js.map +1 -0
  72. package/{lib/esm → esm}/abstract/poseidon.js +1 -0
  73. package/esm/abstract/poseidon.js.map +1 -0
  74. package/{lib/esm → esm}/abstract/utils.js +93 -9
  75. package/esm/abstract/utils.js.map +1 -0
  76. package/{lib/esm → esm}/abstract/weierstrass.js +45 -91
  77. package/esm/abstract/weierstrass.js.map +1 -0
  78. package/{lib/esm → esm}/bls12-381.js +41 -7
  79. package/esm/bls12-381.js.map +1 -0
  80. package/{lib/esm → esm}/bn.js +1 -0
  81. package/esm/bn.js.map +1 -0
  82. package/{lib/esm → esm}/ed25519.js +5 -4
  83. package/esm/ed25519.js.map +1 -0
  84. package/{lib/esm → esm}/ed448.js +2 -1
  85. package/esm/ed448.js.map +1 -0
  86. package/{lib → esm}/index.js +1 -0
  87. package/esm/index.js.map +1 -0
  88. package/{lib/esm → esm}/jubjub.js +1 -0
  89. package/esm/jubjub.js.map +1 -0
  90. package/{lib/esm → esm}/p192.js +1 -0
  91. package/esm/p192.js.map +1 -0
  92. package/{lib/esm → esm}/p224.js +1 -0
  93. package/esm/p224.js.map +1 -0
  94. package/{lib/esm → esm}/p256.js +2 -1
  95. package/esm/p256.js.map +1 -0
  96. package/{lib/esm → esm}/p384.js +2 -1
  97. package/esm/p384.js.map +1 -0
  98. package/{lib/esm → esm}/p521.js +2 -1
  99. package/esm/p521.js.map +1 -0
  100. package/{lib/esm → esm}/package.json +0 -0
  101. package/{lib/esm → esm}/pasta.js +1 -0
  102. package/esm/pasta.js.map +1 -0
  103. package/{lib/esm → esm}/secp256k1.js +41 -50
  104. package/esm/secp256k1.js.map +1 -0
  105. package/{lib/esm → esm}/stark.js +1 -0
  106. package/esm/stark.js.map +1 -0
  107. package/index.d.ts +1 -0
  108. package/index.d.ts.map +1 -0
  109. package/index.js +3 -0
  110. package/index.js.map +1 -0
  111. package/{lib/jubjub.d.ts → jubjub.d.ts} +1 -0
  112. package/jubjub.d.ts.map +1 -0
  113. package/{lib/jubjub.js → jubjub.js} +1 -0
  114. package/jubjub.js.map +1 -0
  115. package/{lib/p192.d.ts → p192.d.ts} +1 -0
  116. package/p192.d.ts.map +1 -0
  117. package/{lib/p192.js → p192.js} +1 -0
  118. package/p192.js.map +1 -0
  119. package/{lib/p224.d.ts → p224.d.ts} +1 -0
  120. package/p224.d.ts.map +1 -0
  121. package/{lib/p224.js → p224.js} +1 -0
  122. package/p224.js.map +1 -0
  123. package/{lib/p256.d.ts → p256.d.ts} +2 -1
  124. package/p256.d.ts.map +1 -0
  125. package/{lib/p256.js → p256.js} +2 -1
  126. package/p256.js.map +1 -0
  127. package/{lib/p384.d.ts → p384.d.ts} +2 -1
  128. package/p384.d.ts.map +1 -0
  129. package/{lib/p384.js → p384.js} +2 -1
  130. package/p384.js.map +1 -0
  131. package/{lib/p521.d.ts → p521.d.ts} +2 -1
  132. package/p521.d.ts.map +1 -0
  133. package/{lib/p521.js → p521.js} +2 -1
  134. package/p521.js.map +1 -0
  135. package/package.json +84 -79
  136. package/{lib/pasta.d.ts → pasta.d.ts} +1 -0
  137. package/pasta.d.ts.map +1 -0
  138. package/{lib/pasta.js → pasta.js} +1 -0
  139. package/pasta.js.map +1 -0
  140. package/{lib/secp256k1.d.ts → secp256k1.d.ts} +15 -5
  141. package/secp256k1.d.ts.map +1 -0
  142. package/{lib/secp256k1.js → secp256k1.js} +38 -47
  143. package/secp256k1.js.map +1 -0
  144. package/src/_shortw_utils.ts +20 -0
  145. package/src/abstract/bls.ts +376 -0
  146. package/src/abstract/curve.ts +199 -0
  147. package/src/abstract/edwards.ts +479 -0
  148. package/src/abstract/hash-to-curve.ts +220 -0
  149. package/src/abstract/modular.ts +417 -0
  150. package/src/abstract/montgomery.ts +184 -0
  151. package/src/abstract/poseidon.ts +119 -0
  152. package/src/abstract/utils.ts +246 -0
  153. package/src/abstract/weierstrass.ts +1175 -0
  154. package/src/bls12-381.ts +1274 -0
  155. package/src/bn.ts +21 -0
  156. package/src/ed25519.ts +428 -0
  157. package/src/ed448.ts +241 -0
  158. package/{lib/esm/index.js → src/index.ts} +0 -1
  159. package/src/jubjub.ts +58 -0
  160. package/src/p192.ts +25 -0
  161. package/src/p224.ts +25 -0
  162. package/src/p256.ts +53 -0
  163. package/src/p384.ts +57 -0
  164. package/src/p521.ts +57 -0
  165. package/src/pasta.ts +31 -0
  166. package/src/secp256k1.ts +260 -0
  167. package/src/stark.ts +356 -0
  168. package/{lib/stark.d.ts → stark.d.ts} +1 -0
  169. package/stark.d.ts.map +1 -0
  170. package/{lib/stark.js → stark.js} +1 -0
  171. package/stark.js.map +1 -0
  172. package/lib/index.d.ts +0 -0
package/src/ed448.ts ADDED
@@ -0,0 +1,241 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { shake256 } from '@noble/hashes/sha3';
3
+ import { concatBytes, randomBytes, utf8ToBytes, wrapConstructor } from '@noble/hashes/utils';
4
+ import { twistedEdwards } from './abstract/edwards.js';
5
+ import { mod, pow2, Fp as Field } from './abstract/modular.js';
6
+ import { montgomery } from './abstract/montgomery.js';
7
+ import * as htf from './abstract/hash-to-curve.js';
8
+
9
+ /**
10
+ * Edwards448 (not Ed448-Goldilocks) curve with following addons:
11
+ * * X448 ECDH
12
+ * Conforms to RFC 8032 https://www.rfc-editor.org/rfc/rfc8032.html#section-5.2
13
+ */
14
+
15
+ const shake256_114 = wrapConstructor(() => shake256.create({ dkLen: 114 }));
16
+ const shake256_64 = wrapConstructor(() => shake256.create({ dkLen: 64 }));
17
+ const ed448P = BigInt(
18
+ '726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018365439'
19
+ );
20
+
21
+ // powPminus3div4 calculates z = x^k mod p, where k = (p-3)/4.
22
+ // Used for efficient square root calculation.
23
+ // ((P-3)/4).toString(2) would produce bits [223x 1, 0, 222x 1]
24
+ function ed448_pow_Pminus3div4(x: bigint): bigint {
25
+ const P = ed448P;
26
+ // prettier-ignore
27
+ const _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3), _11n = BigInt(11);
28
+ // prettier-ignore
29
+ const _22n = BigInt(22), _44n = BigInt(44), _88n = BigInt(88), _223n = BigInt(223);
30
+ const b2 = (x * x * x) % P;
31
+ const b3 = (b2 * b2 * x) % P;
32
+ const b6 = (pow2(b3, _3n, P) * b3) % P;
33
+ const b9 = (pow2(b6, _3n, P) * b3) % P;
34
+ const b11 = (pow2(b9, _2n, P) * b2) % P;
35
+ const b22 = (pow2(b11, _11n, P) * b11) % P;
36
+ const b44 = (pow2(b22, _22n, P) * b22) % P;
37
+ const b88 = (pow2(b44, _44n, P) * b44) % P;
38
+ const b176 = (pow2(b88, _88n, P) * b88) % P;
39
+ const b220 = (pow2(b176, _44n, P) * b44) % P;
40
+ const b222 = (pow2(b220, _2n, P) * b2) % P;
41
+ const b223 = (pow2(b222, _1n, P) * x) % P;
42
+ return (pow2(b223, _223n, P) * b222) % P;
43
+ }
44
+
45
+ function adjustScalarBytes(bytes: Uint8Array): Uint8Array {
46
+ // Section 5: Likewise, for X448, set the two least significant bits of the first byte to 0, and the most
47
+ // significant bit of the last byte to 1.
48
+ bytes[0] &= 252; // 0b11111100
49
+ // and the most significant bit of the last byte to 1.
50
+ bytes[55] |= 128; // 0b10000000
51
+ // NOTE: is is NOOP for 56 bytes scalars (X25519/X448)
52
+ bytes[56] = 0; // Byte outside of group (456 buts vs 448 bits)
53
+ return bytes;
54
+ }
55
+
56
+ const Fp = Field(ed448P, 456, true);
57
+
58
+ const ED448_DEF = {
59
+ // Param: a
60
+ a: BigInt(1),
61
+ // -39081. Negative number is P - number
62
+ d: BigInt(
63
+ '726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018326358'
64
+ ),
65
+ // Finite field 𝔽p over which we'll do calculations; 2n ** 448n - 2n ** 224n - 1n
66
+ Fp,
67
+ // Subgroup order: how many points curve has;
68
+ // 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
69
+ n: BigInt(
70
+ '181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779'
71
+ ),
72
+ nBitLength: 456,
73
+ // Cofactor
74
+ h: BigInt(4),
75
+ // Base point (x, y) aka generator point
76
+ Gx: BigInt(
77
+ '224580040295924300187604334099896036246789641632564134246125461686950415467406032909029192869357953282578032075146446173674602635247710'
78
+ ),
79
+ Gy: BigInt(
80
+ '298819210078481492676017930443930673437544040154080242095928241372331506189835876003536878655418784733982303233503462500531545062832660'
81
+ ),
82
+ // SHAKE256(dom4(phflag,context)||x, 114)
83
+ hash: shake256_114,
84
+ randomBytes,
85
+ adjustScalarBytes,
86
+ // dom4
87
+ domain: (data: Uint8Array, ctx: Uint8Array, phflag: boolean) => {
88
+ if (ctx.length > 255) throw new Error(`Context is too big: ${ctx.length}`);
89
+ return concatBytes(
90
+ utf8ToBytes('SigEd448'),
91
+ new Uint8Array([phflag ? 1 : 0, ctx.length]),
92
+ ctx,
93
+ data
94
+ );
95
+ },
96
+
97
+ // Constant-time ratio of u to v. Allows to combine inversion and square root u/√v.
98
+ // Uses algo from RFC8032 5.1.3.
99
+ uvRatio: (u: bigint, v: bigint): { isValid: boolean; value: bigint } => {
100
+ const P = ed448P;
101
+ // https://datatracker.ietf.org/doc/html/rfc8032#section-5.2.3
102
+ // To compute the square root of (u/v), the first step is to compute the
103
+ // candidate root x = (u/v)^((p+1)/4). This can be done using the
104
+ // following trick, to use a single modular powering for both the
105
+ // inversion of v and the square root:
106
+ // x = (u/v)^((p+1)/4) = u³v(u⁵v³)^((p-3)/4) (mod p)
107
+ const u2v = mod(u * u * v, P); // u²v
108
+ const u3v = mod(u2v * u, P); // u³v
109
+ const u5v3 = mod(u3v * u2v * v, P); // u⁵v³
110
+ const root = ed448_pow_Pminus3div4(u5v3);
111
+ const x = mod(u3v * root, P);
112
+ // Verify that root is exists
113
+ const x2 = mod(x * x, P); // x²
114
+ // If vx² = u, the recovered x-coordinate is x. Otherwise, no
115
+ // square root exists, and the decoding fails.
116
+ return { isValid: mod(x2 * v, P) === u, value: x };
117
+ },
118
+ } as const;
119
+
120
+ export const ed448 = twistedEdwards(ED448_DEF);
121
+ // NOTE: there is no ed448ctx, since ed448 supports ctx by default
122
+ export const ed448ph = twistedEdwards({ ...ED448_DEF, preHash: shake256_64 });
123
+
124
+ export const x448 = montgomery({
125
+ a24: BigInt(39081),
126
+ montgomeryBits: 448,
127
+ nByteLength: 57,
128
+ P: ed448P,
129
+ Gu: '0500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
130
+ powPminus2: (x: bigint): bigint => {
131
+ const P = ed448P;
132
+ const Pminus3div4 = ed448_pow_Pminus3div4(x);
133
+ const Pminus3 = pow2(Pminus3div4, BigInt(2), P);
134
+ return mod(Pminus3 * x, P); // Pminus3 * x = Pminus2
135
+ },
136
+ adjustScalarBytes,
137
+ // The 4-isogeny maps between the Montgomery curve and this Edwards
138
+ // curve are:
139
+ // (u, v) = (y^2/x^2, (2 - x^2 - y^2)*y/x^3)
140
+ // (x, y) = (4*v*(u^2 - 1)/(u^4 - 2*u^2 + 4*v^2 + 1),
141
+ // -(u^5 - 2*u^3 - 4*u*v^2 + u)/
142
+ // (u^5 - 2*u^2*v^2 - 2*u^3 - 2*v^2 + u))
143
+ // xyToU: (p: PointType) => {
144
+ // const P = ed448P;
145
+ // const { x, y } = p;
146
+ // if (x === _0n) throw new Error(`Point with x=0 doesn't have mapping`);
147
+ // const invX = invert(x * x, P); // x^2
148
+ // const u = mod(y * y * invX, P); // (y^2/x^2)
149
+ // return numberToBytesLE(u, 56);
150
+ // },
151
+ });
152
+
153
+ // Hash To Curve Elligator2 Map
154
+ const ELL2_C1 = (Fp.ORDER - BigInt(3)) / BigInt(4); // 1. c1 = (q - 3) / 4 # Integer arithmetic
155
+ const ELL2_J = BigInt(156326);
156
+ function map_to_curve_elligator2_curve448(u: bigint) {
157
+ let tv1 = Fp.sqr(u); // 1. tv1 = u^2
158
+ let e1 = Fp.eql(tv1, Fp.ONE); // 2. e1 = tv1 == 1
159
+ tv1 = Fp.cmov(tv1, Fp.ZERO, e1); // 3. tv1 = CMOV(tv1, 0, e1) # If Z * u^2 == -1, set tv1 = 0
160
+ let xd = Fp.sub(Fp.ONE, tv1); // 4. xd = 1 - tv1
161
+ let x1n = Fp.neg(ELL2_J); // 5. x1n = -J
162
+ let tv2 = Fp.sqr(xd); // 6. tv2 = xd^2
163
+ let gxd = Fp.mul(tv2, xd); // 7. gxd = tv2 * xd # gxd = xd^3
164
+ let gx1 = Fp.mul(tv1, Fp.neg(ELL2_J)); // 8. gx1 = -J * tv1 # x1n + J * xd
165
+ gx1 = Fp.mul(gx1, x1n); // 9. gx1 = gx1 * x1n # x1n^2 + J * x1n * xd
166
+ gx1 = Fp.add(gx1, tv2); // 10. gx1 = gx1 + tv2 # x1n^2 + J * x1n * xd + xd^2
167
+ gx1 = Fp.mul(gx1, x1n); // 11. gx1 = gx1 * x1n # x1n^3 + J * x1n^2 * xd + x1n * xd^2
168
+ let tv3 = Fp.sqr(gxd); // 12. tv3 = gxd^2
169
+ tv2 = Fp.mul(gx1, gxd); // 13. tv2 = gx1 * gxd # gx1 * gxd
170
+ tv3 = Fp.mul(tv3, tv2); // 14. tv3 = tv3 * tv2 # gx1 * gxd^3
171
+ let y1 = Fp.pow(tv3, ELL2_C1); // 15. y1 = tv3^c1 # (gx1 * gxd^3)^((p - 3) / 4)
172
+ y1 = Fp.mul(y1, tv2); // 16. y1 = y1 * tv2 # gx1 * gxd * (gx1 * gxd^3)^((p - 3) / 4)
173
+ let x2n = Fp.mul(x1n, Fp.neg(tv1)); // 17. x2n = -tv1 * x1n # x2 = x2n / xd = -1 * u^2 * x1n / xd
174
+ let y2 = Fp.mul(y1, u); // 18. y2 = y1 * u
175
+ y2 = Fp.cmov(y2, Fp.ZERO, e1); // 19. y2 = CMOV(y2, 0, e1)
176
+ tv2 = Fp.sqr(y1); // 20. tv2 = y1^2
177
+ tv2 = Fp.mul(tv2, gxd); // 21. tv2 = tv2 * gxd
178
+ let e2 = Fp.eql(tv2, gx1); // 22. e2 = tv2 == gx1
179
+ let xn = Fp.cmov(x2n, x1n, e2); // 23. xn = CMOV(x2n, x1n, e2) # If e2, x = x1, else x = x2
180
+ let y = Fp.cmov(y2, y1, e2); // 24. y = CMOV(y2, y1, e2) # If e2, y = y1, else y = y2
181
+ let e3 = Fp.isOdd(y); // 25. e3 = sgn0(y) == 1 # Fix sign of y
182
+ y = Fp.cmov(y, Fp.neg(y), e2 !== e3); // 26. y = CMOV(y, -y, e2 XOR e3)
183
+ return { xn, xd, yn: y, yd: Fp.ONE }; // 27. return (xn, xd, y, 1)
184
+ }
185
+ function map_to_curve_elligator2_edwards448(u: bigint) {
186
+ let { xn, xd, yn, yd } = map_to_curve_elligator2_curve448(u); // 1. (xn, xd, yn, yd) = map_to_curve_elligator2_curve448(u)
187
+ let xn2 = Fp.sqr(xn); // 2. xn2 = xn^2
188
+ let xd2 = Fp.sqr(xd); // 3. xd2 = xd^2
189
+ let xd4 = Fp.sqr(xd2); // 4. xd4 = xd2^2
190
+ let yn2 = Fp.sqr(yn); // 5. yn2 = yn^2
191
+ let yd2 = Fp.sqr(yd); // 6. yd2 = yd^2
192
+ let xEn = Fp.sub(xn2, xd2); // 7. xEn = xn2 - xd2
193
+ let tv2 = Fp.sub(xEn, xd2); // 8. tv2 = xEn - xd2
194
+ xEn = Fp.mul(xEn, xd2); // 9. xEn = xEn * xd2
195
+ xEn = Fp.mul(xEn, yd); // 10. xEn = xEn * yd
196
+ xEn = Fp.mul(xEn, yn); // 11. xEn = xEn * yn
197
+ xEn = Fp.mul(xEn, 4n); // 12. xEn = xEn * 4
198
+ tv2 = Fp.mul(tv2, xn2); // 13. tv2 = tv2 * xn2
199
+ tv2 = Fp.mul(tv2, yd2); // 14. tv2 = tv2 * yd2
200
+ let tv3 = Fp.mul(yn2, 4n); // 15. tv3 = 4 * yn2
201
+ let tv1 = Fp.add(tv3, yd2); // 16. tv1 = tv3 + yd2
202
+ tv1 = Fp.mul(tv1, xd4); // 17. tv1 = tv1 * xd4
203
+ let xEd = Fp.add(tv1, tv2); // 18. xEd = tv1 + tv2
204
+ tv2 = Fp.mul(tv2, xn); // 19. tv2 = tv2 * xn
205
+ let tv4 = Fp.mul(xn, xd4); // 20. tv4 = xn * xd4
206
+ let yEn = Fp.sub(tv3, yd2); // 21. yEn = tv3 - yd2
207
+ yEn = Fp.mul(yEn, tv4); // 22. yEn = yEn * tv4
208
+ yEn = Fp.sub(yEn, tv2); // 23. yEn = yEn - tv2
209
+ tv1 = Fp.add(xn2, xd2); // 24. tv1 = xn2 + xd2
210
+ tv1 = Fp.mul(tv1, xd2); // 25. tv1 = tv1 * xd2
211
+ tv1 = Fp.mul(tv1, xd); // 26. tv1 = tv1 * xd
212
+ tv1 = Fp.mul(tv1, yn2); // 27. tv1 = tv1 * yn2
213
+ tv1 = Fp.mul(tv1, BigInt(-2)); // 28. tv1 = -2 * tv1
214
+ let yEd = Fp.add(tv2, tv1); // 29. yEd = tv2 + tv1
215
+ tv4 = Fp.mul(tv4, yd2); // 30. tv4 = tv4 * yd2
216
+ yEd = Fp.add(yEd, tv4); // 31. yEd = yEd + tv4
217
+ tv1 = Fp.mul(xEd, yEd); // 32. tv1 = xEd * yEd
218
+ let e = Fp.eql(tv1, Fp.ZERO); // 33. e = tv1 == 0
219
+ xEn = Fp.cmov(xEn, Fp.ZERO, e); // 34. xEn = CMOV(xEn, 0, e)
220
+ xEd = Fp.cmov(xEd, Fp.ONE, e); // 35. xEd = CMOV(xEd, 1, e)
221
+ yEn = Fp.cmov(yEn, Fp.ONE, e); // 36. yEn = CMOV(yEn, 1, e)
222
+ yEd = Fp.cmov(yEd, Fp.ONE, e); // 37. yEd = CMOV(yEd, 1, e)
223
+
224
+ const inv = Fp.invertBatch([xEd, yEd]); // batch division
225
+ return { x: Fp.mul(xEn, inv[0]), y: Fp.mul(yEn, inv[1]) }; // 38. return (xEn, xEd, yEn, yEd)
226
+ }
227
+
228
+ const { hashToCurve, encodeToCurve } = htf.createHasher(
229
+ ed448.ExtendedPoint,
230
+ (scalars: bigint[]) => map_to_curve_elligator2_edwards448(scalars[0]),
231
+ {
232
+ DST: 'edwards448_XOF:SHAKE256_ELL2_RO_',
233
+ encodeDST: 'edwards448_XOF:SHAKE256_ELL2_NU_',
234
+ p: Fp.ORDER,
235
+ m: 1,
236
+ k: 224,
237
+ expand: 'xof',
238
+ hash: shake256,
239
+ }
240
+ );
241
+ export { hashToCurve, encodeToCurve };
@@ -1,2 +1 @@
1
- "use strict";
2
1
  throw new Error('Incorrect usage. Import submodules instead');
package/src/jubjub.ts ADDED
@@ -0,0 +1,58 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { sha512 } from '@noble/hashes/sha512';
3
+ import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
4
+ import { twistedEdwards } from './abstract/edwards.js';
5
+ import { blake2s } from '@noble/hashes/blake2s';
6
+ import { Fp } from './abstract/modular.js';
7
+
8
+ /**
9
+ * jubjub Twisted Edwards curve.
10
+ * https://neuromancer.sk/std/other/JubJub
11
+ * jubjub does not use EdDSA, so `hash`/sha512 params are passed because interface expects them.
12
+ */
13
+
14
+ export const jubjub = twistedEdwards({
15
+ // Params: a, d
16
+ a: BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000'),
17
+ d: BigInt('0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1'),
18
+ // Finite field 𝔽p over which we'll do calculations
19
+ // Same value as bls12-381 Fr (not Fp)
20
+ Fp: Fp(BigInt('0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001')),
21
+ // Subgroup order: how many points curve has
22
+ n: BigInt('0xe7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7'),
23
+ // Cofactor
24
+ h: BigInt(8),
25
+ // Base point (x, y) aka generator point
26
+ Gx: BigInt('0x11dafe5d23e1218086a365b99fbf3d3be72f6afd7d1f72623e6b071492d1122b'),
27
+ Gy: BigInt('0x1d523cf1ddab1a1793132e78c866c0c33e26ba5cc220fed7cc3f870e59d292aa'),
28
+ hash: sha512,
29
+ randomBytes,
30
+ } as const);
31
+
32
+ const GH_FIRST_BLOCK = utf8ToBytes(
33
+ '096b36a5804bfacef1691e173c366a47ff5ba84a44f26ddd7e8d9f79d5b42df0'
34
+ );
35
+
36
+ // Returns point at JubJub curve which is prime order and not zero
37
+ export function groupHash(tag: Uint8Array, personalization: Uint8Array) {
38
+ const h = blake2s.create({ personalization, dkLen: 32 });
39
+ h.update(GH_FIRST_BLOCK);
40
+ h.update(tag);
41
+ // NOTE: returns ExtendedPoint, in case it will be multiplied later
42
+ let p = jubjub.ExtendedPoint.fromHex(h.digest());
43
+ // NOTE: cannot replace with isSmallOrder, returns Point*8
44
+ p = p.multiply(jubjub.CURVE.h);
45
+ if (p.equals(jubjub.ExtendedPoint.ZERO)) throw new Error('Point has small order');
46
+ return p;
47
+ }
48
+
49
+ export function findGroupHash(m: Uint8Array, personalization: Uint8Array) {
50
+ const tag = concatBytes(m, new Uint8Array([0]));
51
+ for (let i = 0; i < 256; i++) {
52
+ tag[tag.length - 1] = i;
53
+ try {
54
+ return groupHash(tag, personalization);
55
+ } catch (e) {}
56
+ }
57
+ throw new Error('findGroupHash tag overflow');
58
+ }
package/src/p192.ts ADDED
@@ -0,0 +1,25 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { createCurve } from './_shortw_utils.js';
3
+ import { sha256 } from '@noble/hashes/sha256';
4
+ import { Fp } from './abstract/modular.js';
5
+
6
+ // NIST secp192r1 aka P192
7
+ // https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/secg/secp192r1
8
+ export const P192 = createCurve(
9
+ {
10
+ // Params: a, b
11
+ a: BigInt('0xfffffffffffffffffffffffffffffffefffffffffffffffc'),
12
+ b: BigInt('0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1'),
13
+ // Field over which we'll do calculations; 2n ** 192n - 2n ** 64n - 1n
14
+ Fp: Fp(BigInt('0xfffffffffffffffffffffffffffffffeffffffffffffffff')),
15
+ // Curve order, total count of valid points in the field.
16
+ n: BigInt('0xffffffffffffffffffffffff99def836146bc9b1b4d22831'),
17
+ // Base point (x, y) aka generator point
18
+ Gx: BigInt('0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012'),
19
+ Gy: BigInt('0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811'),
20
+ h: BigInt(1),
21
+ lowS: false,
22
+ } as const,
23
+ sha256
24
+ );
25
+ export const secp192r1 = P192;
package/src/p224.ts ADDED
@@ -0,0 +1,25 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { createCurve } from './_shortw_utils.js';
3
+ import { sha224 } from '@noble/hashes/sha256';
4
+ import { Fp } from './abstract/modular.js';
5
+
6
+ // NIST secp224r1 aka P224
7
+ // https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-224
8
+ export const P224 = createCurve(
9
+ {
10
+ // Params: a, b
11
+ a: BigInt('0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe'),
12
+ b: BigInt('0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4'),
13
+ // Field over which we'll do calculations;
14
+ Fp: Fp(BigInt('0xffffffffffffffffffffffffffffffff000000000000000000000001')),
15
+ // Curve order, total count of valid points in the field
16
+ n: BigInt('0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d'),
17
+ // Base point (x, y) aka generator point
18
+ Gx: BigInt('0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21'),
19
+ Gy: BigInt('0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34'),
20
+ h: BigInt(1),
21
+ lowS: false,
22
+ } as const,
23
+ sha224
24
+ );
25
+ export const secp224r1 = P224;
package/src/p256.ts ADDED
@@ -0,0 +1,53 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { createCurve } from './_shortw_utils.js';
3
+ import { sha256 } from '@noble/hashes/sha256';
4
+ import { Fp as Field } from './abstract/modular.js';
5
+ import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
6
+ import * as htf from './abstract/hash-to-curve.js';
7
+
8
+ // NIST secp256r1 aka P256
9
+ // https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-256
10
+
11
+ // Field over which we'll do calculations; 2n**224n * (2n**32n-1n) + 2n**192n + 2n**96n-1n
12
+ const Fp = Field(BigInt('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'));
13
+ const CURVE_A = Fp.create(BigInt('-3'));
14
+ const CURVE_B = BigInt('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b');
15
+
16
+ const mapSWU = mapToCurveSimpleSWU(Fp, {
17
+ A: CURVE_A,
18
+ B: CURVE_B,
19
+ Z: Fp.create(BigInt('-10')),
20
+ });
21
+
22
+ export const P256 = createCurve(
23
+ {
24
+ // Params: a, b
25
+ a: CURVE_A,
26
+ b: CURVE_B,
27
+ Fp,
28
+ // Curve order, total count of valid points in the field
29
+ n: BigInt('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'),
30
+ // Base point (x, y) aka generator point
31
+ Gx: BigInt('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'),
32
+ Gy: BigInt('0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'),
33
+ h: BigInt(1),
34
+ lowS: false,
35
+ } as const,
36
+ sha256
37
+ );
38
+ export const secp256r1 = P256;
39
+
40
+ const { hashToCurve, encodeToCurve } = htf.createHasher(
41
+ secp256r1.ProjectivePoint,
42
+ (scalars: bigint[]) => mapSWU(scalars[0]),
43
+ {
44
+ DST: 'P256_XMD:SHA-256_SSWU_RO_',
45
+ encodeDST: 'P256_XMD:SHA-256_SSWU_NU_',
46
+ p: Fp.ORDER,
47
+ m: 1,
48
+ k: 128,
49
+ expand: 'xmd',
50
+ hash: sha256,
51
+ }
52
+ );
53
+ export { hashToCurve, encodeToCurve };
package/src/p384.ts ADDED
@@ -0,0 +1,57 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { createCurve } from './_shortw_utils.js';
3
+ import { sha384 } from '@noble/hashes/sha512';
4
+ import { Fp as Field } from './abstract/modular.js';
5
+ import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
6
+ import * as htf from './abstract/hash-to-curve.js';
7
+
8
+ // NIST secp384r1 aka P384
9
+ // https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-384
10
+
11
+ // Field over which we'll do calculations. 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
12
+ // prettier-ignore
13
+ const P = BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff');
14
+ const Fp = Field(P);
15
+ const CURVE_A = Fp.create(BigInt('-3'));
16
+ // prettier-ignore
17
+ const CURVE_B = BigInt('0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef');
18
+
19
+ const mapSWU = mapToCurveSimpleSWU(Fp, {
20
+ A: CURVE_A,
21
+ B: CURVE_B,
22
+ Z: Fp.create(BigInt('-12')),
23
+ });
24
+
25
+ // prettier-ignore
26
+ export const P384 = createCurve({
27
+ // Params: a, b
28
+ a: CURVE_A,
29
+ b: CURVE_B,
30
+ // Field over which we'll do calculations. 2n**384n - 2n**128n - 2n**96n + 2n**32n - 1n
31
+ Fp,
32
+ // Curve order, total count of valid points in the field.
33
+ n: BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973'),
34
+ // Base point (x, y) aka generator point
35
+ Gx: BigInt('0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7'),
36
+ Gy: BigInt('0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f'),
37
+ h: BigInt(1),
38
+ lowS: false,
39
+ } as const,
40
+ sha384
41
+ );
42
+ export const secp384r1 = P384;
43
+
44
+ const { hashToCurve, encodeToCurve } = htf.createHasher(
45
+ secp384r1.ProjectivePoint,
46
+ (scalars: bigint[]) => mapSWU(scalars[0]),
47
+ {
48
+ DST: 'P384_XMD:SHA-384_SSWU_RO_',
49
+ encodeDST: 'P384_XMD:SHA-384_SSWU_NU_',
50
+ p: Fp.ORDER,
51
+ m: 1,
52
+ k: 192,
53
+ expand: 'xmd',
54
+ hash: sha384,
55
+ }
56
+ );
57
+ export { hashToCurve, encodeToCurve };
package/src/p521.ts ADDED
@@ -0,0 +1,57 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { createCurve } from './_shortw_utils.js';
3
+ import { sha512 } from '@noble/hashes/sha512';
4
+ import { Fp as Field } from './abstract/modular.js';
5
+ import { mapToCurveSimpleSWU } from './abstract/weierstrass.js';
6
+ import * as htf from './abstract/hash-to-curve.js';
7
+
8
+ // NIST secp521r1 aka P521
9
+ // Note that it's 521, which differs from 512 of its hash function.
10
+ // https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/nist/P-521
11
+
12
+ // Field over which we'll do calculations; 2n**521n - 1n
13
+ // prettier-ignore
14
+ const P = BigInt('0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff');
15
+ const Fp = Field(P);
16
+
17
+ const CURVE_A = Fp.create(BigInt('-3'));
18
+ // prettier-ignore
19
+ const CURVE_B = BigInt('0x0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00');
20
+
21
+ const mapSWU = mapToCurveSimpleSWU(Fp, {
22
+ A: CURVE_A,
23
+ B: CURVE_B,
24
+ Z: Fp.create(BigInt('-4')),
25
+ });
26
+
27
+ // prettier-ignore
28
+ export const P521 = createCurve({
29
+ // Params: a, b
30
+ a: CURVE_A,
31
+ b: CURVE_B,
32
+ Fp,
33
+ // Curve order, total count of valid points in the field
34
+ n: BigInt('0x01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409'),
35
+ // Base point (x, y) aka generator point
36
+ Gx: BigInt('0x00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66'),
37
+ Gy: BigInt('0x011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650'),
38
+ h: BigInt(1),
39
+ lowS: false,
40
+ allowedPrivateKeyLengths: [130, 131, 132] // P521 keys are variable-length. Normalize to 132b
41
+ } as const, sha512);
42
+ export const secp521r1 = P521;
43
+
44
+ const { hashToCurve, encodeToCurve } = htf.createHasher(
45
+ secp521r1.ProjectivePoint,
46
+ (scalars: bigint[]) => mapSWU(scalars[0]),
47
+ {
48
+ DST: 'P521_XMD:SHA-512_SSWU_RO_',
49
+ encodeDST: 'P521_XMD:SHA-512_SSWU_NU_',
50
+ p: Fp.ORDER,
51
+ m: 1,
52
+ k: 256,
53
+ expand: 'xmd',
54
+ hash: sha512,
55
+ }
56
+ );
57
+ export { hashToCurve, encodeToCurve };
package/src/pasta.ts ADDED
@@ -0,0 +1,31 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ import { sha256 } from '@noble/hashes/sha256';
3
+ import { weierstrass } from './abstract/weierstrass.js';
4
+ import { getHash } from './_shortw_utils.js';
5
+ import * as mod from './abstract/modular.js';
6
+
7
+ export const p = BigInt('0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001');
8
+ export const q = BigInt('0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001');
9
+
10
+ // https://neuromancer.sk/std/other/Pallas
11
+ export const pallas = weierstrass({
12
+ a: BigInt(0),
13
+ b: BigInt(5),
14
+ Fp: mod.Fp(p),
15
+ n: q,
16
+ Gx: mod.mod(BigInt(-1), p),
17
+ Gy: BigInt(2),
18
+ h: BigInt(1),
19
+ ...getHash(sha256),
20
+ });
21
+ // https://neuromancer.sk/std/other/Vesta
22
+ export const vesta = weierstrass({
23
+ a: BigInt(0),
24
+ b: BigInt(5),
25
+ Fp: mod.Fp(q),
26
+ n: p,
27
+ Gx: mod.mod(BigInt(-1), q),
28
+ Gy: BigInt(2),
29
+ h: BigInt(1),
30
+ ...getHash(sha256),
31
+ });