Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix circulars in foundation. Also cleanup fields and optimise to be buffer underlying. #3351

Merged
merged 16 commits into from
Nov 19, 2023
Merged
2 changes: 1 addition & 1 deletion yarn-project/acir-simulator/src/acvm/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, ac
witnessReader.readField(),
witnessReader.readField(),
);
const proverAddress = AztecAddress.fromField(witnessReader.readField());
const proverAddress = new AztecAddress(witnessReader.readField());

return new PublicCircuitPublicInputs(
callContext,
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/acir-simulator/src/acvm/oracle/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Oracle {

async getPublicKeyAndPartialAddress([address]: ACVMField[]) {
const { publicKey, partialAddress } = await this.typedOracle.getCompleteAddress(
AztecAddress.fromField(fromACVMField(address)),
new AztecAddress(fromACVMField(address)),
);
return [publicKey.x, publicKey.y, partialAddress].map(toACVMField);
}
Expand Down Expand Up @@ -190,7 +190,7 @@ export class Oracle {
[argsHash]: ACVMField[],
): Promise<ACVMField[]> {
const callStackItem = await this.typedOracle.callPrivateFunction(
AztecAddress.fromField(fromACVMField(contractAddress)),
new AztecAddress(fromACVMField(contractAddress)),
FunctionSelector.fromField(fromACVMField(functionSelector)),
fromACVMField(argsHash),
);
Expand All @@ -203,7 +203,7 @@ export class Oracle {
[argsHash]: ACVMField[],
): Promise<ACVMField[]> {
const returnValues = await this.typedOracle.callPublicFunction(
AztecAddress.fromField(fromACVMField(contractAddress)),
new AztecAddress(fromACVMField(contractAddress)),
FunctionSelector.fromField(fromACVMField(functionSelector)),
fromACVMField(argsHash),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class PackedArgsCache {
constructor(initialArguments: PackedArguments[] = []) {
this.cache = new Map();
for (const initialArg of initialArguments) {
this.cache.set(initialArg.hash.value, initialArg.args);
this.cache.set(initialArg.hash.toBigInt(), initialArg.args);
}
}

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/acir-simulator/src/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AztecAddress, EthAddress, Fr } from '@aztec/circuits.js';
import { computeSecretMessageHash } from '@aztec/circuits.js/abis';
import { ContractArtifact, FunctionSelector, getFunctionDebugMetadata } from '@aztec/foundation/abi';
import { sha256ToField } from '@aztec/foundation/crypto';
import { sha256 } from '@aztec/foundation/crypto';
import { L1Actor, L1ToL2Message, L2Actor } from '@aztec/types';

import { FunctionArtifactWithDebugMetadata } from '../index.js';
Expand All @@ -24,7 +24,7 @@ export const buildL1ToL2Message = (
const selectorBuf = Buffer.from(selector, 'hex');

const contentBuf = Buffer.concat([selectorBuf, ...contentPreimage.map(field => field.toBuffer())]);
const content = sha256ToField(contentBuf);
const content = Fr.fromBufferReduce(sha256(contentBuf));

const secretHash = computeSecretMessageHash(secret);

Expand Down
36 changes: 18 additions & 18 deletions yarn-project/archiver/src/archiver/archiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,24 @@ describe('Archiver', () => {
// Archiver removes such messages from pending store.
// Also create some more messages to cancel and some that will stay pending.

const messageToCancel1 = Fr.random().toString(true);
const messageToCancel2 = Fr.random().toString(true);
const messageToCancel1 = Fr.random().toString();
const messageToCancel2 = Fr.random().toString();
const l1ToL2MessagesToCancel = [messageToCancel1, messageToCancel2];
const messageToStayPending1 = Fr.random().toString(true);
const messageToStayPending2 = Fr.random().toString(true);
const messageToStayPending1 = Fr.random().toString();
const messageToStayPending2 = Fr.random().toString();

const l1ToL2MessageAddedEvents = [
makeL1ToL2MessageAddedEvents(
100n,
blocks[0].newL1ToL2Messages.map(key => key.toString(true)),
blocks[0].newL1ToL2Messages.map(key => key.toString()),
),
makeL1ToL2MessageAddedEvents(
100n,
blocks[1].newL1ToL2Messages.map(key => key.toString(true)),
blocks[1].newL1ToL2Messages.map(key => key.toString()),
),
makeL1ToL2MessageAddedEvents(
2501n,
blocks[2].newL1ToL2Messages.map(key => key.toString(true)),
blocks[2].newL1ToL2Messages.map(key => key.toString()),
),
makeL1ToL2MessageAddedEvents(2502n, [
messageToCancel1,
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('Archiver', () => {
l1ToL2MessageAddedEvents[3][2].args.entryKey,
l1ToL2MessageAddedEvents[3][3].args.entryKey,
];
const actualPendingMessageKeys = (await archiver.getPendingL1ToL2Messages(10)).map(key => key.toString(true));
const actualPendingMessageKeys = (await archiver.getPendingL1ToL2Messages(10)).map(key => key.toString());
expect(expectedPendingMessageKeys).toEqual(actualPendingMessageKeys);

// Expect logs to correspond to what is set by L2Block.random(...)
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('Archiver', () => {
expect(latestBlockNum).toEqual(0);

const createL1ToL2Messages = () => {
return [Fr.random().toString(true), Fr.random().toString(true)];
return [Fr.random().toString(), Fr.random().toString()];
};

const blocks = blockNumbers.map(x => L2Block.random(x, 4, x, x + 1, x * 2, x * 3));
Expand All @@ -163,11 +163,11 @@ describe('Archiver', () => {
const l1ToL2MessageAddedEvents = [
makeL1ToL2MessageAddedEvents(
100n,
blocks[0].newL1ToL2Messages.map(key => key.toString(true)),
blocks[0].newL1ToL2Messages.map(key => key.toString()),
),
makeL1ToL2MessageAddedEvents(
101n,
blocks[1].newL1ToL2Messages.map(key => key.toString(true)),
blocks[1].newL1ToL2Messages.map(key => key.toString()),
),
makeL1ToL2MessageAddedEvents(102n, additionalL1ToL2MessagesBlock102),
makeL1ToL2MessageAddedEvents(103n, additionalL1ToL2MessagesBlock103),
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('Archiver', () => {

// Check that the only pending L1 to L2 messages are those from eth bock 102
const expectedPendingMessageKeys = additionalL1ToL2MessagesBlock102;
const actualPendingMessageKeys = (await archiver.getPendingL1ToL2Messages(100)).map(key => key.toString(true));
const actualPendingMessageKeys = (await archiver.getPendingL1ToL2Messages(100)).map(key => key.toString());
expect(actualPendingMessageKeys).toEqual(expectedPendingMessageKeys);

await archiver.stop();
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('Archiver', () => {
.mockResolvedValueOnce(
makeL1ToL2MessageAddedEvents(
100n,
block.newL1ToL2Messages.map(x => x.toString(true)),
block.newL1ToL2Messages.map(x => x.toString()),
),
)
.mockResolvedValueOnce([])
Expand Down Expand Up @@ -292,9 +292,9 @@ function makeContractDeploymentEvent(l1BlockNum: bigint, l2Block: L2Block) {
aztecAddress: extendedContractData.contractData.contractAddress.toString(),
portalAddress: extendedContractData.contractData.portalContractAddress.toString(),
l2BlockHash: `0x${l2Block.getCalldataHash().toString('hex')}`,
partialAddress: extendedContractData.partialAddress.toString(true),
pubKeyX: extendedContractData.publicKey.x.toString(true),
pubKeyY: extendedContractData.publicKey.y.toString(true),
partialAddress: extendedContractData.partialAddress.toString(),
pubKeyX: extendedContractData.publicKey.x.toString(),
pubKeyY: extendedContractData.publicKey.y.toString(),
acir: '0x' + acir,
},
transactionHash: `0x${l2Block.number}`,
Expand All @@ -316,8 +316,8 @@ function makeL1ToL2MessageAddedEvents(l1BlockNum: bigint, entryKeys: string[]) {
senderChainId: 1n,
recipient: AztecAddress.random().toString(),
recipientVersion: 1n,
content: Fr.random().toString(true),
secretHash: Fr.random().toString(true),
content: Fr.random().toString(),
secretHash: Fr.random().toString(),
deadline: 100,
fee: 1n,
entryKey: entryKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export class FunctionLeafPreimage {
reader.readObject(FunctionSelector),
reader.readBoolean(),
reader.readBoolean(),
reader.readFr(),
reader.readFr(),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
);
}
}
7 changes: 6 additions & 1 deletion yarn-project/circuits.js/src/structs/global_variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export class GlobalVariables {

static fromBuffer(buffer: Buffer | BufferReader): GlobalVariables {
const reader = BufferReader.asReader(buffer);
return new GlobalVariables(reader.readFr(), reader.readFr(), reader.readFr(), reader.readFr());
return new GlobalVariables(
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
);
}

static fromJSON(obj: any): GlobalVariables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export class NewContractData {
*/
static fromBuffer(buffer: Buffer | BufferReader): NewContractData {
const reader = BufferReader.asReader(buffer);
return new NewContractData(reader.readObject(AztecAddress), new EthAddress(reader.readBytes(32)), reader.readFr());
return new NewContractData(
reader.readObject(AztecAddress),
new EthAddress(reader.readBytes(32)),
Fr.fromBuffer(reader),
);
}

static empty() {
Expand Down Expand Up @@ -142,9 +146,9 @@ export class OptionallyRevealedData {
static fromBuffer(buffer: Buffer | BufferReader): OptionallyRevealedData {
const reader = BufferReader.asReader(buffer);
return new OptionallyRevealedData(
reader.readFr(),
Fr.fromBuffer(reader),
reader.readObject(FunctionData),
reader.readFr(),
Fr.fromBuffer(reader),
new EthAddress(reader.readBytes(32)),
reader.readBoolean(),
reader.readBoolean(),
Expand Down Expand Up @@ -205,7 +209,7 @@ export class PublicDataRead {

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new PublicDataRead(reader.readFr(), reader.readFr());
return new PublicDataRead(Fr.fromBuffer(reader), Fr.fromBuffer(reader));
}

static empty() {
Expand Down Expand Up @@ -263,7 +267,7 @@ export class PublicDataUpdateRequest {

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new PublicDataUpdateRequest(reader.readFr(), reader.readFr(), reader.readFr());
return new PublicDataUpdateRequest(Fr.fromBuffer(reader), Fr.fromBuffer(reader), Fr.fromBuffer(reader));
}

static empty() {
Expand Down Expand Up @@ -398,8 +402,8 @@ export class CombinedAccumulatedData {
reader.readArray(MAX_NEW_L2_TO_L1_MSGS_PER_TX, Fr),
reader.readArray(2, Fr),
reader.readArray(2, Fr),
reader.readFr(),
reader.readFr(),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
reader.readArray(MAX_NEW_CONTRACTS_PER_TX, NewContractData),
reader.readArray(MAX_OPTIONALLY_REVEALED_DATA_LENGTH_PER_TX, OptionallyRevealedData),
reader.readArray(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataUpdateRequest),
Expand Down Expand Up @@ -563,8 +567,8 @@ export class FinalAccumulatedData {
reader.readArray(MAX_NEW_L2_TO_L1_MSGS_PER_TX, Fr),
reader.readArray(2, Fr),
reader.readArray(2, Fr),
reader.readFr(),
reader.readFr(),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
reader.readArray(MAX_NEW_CONTRACTS_PER_TX, NewContractData),
reader.readArray(MAX_OPTIONALLY_REVEALED_DATA_LENGTH_PER_TX, OptionallyRevealedData),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ export class HistoricBlockData {
static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new HistoricBlockData(
reader.readFr(),
reader.readFr(),
reader.readFr(),
reader.readFr(),
reader.readFr(),
reader.readFr(),
reader.readFr(),
reader.readFr(),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class ContractStorageRead {

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new ContractStorageRead(reader.readFr(), reader.readFr());
return new ContractStorageRead(Fr.fromBuffer(reader), Fr.fromBuffer(reader));
}

static empty() {
Expand Down Expand Up @@ -110,7 +110,7 @@ export class ContractStorageUpdateRequest {

static fromBuffer(buffer: Buffer | BufferReader) {
const reader = BufferReader.asReader(buffer);
return new ContractStorageUpdateRequest(reader.readFr(), reader.readFr(), reader.readFr());
return new ContractStorageUpdateRequest(Fr.fromBuffer(reader), Fr.fromBuffer(reader), Fr.fromBuffer(reader));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ReadRequestMembershipWitness {
*/
public hintToCommitment: Fr,
) {
if (hintToCommitment.value > MAX_NEW_COMMITMENTS_PER_CALL) {
if (hintToCommitment.toBigInt() > MAX_NEW_COMMITMENTS_PER_CALL) {
throw new Error(
`Expected ReadRequestMembershipWitness' hintToCommitment(${hintToCommitment}) to be <= NEW_COMMITMENTS_LENGTH(${MAX_NEW_COMMITMENTS_PER_CALL})`,
);
Expand Down Expand Up @@ -124,10 +124,10 @@ export class ReadRequestMembershipWitness {
*/
static fromBuffer(buffer: Buffer | BufferReader): ReadRequestMembershipWitness {
const reader = BufferReader.asReader(buffer);
const leafIndex = reader.readFr();
const leafIndex = Fr.fromBuffer(reader);
const siblingPath = reader.readArray<Fr, typeof NOTE_HASH_TREE_HEIGHT>(NOTE_HASH_TREE_HEIGHT, Fr);
const isTransient = reader.readBoolean();
const hintToCommitment = reader.readFr();
const hintToCommitment = Fr.fromBuffer(reader);
return new ReadRequestMembershipWitness(leafIndex, siblingPath, isTransient, hintToCommitment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class AppendOnlyTreeSnapshot {

static fromBuffer(buffer: Buffer | BufferReader): AppendOnlyTreeSnapshot {
const reader = BufferReader.asReader(buffer);
return new AppendOnlyTreeSnapshot(reader.readFr(), reader.readNumber());
return new AppendOnlyTreeSnapshot(Fr.fromBuffer(reader), reader.readNumber());
}

static fromString(str: string): AppendOnlyTreeSnapshot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class BaseOrMergeRollupPublicInputs {
const reader = BufferReader.asReader(buffer);
return new BaseOrMergeRollupPublicInputs(
reader.readNumber(),
reader.readFr(),
Fr.fromBuffer(reader),
reader.readObject(AggregationObject),
reader.readObject(ConstantRollupData),
reader.readObject(AppendOnlyTreeSnapshot),
Expand All @@ -94,8 +94,8 @@ export class BaseOrMergeRollupPublicInputs {
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readFr(),
reader.readFr(),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
reader.readArray(NUM_FIELDS_PER_SHA256, Fr) as [Fr, Fr],
);
}
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/circuits.js/src/structs/rollup/base_rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ export class ConstantRollupData {
const reader = BufferReader.asReader(buffer);
return new ConstantRollupData(
reader.readObject(AppendOnlyTreeSnapshot),
reader.readFr(),
reader.readFr(),
reader.readFr(),
reader.readFr(),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
reader.readObject(GlobalVariables),
);
}
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/circuits.js/src/structs/rollup/root_rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ export class RootRollupPublicInputs {
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readFr(),
reader.readFr(),
Fr.fromBuffer(reader),
Fr.fromBuffer(reader),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
Expand All @@ -254,8 +254,8 @@ export class RootRollupPublicInputs {
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
[reader.readFr(), reader.readFr()],
[reader.readFr(), reader.readFr()],
[Fr.fromBuffer(reader), Fr.fromBuffer(reader)],
[Fr.fromBuffer(reader), Fr.fromBuffer(reader)],
);
}
}
Loading