Skip to content

Commit

Permalink
1865 - cleanup of makeEmptyXXX methods in factories
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmon committed Aug 30, 2023
1 parent 7ef8d33 commit 6d0d3c8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 82 deletions.
11 changes: 7 additions & 4 deletions yarn-project/circuits.js/src/kernel/public_kernel.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {
CircuitError,
Fr,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX,
MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_TX,
makeTuple,
simulatePublicKernelCircuit,
} from '../index.js';
import { makePublicDataRead, makePublicKernelInputsWithEmptyOutput } from '../tests/factories.js';
import { makePublicDataRead, makePublicKernelInputsWithTweak } from '../tests/factories.js';

describe('kernel/public_kernel', () => {
it('simulates public kernel circuit with previous public kernel', async function () {
const input = await makePublicKernelInputsWithEmptyOutput(1, input => {
const input = await makePublicKernelInputsWithTweak(1, input => {
input.publicCall.callStackItem.functionData.isConstructor = false;
input.publicCall.callStackItem.functionData.isPrivate = false;
input.previousKernel.publicInputs.isPrivate = false;
Expand All @@ -19,15 +21,16 @@ describe('kernel/public_kernel', () => {
});

it('simulates public kernel circuit with previous private kernel', async function () {
const input = await makePublicKernelInputsWithEmptyOutput(1, input => {
const input = await makePublicKernelInputsWithTweak(1, input => {
input.previousKernel.publicInputs.isPrivate = true;
input.previousKernel.publicInputs.end.privateCallStack = makeTuple(MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX, Fr.zero);
});
const result = await simulatePublicKernelCircuit(input);
expect(result).toBeDefined();
});

it('simulating public kernel circuit fails when aggregating proofs will overflow', async function () {
const input = await makePublicKernelInputsWithEmptyOutput(1, input => {
const input = await makePublicKernelInputsWithTweak(1, input => {
input.publicCall.callStackItem.functionData.isConstructor = false;
input.publicCall.callStackItem.functionData.isPrivate = false;
input.previousKernel.publicInputs.isPrivate = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ is_private: 1
"
`;

exports[`structs/kernel serializes and prints private_kernel_public_inputs_final 1`] = `
exports[`structs/kernel serializes and prints private_kernel_public_inputs for ordering circuit 1`] = `
"end:
aggregation_object:
P0: { 0x1, 0x2 }
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/structs/kernel/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('structs/kernel', () => {
);
});

it(`serializes and prints private_kernel_public_inputs_final`, async () => {
it(`serializes and prints private_kernel_public_inputs for ordering circuit`, async () => {
const kernelInputs = makeKernelPublicInputsFinal();
await expectSerializeToMatchSnapshot(
kernelInputs.toBuffer(),
Expand Down
80 changes: 4 additions & 76 deletions yarn-project/circuits.js/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,35 +193,6 @@ export function makeContractStorageRead(seed = 1): ContractStorageRead {
return new ContractStorageRead(fr(seed), fr(seed + 1));
}

/**
* Creates empty accumulated data.
* @param seed - The seed to use for generating the accumulated data.
* @returns An empty accumulated data.
*/
export function makeEmptyAccumulatedData(seed = 1, full = false): CombinedAccumulatedData {
const tupleGenerator = full ? makeTuple : makeHalfFullTuple;

return new CombinedAccumulatedData(
makeAggregationObject(seed),
tupleGenerator(MAX_READ_REQUESTS_PER_TX, fr, seed + 0x80),
tupleGenerator(MAX_READ_REQUESTS_PER_TX, i => makeReadRequestMembershipWitness(i * 123), seed + 0x90),
tupleGenerator(MAX_NEW_COMMITMENTS_PER_TX, fr, seed + 0x100),
tupleGenerator(MAX_NEW_NULLIFIERS_PER_TX, fr, seed + 0x200),
tupleGenerator(MAX_NEW_NULLIFIERS_PER_TX, fr, seed + 0x300),
tupleGenerator(MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX, Fr.zero), // private call stack must be empty
tupleGenerator(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, fr, seed + 0x500),
tupleGenerator(MAX_NEW_L2_TO_L1_MSGS_PER_TX, fr, seed + 0x600),
tupleGenerator(2, fr, seed + 0x700), // encrypted logs hash
tupleGenerator(2, fr, seed + 0x800), // unencrypted logs hash
fr(seed + 0x900), // encrypted_log_preimages_length
fr(seed + 0xa00), // unencrypted_log_preimages_length
tupleGenerator(MAX_NEW_CONTRACTS_PER_TX, makeNewContractData, seed + 0xb00),
tupleGenerator(MAX_OPTIONALLY_REVEALED_DATA_LENGTH_PER_TX, makeOptionallyRevealedData, seed + 0xc00),
tupleGenerator(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, makeEmptyPublicDataUpdateRequest, seed + 0xd00),
tupleGenerator(MAX_PUBLIC_DATA_READS_PER_TX, makeEmptyPublicDataRead, seed + 0xe00),
);
}

/**
* Creates arbitrary accumulated data.
* @param seed - The seed to use for generating the accumulated data.
Expand Down Expand Up @@ -251,31 +222,6 @@ export function makeAccumulatedData(seed = 1, full = false): CombinedAccumulated
);
}

/**
* Creates empty final accumulated data.
* @param seed - The seed to use for generating the final accumulated data.
* @returns An empty final accumulated data.
*/
export function makeEmptyFinalAccumulatedData(seed = 1, full = false): FinalAccumulatedData {
const tupleGenerator = full ? makeTuple : makeHalfFullTuple;

return new FinalAccumulatedData(
makeAggregationObject(seed),
tupleGenerator(MAX_NEW_COMMITMENTS_PER_TX, fr, seed + 0x100),
tupleGenerator(MAX_NEW_NULLIFIERS_PER_TX, fr, seed + 0x200),
tupleGenerator(MAX_NEW_NULLIFIERS_PER_TX, fr, seed + 0x300),
tupleGenerator(MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX, Fr.zero), // private call stack must be empty
tupleGenerator(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, fr, seed + 0x500),
tupleGenerator(MAX_NEW_L2_TO_L1_MSGS_PER_TX, fr, seed + 0x600),
tupleGenerator(2, fr, seed + 0x700), // encrypted logs hash
tupleGenerator(2, fr, seed + 0x800), // unencrypted logs hash
fr(seed + 0x900), // encrypted_log_preimages_length
fr(seed + 0xa00), // unencrypted_log_preimages_length
tupleGenerator(MAX_NEW_CONTRACTS_PER_TX, makeNewContractData, seed + 0xb00),
tupleGenerator(MAX_OPTIONALLY_REVEALED_DATA_LENGTH_PER_TX, makeOptionallyRevealedData, seed + 0xc00),
);
}

/**
* Creates arbitrary final accumulated data.
* @param seed - The seed to use for generating the final accumulated data.
Expand Down Expand Up @@ -382,31 +328,13 @@ export function makePublicCircuitPublicInputs(
);
}

/**
* Creates empty kernel circuit public inputs.
* @param seed - The seed to use for generating the kernel circuit public inputs.
* @returns Empty kernel circuit public inputs.
*/
export function makeEmptyKernelPublicInputs(seed = 1): KernelCircuitPublicInputs {
return new KernelCircuitPublicInputs(makeEmptyAccumulatedData(seed), makeConstantData(seed + 0x100), true);
}

/**
* Creates arbitrary kernel circuit public inputs.
* @param seed - The seed to use for generating the kernel circuit public inputs.
* @returns Kernel circuit public inputs.
*/
export function makeKernelPublicInputs(seed = 1): KernelCircuitPublicInputs {
return new KernelCircuitPublicInputs(makeAccumulatedData(seed, true), makeConstantData(seed + 0x100), true);
}

/**
* Creates empty final ordering kernel circuit public inputs.
* @param seed - The seed to use for generating the final ordering kernel circuit public inputs.
* @returns Empty final ordering kernel circuit public inputs.
*/
export function makeEmptyKernelPublicInputsFinal(seed = 1): KernelCircuitPublicInputsFinal {
return new KernelCircuitPublicInputsFinal(makeEmptyFinalAccumulatedData(seed), makeConstantData(seed + 0x100), true);
return new KernelCircuitPublicInputs(makeAccumulatedData(seed, false), makeConstantData(seed + 0x100), true);
}

/**
Expand Down Expand Up @@ -627,16 +555,16 @@ export async function makePublicKernelInputs(seed = 1): Promise<PublicKernelInpu
}

/**
* Makes arbitrary public kernel inputs with empty output.
* Makes arbitrary public kernel inputs.
* @param seed - The seed to use for generating the public kernel inputs.
* @param tweak - An optional function to tweak the output before computing hashes.
* @returns Public kernel inputs.
*/
export async function makePublicKernelInputsWithEmptyOutput(
export async function makePublicKernelInputsWithTweak(
seed = 1,
tweak?: (publicKernelInputs: PublicKernelInputs) => void,
): Promise<PublicKernelInputs> {
const kernelCircuitPublicInputs = makeEmptyKernelPublicInputs(seed);
const kernelCircuitPublicInputs = makeKernelPublicInputs(seed);
const publicKernelInputs = new PublicKernelInputs(
makePreviousKernelData(seed, kernelCircuitPublicInputs),
await makePublicCallData(seed + 0x1000),
Expand Down

0 comments on commit 6d0d3c8

Please sign in to comment.