Skip to content

Commit

Permalink
test: testing toFields() length (#4364)
Browse files Browse the repository at this point in the history
Testing results of `toFields()` length against the noir constants.
  • Loading branch information
benesjan committed Feb 1, 2024
1 parent b92d690 commit 5d3fce3
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 42 deletions.
4 changes: 2 additions & 2 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions yarn-project/circuits.js/src/structs/call_context.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
});
});
26 changes: 17 additions & 9 deletions yarn-project/circuits.js/src/structs/contract_storage_read.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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);
});
});
17 changes: 14 additions & 3 deletions yarn-project/circuits.js/src/structs/function_data.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
26 changes: 17 additions & 9 deletions yarn-project/circuits.js/src/structs/header.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5d3fce3

Please sign in to comment.