Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Sep 3, 2024
1 parent f046fdb commit 0e3b36a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 19 deletions.
33 changes: 27 additions & 6 deletions yarn-project/circuits.js/src/structs/proof.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Fr } from '@aztec/bb.js';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';

import { AGGREGATION_OBJECT_LENGTH } from '../constants.gen.js';

const EMPTY_PROOF_SIZE = 42;

/**
Expand All @@ -12,6 +14,9 @@ const EMPTY_PROOF_SIZE = 42;
export class Proof {
// Make sure this type is not confused with other buffer wrappers
readonly __proofBrand: any;

readonly publicInputsOffset = 100;

constructor(
/**
* Holds the serialized proof data in a binary buffer format.
Expand Down Expand Up @@ -55,11 +60,27 @@ export class Proof {
}

public withoutPublicInputs(): Buffer {
if (this.numPublicInputs > 0) {
return this.buffer.subarray(Fr.SIZE_IN_BYTES * this.numPublicInputs);
} else {
return this.buffer;
}
console.log(
'indices',
this.publicInputsOffset - 4,
this.publicInputsOffset - 4 + Fr.SIZE_IN_BYTES * this.numPublicInputs,
);
return Buffer.concat([
this.buffer.subarray(4, this.publicInputsOffset),
this.buffer.subarray(this.publicInputsOffset + Fr.SIZE_IN_BYTES * this.numPublicInputs),
]);
}

public extractPublicInputs(): Fr[] {
const reader = BufferReader.asReader(
this.buffer.subarray(this.publicInputsOffset, this.publicInputsOffset + Fr.SIZE_IN_BYTES * this.numPublicInputs),
);
return reader.readArray(this.numPublicInputs, Fr);
}

public extractAggregationObject(): Fr[] {
const publicInputs = this.extractPublicInputs();
return publicInputs.slice(-1 * AGGREGATION_OBJECT_LENGTH);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,64 @@ import { getACVMConfig } from '../fixtures/get_acvm_config.js';
import { getBBConfig } from '../fixtures/get_bb_config.js';
import { getLogger, setupL1Contracts, startAnvil } from '../fixtures/utils.js';

describe('temp', () => {
it('maps', async () => {
const blockResult = JSON.parse(
await readFile(join(fileURLToPath(import.meta.url), '../../fixtures/dumps/block_result.json'), 'utf-8'),
);

const block = L2Block.fromString(blockResult.block);
const proof = Proof.fromString(blockResult.proof);
const proverId = Fr.fromString(blockResult.proverId);
const vkTreeRoot = Fr.fromString(blockResult.vkTreeRoot);
const aggregationObject = blockResult.aggregationObject.map((x: string) => Fr.fromString(x));
const reader = BufferReader.asReader(proof.buffer.subarray(4));
const [circuitSize, numPublicInputs, publicInputsOffset] = reader.readArray(3, Fr);
console.log('reader index after header', reader.index);
const publicInputsFromProof = reader.readArray(numPublicInputs.toNumber(), Fr).map(x => x.toString());
console.log('reader index after pub inputs', reader.index);

const proofStr = `0x${Buffer.concat([
circuitSize.toBuffer(),
numPublicInputs.toBuffer(),
publicInputsOffset.toBuffer(),
reader.readToEnd(),
]).toString('hex')}` as const;

const publicInputsFromBlock = [
block.header.lastArchive.root,
block.header.globalVariables.blockNumber,
block.archive.root,
new Fr(block.archive.nextAvailableLeafIndex),
Fr.ZERO, // prev block hash
block.hash(),
...block.header.globalVariables.toFields(), // start global vars
...block.header.globalVariables.toFields(), // end global vars
new Fr(block.header.contentCommitment.outHash),
block.header.globalVariables.coinbase.toField(), // the fee taker's address
block.header.totalFees, // how much they got
...Array(62).fill(Fr.ZERO), // 31 other (fee takers, fee) pairs
vkTreeRoot,
proverId, // 0x51
...aggregationObject,
].map((x: Fr) => x.toString());

const publicInputs = proof.extractPublicInputs().map(x => x.toString());

expect(publicInputs.length).toEqual(publicInputsFromProof.length);
expect(publicInputs.slice(0, 27)).toEqual(publicInputsFromProof.slice(0, 27));
expect(publicInputs.slice(27, 89)).toEqual(publicInputsFromProof.slice(27, 89));
expect(publicInputs.slice(89, 91)).toEqual(publicInputsFromProof.slice(89, 91));
expect(publicInputs.slice(91)).toEqual(publicInputsFromProof.slice(91));

const actual = '0x' + proof.withoutPublicInputs().toString('hex');
expect(actual.length).toEqual(proofStr.length);
expect(actual).toEqual(proofStr);
expect(proof.extractAggregationObject()).toEqual(aggregationObject);
// expect(proof.extractPublicInputs()).toEqual(publicInputs);
});
});

/**
* Regenerate this test's fixture with
* AZTEC_GENERATE_TEST_DATA=1 yarn workspace @aztec/end-to-end test e2e_prover
Expand Down Expand Up @@ -150,14 +208,6 @@ describe('proof_verification', () => {
const [circuitSize, numPublicInputs, publicInputsOffset] = reader.readArray(3, Fr);
const publicInputs = reader.readArray(numPublicInputs.toNumber(), Fr).map(x => x.toString());

console.log({
circuitSize: circuitSize.toString(),
numPublicInputs: numPublicInputs.toString(),
publicInputsOffset: publicInputsOffset.toString(),
binaryProofSize: proof.buffer.length,
publicInputsLength: publicInputs.length,
});

const proofStr = `0x${Buffer.concat([
circuitSize.toBuffer(),
numPublicInputs.toBuffer(),
Expand Down Expand Up @@ -200,8 +250,6 @@ describe('proof_verification', () => {
...aggregationObject,
].map((x: Fr) => x.toString());

console.log({ len: publicInputsFromProof.length, publicInputsFromProof });
console.log({ len: publicInputs.length, publicInputs });
expect(publicInputs.length).toEqual(publicInputsFromProof.length);
expect(publicInputs.slice(0, 27)).toEqual(publicInputsFromProof.slice(0, 27));
expect(publicInputs.slice(27, 89)).toEqual(publicInputsFromProof.slice(27, 89));
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_prover/full.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('full_prover', () => {
await t.applyBaseSnapshots();
await t.applyMintSnapshot();
await t.setup();
// await t.deployVerifier();
await t.deployVerifier();
({ provenAssets, accounts, tokenSim, logger } = t);
});

Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion yarn-project/foundation/src/serialize/buffer_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { type Tuple } from './types.js';
* const byteArray = reader.readBytes(4);
*/
export class BufferReader {
private index: number;
public index: number;
constructor(private buffer: Buffer, offset = 0) {
this.index = offset;
}
Expand Down

0 comments on commit 0e3b36a

Please sign in to comment.