starknet 11.8.0 → 11.9.1

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/dist/index.d.ts CHANGED
@@ -16119,7 +16119,9 @@ declare class CairoEthAddress {
16119
16119
  }
16120
16120
 
16121
16121
  /**
16122
- * The largest value a Secp256k1Point can carry : both coordinates at their maximum, 512 bits.
16122
+ * The largest number the 512 bits of a Secp256k1Point can hold : both coordinates at their
16123
+ * maximum. It is only a bound : a point must also be on the curve, and this value is not, so
16124
+ * `new CairoSecp256k1Point(SECP256K1_POINT_MAX)` throws.
16123
16125
  * @example
16124
16126
  * ```typescript
16125
16127
  * const result = SECP256K1_POINT_MAX === (1n << 512n) - 1n;
@@ -16154,17 +16156,33 @@ interface Secp256k1PointStruct {
16154
16156
  * class accepts is the 512-bit concatenation `x || y`, x in the upper half : that is the shape an
16155
16157
  * uncompressed public key already has once its `04` prefix is dropped.
16156
16158
  *
16157
- * Both ways in are supported : one number, or the four limbs directly, which is how a response is
16158
- * read back.
16159
+ * Both ways in are supported : one number, or the four limbs directly, in the order a call carries
16160
+ * them.
16161
+ *
16162
+ * What the caller passes in is checked as soon as the point is built : it must be on the curve, or
16163
+ * be (0, 0), which stands for the point at infinity, since a contract refuses anything else. A
16164
+ * point read from a contract response is taken as it comes, without this check.
16159
16165
  * @example
16160
16166
  * ```typescript
16161
- * // one number, x in the upper 256 bits
16162
- * const point = new CairoSecp256k1Point(1n);
16163
- * point.toApiRequest(); // ["0", "0", "1", "0"] x = 0, y = 1
16167
+ * // G, the generator of the curve, as one number : x in the upper 256 bits, y in the lower ones
16168
+ * const G =
16169
+ * 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n;
16170
+ * const point = new CairoSecp256k1Point(G);
16171
+ * point.toStruct();
16172
+ * // { xLow: "0x29bfcdb2dce28d959f2815b16f81798", xHigh: "0x79be667ef9dcbbac55a06295ce870b07",
16173
+ * // yLow: "0xfd17b448a68554199c47d08ffb10d4b8", yHigh: "0x483ada7726a3c4655da4fbfc0e1108a8" }
16174
+ *
16175
+ * // the same point from its four limbs, as a call carries them
16176
+ * const same = new CairoSecp256k1Point(
16177
+ * '0x29bfcdb2dce28d959f2815b16f81798',
16178
+ * '0x79be667ef9dcbbac55a06295ce870b07',
16179
+ * '0xfd17b448a68554199c47d08ffb10d4b8',
16180
+ * '0x483ada7726a3c4655da4fbfc0e1108a8'
16181
+ * );
16182
+ * same.toBigInt() === G; // true
16164
16183
  *
16165
- * // the four limbs, as a call carries them
16166
- * const same = new CairoSecp256k1Point(0, 0, 1, 0);
16167
- * same.toBigInt(); // 1n
16184
+ * new CairoSecp256k1Point(1n); // x = 0, y = 1
16185
+ * // throws Error("Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it")
16168
16186
  * ```
16169
16187
  */
