Skip to content

Commit

Permalink
cleanup of extractPublicCircuitPublicInputs
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 1, 2024
1 parent 4458266 commit cb5d665
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 61 deletions.
61 changes: 2 additions & 59 deletions yarn-project/acir-simulator/src/acvm/deserialize.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
CallContext,
ContractDeploymentData,
ContractStorageRead,
ContractStorageUpdateRequest,
HEADER_LENGTH,
Header,
MAX_NEW_COMMITMENTS_PER_CALL,
Expand All @@ -11,8 +9,6 @@ import {
MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
MAX_READ_REQUESTS_PER_CALL,
NUM_FIELDS_PER_SHA256,
NullifierKeyValidationRequest,
Expand All @@ -22,10 +18,9 @@ import {
SideEffect,
SideEffectLinkedToNoteHash,
} from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { FieldReader, Tuple } from '@aztec/foundation/serialize';
import { FieldReader } from '@aztec/foundation/serialize';

import { getReturnWitness } from '@noir-lang/acvm_js';

Expand Down Expand Up @@ -145,57 +140,5 @@ export function extractPrivateCircuitPublicInputs(
*/
export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, acir: Buffer): PublicCircuitPublicInputs {
const witnessReader = createPublicInputsReader(partialWitness, acir);

const callContext = witnessReader.readObject(CallContext);

const argsHash = witnessReader.readField();
const returnValues = witnessReader.readFieldArray(RETURN_VALUES_LENGTH);

const contractStorageUpdateRequests = new Array(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL).fill(
ContractStorageUpdateRequest.empty(),
);
for (let i = 0; i < MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL; i++) {
const request = new ContractStorageUpdateRequest(
witnessReader.readField(),
witnessReader.readField(),
witnessReader.readField(),
);
contractStorageUpdateRequests[i] = request;
}
const contractStorageReads = new Array(MAX_PUBLIC_DATA_READS_PER_CALL).fill(ContractStorageRead.empty());
for (let i = 0; i < MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL; i++) {
const request = new ContractStorageRead(witnessReader.readField(), witnessReader.readField());
contractStorageReads[i] = request;
}

const publicCallStack = witnessReader.readFieldArray(MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL);
const newCommitments = witnessReader.readArray(MAX_NEW_COMMITMENTS_PER_CALL, SideEffect);
const newNullifiers = witnessReader.readArray(MAX_NEW_NULLIFIERS_PER_CALL, SideEffectLinkedToNoteHash);
const newL2ToL1Msgs = witnessReader.readFieldArray(MAX_NEW_L2_TO_L1_MSGS_PER_CALL);

const unencryptedLogsHash = witnessReader.readFieldArray(NUM_FIELDS_PER_SHA256);
const unencryptedLogPreimagesLength = witnessReader.readField();

const header = Header.fromFields(witnessReader.readFieldArray(HEADER_LENGTH));

const proverAddress = AztecAddress.fromField(witnessReader.readField());

return new PublicCircuitPublicInputs(
callContext,
argsHash,
returnValues,
contractStorageUpdateRequests as Tuple<
ContractStorageUpdateRequest,
typeof MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL
>,
contractStorageReads as Tuple<ContractStorageRead, typeof MAX_PUBLIC_DATA_READS_PER_CALL>,
publicCallStack,
newCommitments,
newNullifiers,
newL2ToL1Msgs,
unencryptedLogsHash,
unencryptedLogPreimagesLength,
header,
proverAddress,
);
return PublicCircuitPublicInputs.fromFields(witnessReader);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { makePublicCircuitPublicInputs } from '../tests/factories.js';
import { PublicCircuitPublicInputs } from './public_circuit_public_inputs.js';

describe('PublicCircuitPublicInputs', () => {
it('serializes to field array and deserializes it back', () => {
const randomInt = Math.floor(Math.random() * 1000);
const expected = makePublicCircuitPublicInputs(randomInt, undefined);

const fieldArray = expected.toFields();
const res = PublicCircuitPublicInputs.fromFields(fieldArray);
expect(res).toEqual(expected);
});

it(`initializes an empty PrivateCircuitPublicInputs`, () => {
const target = PublicCircuitPublicInputs.empty();
expect(target.isEmpty()).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { makeTuple } from '@aztec/foundation/array';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { isArrayEmpty } from '@aztec/foundation/collection';
import { Fr } from '@aztec/foundation/fields';
import { Tuple, serializeToBuffer } from '@aztec/foundation/serialize';
import { FieldReader, Tuple, serializeToBuffer, serializeToFieldArray } from '@aztec/foundation/serialize';
import { FieldsOf } from '@aztec/foundation/types';

import {
Expand All @@ -12,10 +12,17 @@ import {
MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_PUBLIC_DATA_READS_PER_CALL,
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
NUM_FIELDS_PER_SHA256,
RETURN_VALUES_LENGTH,
} from '../constants.gen.js';
import { CallContext } from './call_context.js';
import { ContractStorageRead, ContractStorageUpdateRequest, Header, SideEffect, SideEffectLinkedToNoteHash } from './index.js';
import {
ContractStorageRead,
ContractStorageUpdateRequest,
Header,
SideEffect,
SideEffectLinkedToNoteHash,
} from './index.js';

/**
* Public inputs to a public circuit.
Expand Down Expand Up @@ -164,4 +171,45 @@ export class PublicCircuitPublicInputs {
toBuffer(): Buffer {
return serializeToBuffer(...PublicCircuitPublicInputs.getFields(this));
}

toFields(): Fr[] {
return serializeToFieldArray(...PublicCircuitPublicInputs.getFields(this));
}

static fromFields(fields: Fr[] | FieldReader): PublicCircuitPublicInputs {
const reader = FieldReader.asReader(fields);

const callContext = CallContext.fromFields(reader);
const argsHash = reader.readField();
const returnValues = reader.readFieldArray(RETURN_VALUES_LENGTH);
const contractStorageUpdateRequests = reader.readArray(
MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL,
ContractStorageUpdateRequest,
);
const contractStorageReads = reader.readArray(MAX_PUBLIC_DATA_READS_PER_CALL, ContractStorageRead);
const publicCallStackHashes = reader.readFieldArray(MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL);
const newCommitments = reader.readArray(MAX_NEW_COMMITMENTS_PER_CALL, SideEffect);
const newNullifiers = reader.readArray(MAX_NEW_NULLIFIERS_PER_CALL, SideEffectLinkedToNoteHash);
const newL2ToL1Msgs = reader.readFieldArray(MAX_NEW_L2_TO_L1_MSGS_PER_CALL);
const unencryptedLogsHash = reader.readFieldArray(NUM_FIELDS_PER_SHA256);
const unencryptedLogPreimagesLength = reader.readField();
const historicalHeader = Header.fromFields(reader);
const proverAddress = AztecAddress.fromFields(reader);

return new PublicCircuitPublicInputs(
callContext,
argsHash,
returnValues,
contractStorageUpdateRequests,
contractStorageReads,
publicCallStackHashes,
newCommitments,
newNullifiers,
newL2ToL1Msgs,
unencryptedLogsHash,
unencryptedLogPreimagesLength,
historicalHeader,
proverAddress,
);
}
}

0 comments on commit cb5d665

Please sign in to comment.