Skip to content

Commit

Permalink
feat: regenerate fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed May 21, 2024
1 parent 13e488f commit 31f6d51
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 47 deletions.
2 changes: 1 addition & 1 deletion yarn-project/bb-prover/src/prover/bb_prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export class BBNativeRollupProver implements ServerCircuitProver {
await fs.rm(bbWorkingDirectory, { recursive: true, force: true });

const output = convertOutput(outputWitness);
const proof = new Proof(rawProof);
const proof = new Proof(rawProof, vkData.numPublicInputs);
logger.info(
`Generated proof for ${circuitType} in ${provingResult.duration} ms, size: ${proof.buffer.length} fields`,
{
Expand Down
18 changes: 15 additions & 3 deletions yarn-project/circuits.js/src/structs/proof.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Fr } from '@aztec/bb.js';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';

const EMPTY_PROOF_SIZE = 42;
Expand All @@ -16,6 +17,8 @@ export class Proof {
* Holds the serialized proof data in a binary buffer format.
*/
public buffer: Buffer,

public numPublicInputs = 0,
) {}

/**
Expand All @@ -27,9 +30,10 @@ export class Proof {
*/
static fromBuffer(buffer: Buffer | BufferReader): Proof {
const reader = BufferReader.asReader(buffer);
const numPublicInputs = reader.readNumber();
const size = reader.readNumber();
const buf = reader.readBytes(size);
return new Proof(buf);
return new Proof(buf, numPublicInputs);
}

/**
Expand All @@ -39,7 +43,7 @@ export class Proof {
* @returns A Buffer containing the serialized proof data in custom format.
*/
public toBuffer() {
return serializeToBuffer(this.buffer.length, this.buffer);
return serializeToBuffer(this.numPublicInputs, this.buffer.length, this.buffer);
}

/**
Expand All @@ -50,6 +54,14 @@ export class Proof {
return this.toBuffer().toString('hex');
}

public withoutPublicInputs(): Buffer {
if (this.numPublicInputs > 0) {
return this.buffer.subarray(Fr.SIZE_IN_BYTES * this.numPublicInputs);
} else {
return this.buffer;
}
}

/**
* Deserialize a Proof instance from a hex string.
* @param str - A hex string to deserialize from.
Expand All @@ -66,5 +78,5 @@ export class Proof {
* @returns The empty "proof".
*/
export function makeEmptyProof() {
return new Proof(Buffer.alloc(EMPTY_PROOF_SIZE, 0));
return new Proof(Buffer.alloc(EMPTY_PROOF_SIZE, 0), 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BBCircuitVerifier } from '@aztec/bb-prover';
import { Fr, HEADER_LENGTH, Proof } from '@aztec/circuits.js';
import { type L1ContractAddresses } from '@aztec/ethereum';
import { type Logger } from '@aztec/foundation/log';
import { BufferReader } from '@aztec/foundation/serialize';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { AvailabilityOracleAbi, RollupAbi } from '@aztec/l1-artifacts';

import { type Anvil } from '@viem/anvil';
Expand All @@ -28,9 +28,17 @@ import { getACVMConfig } from '../fixtures/get_acvm_config.js';
import { getBBConfig } from '../fixtures/get_bb_config.js';
import { getLogger, setupL1Contracts, startAnvil } from '../fixtures/utils.js';

/**
* README
*
* If this test starts failing, regenerate its fixture with
* AZTEC_GENERATE_TEST_DATA=1 yarn workspace @aztec/end-to-end test e2e_prover
*/

describe('proof_verification', () => {
let proof: Proof;
let block: L2Block;
let aggregationObject: Fr[];
let anvil: Anvil | undefined;
let rpcUrl: string;
let walletClient: WalletClient<HttpTransport, Chain, Account>;
Expand Down Expand Up @@ -112,12 +120,15 @@ describe('proof_verification', () => {
});

beforeEach(async () => {
proof = Proof.fromString(
await readFile(join(fileURLToPath(import.meta.url), '../../fixtures/dumps/proof.txt'), 'utf-8'),
);
block = L2Block.fromString(
await readFile(join(fileURLToPath(import.meta.url), '../../fixtures/dumps/block.txt'), 'utf-8'),
// regenerate with
// AZTEC_GENERATE_TEST_DATA=1 yarn workspace @aztec/end-to-end test e2e_prover
const blockResult = JSON.parse(
await readFile(join(fileURLToPath(import.meta.url), '../../fixtures/dumps/block_result.json'), 'utf-8'),
);

block = L2Block.fromString(blockResult.block);
proof = Proof.fromString(blockResult.proof);
aggregationObject = blockResult.aggregationObject.map((x: string) => Fr.fromString(x));
});

describe('bb', () => {
Expand All @@ -127,7 +138,7 @@ describe('proof_verification', () => {
});

describe('UltraVerifier', () => {
it('verifies proof', async () => {
it('verifies full proof', async () => {
const reader = BufferReader.asReader(proof.buffer);
// +2 fields for archive
const archive = reader.readArray(2, Fr);
Expand All @@ -144,22 +155,12 @@ describe('proof_verification', () => {
});

it('verifies proof taking public inputs from block', async () => {
const aggObject = proof.buffer.subarray(
Fr.SIZE_IN_BYTES * (HEADER_LENGTH + 2),
Fr.SIZE_IN_BYTES * (HEADER_LENGTH + 2 + AGGREGATION_OBJECT_SIZE),
const proofStr = `0x${proof.withoutPublicInputs().toString('hex')}`;
const publicInputs = [...block.archive.toFields(), ...block.header.toFields(), ...aggregationObject].map(x =>
x.toString(),
);

const justTheProof = `0x${proof.buffer
.subarray(Fr.SIZE_IN_BYTES * (AGGREGATION_OBJECT_SIZE + HEADER_LENGTH + 2))
.toString('hex')}` as const;

const publicInputs = [
...block.archive.toFields(),
...block.header.toFields(),
...BufferReader.asReader(aggObject).readArray(16, Fr),
].map(x => x.toString());

await expect(verifierContract.read.verify([justTheProof, publicInputs])).resolves.toBeTruthy();
await expect(verifierContract.read.verify([proofStr, publicInputs])).resolves.toBeTruthy();
});
});

Expand All @@ -184,20 +185,14 @@ describe('proof_verification', () => {
});

it('verifies proof', async () => {
const aggregationObject = proof.buffer.subarray(
Fr.SIZE_IN_BYTES * (HEADER_LENGTH + 2),
Fr.SIZE_IN_BYTES * (HEADER_LENGTH + 2 + AGGREGATION_OBJECT_SIZE),
);
const proofBuffer = proof.buffer.subarray(Fr.SIZE_IN_BYTES * (HEADER_LENGTH + 2 + AGGREGATION_OBJECT_SIZE));

await availabilityContract.write.publish([`0x${block.body.toBuffer().toString('hex')}`]);

await expect(
rollupContract.write.process([
`0x${block.header.toBuffer().toString('hex')}`,
`0x${block.archive.root.toBuffer().toString('hex')}`,
`0x${aggregationObject.toString('hex')}`,
`0x${proofBuffer.toString('hex')}`,
`0x${serializeToBuffer(aggregationObject).toString('hex')}`,
`0x${proof.withoutPublicInputs().toString('hex')}`,
]),
).resolves.toBeDefined();
});
Expand Down
24 changes: 23 additions & 1 deletion yarn-project/end-to-end/src/e2e_prover/e2e_prover.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Tx } from '@aztec/aztec.js';
import { type Fr, type Tx } from '@aztec/aztec.js';
import { type BBNativeProofCreator } from '@aztec/bb-prover';
import { getTestData, isGenerateTestDataEnabled, writeTestData } from '@aztec/foundation/testing';
import { type ClientProtocolArtifact } from '@aztec/noir-protocol-circuits-types';

import { FullProverTest } from './e2e_prover_test.js';
Expand Down Expand Up @@ -73,6 +74,27 @@ describe('full_prover', () => {
]);
tokenSim.transferPrivate(accounts[0].address, accounts[1].address, privateSendAmount);
tokenSim.transferPublic(accounts[0].address, accounts[1].address, publicSendAmount);

if (isGenerateTestDataEnabled()) {
const blockResults = getTestData('blockResults');
// the first blocks were setup blocks with fake proofs
// the last block is the one that was actually proven to the end
const blockResult: any = blockResults.at(-1);

if (!blockResult) {
// fail the test. User asked for fixtures but we don't have any
throw new Error('No block result found in test data');
}

writeTestData(
'yarn-project/end-to-end/src/fixtures/dumps/block_result.json',
JSON.stringify({
block: blockResult.block.toString(),
proof: blockResult.proof.toString(),
aggregationObject: blockResult.aggregationObject.map((x: Fr) => x.toString()),
}),
);
}
},
TIMEOUT,
);
Expand Down
1 change: 0 additions & 1 deletion yarn-project/end-to-end/src/fixtures/dumps/block.txt

This file was deleted.

Loading

0 comments on commit 31f6d51

Please sign in to comment.