From 5d3fce35a51151eddde6982913c5d4bd865e450d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Thu, 1 Feb 2024 17:20:04 +0100 Subject: [PATCH] test: testing `toFields()` length (#4364) Testing results of `toFields()` length against the noir constants. --- .../src/core/libraries/ConstantsGen.sol | 4 +-- yarn-project/circuits.js/src/constants.gen.ts | 4 +-- .../src/structs/call_context.test.ts | 24 +++++++++++++++++ .../structs/contract_deployment_data.test.ts | 7 +++++ .../src/structs/contract_storage_read.test.ts | 26 ++++++++++++------- .../contract_storage_update_request.test.ts | 26 ++++++++++++------- .../src/structs/function_data.test.ts | 17 +++++++++--- .../circuits.js/src/structs/header.test.ts | 26 ++++++++++++------- .../private_circuit_public_inputs.test.ts | 23 +++++++++++----- .../public_circuit_public_inputs.test.ts | 7 +++++ .../src/crates/types/src/constants.nr | 4 +-- 11 files changed, 126 insertions(+), 42 deletions(-) create mode 100644 yarn-project/circuits.js/src/structs/call_context.test.ts diff --git a/l1-contracts/src/core/libraries/ConstantsGen.sol b/l1-contracts/src/core/libraries/ConstantsGen.sol index 6bf33898baa..d367cd2206c 100644 --- a/l1-contracts/src/core/libraries/ConstantsGen.sol +++ b/l1-contracts/src/core/libraries/ConstantsGen.sol @@ -73,10 +73,10 @@ library Constants { uint256 internal constant HEADER_LENGTH = 18; uint256 internal constant FUNCTION_DATA_LENGTH = 4; uint256 internal constant CONTRACT_DEPLOYMENT_DATA_LENGTH = 6; - uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 200; + uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 204; uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3; uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2; - uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 190; + uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201; uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674; uint256 internal constant CALL_PRIVATE_FUNCTION_RETURN_SIZE = 210; uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 98; diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index 6d2571f17ec..6fcfc964d85 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -59,10 +59,10 @@ export const CALL_CONTEXT_LENGTH = 8; export const HEADER_LENGTH = 18; export const FUNCTION_DATA_LENGTH = 4; export const CONTRACT_DEPLOYMENT_DATA_LENGTH = 6; -export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 200; +export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 204; export const CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3; export const CONTRACT_STORAGE_READ_LENGTH = 2; -export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 190; +export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201; export const GET_NOTES_ORACLE_RETURN_LENGTH = 674; export const CALL_PRIVATE_FUNCTION_RETURN_SIZE = 210; export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 98; diff --git a/yarn-project/circuits.js/src/structs/call_context.test.ts b/yarn-project/circuits.js/src/structs/call_context.test.ts new file mode 100644 index 00000000000..0cc83f9aaa4 --- /dev/null +++ b/yarn-project/circuits.js/src/structs/call_context.test.ts @@ -0,0 +1,24 @@ +import { CALL_CONTEXT_LENGTH } from '../constants.gen.js'; +import { makeCallContext } from '../tests/factories.js'; +import { CallContext } from './call_context.js'; + +describe('CallContext', () => { + let callContext: CallContext; + + beforeAll(() => { + const randomInt = Math.floor(Math.random() * 1000); + callContext = makeCallContext(randomInt); + }); + + it(`serializes to buffer and deserializes it back`, () => { + const buffer = callContext.toBuffer(); + const res = CallContext.fromBuffer(buffer); + expect(res).toEqual(callContext); + expect(res.isEmpty()).toBe(false); + }); + + it('number of fields matches constant', () => { + const fields = callContext.toFields(); + expect(fields.length).toBe(CALL_CONTEXT_LENGTH); + }); +}); diff --git a/yarn-project/circuits.js/src/structs/contract_deployment_data.test.ts b/yarn-project/circuits.js/src/structs/contract_deployment_data.test.ts index 40fa0173a03..d1988a62553 100644 --- a/yarn-project/circuits.js/src/structs/contract_deployment_data.test.ts +++ b/yarn-project/circuits.js/src/structs/contract_deployment_data.test.ts @@ -1,3 +1,4 @@ +import { CONTRACT_DEPLOYMENT_DATA_LENGTH } from '../constants.gen.js'; import { makeContractDeploymentData } from '../tests/factories.js'; import { ContractDeploymentData } from './contract_deployment_data.js'; @@ -14,4 +15,10 @@ describe('ContractDeploymentData', () => { const target = ContractDeploymentData.empty(); expect(target.isEmpty()).toBe(true); }); + + it('number of fields matches constant', () => { + const target = makeContractDeploymentData(327); + const fields = target.toFields(); + expect(fields.length).toBe(CONTRACT_DEPLOYMENT_DATA_LENGTH); + }); }); diff --git a/yarn-project/circuits.js/src/structs/contract_storage_read.test.ts b/yarn-project/circuits.js/src/structs/contract_storage_read.test.ts index 51264c92afd..9bedecfd3e9 100644 --- a/yarn-project/circuits.js/src/structs/contract_storage_read.test.ts +++ b/yarn-project/circuits.js/src/structs/contract_storage_read.test.ts @@ -1,21 +1,29 @@ +import { CONTRACT_STORAGE_READ_LENGTH } from '../constants.gen.js'; import { makeContractStorageRead } from '../tests/factories.js'; import { ContractStorageRead } from './contract_storage_read.js'; describe('ContractStorageRead', () => { - it('serializes to buffer and deserializes it back', () => { + let read: ContractStorageRead; + + beforeAll(() => { const randomInt = Math.floor(Math.random() * 1000); - const expected = makeContractStorageRead(randomInt); - const buffer = expected.toBuffer(); + read = makeContractStorageRead(randomInt); + }); + + it('serializes to buffer and deserializes it back', () => { + const buffer = read.toBuffer(); const res = ContractStorageRead.fromBuffer(buffer); - expect(res).toEqual(expected); + expect(res).toEqual(read); }); it('serializes to field array and deserializes it back', () => { - const randomInt = Math.floor(Math.random() * 1000); - const expected = makeContractStorageRead(randomInt); - - const fieldArray = expected.toFields(); + const fieldArray = read.toFields(); const res = ContractStorageRead.fromFields(fieldArray); - expect(res).toEqual(expected); + expect(res).toEqual(read); + }); + + it('number of fields matches constant', () => { + const fields = read.toFields(); + expect(fields.length).toBe(CONTRACT_STORAGE_READ_LENGTH); }); }); diff --git a/yarn-project/circuits.js/src/structs/contract_storage_update_request.test.ts b/yarn-project/circuits.js/src/structs/contract_storage_update_request.test.ts index e38a0b031aa..6e3ca79cad1 100644 --- a/yarn-project/circuits.js/src/structs/contract_storage_update_request.test.ts +++ b/yarn-project/circuits.js/src/structs/contract_storage_update_request.test.ts @@ -1,21 +1,29 @@ +import { CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH } from '../constants.gen.js'; import { makeContractStorageUpdateRequest } from '../tests/factories.js'; import { ContractStorageUpdateRequest } from './contract_storage_update_request.js'; describe('ContractStorageUpdateRequest', () => { - it('serializes to buffer and deserializes it back', () => { + let request: ContractStorageUpdateRequest; + + beforeAll(() => { const randomInt = Math.floor(Math.random() * 1000); - const expected = makeContractStorageUpdateRequest(randomInt); - const buffer = expected.toBuffer(); + request = makeContractStorageUpdateRequest(randomInt); + }); + + it('serializes to buffer and deserializes it back', () => { + const buffer = request.toBuffer(); const res = ContractStorageUpdateRequest.fromBuffer(buffer); - expect(res).toEqual(expected); + expect(res).toEqual(request); }); it('serializes to field array and deserializes it back', () => { - const randomInt = Math.floor(Math.random() * 1000); - const expected = makeContractStorageUpdateRequest(randomInt); - - const fieldArray = expected.toFields(); + const fieldArray = request.toFields(); const res = ContractStorageUpdateRequest.fromFields(fieldArray); - expect(res).toEqual(expected); + expect(res).toEqual(request); + }); + + it('number of fields matches constant', () => { + const fields = request.toFields(); + expect(fields.length).toBe(CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH); }); }); diff --git a/yarn-project/circuits.js/src/structs/function_data.test.ts b/yarn-project/circuits.js/src/structs/function_data.test.ts index 269198d6ef7..4a953ccdb0c 100644 --- a/yarn-project/circuits.js/src/structs/function_data.test.ts +++ b/yarn-project/circuits.js/src/structs/function_data.test.ts @@ -1,13 +1,24 @@ import { FunctionSelector } from '@aztec/foundation/abi'; +import { FUNCTION_DATA_LENGTH } from '../constants.gen.js'; import { FunctionData } from './function_data.js'; describe('FunctionData', () => { + let functionData: FunctionData; + + beforeAll(() => { + functionData = new FunctionData(new FunctionSelector(123), false, true, true); + }); + it(`serializes to buffer and deserializes it back`, () => { - const expected = new FunctionData(new FunctionSelector(123), false, true, true); - const buffer = expected.toBuffer(); + const buffer = functionData.toBuffer(); const res = FunctionData.fromBuffer(buffer); - expect(res).toEqual(expected); + expect(res).toEqual(functionData); expect(res.isEmpty()).toBe(false); }); + + it('number of fields matches constant', () => { + const fields = functionData.toFields(); + expect(fields.length).toBe(FUNCTION_DATA_LENGTH); + }); }); diff --git a/yarn-project/circuits.js/src/structs/header.test.ts b/yarn-project/circuits.js/src/structs/header.test.ts index 9f25ffc0648..716c0a660b5 100644 --- a/yarn-project/circuits.js/src/structs/header.test.ts +++ b/yarn-project/circuits.js/src/structs/header.test.ts @@ -1,22 +1,25 @@ +import { HEADER_LENGTH } from '../constants.gen.js'; import { makeHeader } from '../tests/factories.js'; import { Header } from './header.js'; describe('Header', () => { - it('serializes to buffer and deserializes it back', () => { + let header: Header; + + beforeAll(() => { const randomInt = Math.floor(Math.random() * 1000); - const expected = makeHeader(randomInt, undefined); - const buffer = expected.toBuffer(); + header = makeHeader(randomInt, undefined); + }); + + it('serializes to buffer and deserializes it back', () => { + const buffer = header.toBuffer(); const res = Header.fromBuffer(buffer); - expect(res).toEqual(expected); + expect(res).toEqual(header); }); it('serializes to field array and deserializes it back', () => { - const randomInt = Math.floor(Math.random() * 1000); - const expected = makeHeader(randomInt, undefined); - - const fieldArray = expected.toFields(); + const fieldArray = header.toFields(); const res = Header.fromFields(fieldArray); - expect(res).toEqual(expected); + expect(res).toEqual(header); }); it('computes hash', () => { @@ -25,4 +28,9 @@ describe('Header', () => { const hash = header.hash(); expect(hash).toMatchSnapshot(); }); + + it('number of fields matches constant', () => { + const fields = header.toFields(); + expect(fields.length).toBe(HEADER_LENGTH); + }); }); diff --git a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.test.ts b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.test.ts index fd7cea4b38d..8b8753e51cc 100644 --- a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.test.ts +++ b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.test.ts @@ -1,23 +1,34 @@ +import { PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH } from '../constants.gen.js'; import { makePrivateCircuitPublicInputs } from '../tests/factories.js'; import { PrivateCircuitPublicInputs } from './private_circuit_public_inputs.js'; describe('PrivateCircuitPublicInputs', () => { + let inputs: PrivateCircuitPublicInputs; + + beforeAll(() => { + const randomInt = Math.floor(Math.random() * 1000); + inputs = makePrivateCircuitPublicInputs(randomInt); + }); + it('serializes to buffer and back', () => { - const target = makePrivateCircuitPublicInputs(100); - const buffer = target.toBuffer(); + const buffer = inputs.toBuffer(); const result = PrivateCircuitPublicInputs.fromBuffer(buffer); - expect(result).toEqual(target); + expect(result).toEqual(inputs); }); it('serializes to fields and back', () => { - const target = makePrivateCircuitPublicInputs(100); - const fields = target.toFields(); + const fields = inputs.toFields(); const result = PrivateCircuitPublicInputs.fromFields(fields); - expect(result).toEqual(target); + expect(result).toEqual(inputs); }); it(`initializes an empty PrivateCircuitPublicInputs`, () => { const target = PrivateCircuitPublicInputs.empty(); expect(target.isEmpty()).toBe(true); }); + + it('number of fields matches constant', () => { + const fields = inputs.toFields(); + expect(fields.length).toBe(PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH); + }); }); diff --git a/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.test.ts b/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.test.ts index 7fb1145ea63..29ff4513801 100644 --- a/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.test.ts +++ b/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.test.ts @@ -1,3 +1,4 @@ +import { PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH } from '../constants.gen.js'; import { makePublicCircuitPublicInputs } from '../tests/factories.js'; import { PublicCircuitPublicInputs } from './public_circuit_public_inputs.js'; @@ -15,4 +16,10 @@ describe('PublicCircuitPublicInputs', () => { const target = PublicCircuitPublicInputs.empty(); expect(target.isEmpty()).toBe(true); }); + + it('number of fields matches constant', () => { + const target = makePublicCircuitPublicInputs(327); + const fields = target.toFields(); + expect(fields.length).toBe(PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH); + }); }); diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr index baeddd68ea5..40a15506cdf 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr @@ -104,11 +104,11 @@ global CONTRACT_DEPLOYMENT_DATA_LENGTH: Field = 6; // Change this ONLY if you have changed the PrivateCircuitPublicInputs structure. // In other words, if the structure/size of the public inputs of a function call changes then we should change this // constant as well CALL_PRIVATE_FUNCTION_RETURN_SIZE and PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH -global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: Field = 200; +global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: Field = 204; global CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH: Field = 3; global CONTRACT_STORAGE_READ_LENGTH: Field = 2; // Change this ONLY if you have changed the PublicCircuitPublicInputs structure. -global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: Field = 190; +global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: Field = 201; global GET_NOTES_ORACLE_RETURN_LENGTH: Field = 674; global CALL_PRIVATE_FUNCTION_RETURN_SIZE: Field = 210; global PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: Field = 98;