starknet 11.9.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.mjs CHANGED
@@ -3720,7 +3720,7 @@ var CairoUint512 = class _CairoUint512 {
3720
3720
  * ```
3721
3721
  */
3722
3722
  static validateProps(limb0, limb1, limb2, limb3) {
3723
- const validateLimb = (limb, name) => {
3723
+ const validateLimb2 = (limb, name) => {
3724
3724
  assert(limb !== null, `${name} cannot be null`);
3725
3725
  assert(limb !== void 0, `${name} cannot be undefined`);
3726
3726
  assert(!isText(limb), `${name} cannot be built from text`);
@@ -3729,10 +3729,10 @@ var CairoUint512 = class _CairoUint512 {
3729
3729
  assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`);
3730
3730
  return BigInt(limb);
3731
3731
  };
3732
- const l0 = validateLimb(limb0, "limb0");
3733
- const l1 = validateLimb(limb1, "limb1");
3734
- const l2 = validateLimb(limb2, "limb2");
3735
- const l3 = validateLimb(limb3, "limb3");
3732
+ const l0 = validateLimb2(limb0, "limb0");
3733
+ const l1 = validateLimb2(limb1, "limb1");
3734
+ const l2 = validateLimb2(limb2, "limb2");
3735
+ const l3 = validateLimb2(limb3, "limb3");
3736
3736
  [l0, l1, l2, l3].forEach((value, index) => {
3737
3737
  assert(
3738
3738
  value >= UINT_128_MIN && value <= UINT_128_MAX,
@@ -6494,13 +6494,44 @@ var CairoBool = class _CairoBool {
6494
6494
  // src/utils/cairoDataTypes/secp256k1Point.ts
6495
6495
  var SECP256K1_POINT_MAX = (1n << 512n) - 1n;
6496
6496
  var SECP256K1_POINT_MIN = 0n;
6497
+ var SECP256K1_PRIME = 2n ** 256n - 2n ** 32n - 977n;
6498
+ function assertOnSecp256k1Curve(x, y) {
6499
+ if (x === 0n && y === 0n) {
6500
+ return;
6501
+ }
6502
+ assert(
6503
+ x < SECP256K1_PRIME,
6504
+ "Secp256k1Point: x must be smaller than p, the prime of the secp256k1 curve"
6505
+ );
6506
+ assert(
6507
+ y < SECP256K1_PRIME,
6508
+ "Secp256k1Point: y must be smaller than p, the prime of the secp256k1 curve"
6509
+ );
6510
+ assert(
6511
+ y * y % SECP256K1_PRIME === (x * x * x + 7n) % SECP256K1_PRIME,
6512
+ "Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it"
6513
+ );
6514
+ }
6515
+ function validateLimb(limb, name) {
6516
+ assert(limb !== null, `${name} cannot be null`);
6517
+ assert(limb !== void 0, `${name} cannot be undefined`);
6518
+ assert(!isText(limb), `${name} cannot be built from text`);
6519
+ assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
6520
+ assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
6521
+ assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`);
6522
+ const bigInt = BigInt(limb);
6523
+ assert(bigInt >= 0n, `${name} must be non-negative`);
6524
+ assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`);
6525
+ return bigInt;
6526
+ }
6497
6527
  var CairoSecp256k1Point = class _CairoSecp256k1Point {
6498
6528
  /**
6499
6529
  * The low 128 bits of the x coordinate.
6500
6530
  * @example
6501
6531
  * ```typescript
6502
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xLow;
6503
- * // result = 1n
6532
+ * // G, the generator of the curve, as in the example of the class
6533
+ * const result = new CairoSecp256k1Point(G).xLow;
6534
+ * // result = 0x29bfcdb2dce28d959f2815b16f81798n
6504
6535
  * ```
6505
6536
  */
6506
6537
  xLow;
@@ -6508,8 +6539,9 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6508
6539
  * The high 128 bits of the x coordinate.
6509
6540
  * @example
6510
6541
  * ```typescript
6511
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xHigh;
6512
- * // result = 2n
6542
+ * // G, the generator of the curve, as in the example of the class
6543
+ * const result = new CairoSecp256k1Point(G).xHigh;
6544
+ * // result = 0x79be667ef9dcbbac55a06295ce870b07n
6513
6545
  * ```
6514
6546
  */
6515
6547
  xHigh;
@@ -6517,8 +6549,9 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6517
6549
  * The low 128 bits of the y coordinate.
6518
6550
  * @example
6519
6551
  * ```typescript
6520
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yLow;
6521
- * // result = 3n
6552
+ * // G, the generator of the curve, as in the example of the class
6553
+ * const result = new CairoSecp256k1Point(G).yLow;
6554
+ * // result = 0xfd17b448a68554199c47d08ffb10d4b8n
6522
6555
  * ```
6523
6556
  */
6524
6557
  yLow;
@@ -6526,8 +6559,9 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6526
6559
  * The high 128 bits of the y coordinate.
6527
6560
  * @example
6528
6561
  * ```typescript
6529
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yHigh;
6530
- * // result = 4n
6562
+ * // G, the generator of the curve, as in the example of the class
6563
+ * const result = new CairoSecp256k1Point(G).yHigh;
6564
+ * // result = 0x483ada7726a3c4655da4fbfc0e1108a8n
6531
6565
  * ```
6532
6566
  */
6533
6567
  yHigh;
@@ -6544,14 +6578,14 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6544
6578
  * @param {any[]} arr either one value — a 512-bit number or a {@link Secp256k1PointStruct} — or
6545
6579
  * the four limbs
6546
6580
  * @throws {Error} when a value is null, undefined, of an unread type, text, a decimal number, or out
6547
- * of range — see {@link CairoSecp256k1Point.validate} and {@link CairoSecp256k1Point.validateProps}
6548
- * — or when the argument count is neither 1 nor 4
6581
+ * of range, or when the point is not on the curve — see {@link CairoSecp256k1Point.validate} and
6582
+ * {@link CairoSecp256k1Point.validateProps} — or when the argument count is neither 1 nor 4
6549
6583
  * @example
6550
6584
  * ```typescript
6551
- * const result = new CairoSecp256k1Point(1n).toApiRequest();
6552
- * // result = ["0", "0", "1", "0"]
6553
- * const result2 = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest();
6554
- * // result2 = ["1", "2", "3", "4"]
6585
+ * const result = new CairoSecp256k1Point(0n).toApiRequest();
6586
+ * // result = ["0", "0", "0", "0"] (0, 0), the point at infinity
6587
+ * new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 });
6588
+ * // 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")
6555
6589
  * ```
6556
6590
  */
6557
6591
  constructor(...arr) {
@@ -6586,17 +6620,22 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6586
6620
  }
6587
6621
  }
6588
6622
  /**
6589
- * Throw unless the value can be carried by a Secp256k1Point, and return it as a number.
6623
+ * Throw unless the value is a point that a contract accepts, and return it as a number.
6624
+ *
6625
+ * The value must fit in 512 bits, and the point it stands for must be on the curve, or be (0, 0),
6626
+ * the point at infinity.
6590
6627
  *
6591
6628
  * Unlike the other classes here this one gives the number back rather than returning nothing :
6592
6629
  * the constructor needs it, and computing it twice would mean splitting a 512-bit value twice.
6593
6630
  * @param {BigNumberish} input the 512-bit value to check
6594
6631
  * @returns {bigint} the value, once checked
6595
- * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or outside [0, 2^512 - 1]
6632
+ * @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
6596
6633
  * @example
6597
6634
  * ```typescript
6598
- * const result = CairoSecp256k1Point.validate('0x1234');
6599
- * // result = 4660n
6635
+ * const result = CairoSecp256k1Point.validate(0n);
6636
+ * // result = 0n (0, 0), the point at infinity
6637
+ * CairoSecp256k1Point.validate('0x1234');
6638
+ * // 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")
6600
6639
  * CairoSecp256k1Point.validate(SECP256K1_POINT_MAX + 1n);
6601
6640
  * // throws Error("input is bigger than SECP256K1_POINT_MAX")
6602
6641
  * CairoSecp256k1Point.validate('abc');
@@ -6621,20 +6660,31 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6621
6660
  const bigInt = BigInt(input);
6622
6661
  assert(bigInt >= SECP256K1_POINT_MIN, "input is smaller than SECP256K1_POINT_MIN");
6623
6662
  assert(bigInt <= SECP256K1_POINT_MAX, "input is bigger than SECP256K1_POINT_MAX");
6663
+ assertOnSecp256k1Curve(bigInt >> 256n, bigInt & UINT_256_MAX);
6624
6664
  return bigInt;
6625
6665
  }
6626
6666
  /**
6627
- * Throw unless the four limbs can each be carried by 128 bits, and return them as numbers.
6667
+ * Throw unless the four limbs each fit in 128 bits and together make a point that a contract
6668
+ * accepts, and return them as numbers.
6628
6669
  * @param {BigNumberish} xLow the low 128 bits of x
6629
6670
  * @param {BigNumberish} xHigh the high 128 bits of x
6630
6671
  * @param {BigNumberish} yLow the low 128 bits of y
6631
6672
  * @param {BigNumberish} yHigh the high 128 bits of y
6632
6673
  * @returns {{xLow: bigint, xHigh: bigint, yLow: bigint, yHigh: bigint}} the four limbs, checked
6633
- * @throws {Error} when a limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits
6674
+ * @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
6634
6675
  * @example
6635
6676
  * ```typescript
6636
- * const result = CairoSecp256k1Point.validateProps(1, 2, 3, 4);
6637
- * // result = { xLow: 1n, xHigh: 2n, yLow: 3n, yHigh: 4n }
6677
+ * // G, the generator of the curve
6678
+ * const result = CairoSecp256k1Point.validateProps(
6679
+ * '0x29bfcdb2dce28d959f2815b16f81798',
6680
+ * '0x79be667ef9dcbbac55a06295ce870b07',
6681
+ * '0xfd17b448a68554199c47d08ffb10d4b8',
6682
+ * '0x483ada7726a3c4655da4fbfc0e1108a8'
6683
+ * );
6684
+ * // result = { xLow: 0x29bfcdb2dce28d959f2815b16f81798n, xHigh: 0x79be667ef9dcbbac55a06295ce870b07n,
6685
+ * // yLow: 0xfd17b448a68554199c47d08ffb10d4b8n, yHigh: 0x483ada7726a3c4655da4fbfc0e1108a8n }
6686
+ * CairoSecp256k1Point.validateProps(1, 2, 3, 4);
6687
+ * // 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")
6638
6688
  * CairoSecp256k1Point.validateProps(1, 2, 3, 2n ** 128n);
6639
6689
  * // throws Error("yHigh must fit in 128 bits")
6640
6690
  * CairoSecp256k1Point.validateProps(1.5, 2, 3, 4);
@@ -6644,38 +6694,28 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6644
6694
  * ```
6645
6695
  */
6646
6696
  static validateProps(xLow, xHigh, yLow, yHigh) {
6647
- const validateLimb = (limb, name) => {
6648
- assert(limb !== null, `${name} cannot be null`);
6649
- assert(limb !== void 0, `${name} cannot be undefined`);
6650
- assert(!isText(limb), `${name} cannot be built from text`);
6651
- assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
6652
- assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
6653
- assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`);
6654
- const bigInt = BigInt(limb);
6655
- assert(bigInt >= 0n, `${name} must be non-negative`);
6656
- assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`);
6657
- return bigInt;
6658
- };
6659
- return {
6697
+ const limbs = {
6660
6698
  xLow: validateLimb(xLow, "xLow"),
6661
6699
  xHigh: validateLimb(xHigh, "xHigh"),
6662
6700
  yLow: validateLimb(yLow, "yLow"),
6663
6701
  yHigh: validateLimb(yHigh, "yHigh")
6664
6702
  };
6703
+ assertOnSecp256k1Curve((limbs.xHigh << 128n) + limbs.xLow, (limbs.yHigh << 128n) + limbs.yLow);
6704
+ return limbs;
6665
6705
  }
6666
6706
  /**
6667
- * Can this value be carried by a Secp256k1Point?
6707
+ * Is this value a point that a contract accepts as a Secp256k1Point?
6668
6708
  *
6669
6709
  * The non-throwing form of {@link CairoSecp256k1Point.validate}, so it answers false for every
6670
6710
  * input that one refuses, whatever the reason.
6671
6711
  * @param {any} data the value to test
6672
- * @returns {boolean} true when the value fits in 512 bits
6712
+ * @returns {boolean} true when the value fits in 512 bits and stands for a point on the curve, or for (0, 0)
6673
6713
  * @example
6674
6714
  * ```typescript
6675
- * const result = CairoSecp256k1Point.is(SECP256K1_POINT_MAX);
6676
- * // result = true
6677
- * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX + 1n);
6678
- * // result2 = false
6715
+ * const result = CairoSecp256k1Point.is(0n);
6716
+ * // result = true (0, 0), the point at infinity
6717
+ * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX);
6718
+ * // result2 = false x is not smaller than p
6679
6719
  * ```
6680
6720
  */
6681
6721
  static is(data) {
@@ -6703,21 +6743,31 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6703
6743
  }
6704
6744
  /**
6705
6745
  * Read one point off a contract response, advancing the iterator past its four felts.
6746
+ *
6747
+ * A response is read as it comes : each felt must fit in 128 bits, but the point is not checked
6748
+ * against the curve. What a node answers is taken as correct, and a contract can only return a
6749
+ * point on the curve, or (0, 0).
6706
6750
  * @param {Iterator<string>} responseIterator the response felts, positioned on this point
6707
6751
  * @returns {CairoSecp256k1Point} the point that was read
6752
+ * @throws {Error} when the response ends before four felts, or when a felt does not fit in 128 bits
6708
6753
  * @example
6709
6754
  * ```typescript
6710
6755
  * const response = ['0x0', '0x0', '0x1', '0x0'];
6711
6756
  * const result = CairoSecp256k1Point.factoryFromApiResponse(response.values()).toBigInt();
6712
- * // result = 1n
6757
+ * // result = 1n not on the curve, and read all the same
6713
6758
  * ```
6714
6759
  */
6715
6760
  static factoryFromApiResponse(responseIterator) {
6716
- const xLow = getNext(responseIterator);
6717
- const xHigh = getNext(responseIterator);
6718
- const yLow = getNext(responseIterator);
6719
- const yHigh = getNext(responseIterator);
6720
- return new _CairoSecp256k1Point(xLow, xHigh, yLow, yHigh);
6761
+ const xLow = validateLimb(getNext(responseIterator), "xLow");
6762
+ const xHigh = validateLimb(getNext(responseIterator), "xHigh");
6763
+ const yLow = validateLimb(getNext(responseIterator), "yLow");
6764
+ const yHigh = validateLimb(getNext(responseIterator), "yHigh");
6765
+ const point = Object.create(_CairoSecp256k1Point.prototype);
6766
+ point.xLow = xLow;
6767
+ point.xHigh = xHigh;
6768
+ point.yLow = yLow;
6769
+ point.yHigh = yHigh;
6770
+ return point;
6721
6771
  }
6722
6772
  /**
6723
6773
  * The point as the single 512-bit number `x || y`.
@@ -6727,10 +6777,11 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6727
6777
  * @returns {bigint} the two coordinates concatenated, x in the upper 256 bits
6728
6778
  * @example
6729
6779
  * ```typescript
6730
- * const result = new CairoSecp256k1Point(1n).toBigInt();
6731
- * // result = 1n
6732
- * const result2 = new CairoSecp256k1Point(0, 0, 3, 0).toBigInt();
6733
- * // result2 = 3n
6780
+ * // G, the generator of the curve, as in the example of the class
6781
+ * const result = new CairoSecp256k1Point(G).toBigInt() === G;
6782
+ * // result = true
6783
+ * const result2 = new CairoSecp256k1Point(0, 0, 0, 0).toBigInt();
6784
+ * // result2 = 0n (0, 0), the point at infinity
6734
6785
  * ```
6735
6786
  */
6736
6787
  toBigInt() {
@@ -6743,8 +6794,10 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6743
6794
  * @returns {Secp256k1PointStruct} the limbs, each 0x-prefixed and unpadded
6744
6795
  * @example
6745
6796
  * ```typescript
6746
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toStruct();
6747
- * // result = { xLow: "0x1", xHigh: "0x2", yLow: "0x3", yHigh: "0x4" }
6797
+ * // G, the generator of the curve, as in the example of the class
6798
+ * const result = new CairoSecp256k1Point(G).toStruct();
6799
+ * // result = { xLow: "0x29bfcdb2dce28d959f2815b16f81798", xHigh: "0x79be667ef9dcbbac55a06295ce870b07",
6800
+ * // yLow: "0xfd17b448a68554199c47d08ffb10d4b8", yHigh: "0x483ada7726a3c4655da4fbfc0e1108a8" }
6748
6801
  * ```
6749
6802
  */
6750
6803
  toStruct() {
@@ -6760,8 +6813,8 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6760
6813
  * @returns {string} the 512-bit value as a 0x-prefixed hex string
6761
6814
  * @example
6762
6815
  * ```typescript
6763
- * const result = new CairoSecp256k1Point(4660n).toHexString();
6764
- * // result = "0x1234"
6816
+ * const result = new CairoSecp256k1Point(0n).toHexString();
6817
+ * // result = "0x0" (0, 0), the point at infinity
6765
6818
  * ```
6766
6819
  */
6767
6820
  toHexString() {
@@ -6772,8 +6825,10 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6772
6825
  * @returns {string[]} the limbs as decimal strings, `[xLow, xHigh, yLow, yHigh]`, flagged as compiled
6773
6826
  * @example
6774
6827
  * ```typescript
6775
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest();
6776
- * // result = ["1", "2", "3", "4"]
6828
+ * // G, the generator of the curve, as in the example of the class
6829
+ * const result = new CairoSecp256k1Point(G).toApiRequest();
6830
+ * // result = ["3468390537006497937951914270391801752", "161825202758953104525843685720298294023",
6831
+ * // "336417762351022071123394393598455764152", "96009999919712310848645357523629574312"]
6777
6832
  * ```
6778
6833
  */
6779
6834
  toApiRequest() {
@@ -6791,11 +6846,19 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6791
6846
  * read as a point whose upper limbs are zero. A longer one is refused rather than truncated.
6792
6847
  * @param {string} hexString the value, 0x-prefixed or not, at most 128 hex digits
6793
6848
  * @returns {CairoSecp256k1Point} the point the string spells
6794
- * @throws {Error} when the string holds more than 128 hex digits
6795
- * @example
6796
- * ```typescript
6797
- * const result = CairoSecp256k1Point.fromHex('0x1').toApiRequest();
6798
- * // result = ["0", "0", "1", "0"]
6849
+ * @throws {Error} when the string holds more than 128 hex digits, or when the point is not on the
6850
+ * curve
6851
+ * @example
6852
+ * ```typescript
6853
+ * // G, the generator of the curve
6854
+ * const result = CairoSecp256k1Point.fromHex(
6855
+ * '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'
6856
+ * ).xHigh;
6857
+ * // result = 0x79be667ef9dcbbac55a06295ce870b07n
6858
+ * const result2 = CairoSecp256k1Point.fromHex('0x0').toApiRequest();
6859
+ * // result2 = ["0", "0", "0", "0"] left-padded to 128 hex digits : (0, 0), the point at infinity
6860
+ * CairoSecp256k1Point.fromHex('0x1'); // x = 0, y = 1
6861
+ * // 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")
6799
6862
  * ```
6800
6863
  */
6801
6864
  static fromHex(hexString) {