16170
16188
  declare class CairoSecp256k1Point {
@@ -16172,8 +16190,9 @@ declare class CairoSecp256k1Point {
16172
16190
  * The low 128 bits of the x coordinate.
16173
16191
  * @example
16174
16192
  * ```typescript
16175
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xLow;
16176
- * // result = 1n
16193
+ * // G, the generator of the curve, as in the example of the class
16194
+ * const result = new CairoSecp256k1Point(G).xLow;
16195
+ * // result = 0x29bfcdb2dce28d959f2815b16f81798n
16177
16196
  * ```
16178
16197
  */
16179
16198
  xLow: bigint;
@@ -16181,8 +16200,9 @@ declare class CairoSecp256k1Point {
16181
16200
  * The high 128 bits of the x coordinate.
16182
16201
  * @example
16183
16202
  * ```typescript
16184
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xHigh;
16185
- * // result = 2n
16203
+ * // G, the generator of the curve, as in the example of the class
16204
+ * const result = new CairoSecp256k1Point(G).xHigh;
16205
+ * // result = 0x79be667ef9dcbbac55a06295ce870b07n
16186
16206
  * ```
16187
16207
  */
16188
16208
  xHigh: bigint;
@@ -16190,8 +16210,9 @@ declare class CairoSecp256k1Point {
16190
16210
  * The low 128 bits of the y coordinate.
16191
16211
  * @example
16192
16212
  * ```typescript
16193
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yLow;
16194
- * // result = 3n
16213
+ * // G, the generator of the curve, as in the example of the class
16214
+ * const result = new CairoSecp256k1Point(G).yLow;
16215
+ * // result = 0xfd17b448a68554199c47d08ffb10d4b8n
16195
16216
  * ```
16196
16217
  */
16197
16218
  yLow: bigint;
@@ -16199,8 +16220,9 @@ declare class CairoSecp256k1Point {
16199
16220
  * The high 128 bits of the y coordinate.
16200
16221
  * @example
16201
16222
  * ```typescript
16202
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yHigh;
16203
- * // result = 4n
16223
+ * // G, the generator of the curve, as in the example of the class
16224
+ * const result = new CairoSecp256k1Point(G).yHigh;
16225
+ * // result = 0x483ada7726a3c4655da4fbfc0e1108a8n
16204
16226
  * ```
16205
16227
  */
16206
16228
  yHigh: bigint;
@@ -16222,17 +16244,22 @@ declare class CairoSecp256k1Point {
16222
16244
  */
16223
16245
  constructor(xLow: BigNumberish, xHigh: BigNumberish, yLow: BigNumberish, yHigh: BigNumberish);
16224
16246
  /**
16225
- * Throw unless the value can be carried by a Secp256k1Point, and return it as a number.
16247
+ * Throw unless the value is a point that a contract accepts, and return it as a number.
16248
+ *
16249
+ * The value must fit in 512 bits, and the point it stands for must be on the curve, or be (0, 0),
16250
+ * the point at infinity.
16226
16251
  *
16227
16252
  * Unlike the other classes here this one gives the number back rather than returning nothing :
16228
16253
  * the constructor needs it, and computing it twice would mean splitting a 512-bit value twice.
16229
16254
  * @param {BigNumberish} input the 512-bit value to check
16230
16255
  * @returns {bigint} the value, once checked
16231
- * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or outside [0, 2^512 - 1]
16256
+ * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or outside [0, 2^512 - 1], or when the point is not on the curve
16232
16257
  * @example
16233
16258
  * ```typescript
16234
- * const result = CairoSecp256k1Point.validate('0x1234');
16235
- * // result = 4660n
16259
+ * const result = CairoSecp256k1Point.validate(0n);
16260
+ * // result = 0n (0, 0), the point at infinity
16261
+ * CairoSecp256k1Point.validate('0x1234');
16262
+ * // throws Error("Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it")
16236
16263
  * CairoSecp256k1Point.validate(SECP256K1_POINT_MAX + 1n);
16237
16264
  * // throws Error("input is bigger than SECP256K1_POINT_MAX")
16238
16265
  * CairoSecp256k1Point.validate('abc');
@@ -16243,17 +16270,27 @@ declare class CairoSecp256k1Point {
16243
16270
  */
16244
16271
  static validate(input: BigNumberish | unknown): bigint;
16245
16272
  /**
16246
- * Throw unless the four limbs can each be carried by 128 bits, and return them as numbers.
16273
+ * Throw unless the four limbs each fit in 128 bits and together make a point that a contract
16274
+ * accepts, and return them as numbers.
16247
16275
  * @param {BigNumberish} xLow the low 128 bits of x
16248
16276
  * @param {BigNumberish} xHigh the high 128 bits of x
16249
16277
  * @param {BigNumberish} yLow the low 128 bits of y
16250
16278
  * @param {BigNumberish} yHigh the high 128 bits of y
16251
16279
  * @returns {{xLow: bigint, xHigh: bigint, yLow: bigint, yHigh: bigint}} the four limbs, checked
16252
- * @throws {Error} when a limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits
16280
+ * @throws {Error} when a limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits, or when the point is not on the curve
16253
16281
  * @example
16254
16282
  * ```typescript
16255
- * const result = CairoSecp256k1Point.validateProps(1, 2, 3, 4);
16256
- * // result = { xLow: 1n, xHigh: 2n, yLow: 3n, yHigh: 4n }
16283
+ * // G, the generator of the curve
16284
+ * const result = CairoSecp256k1Point.validateProps(
16285
+ * '0x29bfcdb2dce28d959f2815b16f81798',
16286
+ * '0x79be667ef9dcbbac55a06295ce870b07',
16287
+ * '0xfd17b448a68554199c47d08ffb10d4b8',
16288
+ * '0x483ada7726a3c4655da4fbfc0e1108a8'
16289
+ * );
16290
+ * // result = { xLow: 0x29bfcdb2dce28d959f2815b16f81798n, xHigh: 0x79be667ef9dcbbac55a06295ce870b07n,
16291
+ * // yLow: 0xfd17b448a68554199c47d08ffb10d4b8n, yHigh: 0x483ada7726a3c4655da4fbfc0e1108a8n }
16292
+ * CairoSecp256k1Point.validateProps(1, 2, 3, 4);
16293
+ * // throws Error("Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it")
16257
16294
  * CairoSecp256k1Point.validateProps(1, 2, 3, 2n ** 128n);
16258
16295
  * // throws Error("yHigh must fit in 128 bits")
16259
16296
  * CairoSecp256k1Point.validateProps(1.5, 2, 3, 4);
@@ -16269,18 +16306,18 @@ declare class CairoSecp256k1Point {
16269
16306
  yHigh: bigint;
16270
16307
  };
16271
16308
  /**
16272
- * Can this value be carried by a Secp256k1Point?
16309
+ * Is this value a point that a contract accepts as a Secp256k1Point?
16273
16310
  *
16274
16311
  * The non-throwing form of {@link CairoSecp256k1Point.validate}, so it answers false for every
16275
16312
  * input that one refuses, whatever the reason.
16276
16313
  * @param {any} data the value to test
16277
- * @returns {boolean} true when the value fits in 512 bits
16314
+ * @returns {boolean} true when the value fits in 512 bits and stands for a point on the curve, or for (0, 0)
16278
16315
  * @example
16279
16316
  * ```typescript
16280
- * const result = CairoSecp256k1Point.is(SECP256K1_POINT_MAX);
16281
- * // result = true
16282
- * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX + 1n);
16283
- * // result2 = false
16317
+ * const result = CairoSecp256k1Point.is(0n);
16318
+ * // result = true (0, 0), the point at infinity
16319
+ * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX);
16320
+ * // result2 = false x is not smaller than p
16284
16321
  * ```
16285
16322
  */
16286
16323
  static is(data: any): boolean;
@@ -16299,13 +16336,18 @@ declare class CairoSecp256k1Point {
16299
16336
  static isAbiType(abiType: string): boolean;
16300
16337
  /**
16301
16338
  * Read one point off a contract response, advancing the iterator past its four felts.
16339
+ *
16340
+ * A response is read as it comes : each felt must fit in 128 bits, but the point is not checked
16341
+ * against the curve. What a node answers is taken as correct, and a contract can only return a
16342
+ * point on the curve, or (0, 0).
16302
16343
  * @param {Iterator<string>} responseIterator the response felts, positioned on this point
16303
16344
  * @returns {CairoSecp256k1Point} the point that was read
16345
+ * @throws {Error} when the response ends before four felts, or when a felt does not fit in 128 bits
16304
16346
  * @example
16305
16347
  * ```typescript
16306
16348
  * const response = ['0x0', '0x0', '0x1', '0x0'];
16307
16349
  * const result = CairoSecp256k1Point.factoryFromApiResponse(response.values()).toBigInt();
16308
- * // result = 1n
16350
+ * // result = 1n not on the curve, and read all the same
16309
16351
  * ```
16310
16352
  */
16311
16353
  static factoryFromApiResponse(responseIterator: Iterator<string>): CairoSecp256k1Point;
@@ -16317,10 +16359,11 @@ declare class CairoSecp256k1Point {
16317
16359
  * @returns {bigint} the two coordinates concatenated, x in the upper 256 bits
16318
16360
  * @example
16319
16361
  * ```typescript
16320
- * const result = new CairoSecp256k1Point(1n).toBigInt();
16321
- * // result = 1n
16322
- * const result2 = new CairoSecp256k1Point(0, 0, 3, 0).toBigInt();
16323
- * // result2 = 3n
16362
+ * // G, the generator of the curve, as in the example of the class
16363
+ * const result = new CairoSecp256k1Point(G).toBigInt() === G;
16364
+ * // result = true
16365
+ * const result2 = new CairoSecp256k1Point(0, 0, 0, 0).toBigInt();
16366
+ * // result2 = 0n (0, 0), the point at infinity
16324
16367
  * ```
16325
16368
  */
16326
16369
  toBigInt(): bigint;
@@ -16329,8 +16372,10 @@ declare class CairoSecp256k1Point {
16329
16372
  * @returns {Secp256k1PointStruct} the limbs, each 0x-prefixed and unpadded
16330
16373
  * @example
16331
16374
  * ```typescript
16332
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toStruct();
16333
- * // result = { xLow: "0x1", xHigh: "0x2", yLow: "0x3", yHigh: "0x4" }
16375
+ * // G, the generator of the curve, as in the example of the class
16376
+ * const result = new CairoSecp256k1Point(G).toStruct();
16377
+ * // result = { xLow: "0x29bfcdb2dce28d959f2815b16f81798", xHigh: "0x79be667ef9dcbbac55a06295ce870b07",
16378
+ * // yLow: "0xfd17b448a68554199c47d08ffb10d4b8", yHigh: "0x483ada7726a3c4655da4fbfc0e1108a8" }
16334
16379
  * ```
16335
16380
  */
16336
16381
  toStruct(): Secp256k1PointStruct;
@@ -16339,8 +16384,8 @@ declare class CairoSecp256k1Point {
16339
16384
  * @returns {string} the 512-bit value as a 0x-prefixed hex string
16340
16385
  * @example
16341
16386
  * ```typescript
16342
- * const result = new CairoSecp256k1Point(4660n).toHexString();
16343
- * // result = "0x1234"
16387
+ * const result = new CairoSecp256k1Point(0n).toHexString();
16388
+ * // result = "0x0" (0, 0), the point at infinity
16344
16389
  * ```
16345
16390
  */
16346
16391
  toHexString(): string;
@@ -16349,8 +16394,10 @@ declare class CairoSecp256k1Point {
16349
16394
  * @returns {string[]} the limbs as decimal strings, `[xLow, xHigh, yLow, yHigh]`, flagged as compiled
16350
16395
  * @example
16351
16396
  * ```typescript
16352
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest();
16353
- * // result = ["1", "2", "3", "4"]
16397
+ * // G, the generator of the curve, as in the example of the class
16398
+ * const result = new CairoSecp256k1Point(G).toApiRequest();
16399
+ * // result = ["3468390537006497937951914270391801752", "161825202758953104525843685720298294023",
16400
+ * // "336417762351022071123394393598455764152", "96009999919712310848645357523629574312"]
16354
16401
  * ```
16355
16402
  */
16356
16403
  toApiRequest(): string[];
@@ -16361,11 +16408,19 @@ declare class CairoSecp256k1Point {
16361
16408
  * read as a point whose upper limbs are zero. A longer one is refused rather than truncated.
16362
16409
  * @param {string} hexString the value, 0x-prefixed or not, at most 128 hex digits
16363
16410
  * @returns {CairoSecp256k1Point} the point the string spells
16364
- * @throws {Error} when the string holds more than 128 hex digits
16365
- * @example
16366
- * ```typescript
16367
- * const result = CairoSecp256k1Point.fromHex('0x1').toApiRequest();
16368
- * // result = ["0", "0", "1", "0"]
16411
+ * @throws {Error} when the string holds more than 128 hex digits, or when the point is not on the
16412
+ * curve
16413
+ * @example
16414
+ * ```typescript
16415
+ * // G, the generator of the curve
16416
+ * const result = CairoSecp256k1Point.fromHex(
16417
+ * '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'
16418
+ * ).xHigh;
16419
+ * // result = 0x79be667ef9dcbbac55a06295ce870b07n
16420
+ * const result2 = CairoSecp256k1Point.fromHex('0x0').toApiRequest();
16421
+ * // result2 = ["0", "0", "0", "0"] left-padded to 128 hex digits : (0, 0), the point at infinity
16422
+ * CairoSecp256k1Point.fromHex('0x1'); // x = 0, y = 1
16423
+ * // throws Error("Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it")
16369
16424
  * ```
16370
16425
  */
16371
16426
  static fromHex(hexString: string): CairoSecp256k1Point;
@@ -9358,7 +9358,7 @@ ${indent}}` : "}";
9358
9358
  * ```
9359
9359
  */
9360
9360
  static validateProps(limb0, limb1, limb2, limb3) {
9361
- const validateLimb = (limb, name) => {
9361
+ const validateLimb2 = (limb, name) => {
9362
9362
  assert(limb !== null, `${name} cannot be null`);
9363
9363
  assert(limb !== void 0, `${name} cannot be undefined`);
9364
9364
  assert(!isText(limb), `${name} cannot be built from text`);
@@ -9367,10 +9367,10 @@ ${indent}}` : "}";
9367
9367
  assert(!isNumber2(limb) || Number.isInteger(limb), `${name} must be an integer`);
9368
9368
  return BigInt(limb);
9369
9369
  };
9370
- const l0 = validateLimb(limb0, "limb0");
9371
- const l1 = validateLimb(limb1, "limb1");
9372
- const l2 = validateLimb(limb2, "limb2");
9373
- const l3 = validateLimb(limb3, "limb3");
9370
+ const l0 = validateLimb2(limb0, "limb0");
9371
+ const l1 = validateLimb2(limb1, "limb1");
9372
+ const l2 = validateLimb2(limb2, "limb2");
9373
+ const l3 = validateLimb2(limb3, "limb3");
9374
9374
  [l0, l1, l2, l3].forEach((value, index) => {
9375
9375
  assert(
9376
9376
  value >= UINT_128_MIN && value <= UINT_128_MAX,
@@ -12132,13 +12132,44 @@ ${indent}}` : "}";
12132
12132
  // src/utils/cairoDataTypes/secp256k1Point.ts
12133
12133
  var SECP256K1_POINT_MAX = (1n << 512n) - 1n;
12134
12134
  var SECP256K1_POINT_MIN = 0n;
12135
+ var SECP256K1_PRIME = 2n ** 256n - 2n ** 32n - 977n;
12136
+ function assertOnSecp256k1Curve(x, y) {
12137
+ if (x === 0n && y === 0n) {
12138
+ return;
12139
+ }
12140
+ assert(
12141
+ x < SECP256K1_PRIME,
12142
+ "Secp256k1Point: x must be smaller than p, the prime of the secp256k1 curve"
12143
+ );
12144
+ assert(
12145
+ y < SECP256K1_PRIME,
12146
+ "Secp256k1Point: y must be smaller than p, the prime of the secp256k1 curve"
12147
+ );
12148
+ assert(
12149
+ y * y % SECP256K1_PRIME === (x * x * x + 7n) % SECP256K1_PRIME,
12150
+ "Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it"
12151
+ );
12152
+ }
12153
+ function validateLimb(limb, name) {
12154
+ assert(limb !== null, `${name} cannot be null`);
12155
+ assert(limb !== void 0, `${name} cannot be undefined`);
12156
+ assert(!isText(limb), `${name} cannot be built from text`);
12157
+ assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
12158
+ assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
12159
+ assert(!isNumber2(limb) || Number.isInteger(limb), `${name} must be an integer`);
12160
+ const bigInt = BigInt(limb);
12161
+ assert(bigInt >= 0n, `${name} must be non-negative`);
12162
+ assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`);
12163
+ return bigInt;
12164
+ }
12135
12165
  var CairoSecp256k1Point = class _CairoSecp256k1Point {
12136
12166
  /**
12137
12167
  * The low 128 bits of the x coordinate.
12138
12168
  * @example
12139
12169
  * ```typescript
12140
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xLow;
12141
- * // result = 1n
12170
+ * // G, the generator of the curve, as in the example of the class
12171
+ * const result = new CairoSecp256k1Point(G).xLow;
12172
+ * // result = 0x29bfcdb2dce28d959f2815b16f81798n
12142
12173
  * ```
12143
12174
  */
12144
12175
  xLow;
@@ -12146,8 +12177,9 @@ ${indent}}` : "}";
12146
12177
  * The high 128 bits of the x coordinate.
12147
12178
  * @example
12148
12179
  * ```typescript
12149
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xHigh;
12150
- * // result = 2n
12180
+ * // G, the generator of the curve, as in the example of the class
12181
+ * const result = new CairoSecp256k1Point(G).xHigh;
12182
+ * // result = 0x79be667ef9dcbbac55a06295ce870b07n
12151
12183
  * ```
12152
12184
  */
12153
12185
  xHigh;
@@ -12155,8 +12187,9 @@ ${indent}}` : "}";
12155
12187
  * The low 128 bits of the y coordinate.
12156
12188
  * @example
12157
12189
  * ```typescript
12158
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yLow;
12159
- * // result = 3n
12190
+ * // G, the generator of the curve, as in the example of the class
12191
+ * const result = new CairoSecp256k1Point(G).yLow;
12192
+ * // result = 0xfd17b448a68554199c47d08ffb10d4b8n
12160
12193
  * ```
12161
12194
  */
12162
12195
  yLow;
@@ -12164,8 +12197,9 @@ ${indent}}` : "}";
12164
12197
  * The high 128 bits of the y coordinate.
12165
12198
  * @example
12166
12199
  * ```typescript
12167
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yHigh;
12168
- * // result = 4n
12200
+ * // G, the generator of the curve, as in the example of the class
12201
+ * const result = new CairoSecp256k1Point(G).yHigh;
12202
+ * // result = 0x483ada7726a3c4655da4fbfc0e1108a8n
12169
12203
  * ```
12170
12204
  */
12171
12205
  yHigh;
@@ -12182,14 +12216,14 @@ ${indent}}` : "}";
12182
12216
  * @param {any[]} arr either one value — a 512-bit number or a {@link Secp256k1PointStruct} — or
12183
12217
  * the four limbs
12184
12218
  * @throws {Error} when a value is null, undefined, of an unread type, text, a decimal number, or out
12185
- * of range — see {@link CairoSecp256k1Point.validate} and {@link CairoSecp256k1Point.validateProps}
12186
- * — or when the argument count is neither 1 nor 4
12219
+ * of range, or when the point is not on the curve — see {@link CairoSecp256k1Point.validate} and
12220
+ * {@link CairoSecp256k1Point.validateProps} — or when the argument count is neither 1 nor 4
12187
12221
  * @example
12188
12222
  * ```typescript
12189
- * const result = new CairoSecp256k1Point(1n).toApiRequest();
12190
- * // result = ["0", "0", "1", "0"]
12191
- * const result2 = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest();
12192
- * // result2 = ["1", "2", "3", "4"]
12223
+ * const result = new CairoSecp256k1Point(0n).toApiRequest();
12224
+ * // result = ["0", "0", "0", "0"] (0, 0), the point at infinity
12225
+ * new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 });
12226
+ * // throws Error("Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it")
12193
12227
  * ```
12194
12228
  */
12195
12229
  constructor(...arr) {
@@ -12224,17 +12258,22 @@ ${indent}}` : "}";
12224
12258
  }
12225
12259
  }
12226
12260
  /**
12227
- * Throw unless the value can be carried by a Secp256k1Point, and return it as a number.
12261
+ * Throw unless the value is a point that a contract accepts, and return it as a number.
12262
+ *
12263
+ * The value must fit in 512 bits, and the point it stands for must be on the curve, or be (0, 0),
12264
+ * the point at infinity.
12228
12265
  *
12229
12266
  * Unlike the other classes here this one gives the number back rather than returning nothing :
12230
12267
  * the constructor needs it, and computing it twice would mean splitting a 512-bit value twice.
12231
12268
  * @param {BigNumberish} input the 512-bit value to check
12232
12269
  * @returns {bigint} the value, once checked
12233
- * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or outside [0, 2^512 - 1]
12270
+ * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or outside [0, 2^512 - 1], or when the point is not on the curve
12234
12271
  * @example
12235
12272
  * ```typescript
12236
- * const result = CairoSecp256k1Point.validate('0x1234');
12237
- * // result = 4660n
12273
+ * const result = CairoSecp256k1Point.validate(0n);
12274
+ * // result = 0n (0, 0), the point at infinity
12275
+ * CairoSecp256k1Point.validate('0x1234');
12276
+ * // throws Error("Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it")
12238
12277
  * CairoSecp256k1Point.validate(SECP256K1_POINT_MAX + 1n);
12239
12278
  * // throws Error("input is bigger than SECP256K1_POINT_MAX")
12240
12279
  * CairoSecp256k1Point.validate('abc');
@@ -12259,20 +12298,31 @@ ${indent}}` : "}";
12259
12298
  const bigInt = BigInt(input);
12260
12299
  assert(bigInt >= SECP256K1_POINT_MIN, "input is smaller than SECP256K1_POINT_MIN");
12261
12300
  assert(bigInt <= SECP256K1_POINT_MAX, "input is bigger than SECP256K1_POINT_MAX");
12301
+ assertOnSecp256k1Curve(bigInt >> 256n, bigInt & UINT_256_MAX);
12262
12302
  return bigInt;
12263
12303
  }
12264
12304
  /**
12265
- * Throw unless the four limbs can each be carried by 128 bits, and return them as numbers.
12305
+ * Throw unless the four limbs each fit in 128 bits and together make a point that a contract
12306
+ * accepts, and return them as numbers.
12266
12307
  * @param {BigNumberish} xLow the low 128 bits of x
12267
12308
  * @param {BigNumberish} xHigh the high 128 bits of x
12268
12309
  * @param {BigNumberish} yLow the low 128 bits of y
12269
12310
  * @param {BigNumberish} yHigh the high 128 bits of y
12270
12311
  * @returns {{xLow: bigint, xHigh: bigint, yLow: bigint, yHigh: bigint}} the four limbs, checked
12271
- * @throws {Error} when a limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits
12312
+ * @throws {Error} when a limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits, or when the point is not on the curve
12272
12313
  * @example
12273
12314
  * ```typescript
12274
- * const result = CairoSecp256k1Point.validateProps(1, 2, 3, 4);
12275
- * // result = { xLow: 1n, xHigh: 2n, yLow: 3n, yHigh: 4n }
12315
+ * // G, the generator of the curve
12316
+ * const result = CairoSecp256k1Point.validateProps(
12317
+ * '0x29bfcdb2dce28d959f2815b16f81798',
12318
+ * '0x79be667ef9dcbbac55a06295ce870b07',
12319
+ * '0xfd17b448a68554199c47d08ffb10d4b8',
12320
+ * '0x483ada7726a3c4655da4fbfc0e1108a8'
12321
+ * );
12322
+ * // result = { xLow: 0x29bfcdb2dce28d959f2815b16f81798n, xHigh: 0x79be667ef9dcbbac55a06295ce870b07n,
12323
+ * // yLow: 0xfd17b448a68554199c47d08ffb10d4b8n, yHigh: 0x483ada7726a3c4655da4fbfc0e1108a8n }
12324
+ * CairoSecp256k1Point.validateProps(1, 2, 3, 4);
12325
+ * // throws Error("Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it")
12276
12326
  * CairoSecp256k1Point.validateProps(1, 2, 3, 2n ** 128n);
12277
12327
  * // throws Error("yHigh must fit in 128 bits")
12278
12328
  * CairoSecp256k1Point.validateProps(1.5, 2, 3, 4);
@@ -12282,38 +12332,28 @@ ${indent}}` : "}";
12282
12332
  * ```
12283
12333
  */
12284
12334
  static validateProps(xLow, xHigh, yLow, yHigh) {
12285
- const validateLimb = (limb, name) => {
12286
- assert(limb !== null, `${name} cannot be null`);
12287
- assert(limb !== void 0, `${name} cannot be undefined`);
12288
- assert(!isText(limb), `${name} cannot be built from text`);
12289
- assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
12290
- assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
12291
- assert(!isNumber2(limb) || Number.isInteger(limb), `${name} must be an integer`);
12292
- const bigInt = BigInt(limb);
12293
- assert(bigInt >= 0n, `${name} must be non-negative`);
12294
- assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`);
12295
- return bigInt;
12296
- };
12297
- return {
12335
+ const limbs = {
12298
12336
  xLow: validateLimb(xLow, "xLow"),
12299
12337
  xHigh: validateLimb(xHigh, "xHigh"),
12300
12338
  yLow: validateLimb(yLow, "yLow"),
12301
12339
  yHigh: validateLimb(yHigh, "yHigh")
12302
12340
  };
12341
+ assertOnSecp256k1Curve((limbs.xHigh << 128n) + limbs.xLow, (limbs.yHigh << 128n) + limbs.yLow);
12342
+ return limbs;
12303
12343
  }
12304
12344
  /**
12305
- * Can this value be carried by a Secp256k1Point?
12345
+ * Is this value a point that a contract accepts as a Secp256k1Point?
12306
12346
  *
12307
12347
  * The non-throwing form of {@link CairoSecp256k1Point.validate}, so it answers false for every
12308
12348
  * input that one refuses, whatever the reason.
12309
12349
  * @param {any} data the value to test
12310
- * @returns {boolean} true when the value fits in 512 bits
12350
+ * @returns {boolean} true when the value fits in 512 bits and stands for a point on the curve, or for (0, 0)
12311
12351
  * @example
12312
12352
  * ```typescript
12313
- * const result = CairoSecp256k1Point.is(SECP256K1_POINT_MAX);
12314
- * // result = true
12315
- * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX + 1n);
12316
- * // result2 = false
12353
+ * const result = CairoSecp256k1Point.is(0n);
12354
+ * // result = true (0, 0), the point at infinity
12355
+ * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX);
12356
+ * // result2 = false x is not smaller than p
12317
12357
  * ```
12318
12358
  */
12319
12359
  static is(data) {
@@ -12341,21 +12381,31 @@ ${indent}}` : "}";
12341
12381
  }
12342
12382
  /**
12343
12383
  * Read one point off a contract response, advancing the iterator past its four felts.
12384
+ *
12385
+ * A response is read as it comes : each felt must fit in 128 bits, but the point is not checked
12386
+ * against the curve. What a node answers is taken as correct, and a contract can only return a
12387
+ * point on the curve, or (0, 0).
12344
12388
  * @param {Iterator<string>} responseIterator the response felts, positioned on this point
12345
12389
  * @returns {CairoSecp256k1Point} the point that was read
12390
+ * @throws {Error} when the response ends before four felts, or when a felt does not fit in 128 bits
12346
12391
  * @example
12347
12392
  * ```typescript
12348
12393
  * const response = ['0x0', '0x0', '0x1', '0x0'];
12349
12394
  * const result = CairoSecp256k1Point.factoryFromApiResponse(response.values()).toBigInt();
12350
- * // result = 1n
12395
+ * // result = 1n not on the curve, and read all the same
12351
12396
  * ```
12352
12397
  */
12353
12398
  static factoryFromApiResponse(responseIterator) {
12354
- const xLow = getNext(responseIterator);
12355
- const xHigh = getNext(responseIterator);
12356
- const yLow = getNext(responseIterator);
12357
- const yHigh = getNext(responseIterator);
12358
- return new _CairoSecp256k1Point(xLow, xHigh, yLow, yHigh);
12399
+ const xLow = validateLimb(getNext(responseIterator), "xLow");
12400
+ const xHigh = validateLimb(getNext(responseIterator), "xHigh");
12401
+ const yLow = validateLimb(getNext(responseIterator), "yLow");
12402
+ const yHigh = validateLimb(getNext(responseIterator), "yHigh");
12403
+ const point = Object.create(_CairoSecp256k1Point.prototype);
12404
+ point.xLow = xLow;
12405
+ point.xHigh = xHigh;
12406
+ point.yLow = yLow;
12407
+ point.yHigh = yHigh;
12408
+ return point;
12359
12409
  }
12360
12410
  /**
12361
12411
  * The point as the single 512-bit number `x || y`.
@@ -12365,10 +12415,11 @@ ${indent}}` : "}";
12365
12415
  * @returns {bigint} the two coordinates concatenated, x in the upper 256 bits
12366
12416
  * @example
12367
12417
  * ```typescript
12368
- * const result = new CairoSecp256k1Point(1n).toBigInt();
12369
- * // result = 1n
12370
- * const result2 = new CairoSecp256k1Point(0, 0, 3, 0).toBigInt();
12371
- * // result2 = 3n
12418
+ * // G, the generator of the curve, as in the example of the class
12419
+ * const result = new CairoSecp256k1Point(G).toBigInt() === G;
12420
+ * // result = true
12421
+ * const result2 = new CairoSecp256k1Point(0, 0, 0, 0).toBigInt();
12422
+ * // result2 = 0n (0, 0), the point at infinity
12372
12423
  * ```
12373
12424
  */
12374
12425
  toBigInt() {
@@ -12381,8 +12432,10 @@ ${indent}}` : "}";
12381
12432
  * @returns {Secp256k1PointStruct} the limbs, each 0x-prefixed and unpadded
12382
12433
  * @example
12383
12434
  * ```typescript
12384
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toStruct();
12385
- * // result = { xLow: "0x1", xHigh: "0x2", yLow: "0x3", yHigh: "0x4" }
12435
+ * // G, the generator of the curve, as in the example of the class
12436
+ * const result = new CairoSecp256k1Point(G).toStruct();
12437
+ * // result = { xLow: "0x29bfcdb2dce28d959f2815b16f81798", xHigh: "0x79be667ef9dcbbac55a06295ce870b07",
12438
+ * // yLow: "0xfd17b448a68554199c47d08ffb10d4b8", yHigh: "0x483ada7726a3c4655da4fbfc0e1108a8" }
12386
12439
  * ```
12387
12440
  */
12388
12441
  toStruct() {
@@ -12398,8 +12451,8 @@ ${indent}}` : "}";
12398
12451
  * @returns {string} the 512-bit value as a 0x-prefixed hex string
12399
12452
  * @example
12400
12453
  * ```typescript
12401
- * const result = new CairoSecp256k1Point(4660n).toHexString();
12402
- * // result = "0x1234"
12454
+ * const result = new CairoSecp256k1Point(0n).toHexString();
12455
+ * // result = "0x0" (0, 0), the point at infinity
12403
12456
  * ```
12404
12457
  */
12405
12458
  toHexString() {
@@ -12410,8 +12463,10 @@ ${indent}}` : "}";
12410
12463
  * @returns {string[]} the limbs as decimal strings, `[xLow, xHigh, yLow, yHigh]`, flagged as compiled
12411
12464
  * @example
12412
12465
  * ```typescript
12413
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest();
12414
- * // result = ["1", "2", "3", "4"]
12466
+ * // G, the generator of the curve, as in the example of the class
12467
+ * const result = new CairoSecp256k1Point(G).toApiRequest();
12468
+ * // result = ["3468390537006497937951914270391801752", "161825202758953104525843685720298294023",
12469
+ * // "336417762351022071123394393598455764152", "96009999919712310848645357523629574312"]
12415
12470
  * ```
12416
12471
  */
12417
12472
  toApiRequest() {
@@ -12429,11 +12484,19 @@ ${indent}}` : "}";
12429
12484
  * read as a point whose upper limbs are zero. A longer one is refused rather than truncated.
12430
12485
  * @param {string} hexString the value, 0x-prefixed or not, at most 128 hex digits
12431
12486
  * @returns {CairoSecp256k1Point} the point the string spells
12432
- * @throws {Error} when the string holds more than 128 hex digits
12433
- * @example
12434
- * ```typescript
12435
- * const result = CairoSecp256k1Point.fromHex('0x1').toApiRequest();
12436
- * // result = ["0", "0", "1", "0"]
12487
+ * @throws {Error} when the string holds more than 128 hex digits, or when the point is not on the
12488
+ * curve
12489
+ * @example
12490
+ * ```typescript
12491
+ * // G, the generator of the curve
12492
+ * const result = CairoSecp256k1Point.fromHex(
12493
+ * '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'
12494
+ * ).xHigh;
12495
+ * // result = 0x79be667ef9dcbbac55a06295ce870b07n
12496
+ * const result2 = CairoSecp256k1Point.fromHex('0x0').toApiRequest();
12497
+ * // result2 = ["0", "0", "0", "0"] left-padded to 128 hex digits : (0, 0), the point at infinity
12498
+ * CairoSecp256k1Point.fromHex('0x1'); // x = 0, y = 1
12499
+ * // throws Error("Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it")
12437
12500
  * ```
12438
12501
  */
12439
12502
  static fromHex(hexString) {