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.js CHANGED
@@ -3929,7 +3929,7 @@ var CairoUint512 = class _CairoUint512 {
3929
3929
  * ```
3930
3930
  */
3931
3931
  static validateProps(limb0, limb1, limb2, limb3) {
3932
- const validateLimb = (limb, name) => {
3932
+ const validateLimb2 = (limb, name) => {
3933
3933
  assert(limb !== null, `${name} cannot be null`);
3934
3934
  assert(limb !== void 0, `${name} cannot be undefined`);
3935
3935
  assert(!isText(limb), `${name} cannot be built from text`);
@@ -3938,10 +3938,10 @@ var CairoUint512 = class _CairoUint512 {
3938
3938
  assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`);
3939
3939
  return BigInt(limb);
3940
3940
  };
3941
- const l0 = validateLimb(limb0, "limb0");
3942
- const l1 = validateLimb(limb1, "limb1");
3943
- const l2 = validateLimb(limb2, "limb2");
3944
- const l3 = validateLimb(limb3, "limb3");
3941
+ const l0 = validateLimb2(limb0, "limb0");
3942
+ const l1 = validateLimb2(limb1, "limb1");
3943
+ const l2 = validateLimb2(limb2, "limb2");
3944
+ const l3 = validateLimb2(limb3, "limb3");
3945
3945
  [l0, l1, l2, l3].forEach((value, index) => {
3946
3946
  assert(
3947
3947
  value >= UINT_128_MIN && value <= UINT_128_MAX,
@@ -6703,13 +6703,44 @@ var CairoBool = class _CairoBool {
6703
6703
  // src/utils/cairoDataTypes/secp256k1Point.ts
6704
6704
  var SECP256K1_POINT_MAX = (1n << 512n) - 1n;
6705
6705
  var SECP256K1_POINT_MIN = 0n;
6706
+ var SECP256K1_PRIME = 2n ** 256n - 2n ** 32n - 977n;
6707
+ function assertOnSecp256k1Curve(x, y) {
6708
+ if (x === 0n && y === 0n) {
6709
+ return;
6710
+ }
6711
+ assert(
6712
+ x < SECP256K1_PRIME,
6713
+ "Secp256k1Point: x must be smaller than p, the prime of the secp256k1 curve"
6714
+ );
6715
+ assert(
6716
+ y < SECP256K1_PRIME,
6717
+ "Secp256k1Point: y must be smaller than p, the prime of the secp256k1 curve"
6718
+ );
6719
+ assert(
6720
+ y * y % SECP256K1_PRIME === (x * x * x + 7n) % SECP256K1_PRIME,
6721
+ "Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it"
6722
+ );
6723
+ }
6724
+ function validateLimb(limb, name) {
6725
+ assert(limb !== null, `${name} cannot be null`);
6726
+ assert(limb !== void 0, `${name} cannot be undefined`);
6727
+ assert(!isText(limb), `${name} cannot be built from text`);
6728
+ assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
6729
+ assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
6730
+ assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`);
6731
+ const bigInt = BigInt(limb);
6732
+ assert(bigInt >= 0n, `${name} must be non-negative`);
6733
+ assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`);
6734
+ return bigInt;
6735
+ }
6706
6736
  var CairoSecp256k1Point = class _CairoSecp256k1Point {
6707
6737
  /**
6708
6738
  * The low 128 bits of the x coordinate.
6709
6739
  * @example
6710
6740
  * ```typescript
6711
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xLow;
6712
- * // result = 1n
6741
+ * // G, the generator of the curve, as in the example of the class
6742
+ * const result = new CairoSecp256k1Point(G).xLow;
6743
+ * // result = 0x29bfcdb2dce28d959f2815b16f81798n
6713
6744
  * ```
6714
6745
  */
6715
6746
  xLow;
@@ -6717,8 +6748,9 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6717
6748
  * The high 128 bits of the x coordinate.
6718
6749
  * @example
6719
6750
  * ```typescript
6720
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xHigh;
6721
- * // result = 2n
6751
+ * // G, the generator of the curve, as in the example of the class
6752
+ * const result = new CairoSecp256k1Point(G).xHigh;
6753
+ * // result = 0x79be667ef9dcbbac55a06295ce870b07n
6722
6754
  * ```
6723
6755
  */
6724
6756
  xHigh;
@@ -6726,8 +6758,9 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6726
6758
  * The low 128 bits of the y coordinate.
6727
6759
  * @example
6728
6760
  * ```typescript
6729
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yLow;
6730
- * // result = 3n
6761
+ * // G, the generator of the curve, as in the example of the class
6762
+ * const result = new CairoSecp256k1Point(G).yLow;
6763
+ * // result = 0xfd17b448a68554199c47d08ffb10d4b8n
6731
6764
  * ```
6732
6765
  */
6733
6766
  yLow;
@@ -6735,8 +6768,9 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6735
6768
  * The high 128 bits of the y coordinate.
6736
6769
  * @example
6737
6770
  * ```typescript
6738
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yHigh;
6739
- * // result = 4n
6771
+ * // G, the generator of the curve, as in the example of the class
6772
+ * const result = new CairoSecp256k1Point(G).yHigh;
6773
+ * // result = 0x483ada7726a3c4655da4fbfc0e1108a8n
6740
6774
  * ```
6741
6775
  */
6742
6776
  yHigh;
@@ -6753,14 +6787,14 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6753
6787
  * @param {any[]} arr either one value — a 512-bit number or a {@link Secp256k1PointStruct} — or
6754
6788
  * the four limbs
6755
6789
  * @throws {Error} when a value is null, undefined, of an unread type, text, a decimal number, or out
6756
- * of range — see {@link CairoSecp256k1Point.validate} and {@link CairoSecp256k1Point.validateProps}
6757
- * — or when the argument count is neither 1 nor 4
6790
+ * of range, or when the point is not on the curve — see {@link CairoSecp256k1Point.validate} and
6791
+ * {@link CairoSecp256k1Point.validateProps} — or when the argument count is neither 1 nor 4
6758
6792
  * @example
6759
6793
  * ```typescript
6760
- * const result = new CairoSecp256k1Point(1n).toApiRequest();
6761
- * // result = ["0", "0", "1", "0"]
6762
- * const result2 = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest();
6763
- * // result2 = ["1", "2", "3", "4"]
6794
+ * const result = new CairoSecp256k1Point(0n).toApiRequest();
6795
+ * // result = ["0", "0", "0", "0"] (0, 0), the point at infinity
6796
+ * new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 });
6797
+ * // 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")
6764
6798
  * ```
6765
6799
  */
6766
6800
  constructor(...arr) {
@@ -6795,17 +6829,22 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6795
6829
  }
6796
6830
  }
6797
6831
  /**
6798
- * Throw unless the value can be carried by a Secp256k1Point, and return it as a number.
6832
+ * Throw unless the value is a point that a contract accepts, and return it as a number.
6833
+ *
6834
+ * The value must fit in 512 bits, and the point it stands for must be on the curve, or be (0, 0),
6835
+ * the point at infinity.
6799
6836
  *
6800
6837
  * Unlike the other classes here this one gives the number back rather than returning nothing :
6801
6838
  * the constructor needs it, and computing it twice would mean splitting a 512-bit value twice.
6802
6839
  * @param {BigNumberish} input the 512-bit value to check
6803
6840
  * @returns {bigint} the value, once checked
6804
- * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or outside [0, 2^512 - 1]
6841
+ * @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
6805
6842
  * @example
6806
6843
  * ```typescript
6807
- * const result = CairoSecp256k1Point.validate('0x1234');
6808
- * // result = 4660n
6844
+ * const result = CairoSecp256k1Point.validate(0n);
6845
+ * // result = 0n (0, 0), the point at infinity
6846
+ * CairoSecp256k1Point.validate('0x1234');
6847
+ * // 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")
6809
6848
  * CairoSecp256k1Point.validate(SECP256K1_POINT_MAX + 1n);
6810
6849
  * // throws Error("input is bigger than SECP256K1_POINT_MAX")
6811
6850
  * CairoSecp256k1Point.validate('abc');
@@ -6830,20 +6869,31 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6830
6869
  const bigInt = BigInt(input);
6831
6870
  assert(bigInt >= SECP256K1_POINT_MIN, "input is smaller than SECP256K1_POINT_MIN");
6832
6871
  assert(bigInt <= SECP256K1_POINT_MAX, "input is bigger than SECP256K1_POINT_MAX");
6872
+ assertOnSecp256k1Curve(bigInt >> 256n, bigInt & UINT_256_MAX);
6833
6873
  return bigInt;
6834
6874
  }
6835
6875
  /**
6836
- * Throw unless the four limbs can each be carried by 128 bits, and return them as numbers.
6876
+ * Throw unless the four limbs each fit in 128 bits and together make a point that a contract
6877
+ * accepts, and return them as numbers.
6837
6878
  * @param {BigNumberish} xLow the low 128 bits of x
6838
6879
  * @param {BigNumberish} xHigh the high 128 bits of x
6839
6880
  * @param {BigNumberish} yLow the low 128 bits of y
6840
6881
  * @param {BigNumberish} yHigh the high 128 bits of y
6841
6882
  * @returns {{xLow: bigint, xHigh: bigint, yLow: bigint, yHigh: bigint}} the four limbs, checked
6842
- * @throws {Error} when a limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits
6883
+ * @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
6843
6884
  * @example
6844
6885
  * ```typescript
6845
- * const result = CairoSecp256k1Point.validateProps(1, 2, 3, 4);
6846
- * // result = { xLow: 1n, xHigh: 2n, yLow: 3n, yHigh: 4n }
6886
+ * // G, the generator of the curve
6887
+ * const result = CairoSecp256k1Point.validateProps(
6888
+ * '0x29bfcdb2dce28d959f2815b16f81798',
6889
+ * '0x79be667ef9dcbbac55a06295ce870b07',
6890
+ * '0xfd17b448a68554199c47d08ffb10d4b8',
6891
+ * '0x483ada7726a3c4655da4fbfc0e1108a8'
6892
+ * );
6893
+ * // result = { xLow: 0x29bfcdb2dce28d959f2815b16f81798n, xHigh: 0x79be667ef9dcbbac55a06295ce870b07n,
6894
+ * // yLow: 0xfd17b448a68554199c47d08ffb10d4b8n, yHigh: 0x483ada7726a3c4655da4fbfc0e1108a8n }
6895
+ * CairoSecp256k1Point.validateProps(1, 2, 3, 4);
6896
+ * // 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")
6847
6897
  * CairoSecp256k1Point.validateProps(1, 2, 3, 2n ** 128n);
6848
6898
  * // throws Error("yHigh must fit in 128 bits")
6849
6899
  * CairoSecp256k1Point.validateProps(1.5, 2, 3, 4);
@@ -6853,38 +6903,28 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6853
6903
  * ```
6854
6904
  */
6855
6905
  static validateProps(xLow, xHigh, yLow, yHigh) {
6856
- const validateLimb = (limb, name) => {
6857
- assert(limb !== null, `${name} cannot be null`);
6858
- assert(limb !== void 0, `${name} cannot be undefined`);
6859
- assert(!isText(limb), `${name} cannot be built from text`);
6860
- assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`);
6861
- assert(isBigNumberish(limb), `${name} must be a BigNumberish`);
6862
- assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`);
6863
- const bigInt = BigInt(limb);
6864
- assert(bigInt >= 0n, `${name} must be non-negative`);
6865
- assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`);
6866
- return bigInt;
6867
- };
6868
- return {
6906
+ const limbs = {
6869
6907
  xLow: validateLimb(xLow, "xLow"),
6870
6908
  xHigh: validateLimb(xHigh, "xHigh"),
6871
6909
  yLow: validateLimb(yLow, "yLow"),
6872
6910
  yHigh: validateLimb(yHigh, "yHigh")
6873
6911
  };
6912
+ assertOnSecp256k1Curve((limbs.xHigh << 128n) + limbs.xLow, (limbs.yHigh << 128n) + limbs.yLow);
6913
+ return limbs;
6874
6914
  }
6875
6915
  /**
6876
- * Can this value be carried by a Secp256k1Point?
6916
+ * Is this value a point that a contract accepts as a Secp256k1Point?
6877
6917
  *
6878
6918
  * The non-throwing form of {@link CairoSecp256k1Point.validate}, so it answers false for every
6879
6919
  * input that one refuses, whatever the reason.
6880
6920
  * @param {any} data the value to test
6881
- * @returns {boolean} true when the value fits in 512 bits
6921
+ * @returns {boolean} true when the value fits in 512 bits and stands for a point on the curve, or for (0, 0)
6882
6922
  * @example
6883
6923
  * ```typescript
6884
- * const result = CairoSecp256k1Point.is(SECP256K1_POINT_MAX);
6885
- * // result = true
6886
- * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX + 1n);
6887
- * // result2 = false
6924
+ * const result = CairoSecp256k1Point.is(0n);
6925
+ * // result = true (0, 0), the point at infinity
6926
+ * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX);
6927
+ * // result2 = false x is not smaller than p
6888
6928
  * ```
6889
6929
  */
6890
6930
  static is(data) {
@@ -6912,21 +6952,31 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6912
6952
  }
6913
6953
  /**
6914
6954
  * Read one point off a contract response, advancing the iterator past its four felts.
6955
+ *
6956
+ * A response is read as it comes : each felt must fit in 128 bits, but the point is not checked
6957
+ * against the curve. What a node answers is taken as correct, and a contract can only return a
6958
+ * point on the curve, or (0, 0).
6915
6959
  * @param {Iterator<string>} responseIterator the response felts, positioned on this point
6916
6960
  * @returns {CairoSecp256k1Point} the point that was read
6961
+ * @throws {Error} when the response ends before four felts, or when a felt does not fit in 128 bits
6917
6962
  * @example
6918
6963
  * ```typescript
6919
6964
  * const response = ['0x0', '0x0', '0x1', '0x0'];
6920
6965
  * const result = CairoSecp256k1Point.factoryFromApiResponse(response.values()).toBigInt();
6921
- * // result = 1n
6966
+ * // result = 1n not on the curve, and read all the same
6922
6967
  * ```
6923
6968
  */
6924
6969
  static factoryFromApiResponse(responseIterator) {
6925
- const xLow = getNext(responseIterator);
6926
- const xHigh = getNext(responseIterator);
6927
- const yLow = getNext(responseIterator);
6928
- const yHigh = getNext(responseIterator);
6929
- return new _CairoSecp256k1Point(xLow, xHigh, yLow, yHigh);
6970
+ const xLow = validateLimb(getNext(responseIterator), "xLow");
6971
+ const xHigh = validateLimb(getNext(responseIterator), "xHigh");
6972
+ const yLow = validateLimb(getNext(responseIterator), "yLow");
6973
+ const yHigh = validateLimb(getNext(responseIterator), "yHigh");
6974
+ const point = Object.create(_CairoSecp256k1Point.prototype);
6975
+ point.xLow = xLow;
6976
+ point.xHigh = xHigh;
6977
+ point.yLow = yLow;
6978
+ point.yHigh = yHigh;
6979
+ return point;
6930
6980
  }
6931
6981
  /**
6932
6982
  * The point as the single 512-bit number `x || y`.
@@ -6936,10 +6986,11 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6936
6986
  * @returns {bigint} the two coordinates concatenated, x in the upper 256 bits
6937
6987
  * @example
6938
6988
  * ```typescript
6939
- * const result = new CairoSecp256k1Point(1n).toBigInt();
6940
- * // result = 1n
6941
- * const result2 = new CairoSecp256k1Point(0, 0, 3, 0).toBigInt();
6942
- * // result2 = 3n
6989
+ * // G, the generator of the curve, as in the example of the class
6990
+ * const result = new CairoSecp256k1Point(G).toBigInt() === G;
6991
+ * // result = true
6992
+ * const result2 = new CairoSecp256k1Point(0, 0, 0, 0).toBigInt();
6993
+ * // result2 = 0n (0, 0), the point at infinity
6943
6994
  * ```
6944
6995
  */
6945
6996
  toBigInt() {
@@ -6952,8 +7003,10 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6952
7003
  * @returns {Secp256k1PointStruct} the limbs, each 0x-prefixed and unpadded
6953
7004
  * @example
6954
7005
  * ```typescript
6955
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toStruct();
6956
- * // result = { xLow: "0x1", xHigh: "0x2", yLow: "0x3", yHigh: "0x4" }
7006
+ * // G, the generator of the curve, as in the example of the class
7007
+ * const result = new CairoSecp256k1Point(G).toStruct();
7008
+ * // result = { xLow: "0x29bfcdb2dce28d959f2815b16f81798", xHigh: "0x79be667ef9dcbbac55a06295ce870b07",
7009
+ * // yLow: "0xfd17b448a68554199c47d08ffb10d4b8", yHigh: "0x483ada7726a3c4655da4fbfc0e1108a8" }
6957
7010
  * ```
6958
7011
  */
6959
7012
  toStruct() {
@@ -6969,8 +7022,8 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6969
7022
  * @returns {string} the 512-bit value as a 0x-prefixed hex string
6970
7023
  * @example
6971
7024
  * ```typescript
6972
- * const result = new CairoSecp256k1Point(4660n).toHexString();
6973
- * // result = "0x1234"
7025
+ * const result = new CairoSecp256k1Point(0n).toHexString();
7026
+ * // result = "0x0" (0, 0), the point at infinity
6974
7027
  * ```
6975
7028
  */
6976
7029
  toHexString() {
@@ -6981,8 +7034,10 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
6981
7034
  * @returns {string[]} the limbs as decimal strings, `[xLow, xHigh, yLow, yHigh]`, flagged as compiled
6982
7035
  * @example
6983
7036
  * ```typescript
6984
- * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest();
6985
- * // result = ["1", "2", "3", "4"]
7037
+ * // G, the generator of the curve, as in the example of the class
7038
+ * const result = new CairoSecp256k1Point(G).toApiRequest();
7039
+ * // result = ["3468390537006497937951914270391801752", "161825202758953104525843685720298294023",
7040
+ * // "336417762351022071123394393598455764152", "96009999919712310848645357523629574312"]
6986
7041
  * ```
6987
7042
  */
6988
7043
  toApiRequest() {
@@ -7000,11 +7055,19 @@ var CairoSecp256k1Point = class _CairoSecp256k1Point {
7000
7055
  * read as a point whose upper limbs are zero. A longer one is refused rather than truncated.
7001
7056
  * @param {string} hexString the value, 0x-prefixed or not, at most 128 hex digits
7002
7057
  * @returns {CairoSecp256k1Point} the point the string spells
7003
- * @throws {Error} when the string holds more than 128 hex digits
7004
- * @example
7005
- * ```typescript
7006
- * const result = CairoSecp256k1Point.fromHex('0x1').toApiRequest();
7007
- * // result = ["0", "0", "1", "0"]
7058
+ * @throws {Error} when the string holds more than 128 hex digits, or when the point is not on the
7059
+ * curve
7060
+ * @example
7061
+ * ```typescript
7062
+ * // G, the generator of the curve
7063
+ * const result = CairoSecp256k1Point.fromHex(
7064
+ * '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'
7065
+ * ).xHigh;
7066
+ * // result = 0x79be667ef9dcbbac55a06295ce870b07n
7067
+ * const result2 = CairoSecp256k1Point.fromHex('0x0').toApiRequest();
7068
+ * // result2 = ["0", "0", "0", "0"] left-padded to 128 hex digits : (0, 0), the point at infinity
7069
+ * CairoSecp256k1Point.fromHex('0x1'); // x = 0, y = 1
7070
+ * // 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")
7008
7071
  * ```
7009
7072
  */
7010
7073
  static fromHex(hexString) {