From e13cbee3ba27d06e4a38433ab0f2a6e123bdc792 Mon Sep 17 00:00:00 2001 From: PhilippeR26 Date: Tue, 24 Nov 2026 12:40:56 +0100 Subject: [PATCH 1/2] fix(calldata): refuse a Secp256k1Point that is not on the secp256k1 curve A point given by the caller is now checked as soon as it is built: it must be on the curve y^2 = x^3 + 7 modulo p with coordinates smaller than p, or be (0, 0), the point at infinity. A contract refuses any other point when it reads its parameters, so a deployment with such a key failed on the node, after the class was declared and paid. A point read from a node response is still taken as it comes. --- .../CairoSecp256k1Point.test.ts | 229 ++++++++++----- .../calldata/parser/parsingStrategy.test.ts | 20 +- __tests__/utils/contract.test.ts | 72 +++++ __tests__/utils/secp256k1Point.test.ts | 12 + src/utils/cairoDataTypes/secp256k1Point.ts | 267 +++++++++++++----- 5 files changed, 463 insertions(+), 137 deletions(-) diff --git a/__tests__/utils/cairoDataTypes/CairoSecp256k1Point.test.ts b/__tests__/utils/cairoDataTypes/CairoSecp256k1Point.test.ts index 16a79ec3f..8bc319598 100644 --- a/__tests__/utils/cairoDataTypes/CairoSecp256k1Point.test.ts +++ b/__tests__/utils/cairoDataTypes/CairoSecp256k1Point.test.ts @@ -5,35 +5,54 @@ import { Literal, } from '../../../src'; -// a full-width 512-bit value, x || y, as an uncompressed public key reads once its 04 prefix is gone -const PUB_KEY = BigInt(`0x${'1a'.repeat(64)}`); - -/** Recompose the 512-bit value the four limbs stand for, x in the upper 256 bits. */ -const limbsToValue = (xLow: bigint, xHigh: bigint, yLow: bigint, yHigh: bigint) => - (xHigh * 2n ** 128n + xLow) * 2n ** 256n + yHigh * 2n ** 128n + yLow; +// G, the generator of the secp256k1 curve, as one 512-bit number x || y, then as its four limbs +const G = + 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n; +const G_X_LOW = 0x029bfcdb2dce28d959f2815b16f81798n; +const G_X_HIGH = 0x79be667ef9dcbbac55a06295ce870b07n; +const G_Y_LOW = 0xfd17b448a68554199c47d08ffb10d4b8n; +const G_Y_HIGH = 0x483ada7726a3c4655da4fbfc0e1108a8n; + +// an Ethereum public key, x || y, as an uncompressed key reads once its 04 prefix is gone. It is the +// key of __tests__/utils/secp256k1Point.test.ts, where its four limbs are written out too. +const PUB_KEY = + 0x8c7aea7d673a5858bdca128d124fb0765cceb2c16f198f4c14b328aa571331e6f6c87f51d5224d73d118765cb19d7565212f80be5048bff926ba791c17541c92n; + +// p, the prime of the secp256k1 curve +const P = 2n ** 256n - 2n ** 32n - 977n; + +// Two points on the curve with one small coordinate, computed with @noble/curves and checked with +// y^2 = x^3 + 7 modulo p : (1, Y_OF_X_1) and (X_OF_Y_1, 1). Adding p to the small coordinate still +// fits in 256 bits, which gives a coordinate that is right modulo p but not smaller than p. +const Y_OF_X_1 = 0x4218f20ae6c646b363db68605822fb14264ca8d2587fdd6fbc750d587e76a7een; +const X_OF_Y_1 = 0x1fe1e5ef3fceb5c135ab7741333ce5a6e80d68167653f6b2b24bcbcfaaaff507n; + +const NOT_ON_CURVE = + 'Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it'; +const X_NOT_BELOW_P = 'Secp256k1Point: x must be smaller than p, the prime of the secp256k1 curve'; +const Y_NOT_BELOW_P = 'Secp256k1Point: y must be smaller than p, the prime of the secp256k1 curve'; describe('CairoSecp256k1Point class Unit Tests', () => { describe('constructor from a single 512-bit value', () => { test('should split x into the upper 256 bits and y into the lower ones', () => { - const point = new CairoSecp256k1Point(1n); - expect(point.xLow).toBe(0n); - expect(point.xHigh).toBe(0n); - expect(point.yLow).toBe(1n); - expect(point.yHigh).toBe(0n); + const point = new CairoSecp256k1Point(G); + expect(point.xLow).toBe(G_X_LOW); + expect(point.xHigh).toBe(G_X_HIGH); + expect(point.yLow).toBe(G_Y_LOW); + expect(point.yHigh).toBe(G_Y_HIGH); }); test('should split each coordinate into its two limbs', () => { - const value = limbsToValue(1n, 2n, 3n, 4n); - const point = new CairoSecp256k1Point(value); - expect(point.xLow).toBe(1n); - expect(point.xHigh).toBe(2n); - expect(point.yLow).toBe(3n); - expect(point.yHigh).toBe(4n); + const point = new CairoSecp256k1Point(PUB_KEY); + expect(point.xLow).toBe(0x5cceb2c16f198f4c14b328aa571331e6n); + expect(point.xHigh).toBe(0x8c7aea7d673a5858bdca128d124fb076n); + expect(point.yLow).toBe(0x212f80be5048bff926ba791c17541c92n); + expect(point.yHigh).toBe(0xf6c87f51d5224d73d118765cb19d7565n); }); test('should accept a hexadecimal or decimal string', () => { - expect(new CairoSecp256k1Point('0x1234').toBigInt()).toBe(4660n); - expect(new CairoSecp256k1Point('4660').toBigInt()).toBe(4660n); + expect(new CairoSecp256k1Point(`0x${G.toString(16)}`).toBigInt()).toBe(G); + expect(new CairoSecp256k1Point(G.toString(10)).toBigInt()).toBe(G); }); test('should carry a full-width key unchanged', () => { @@ -43,36 +62,41 @@ describe('CairoSecp256k1Point class Unit Tests', () => { describe('constructor from the four limbs', () => { test('should keep the limbs in the order a call carries them', () => { - const point = new CairoSecp256k1Point(1, 2, 3, 4); - expect(point.xLow).toBe(1n); - expect(point.xHigh).toBe(2n); - expect(point.yLow).toBe(3n); - expect(point.yHigh).toBe(4n); + const point = new CairoSecp256k1Point(G_X_LOW, G_X_HIGH, G_Y_LOW, G_Y_HIGH); + expect(point.xLow).toBe(G_X_LOW); + expect(point.xHigh).toBe(G_X_HIGH); + expect(point.yLow).toBe(G_Y_LOW); + expect(point.yHigh).toBe(G_Y_HIGH); }); test('should agree with the single-value form', () => { - expect(new CairoSecp256k1Point(0, 0, 1, 0).toBigInt()).toBe(1n); - expect(new CairoSecp256k1Point(0, 0, 3, 0).toBigInt()).toBe(3n); + expect(new CairoSecp256k1Point(G_X_LOW, G_X_HIGH, G_Y_LOW, G_Y_HIGH).toBigInt()).toBe(G); + expect(new CairoSecp256k1Point(0, 0, 0, 0).toBigInt()).toBe(0n); }); }); describe('constructor from a Secp256k1PointStruct', () => { test('should read the four named limbs', () => { - const point = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }); - expect(point.xLow).toBe(1n); - expect(point.xHigh).toBe(2n); - expect(point.yLow).toBe(3n); - expect(point.yHigh).toBe(4n); + const point = new CairoSecp256k1Point({ + xLow: G_X_LOW, + xHigh: G_X_HIGH, + yLow: G_Y_LOW, + yHigh: G_Y_HIGH, + }); + expect(point.xLow).toBe(G_X_LOW); + expect(point.xHigh).toBe(G_X_HIGH); + expect(point.yLow).toBe(G_Y_LOW); + expect(point.yHigh).toBe(G_Y_HIGH); }); test('should accept hexadecimal limbs', () => { const point = new CairoSecp256k1Point({ - xLow: '0x1', - xHigh: '0x2', - yLow: '0x3', - yHigh: '0x4', + xLow: '0x29bfcdb2dce28d959f2815b16f81798', + xHigh: '0x79be667ef9dcbbac55a06295ce870b07', + yLow: '0xfd17b448a68554199c47d08ffb10d4b8', + yHigh: '0x483ada7726a3c4655da4fbfc0e1108a8', }); - expect(point.toBigInt()).toBe(limbsToValue(1n, 2n, 3n, 4n)); + expect(point.toBigInt()).toBe(G); }); test('should refuse an object that is not one, reading it as a value instead', () => { @@ -84,9 +108,12 @@ describe('CairoSecp256k1Point class Unit Tests', () => { }); describe('range', () => { - test('should accept the whole 512-bit range', () => { - expect(() => new CairoSecp256k1Point(SECP256K1_POINT_MIN)).not.toThrow(); - expect(new CairoSecp256k1Point(SECP256K1_POINT_MAX).toBigInt()).toBe(SECP256K1_POINT_MAX); + test('should accept the smallest value, which is (0, 0), the point at infinity', () => { + expect(new CairoSecp256k1Point(SECP256K1_POINT_MIN).toBigInt()).toBe(0n); + }); + + test('should refuse the largest 512-bit value, since its x is not smaller than p', () => { + expect(() => new CairoSecp256k1Point(SECP256K1_POINT_MAX)).toThrow(X_NOT_BELOW_P); }); test('should reject a value one bit too wide', () => { @@ -159,11 +186,18 @@ describe('CairoSecp256k1Point class Unit Tests', () => { describe('validateProps static method', () => { test('should return the four limbs as bigints', () => { - expect(CairoSecp256k1Point.validateProps(1, 2, 3, 4)).toEqual({ - xLow: 1n, - xHigh: 2n, - yLow: 3n, - yHigh: 4n, + expect( + CairoSecp256k1Point.validateProps( + '0x29bfcdb2dce28d959f2815b16f81798', + '0x79be667ef9dcbbac55a06295ce870b07', + '0xfd17b448a68554199c47d08ffb10d4b8', + '0x483ada7726a3c4655da4fbfc0e1108a8' + ) + ).toEqual({ + xLow: G_X_LOW, + xHigh: G_X_HIGH, + yLow: G_Y_LOW, + yHigh: G_Y_HIGH, }); }); @@ -208,8 +242,8 @@ describe('CairoSecp256k1Point class Unit Tests', () => { describe('validate static method', () => { test('should return the checked value', () => { - expect(CairoSecp256k1Point.validate('0x1234')).toBe(4660n); - expect(CairoSecp256k1Point.validate(SECP256K1_POINT_MAX)).toBe(SECP256K1_POINT_MAX); + expect(CairoSecp256k1Point.validate(`0x${G.toString(16)}`)).toBe(G); + expect(CairoSecp256k1Point.validate(0n)).toBe(0n); }); test('should throw outside the range', () => { @@ -220,13 +254,15 @@ describe('CairoSecp256k1Point class Unit Tests', () => { }); describe('is static method', () => { - test('should return true for a value that fits in 512 bits', () => { + test('should return true for a point on the curve, and for (0, 0)', () => { expect(CairoSecp256k1Point.is(0)).toBe(true); + expect(CairoSecp256k1Point.is(G)).toBe(true); expect(CairoSecp256k1Point.is(PUB_KEY)).toBe(true); - expect(CairoSecp256k1Point.is(SECP256K1_POINT_MAX)).toBe(true); }); test('should return false for anything this class refuses', () => { + expect(CairoSecp256k1Point.is(1n)).toBe(false); + expect(CairoSecp256k1Point.is(SECP256K1_POINT_MAX)).toBe(false); expect(CairoSecp256k1Point.is(SECP256K1_POINT_MAX + 1n)).toBe(false); expect(CairoSecp256k1Point.is(-1)).toBe(false); expect(CairoSecp256k1Point.is('abc')).toBe(false); @@ -248,14 +284,17 @@ describe('CairoSecp256k1Point class Unit Tests', () => { describe('toApiRequest method', () => { test('should return the four limbs as decimal strings', () => { - expect([ - ...new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest(), - ]).toEqual(['1', '2', '3', '4']); - expect([...new CairoSecp256k1Point(1n).toApiRequest()]).toEqual(['0', '0', '1', '0']); + expect([...new CairoSecp256k1Point(G).toApiRequest()]).toEqual([ + '3468390537006497937951914270391801752', + '161825202758953104525843685720298294023', + '336417762351022071123394393598455764152', + '96009999919712310848645357523629574312', + ]); + expect([...new CairoSecp256k1Point(0n).toApiRequest()]).toEqual(['0', '0', '0', '0']); }); test('should flag the result as compiled', () => { - const request = new CairoSecp256k1Point(1n).toApiRequest(); + const request = new CairoSecp256k1Point(G).toApiRequest(); expect(request).toHaveProperty('__compiled__', true); expect(Object.keys(request)).toEqual(['0', '1', '2', '3']); }); @@ -263,32 +302,39 @@ describe('CairoSecp256k1Point class Unit Tests', () => { describe('toStruct and toHexString methods', () => { test('should return the limbs as unpadded hexadecimal strings', () => { - expect(new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toStruct()).toEqual({ - xLow: '0x1', - xHigh: '0x2', - yLow: '0x3', - yHigh: '0x4', + expect(new CairoSecp256k1Point(G).toStruct()).toEqual({ + xLow: '0x29bfcdb2dce28d959f2815b16f81798', + xHigh: '0x79be667ef9dcbbac55a06295ce870b07', + yLow: '0xfd17b448a68554199c47d08ffb10d4b8', + yHigh: '0x483ada7726a3c4655da4fbfc0e1108a8', }); }); test('should return the whole point as one hexadecimal string', () => { - expect(new CairoSecp256k1Point(4660n).toHexString()).toBe('0x1234'); + expect(new CairoSecp256k1Point(G).toHexString()).toBe( + '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8' + ); + expect(new CairoSecp256k1Point(0n).toHexString()).toBe('0x0'); }); }); describe('fromHex static method', () => { test('should left-pad a short string to 128 hex digits', () => { - expect([...CairoSecp256k1Point.fromHex('0x1').toApiRequest()]).toEqual(['0', '0', '1', '0']); + // 65 hex digits : x = 1, then the 64 digits of y + const point = CairoSecp256k1Point.fromHex(`0x1${Y_OF_X_1.toString(16).padStart(64, '0')}`); + expect(point.xLow).toBe(1n); + expect(point.xHigh).toBe(0n); + expect(point.toBigInt()).toBe(2n ** 256n + Y_OF_X_1); expect(CairoSecp256k1Point.fromHex('0x').toBigInt()).toBe(0n); }); test('should read a full-width key', () => { - expect(CairoSecp256k1Point.fromHex(`0x${'1a'.repeat(64)}`).toBigInt()).toBe(PUB_KEY); + expect(CairoSecp256k1Point.fromHex(`0x${PUB_KEY.toString(16)}`).toBigInt()).toBe(PUB_KEY); }); test('should agree with the single-value constructor', () => { - expect(CairoSecp256k1Point.fromHex('0x1234').toBigInt()).toBe( - new CairoSecp256k1Point(4660n).toBigInt() + expect(CairoSecp256k1Point.fromHex(`0x${G.toString(16)}`).toBigInt()).toBe( + new CairoSecp256k1Point(G).toBigInt() ); }); @@ -315,15 +361,70 @@ describe('CairoSecp256k1Point class Unit Tests', () => { expect(CairoSecp256k1Point.factoryFromApiResponse(iterator).toBigInt()).toBe(1n); expect(CairoSecp256k1Point.factoryFromApiResponse(iterator).toBigInt()).toBe(2n); }); + + test('should read a point that is not on the curve as it comes, since a response is not checked', () => { + // (1, 1), which the constructor refuses + const point = CairoSecp256k1Point.factoryFromApiResponse( + ['0x1', '0x0', '0x1', '0x0'].values() + ); + expect(point).toBeInstanceOf(CairoSecp256k1Point); + expect(point.toBigInt()).toBe(2n ** 256n + 1n); + }); + + test('should still refuse a felt wider than 128 bits', () => { + expect(() => + CairoSecp256k1Point.factoryFromApiResponse( + [`0x${(2n ** 128n).toString(16)}`, '0x0', '0x0', '0x0'].values() + ) + ).toThrow('xLow must fit in 128 bits'); + }); }); describe('round-trip consistency', () => { test('should survive a serialize then read back cycle', () => { - [0n, 1n, 4660n, PUB_KEY, SECP256K1_POINT_MAX].forEach((value) => { + [0n, G, PUB_KEY].forEach((value) => { const felts = [...new CairoSecp256k1Point(value).toApiRequest()]; expect(felts).toHaveLength(4); expect(CairoSecp256k1Point.factoryFromApiResponse(felts.values()).toBigInt()).toBe(value); }); }); }); + + describe('the curve check', () => { + test('should accept (0, 0), the point at infinity, in every form', () => { + expect(new CairoSecp256k1Point(0n).toBigInt()).toBe(0n); + expect(new CairoSecp256k1Point(0, 0, 0, 0).toBigInt()).toBe(0n); + expect(new CairoSecp256k1Point({ xLow: 0, xHigh: 0, yLow: 0, yHigh: 0 }).toBigInt()).toBe(0n); + expect(CairoSecp256k1Point.fromHex('0x0').toBigInt()).toBe(0n); + }); + + test('should refuse a point that is not on the curve, in every form', () => { + // (1, 1) : 1^2 is not 1^3 + 7 + expect(() => new CairoSecp256k1Point(2n ** 256n + 1n)).toThrow(NOT_ON_CURVE); + expect(() => new CairoSecp256k1Point(1, 0, 1, 0)).toThrow(NOT_ON_CURVE); + expect(() => new CairoSecp256k1Point({ xLow: 1, xHigh: 0, yLow: 1, yHigh: 0 })).toThrow( + NOT_ON_CURVE + ); + expect(() => CairoSecp256k1Point.fromHex(`0x1${'1'.padStart(64, '0')}`)).toThrow( + NOT_ON_CURVE + ); + expect(() => CairoSecp256k1Point.validate(2n ** 256n + 1n)).toThrow(NOT_ON_CURVE); + expect(() => CairoSecp256k1Point.validateProps(1, 0, 1, 0)).toThrow(NOT_ON_CURVE); + expect(CairoSecp256k1Point.is(2n ** 256n + 1n)).toBe(false); + }); + + test('should refuse an x that is right modulo p but not smaller than p', () => { + expect(new CairoSecp256k1Point(2n ** 256n + Y_OF_X_1).toBigInt()).toBe(2n ** 256n + Y_OF_X_1); + expect(() => new CairoSecp256k1Point((1n + P) * 2n ** 256n + Y_OF_X_1)).toThrow( + X_NOT_BELOW_P + ); + }); + + test('should refuse a y that is right modulo p but not smaller than p', () => { + expect(new CairoSecp256k1Point(X_OF_Y_1 * 2n ** 256n + 1n).toBigInt()).toBe( + X_OF_Y_1 * 2n ** 256n + 1n + ); + expect(() => new CairoSecp256k1Point(X_OF_Y_1 * 2n ** 256n + 1n + P)).toThrow(Y_NOT_BELOW_P); + }); + }); }); diff --git a/__tests__/utils/calldata/parser/parsingStrategy.test.ts b/__tests__/utils/calldata/parser/parsingStrategy.test.ts index b060a2563..23168b18e 100644 --- a/__tests__/utils/calldata/parser/parsingStrategy.test.ts +++ b/__tests__/utils/calldata/parser/parsingStrategy.test.ts @@ -183,7 +183,9 @@ const expectedLimbs = (value: bigint): string[] => { }; describe('the leaf types the strategy now carries', () => { - const PUB_KEY = BigInt(`0x${'1a'.repeat(64)}`); + // an Ethereum public key, x || y : a point on the curve, as a point must be to be sent + const PUB_KEY = + 0x8c7aea7d673a5858bdca128d124fb0765cceb2c16f198f4c14b328aa571331e6f6c87f51d5224d73d118765cb19d7565212f80be5048bff926ba791c17541c92n; describe('core::bool', () => { test('serializes the two values a bool has', () => { @@ -249,7 +251,8 @@ describe('the leaf types the strategy now carries', () => { test.each(abiVersions)('%s emits four felts, x then y', (_name, abiFor) => { const callData = new CallData(abiFor(SECP256K1_POINT)); expect(callData.compile('fn', [PUB_KEY])).toEqual(expectedLimbs(PUB_KEY)); - expect(callData.compile('fn', [1n])).toEqual(['0', '0', '1', '0']); + // (0, 0), the point at infinity + expect(callData.compile('fn', [0n])).toEqual(['0', '0', '0', '0']); }); test('both strategies emit the same four felts, since this is not a check', () => { @@ -277,5 +280,18 @@ describe('the leaf types the strategy now carries', () => { 'input is bigger than SECP256K1_POINT_MAX' ); }); + + test('refuses a point that is not on the curve, in both strategies', () => { + // (1, 1) : 1^2 is not 1^3 + 7 + const notOnCurve = 2n ** 256n + 1n; + const message = + 'Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it'; + expect(() => new CallData(abiV2(SECP256K1_POINT)).compile('fn', [notOnCurve])).toThrow( + message + ); + expect(() => + new CallData(abiV2(SECP256K1_POINT), fastCairoTypeStrategy).compile('fn', [notOnCurve]) + ).toThrow(message); + }); }); }); diff --git a/__tests__/utils/contract.test.ts b/__tests__/utils/contract.test.ts index 5f5cf4b27..8764b23fa 100644 --- a/__tests__/utils/contract.test.ts +++ b/__tests__/utils/contract.test.ts @@ -388,6 +388,78 @@ describe('Deployer.buildDeployerCall with already compiled felts for a NonZero p }); }); +describe('Deployer.buildDeployerCall for a Secp256k1Point parameter', () => { + // an account whose constructor takes public_key: Secp256k1Point. The constructor refuses a point + // that is not on the curve, so the deployment would fail on the node. + const accountEthAbi = CONTRACTS.AccountEthOz090.sierra.abi; + const classHash = '0x1234'; + const deployerAddress = '0xabc'; + const salt = '0x1'; + // an Ethereum public key, x || y, then its four limbs as the constructor takes them + const ethPubKey = + '0x8c7aea7d673a5858bdca128d124fb0765cceb2c16f198f4c14b328aa571331e6f6c87f51d5224d73d118765cb19d7565212f80be5048bff926ba791c17541c92'; + const ethPubKeyFelts = [ + '0x5cceb2c16f198f4c14b328aa571331e6', + '0x8c7aea7d673a5858bdca128d124fb076', + '0x212f80be5048bff926ba791c17541c92', + '0xf6c87f51d5224d73d118765cb19d7565', + ].map((felt) => BigInt(felt).toString()); + // (1, 1) : 1^2 is not 1^3 + 7 + const notOnCurve = `0x${'1'.padStart(64, '0')}${'1'.padStart(64, '0')}`; + const notOnCurveMessage = + 'Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it'; + + test('accepts a point on the curve, as a parameter', () => { + const deployer = new Deployer(); + const calldata = deployer.buildDeployerCall( + { classHash, salt, abi: accountEthAbi, constructorCalldata: { public_key: ethPubKey } }, + deployerAddress + ).calls[0].calldata as Calldata; + + expect(calldata.slice(4)).toEqual(ethPubKeyFelts); + }); + + test('accepts a point on the curve, as already compiled felts', () => { + const deployer = new Deployer(); + const calldata = deployer.buildDeployerCall( + { + classHash, + salt, + abi: accountEthAbi, + constructorCalldata: CallData.compile(ethPubKeyFelts), + }, + deployerAddress + ).calls[0].calldata as Calldata; + + expect(calldata.slice(4)).toEqual(ethPubKeyFelts); + }); + + test('refuses a point that is not on the curve, as a parameter', () => { + const deployer = new Deployer(); + expect(() => + deployer.buildDeployerCall( + { classHash, salt, abi: accountEthAbi, constructorCalldata: { public_key: notOnCurve } }, + deployerAddress + ) + ).toThrow(notOnCurveMessage); + }); + + test('refuses a point that is not on the curve, as already compiled felts', () => { + const deployer = new Deployer(); + expect(() => + deployer.buildDeployerCall( + { + classHash, + salt, + abi: accountEthAbi, + constructorCalldata: CallData.compile(['1', '0', '1', '0']), + }, + deployerAddress + ) + ).toThrow(`The compiled constructor calldata does not match the abi: ${notOnCurveMessage}`); + }); +}); + describe('Contract method options', () => { // one view function with one parameter; `as const` lets typedv2() know its types const abi = [ diff --git a/__tests__/utils/secp256k1Point.test.ts b/__tests__/utils/secp256k1Point.test.ts index b84dbadcb..04e592f93 100644 --- a/__tests__/utils/secp256k1Point.test.ts +++ b/__tests__/utils/secp256k1Point.test.ts @@ -33,6 +33,18 @@ describe('secp256k1Point cairo type test', () => { ); }); + test('secp256k1Point not on the curve', () => { + // (1, 1) : 1^2 is not 1^3 + 7, so the account constructor would refuse it + const point = `0x${'1'.padStart(64, '0')}${'1'.padStart(64, '0')}`; + expect(() => { + myCallDataAccount.compile('constructor', { + public_key: point, + }); + }).toThrow( + 'Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it' + ); + }); + test('secp256k1Point parse', () => { const res = myCallDataTest.parse('get_public_key', [ '0x5cceb2c16f198f4c14b328aa571331e6', diff --git a/src/utils/cairoDataTypes/secp256k1Point.ts b/src/utils/cairoDataTypes/secp256k1Point.ts index 27c39d7ae..d59603e6c 100644 --- a/src/utils/cairoDataTypes/secp256k1Point.ts +++ b/src/utils/cairoDataTypes/secp256k1Point.ts @@ -6,10 +6,12 @@ import { isText } from '../shortString'; import { isNumber, isObject } from '../typed'; import assert from '../assert'; import { addCompiledFlag } from '../helpers'; -import { UINT_128_MAX } from './uint256'; +import { UINT_128_MAX, UINT_256_MAX } from './uint256'; /** - * The largest value a Secp256k1Point can carry : both coordinates at their maximum, 512 bits. + * The largest number the 512 bits of a Secp256k1Point can hold : both coordinates at their + * maximum. It is only a bound : a point must also be on the curve, and this value is not, so + * `new CairoSecp256k1Point(SECP256K1_POINT_MAX)` throws. * @example * ```typescript * const result = SECP256K1_POINT_MAX === (1n << 512n) - 1n; @@ -28,6 +30,77 @@ export const SECP256K1_POINT_MAX = (1n << 512n) - 1n; */ export const SECP256K1_POINT_MIN = 0n; +// p, the prime of the secp256k1 curve : each coordinate of a point is a number modulo p +const SECP256K1_PRIME = 2n ** 256n - 2n ** 32n - 977n; + +/** + * Throw unless (x, y) is a point that a contract accepts as a Secp256k1Point. + * + * A contract reads such a parameter with the `secp256k1_new` syscall. It accepts (0, 0), which + * stands for the point at infinity. Otherwise it refuses a coordinate that is not smaller than p, + * and a point that is not on the curve y^2 = x^3 + 7 modulo p. + * @param {bigint} x the x coordinate + * @param {bigint} y the y coordinate + * @throws {Error} when a coordinate is not smaller than p, or when the point is not on the curve + * @example + * ```typescript + * // G, the generator of the curve + * assertOnSecp256k1Curve( + * 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n, + * 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n + * ); + * // no error + * assertOnSecp256k1Curve(0n, 0n); + * // no error : the point at infinity + * assertOnSecp256k1Curve(1n, 1n); + * // 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") + * ``` + */ +function assertOnSecp256k1Curve(x: bigint, y: bigint): void { + if (x === 0n && y === 0n) { + return; + } + assert( + x < SECP256K1_PRIME, + 'Secp256k1Point: x must be smaller than p, the prime of the secp256k1 curve' + ); + assert( + y < SECP256K1_PRIME, + 'Secp256k1Point: y must be smaller than p, the prime of the secp256k1 curve' + ); + assert( + (y * y) % SECP256K1_PRIME === (x * x * x + 7n) % SECP256K1_PRIME, + 'Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it' + ); +} + +/** + * Throw unless one limb of a point is a whole number that fits in 128 bits, and return it. + * @param {BigNumberish} limb the limb to check + * @param {string} name the name of the limb, used in the error message + * @returns {bigint} the limb, once checked + * @throws {Error} when the limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits + * @example + * ```typescript + * const result = validateLimb('0x12', 'xLow'); + * // result = 18n + * validateLimb(2n ** 128n, 'yHigh'); + * // throws Error("yHigh must fit in 128 bits") + * ``` + */ +function validateLimb(limb: BigNumberish, name: string): bigint { + assert(limb !== null, `${name} cannot be null`); + assert(limb !== undefined, `${name} cannot be undefined`); + assert(!isText(limb), `${name} cannot be built from text`); + assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`); + assert(isBigNumberish(limb), `${name} must be a BigNumberish`); + assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`); + const bigInt = BigInt(limb); + assert(bigInt >= 0n, `${name} must be non-negative`); + assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`); + return bigInt; +} + /** * The four 128-bit limbs a Secp256k1Point occupies on the wire, in the order a call carries them. */ @@ -47,17 +120,33 @@ export interface Secp256k1PointStruct { * class accepts is the 512-bit concatenation `x || y`, x in the upper half : that is the shape an * uncompressed public key already has once its `04` prefix is dropped. * - * Both ways in are supported : one number, or the four limbs directly, which is how a response is - * read back. + * Both ways in are supported : one number, or the four limbs directly, in the order a call carries + * them. + * + * What the caller passes in is checked as soon as the point is built : it must be on the curve, or + * be (0, 0), which stands for the point at infinity, since a contract refuses anything else. A + * point read from a contract response is taken as it comes, without this check. * @example * ```typescript - * // one number, x in the upper 256 bits - * const point = new CairoSecp256k1Point(1n); - * point.toApiRequest(); // ["0", "0", "1", "0"] x = 0, y = 1 + * // G, the generator of the curve, as one number : x in the upper 256 bits, y in the lower ones + * const G = + * 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n; + * const point = new CairoSecp256k1Point(G); + * point.toStruct(); + * // { xLow: "0x29bfcdb2dce28d959f2815b16f81798", xHigh: "0x79be667ef9dcbbac55a06295ce870b07", + * // yLow: "0xfd17b448a68554199c47d08ffb10d4b8", yHigh: "0x483ada7726a3c4655da4fbfc0e1108a8" } + * + * // the same point from its four limbs, as a call carries them + * const same = new CairoSecp256k1Point( + * '0x29bfcdb2dce28d959f2815b16f81798', + * '0x79be667ef9dcbbac55a06295ce870b07', + * '0xfd17b448a68554199c47d08ffb10d4b8', + * '0x483ada7726a3c4655da4fbfc0e1108a8' + * ); + * same.toBigInt() === G; // true * - * // the four limbs, as a call carries them - * const same = new CairoSecp256k1Point(0, 0, 1, 0); - * same.toBigInt(); // 1n + * new CairoSecp256k1Point(1n); // x = 0, y = 1 + * // 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") * ``` */ export class CairoSecp256k1Point { @@ -65,8 +154,9 @@ export class CairoSecp256k1Point { * The low 128 bits of the x coordinate. * @example * ```typescript - * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xLow; - * // result = 1n + * // G, the generator of the curve, as in the example of the class + * const result = new CairoSecp256k1Point(G).xLow; + * // result = 0x29bfcdb2dce28d959f2815b16f81798n * ``` */ public xLow: bigint; @@ -75,8 +165,9 @@ export class CairoSecp256k1Point { * The high 128 bits of the x coordinate. * @example * ```typescript - * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).xHigh; - * // result = 2n + * // G, the generator of the curve, as in the example of the class + * const result = new CairoSecp256k1Point(G).xHigh; + * // result = 0x79be667ef9dcbbac55a06295ce870b07n * ``` */ public xHigh: bigint; @@ -85,8 +176,9 @@ export class CairoSecp256k1Point { * The low 128 bits of the y coordinate. * @example * ```typescript - * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yLow; - * // result = 3n + * // G, the generator of the curve, as in the example of the class + * const result = new CairoSecp256k1Point(G).yLow; + * // result = 0xfd17b448a68554199c47d08ffb10d4b8n * ``` */ public yLow: bigint; @@ -95,8 +187,9 @@ export class CairoSecp256k1Point { * The high 128 bits of the y coordinate. * @example * ```typescript - * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).yHigh; - * // result = 4n + * // G, the generator of the curve, as in the example of the class + * const result = new CairoSecp256k1Point(G).yHigh; + * // result = 0x483ada7726a3c4655da4fbfc0e1108a8n * ``` */ public yHigh: bigint; @@ -128,14 +221,14 @@ export class CairoSecp256k1Point { * @param {any[]} arr either one value — a 512-bit number or a {@link Secp256k1PointStruct} — or * the four limbs * @throws {Error} when a value is null, undefined, of an unread type, text, a decimal number, or out - * of range — see {@link CairoSecp256k1Point.validate} and {@link CairoSecp256k1Point.validateProps} - * — or when the argument count is neither 1 nor 4 + * of range, or when the point is not on the curve — see {@link CairoSecp256k1Point.validate} and + * {@link CairoSecp256k1Point.validateProps} — or when the argument count is neither 1 nor 4 * @example * ```typescript - * const result = new CairoSecp256k1Point(1n).toApiRequest(); - * // result = ["0", "0", "1", "0"] - * const result2 = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest(); - * // result2 = ["1", "2", "3", "4"] + * const result = new CairoSecp256k1Point(0n).toApiRequest(); + * // result = ["0", "0", "0", "0"] (0, 0), the point at infinity + * new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }); + * // 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") * ``` */ public constructor(...arr: any[]) { @@ -183,17 +276,22 @@ export class CairoSecp256k1Point { } /** - * Throw unless the value can be carried by a Secp256k1Point, and return it as a number. + * Throw unless the value is a point that a contract accepts, and return it as a number. + * + * The value must fit in 512 bits, and the point it stands for must be on the curve, or be (0, 0), + * the point at infinity. * * Unlike the other classes here this one gives the number back rather than returning nothing : * the constructor needs it, and computing it twice would mean splitting a 512-bit value twice. * @param {BigNumberish} input the 512-bit value to check * @returns {bigint} the value, once checked - * @throws {Error} when the value is null, undefined, text, `'0x'`, of an unread type, a decimal number, or outside [0, 2^512 - 1] + * @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 * @example * ```typescript - * const result = CairoSecp256k1Point.validate('0x1234'); - * // result = 4660n + * const result = CairoSecp256k1Point.validate(0n); + * // result = 0n (0, 0), the point at infinity + * CairoSecp256k1Point.validate('0x1234'); + * // 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") * CairoSecp256k1Point.validate(SECP256K1_POINT_MAX + 1n); * // throws Error("input is bigger than SECP256K1_POINT_MAX") * CairoSecp256k1Point.validate('abc'); @@ -220,21 +318,33 @@ export class CairoSecp256k1Point { const bigInt = BigInt(input as BigNumberish); assert(bigInt >= SECP256K1_POINT_MIN, 'input is smaller than SECP256K1_POINT_MIN'); assert(bigInt <= SECP256K1_POINT_MAX, 'input is bigger than SECP256K1_POINT_MAX'); + // x is the upper 256 bits, y the lower 256 bits + assertOnSecp256k1Curve(bigInt >> 256n, bigInt & UINT_256_MAX); return bigInt; } /** - * Throw unless the four limbs can each be carried by 128 bits, and return them as numbers. + * Throw unless the four limbs each fit in 128 bits and together make a point that a contract + * accepts, and return them as numbers. * @param {BigNumberish} xLow the low 128 bits of x * @param {BigNumberish} xHigh the high 128 bits of x * @param {BigNumberish} yLow the low 128 bits of y * @param {BigNumberish} yHigh the high 128 bits of y * @returns {{xLow: bigint, xHigh: bigint, yLow: bigint, yHigh: bigint}} the four limbs, checked - * @throws {Error} when a limb is null, undefined, text, `'0x'`, not a number, a decimal number, negative, or wider than 128 bits + * @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 * @example * ```typescript - * const result = CairoSecp256k1Point.validateProps(1, 2, 3, 4); - * // result = { xLow: 1n, xHigh: 2n, yLow: 3n, yHigh: 4n } + * // G, the generator of the curve + * const result = CairoSecp256k1Point.validateProps( + * '0x29bfcdb2dce28d959f2815b16f81798', + * '0x79be667ef9dcbbac55a06295ce870b07', + * '0xfd17b448a68554199c47d08ffb10d4b8', + * '0x483ada7726a3c4655da4fbfc0e1108a8' + * ); + * // result = { xLow: 0x29bfcdb2dce28d959f2815b16f81798n, xHigh: 0x79be667ef9dcbbac55a06295ce870b07n, + * // yLow: 0xfd17b448a68554199c47d08ffb10d4b8n, yHigh: 0x483ada7726a3c4655da4fbfc0e1108a8n } + * CairoSecp256k1Point.validateProps(1, 2, 3, 4); + * // 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") * CairoSecp256k1Point.validateProps(1, 2, 3, 2n ** 128n); * // throws Error("yHigh must fit in 128 bits") * CairoSecp256k1Point.validateProps(1.5, 2, 3, 4); @@ -249,40 +359,30 @@ export class CairoSecp256k1Point { yLow: BigNumberish, yHigh: BigNumberish ): { xLow: bigint; xHigh: bigint; yLow: bigint; yHigh: bigint } { - const validateLimb = (limb: BigNumberish, name: string): bigint => { - assert(limb !== null, `${name} cannot be null`); - assert(limb !== undefined, `${name} cannot be undefined`); - assert(!isText(limb), `${name} cannot be built from text`); - assert(!isEmptyHex(limb), `${name} cannot be '0x', which holds no hexadecimal digit`); - assert(isBigNumberish(limb), `${name} must be a BigNumberish`); - assert(!isNumber(limb) || Number.isInteger(limb), `${name} must be an integer`); - const bigInt = BigInt(limb); - assert(bigInt >= 0n, `${name} must be non-negative`); - assert(bigInt <= UINT_128_MAX, `${name} must fit in 128 bits`); - return bigInt; - }; - - return { + const limbs = { xLow: validateLimb(xLow, 'xLow'), xHigh: validateLimb(xHigh, 'xHigh'), yLow: validateLimb(yLow, 'yLow'), yHigh: validateLimb(yHigh, 'yHigh'), }; + // each coordinate is its high 128 bits followed by its low 128 bits + assertOnSecp256k1Curve((limbs.xHigh << 128n) + limbs.xLow, (limbs.yHigh << 128n) + limbs.yLow); + return limbs; } /** - * Can this value be carried by a Secp256k1Point? + * Is this value a point that a contract accepts as a Secp256k1Point? * * The non-throwing form of {@link CairoSecp256k1Point.validate}, so it answers false for every * input that one refuses, whatever the reason. * @param {any} data the value to test - * @returns {boolean} true when the value fits in 512 bits + * @returns {boolean} true when the value fits in 512 bits and stands for a point on the curve, or for (0, 0) * @example * ```typescript - * const result = CairoSecp256k1Point.is(SECP256K1_POINT_MAX); - * // result = true - * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX + 1n); - * // result2 = false + * const result = CairoSecp256k1Point.is(0n); + * // result = true (0, 0), the point at infinity + * const result2 = CairoSecp256k1Point.is(SECP256K1_POINT_MAX); + * // result2 = false x is not smaller than p * ``` */ static is(data: any): boolean { @@ -312,21 +412,33 @@ export class CairoSecp256k1Point { /** * Read one point off a contract response, advancing the iterator past its four felts. + * + * A response is read as it comes : each felt must fit in 128 bits, but the point is not checked + * against the curve. What a node answers is taken as correct, and a contract can only return a + * point on the curve, or (0, 0). * @param {Iterator} responseIterator the response felts, positioned on this point * @returns {CairoSecp256k1Point} the point that was read + * @throws {Error} when the response ends before four felts, or when a felt does not fit in 128 bits * @example * ```typescript * const response = ['0x0', '0x0', '0x1', '0x0']; * const result = CairoSecp256k1Point.factoryFromApiResponse(response.values()).toBigInt(); - * // result = 1n + * // result = 1n not on the curve, and read all the same * ``` */ static factoryFromApiResponse(responseIterator: Iterator): CairoSecp256k1Point { - const xLow = getNext(responseIterator); - const xHigh = getNext(responseIterator); - const yLow = getNext(responseIterator); - const yHigh = getNext(responseIterator); - return new CairoSecp256k1Point(xLow, xHigh, yLow, yHigh); + const xLow = validateLimb(getNext(responseIterator), 'xLow'); + const xHigh = validateLimb(getNext(responseIterator), 'xHigh'); + const yLow = validateLimb(getNext(responseIterator), 'yLow'); + const yHigh = validateLimb(getNext(responseIterator), 'yHigh'); + // Built without the constructor, which would check the curve : only what the caller passes in + // is checked, and what a node answers is read as it comes. + const point = Object.create(CairoSecp256k1Point.prototype) as CairoSecp256k1Point; + point.xLow = xLow; + point.xHigh = xHigh; + point.yLow = yLow; + point.yHigh = yHigh; + return point; } /** @@ -337,10 +449,11 @@ export class CairoSecp256k1Point { * @returns {bigint} the two coordinates concatenated, x in the upper 256 bits * @example * ```typescript - * const result = new CairoSecp256k1Point(1n).toBigInt(); - * // result = 1n - * const result2 = new CairoSecp256k1Point(0, 0, 3, 0).toBigInt(); - * // result2 = 3n + * // G, the generator of the curve, as in the example of the class + * const result = new CairoSecp256k1Point(G).toBigInt() === G; + * // result = true + * const result2 = new CairoSecp256k1Point(0, 0, 0, 0).toBigInt(); + * // result2 = 0n (0, 0), the point at infinity * ``` */ toBigInt(): bigint { @@ -354,8 +467,10 @@ export class CairoSecp256k1Point { * @returns {Secp256k1PointStruct} the limbs, each 0x-prefixed and unpadded * @example * ```typescript - * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toStruct(); - * // result = { xLow: "0x1", xHigh: "0x2", yLow: "0x3", yHigh: "0x4" } + * // G, the generator of the curve, as in the example of the class + * const result = new CairoSecp256k1Point(G).toStruct(); + * // result = { xLow: "0x29bfcdb2dce28d959f2815b16f81798", xHigh: "0x79be667ef9dcbbac55a06295ce870b07", + * // yLow: "0xfd17b448a68554199c47d08ffb10d4b8", yHigh: "0x483ada7726a3c4655da4fbfc0e1108a8" } * ``` */ toStruct(): Secp256k1PointStruct { @@ -372,8 +487,8 @@ export class CairoSecp256k1Point { * @returns {string} the 512-bit value as a 0x-prefixed hex string * @example * ```typescript - * const result = new CairoSecp256k1Point(4660n).toHexString(); - * // result = "0x1234" + * const result = new CairoSecp256k1Point(0n).toHexString(); + * // result = "0x0" (0, 0), the point at infinity * ``` */ toHexString(): string { @@ -385,8 +500,10 @@ export class CairoSecp256k1Point { * @returns {string[]} the limbs as decimal strings, `[xLow, xHigh, yLow, yHigh]`, flagged as compiled * @example * ```typescript - * const result = new CairoSecp256k1Point({ xLow: 1, xHigh: 2, yLow: 3, yHigh: 4 }).toApiRequest(); - * // result = ["1", "2", "3", "4"] + * // G, the generator of the curve, as in the example of the class + * const result = new CairoSecp256k1Point(G).toApiRequest(); + * // result = ["3468390537006497937951914270391801752", "161825202758953104525843685720298294023", + * // "336417762351022071123394393598455764152", "96009999919712310848645357523629574312"] * ``` */ toApiRequest(): string[] { @@ -405,11 +522,19 @@ export class CairoSecp256k1Point { * read as a point whose upper limbs are zero. A longer one is refused rather than truncated. * @param {string} hexString the value, 0x-prefixed or not, at most 128 hex digits * @returns {CairoSecp256k1Point} the point the string spells - * @throws {Error} when the string holds more than 128 hex digits + * @throws {Error} when the string holds more than 128 hex digits, or when the point is not on the + * curve * @example * ```typescript - * const result = CairoSecp256k1Point.fromHex('0x1').toApiRequest(); - * // result = ["0", "0", "1", "0"] + * // G, the generator of the curve + * const result = CairoSecp256k1Point.fromHex( + * '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8' + * ).xHigh; + * // result = 0x79be667ef9dcbbac55a06295ce870b07n + * const result2 = CairoSecp256k1Point.fromHex('0x0').toApiRequest(); + * // result2 = ["0", "0", "0", "0"] left-padded to 128 hex digits : (0, 0), the point at infinity + * CairoSecp256k1Point.fromHex('0x1'); // x = 0, y = 1 + * // 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") * ``` */ static fromHex(hexString: string): CairoSecp256k1Point { From b01250c14cbea3e1bf2aa3e987ea66bf0fc6301f Mon Sep 17 00:00:00 2001 From: PhilippeR26 Date: Tue, 24 Nov 2026 16:49:42 +0100 Subject: [PATCH 2/2] test(calldata): build the long secp256k1 constants with BigInt to avoid CodeQL false alerts --- .../cairoDataTypes/CairoSecp256k1Point.test.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/__tests__/utils/cairoDataTypes/CairoSecp256k1Point.test.ts b/__tests__/utils/cairoDataTypes/CairoSecp256k1Point.test.ts index 8bc319598..8dfc4fad6 100644 --- a/__tests__/utils/cairoDataTypes/CairoSecp256k1Point.test.ts +++ b/__tests__/utils/cairoDataTypes/CairoSecp256k1Point.test.ts @@ -5,9 +5,13 @@ import { Literal, } from '../../../src'; +// G, PUB_KEY, Y_OF_X_1 and X_OF_Y_1 are built with BigInt() rather than written as bigint literals : +// written as literals, CodeQL took them for undefined and reported each use of them. + // G, the generator of the secp256k1 curve, as one 512-bit number x || y, then as its four limbs -const G = - 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n; +const G = BigInt( + '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8' +); const G_X_LOW = 0x029bfcdb2dce28d959f2815b16f81798n; const G_X_HIGH = 0x79be667ef9dcbbac55a06295ce870b07n; const G_Y_LOW = 0xfd17b448a68554199c47d08ffb10d4b8n; @@ -15,8 +19,9 @@ const G_Y_HIGH = 0x483ada7726a3c4655da4fbfc0e1108a8n; // an Ethereum public key, x || y, as an uncompressed key reads once its 04 prefix is gone. It is the // key of __tests__/utils/secp256k1Point.test.ts, where its four limbs are written out too. -const PUB_KEY = - 0x8c7aea7d673a5858bdca128d124fb0765cceb2c16f198f4c14b328aa571331e6f6c87f51d5224d73d118765cb19d7565212f80be5048bff926ba791c17541c92n; +const PUB_KEY = BigInt( + '0x8c7aea7d673a5858bdca128d124fb0765cceb2c16f198f4c14b328aa571331e6f6c87f51d5224d73d118765cb19d7565212f80be5048bff926ba791c17541c92' +); // p, the prime of the secp256k1 curve const P = 2n ** 256n - 2n ** 32n - 977n; @@ -24,8 +29,8 @@ const P = 2n ** 256n - 2n ** 32n - 977n; // Two points on the curve with one small coordinate, computed with @noble/curves and checked with // y^2 = x^3 + 7 modulo p : (1, Y_OF_X_1) and (X_OF_Y_1, 1). Adding p to the small coordinate still // fits in 256 bits, which gives a coordinate that is right modulo p but not smaller than p. -const Y_OF_X_1 = 0x4218f20ae6c646b363db68605822fb14264ca8d2587fdd6fbc750d587e76a7een; -const X_OF_Y_1 = 0x1fe1e5ef3fceb5c135ab7741333ce5a6e80d68167653f6b2b24bcbcfaaaff507n; +const Y_OF_X_1 = BigInt('0x4218f20ae6c646b363db68605822fb14264ca8d2587fdd6fbc750d587e76a7ee'); +const X_OF_Y_1 = BigInt('0x1fe1e5ef3fceb5c135ab7741333ce5a6e80d68167653f6b2b24bcbcfaaaff507'); const NOT_ON_CURVE = 'Secp256k1Point: this point is not on the secp256k1 curve (y^2 must equal x^3 + 7 modulo p), so a contract would refuse it';