Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 165 additions & 64 deletions __tests__/utils/cairoDataTypes/CairoSecp256k1Point.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,54 @@
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);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
expect(new CairoSecp256k1Point(G.toString(10)).toBigInt()).toBe(G);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
});

test('should carry a full-width key unchanged', () => {
Expand All @@ -43,36 +62,41 @@

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', () => {
Expand All @@ -84,9 +108,12 @@
});

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', () => {
Expand Down Expand Up @@ -159,11 +186,18 @@

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,
});
});

Expand Down Expand Up @@ -208,8 +242,8 @@

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);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
expect(CairoSecp256k1Point.validate(0n)).toBe(0n);
});

test('should throw outside the range', () => {
Expand All @@ -220,13 +254,15 @@
});

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);
Expand All @@ -248,47 +284,57 @@

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']);
});
});

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')}`);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
expect(point.xLow).toBe(1n);
expect(point.xHigh).toBe(0n);
expect(point.toBigInt()).toBe(2n ** 256n + Y_OF_X_1);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
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);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
});

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(
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
new CairoSecp256k1Point(G).toBigInt()
);
});

Expand All @@ -315,15 +361,70 @@
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);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
expect(() => new CairoSecp256k1Point((1n + P) * 2n ** 256n + Y_OF_X_1)).toThrow(
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
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(
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
X_OF_Y_1 * 2n ** 256n + 1n
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
);
expect(() => new CairoSecp256k1Point(X_OF_Y_1 * 2n ** 256n + 1n + P)).toThrow(Y_NOT_BELOW_P);
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
});
});
});
20 changes: 18 additions & 2 deletions __tests__/utils/calldata/parser/parsingStrategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});
Loading