Skip to content

Commit

Permalink
Bumped circuits (#198)
Browse files Browse the repository at this point in the history
* Bumped circuits

* Updated snapshot

* Updated codebase with BaseOrMergePublicInputs

* New snapshot

* Test fixes

* Some TX wasm refactoring

* Fixed build issue

* Fixes

* Removed logging
  • Loading branch information
PhilWindle authored Apr 6, 2023
1 parent 039161a commit b84596d
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 137 deletions.
2 changes: 1 addition & 1 deletion circuits
Submodule circuits updated 33 files
+1 −1 cpp/barretenberg
+3 −1 cpp/cmake/barretenberg.cmake
+1 −0 cpp/dockerfiles/Dockerfile.wasm-linux-clang
+13 −11 cpp/src/aztec3/circuits/abis/c_bind.cpp
+23 −10 cpp/src/aztec3/circuits/abis/rollup/base/base_or_merge_rollup_public_inputs.hpp
+0 −59 cpp/src/aztec3/circuits/abis/rollup/base/previous_rollup_data.hpp
+0 −104 cpp/src/aztec3/circuits/abis/rollup/merge/merge_rollup_public_inputs.hpp
+6 −6 cpp/src/aztec3/circuits/abis/rollup/merge/previous_rollup_data.hpp
+1 −1 cpp/src/aztec3/circuits/abis/rollup/root/root_rollup_inputs.hpp
+1 −1 cpp/src/aztec3/circuits/apps/test_apps/basic_contract_deployment/basic_contract_deployment.cpp
+1 −1 cpp/src/aztec3/circuits/apps/test_apps/escrow/deposit.cpp
+1 −1 cpp/src/aztec3/circuits/apps/test_apps/escrow/transfer.cpp
+1 −1 cpp/src/aztec3/circuits/apps/test_apps/escrow/withdraw.cpp
+1 −1 cpp/src/aztec3/circuits/apps/test_apps/private_to_private_function_call/function_2_1.cpp
+50 −20 cpp/src/aztec3/circuits/kernel/private/.test.cpp
+18 −8 cpp/src/aztec3/circuits/kernel/private/native_private_kernel_circuit.cpp
+12 −7 cpp/src/aztec3/circuits/kernel/private/private_kernel_circuit.cpp
+0 −3 cpp/src/aztec3/circuits/mock/mock_kernel_circuit.hpp
+35 −40 cpp/src/aztec3/circuits/rollup/base/.test.cpp
+8 −24 cpp/src/aztec3/circuits/rollup/base/c_bind.cpp
+2 −3 cpp/src/aztec3/circuits/rollup/base/init.hpp
+7 −5 cpp/src/aztec3/circuits/rollup/base/native_base_rollup_circuit.cpp
+2 −2 cpp/src/aztec3/circuits/rollup/base/native_base_rollup_circuit.hpp
+1 −23 cpp/src/aztec3/circuits/rollup/base/utils.cpp
+0 −2 cpp/src/aztec3/circuits/rollup/base/utils.hpp
+170 −0 cpp/src/aztec3/circuits/rollup/merge/.test.cpp
+3 −2 cpp/src/aztec3/circuits/rollup/merge/init.hpp
+130 −11 cpp/src/aztec3/circuits/rollup/merge/native_merge_rollup_circuit.cpp
+1 −1 cpp/src/aztec3/circuits/rollup/merge/native_merge_rollup_circuit.hpp
+72 −0 cpp/src/aztec3/circuits/rollup/merge/utils.cpp
+17 −0 cpp/src/aztec3/circuits/rollup/merge/utils.hpp
+18 −17 cpp/src/aztec3/circuits/rollup/root/.test.cpp
+23 −4 cpp/src/aztec3/circuits/rollup/root/native_root_rollup_circuit.cpp
38 changes: 2 additions & 36 deletions yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -122,42 +122,8 @@ Object {
`;

exports[`abis wasm bindings computes contract leaf 1`] = `
Object {
"data": Array [
37,
164,
27,
46,
63,
9,
182,
227,
172,
238,
84,
38,
169,
47,
59,
48,
111,
49,
185,
159,
58,
31,
73,
162,
28,
126,
118,
148,
55,
26,
100,
78,
],
"type": "Buffer",
Fr {
"value": 13761632275218847273520262751574664149269181891159611676226614084913136324343n,
}
`;

Expand Down
9 changes: 5 additions & 4 deletions yarn-project/circuits.js/src/abis/abis.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Fr, FunctionData, NullifierLeafPreimage } from '../index.js';
import { fr, makeAztecAddress, makeBytes, makeTxRequest, makeVerificationKey } from '../tests/factories.js';
import { Fr, FunctionData, NewContractData } from '../index.js';
import { makeEthAddress } from '../tests/factories.js';
import { makeAztecAddress, makeBytes, makeTxRequest, makeVerificationKey } from '../tests/factories.js';
import { CircuitsWasm } from '../wasm/circuits_wasm.js';
import {
computeContractAddress,
Expand Down Expand Up @@ -72,8 +73,8 @@ describe('abis wasm bindings', () => {
});

it('computes contract leaf', async () => {
const leafPreImage = new NullifierLeafPreimage(fr(2), fr(2 + 0x100), 2 + 0x200);
const res = await computeContractLeaf(wasm, leafPreImage);
const cd = new NewContractData(makeAztecAddress(), makeEthAddress(), new Fr(3n));
const res = await computeContractLeaf(wasm, cd);
expect(res).toMatchSnapshot();
});
});
76 changes: 59 additions & 17 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
import { Buffer } from 'buffer';
import { AztecAddress, Fr, serializeBufferArrayToVector } from '@aztec/foundation';
import { CircuitsWasm } from '../wasm/index.js';
import { FunctionData, FUNCTION_SELECTOR_NUM_BYTES, NullifierLeafPreimage, TxRequest } from '../index.js';
import { FunctionData, FUNCTION_SELECTOR_NUM_BYTES, TxRequest, NewContractData } from '../index.js';
import { serializeToBuffer } from '../utils/serialize.js';

export async function wasmCall(
wasm: CircuitsWasm,
fnName: string,
input: { toBuffer: () => Buffer },
expectedOutputLength: number,
): Promise<Buffer> {
const inputData = input.toBuffer();
const outputBuf = wasm.call('bbmalloc', expectedOutputLength);
const inputBuf = wasm.call('bbmalloc', inputData.length);
wasm.writeMemory(inputBuf, inputData);
await wasm.asyncCall(fnName, inputBuf, outputBuf);
const buf = Buffer.from(wasm.getMemorySlice(outputBuf, outputBuf + expectedOutputLength));
wasm.call('bbfree', outputBuf);
wasm.call('bbfree', inputBuf);
return buf;
}

export async function hashTxRequest(wasm: CircuitsWasm, txRequest: TxRequest) {
const data = txRequest.toBuffer();
wasm.call('pedersen__init');
wasm.writeMemory(0, data);
await wasm.asyncCall('abis__hash_tx_request', 0, data.length);
return Buffer.from(wasm.getMemorySlice(data.length, data.length + 32));
return await wasmCall(wasm, 'abis__hash_tx_request', txRequest, 32);
}

export async function computeFunctionSelector(wasm: CircuitsWasm, funcSig: string) {
const buf = Buffer.from(funcSig);
wasm.writeMemory(0, buf);
await wasm.asyncCall('abis__compute_function_selector', 0, buf.length);
return Buffer.from(wasm.getMemorySlice(buf.length, buf.length + FUNCTION_SELECTOR_NUM_BYTES));
return await wasmCall(
wasm,
'abis__compute_function_selector',
{ toBuffer: () => Buffer.from(funcSig) },
FUNCTION_SELECTOR_NUM_BYTES,
);
}

export async function hashVK(wasm: CircuitsWasm, vkBuf: Buffer) {
wasm.call('pedersen__init');
wasm.writeMemory(0, vkBuf);
await wasm.asyncCall('abis__hash_vk', 0, vkBuf.length);
return Buffer.from(wasm.getMemorySlice(vkBuf.length, vkBuf.length + 32));
return await wasmCall(wasm, 'abis__hash_vk', { toBuffer: () => vkBuf }, 32);
}

export async function computeFunctionLeaf(wasm: CircuitsWasm, fnLeaf: Buffer) {
Expand All @@ -41,6 +55,28 @@ export async function computeFunctionTreeRoot(wasm: CircuitsWasm, fnLeafs: Buffe
return Buffer.from(wasm.getMemorySlice(inputVector.length, inputVector.length + 32));
}

// not yet working
// export async function inputBuffersToOutputBuffer(
// wasm: CircuitsWasm,
// fnName: string,
// buffers: Buffer[],
// expectedOutputLength: number,
// ) {
// const offsets: number[] = [];
// const totalLength = buffers.reduce((total, cur) => {
// offsets.push(total);
// return total + cur.length;
// }, 0);
// const inputBuf = wasm.call('bbmalloc', totalLength);
// const outputBuf = wasm.call('bbmalloc', expectedOutputLength);
// wasm.writeMemory(inputBuf, Buffer.concat(buffers));
// await wasm.asyncCall(fnName, ...offsets.map(x => x + inputBuf), outputBuf);
// const output = Buffer.from(wasm.getMemorySlice(outputBuf, outputBuf + expectedOutputLength));
// wasm.call('bbfree', inputBuf);
// wasm.call('bbfree', outputBuf);
// return output;
// }

export async function hashConstructor(
wasm: CircuitsWasm,
functionData: FunctionData,
Expand All @@ -59,6 +95,14 @@ export async function hashConstructor(
wasm.writeMemory(memLoc2, constructorVKHash);
await wasm.asyncCall('abis__hash_constructor', 0, memLoc1, memLoc2, memLoc3);
return Buffer.from(wasm.getMemorySlice(memLoc3, memLoc3 + 32));

// wasm.call('pedersen__init');
// return await inputBuffersToOutputBuffer(
// wasm,
// 'abis__hash_constructor',
// [functionData.toBuffer(), serializeToBuffer(args.map(fr => fr.toBuffer())), constructorVKHash],
// 32,
// );
}

export async function computeContractAddress(
Expand All @@ -83,10 +127,8 @@ export async function computeContractAddress(
return AztecAddress.fromBuffer(resultBuf);
}

export async function computeContractLeaf(wasm: CircuitsWasm, leafPreimage: NullifierLeafPreimage) {
const data = leafPreimage.toBuffer();
export async function computeContractLeaf(wasm: CircuitsWasm, cd: NewContractData) {
wasm.call('pedersen__init');
wasm.writeMemory(0, leafPreimage.toBuffer());
await wasm.asyncCall('abis__compute_contract_leaf', 0, data.length);
return Buffer.from(wasm.getMemorySlice(data.length, data.length + 32));
const value = await wasmCall(wasm, 'abis__compute_contract_leaf', { toBuffer: () => cd.toBuffer() }, 32);
return Fr.fromBuffer(value);
}
6 changes: 3 additions & 3 deletions yarn-project/circuits.js/src/rollup/rollup_wasm_wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BaseRollupInputs, BaseRollupPublicInputs, RootRollupInputs, RootRollupPublicInputs } from '../index.js';
import { BaseRollupInputs, BaseOrMergeRollupPublicInputs, RootRollupInputs, RootRollupPublicInputs } from '../index.js';
import { uint8ArrayToNum } from '../utils/serialize.js';
import { CircuitsWasm } from '../wasm/circuits_wasm.js';

export class RollupWasmWrapper {
constructor(private wasm: CircuitsWasm) {}

public simulateBaseRollup(baseRollupInputs: BaseRollupInputs): Promise<BaseRollupPublicInputs> {
return this.callWasm('base_rollup__sim', baseRollupInputs, BaseRollupPublicInputs);
public simulateBaseRollup(baseRollupInputs: BaseRollupInputs): Promise<BaseOrMergeRollupPublicInputs> {
return this.callWasm('base_rollup__sim', baseRollupInputs, BaseOrMergeRollupPublicInputs);
}

public simulateRootRollup(rootRollupInputs: RootRollupInputs): Promise<RootRollupPublicInputs> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,11 @@ merge_rollup_vk_hash: 0x404
`;

exports[`structs/base_rollup serializes and prints BaseRollupPublicInputs 1`] = `
"end_aggregation_object:
"rollup_type:
0
rollup_subtree_height:
0x0
end_aggregation_object:
P0: { 0x100, 0x101 }
P1: { 0x200, 0x201 }
public_inputs: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`structs/root_rollup serializes a RootRollupInput and prints it 1`] = `
"previous_rollup_data: [ merge_rollup_public_inputs: end_aggregation_object:
"previous_rollup_data: [ base_or_merge_rollup_public_inputs: rollup_type:
0
rollup_subtree_height:
0x0
end_aggregation_object:
P0: { 0x100, 0x101 }
P1: { 0x200, 0x201 }
public_inputs: [
Expand Down Expand Up @@ -58,20 +62,16 @@ next_available_leaf_index: 800
calldata_hash: [ 0x901 0x902 ]
proof: [ 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 ]
vk: key.composer_type: 0
key.circuit_size: 65
key.num_public_inputs: 66
key.commitments: [
A: { 0x200, 0x300 }
]
key.contains_recursive_proof: 1
key.recursive_proof_public_input_indices: [ 190 191 192 193 194 ]
vk: 0x12b300
vk_index: 110
vk_sibling_path: leaf_index: 120
sibling_path: [ 0x120 0x121 0x122 0x123 0x124 0x125 0x126 0x127 ]
merge_rollup_public_inputs: end_aggregation_object:
base_or_merge_rollup_public_inputs: rollup_type:
0
rollup_subtree_height:
0x0
end_aggregation_object:
P0: { 0x1100, 0x1101 }
P1: { 0x1200, 0x1201 }
public_inputs: [
Expand Down Expand Up @@ -128,15 +128,7 @@ next_available_leaf_index: 1800
calldata_hash: [ 0x1901 0x1902 ]
proof: [ 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 ]
vk: key.composer_type: 0
key.circuit_size: 65
key.num_public_inputs: 66
key.commitments: [
A: { 0x200, 0x300 }
]
key.contains_recursive_proof: 1
key.recursive_proof_public_input_indices: [ 190 191 192 193 194 ]
vk: 0x133e40
vk_index: 1110
vk_sibling_path: leaf_index: 1120
sibling_path: [ 0x1120 0x1121 0x1122 0x1123 0x1124 0x1125 0x1126 0x1127 ]
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/circuits.js/src/structs/base_rollup.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectReserializeToMatchObject, expectSerializeToMatchSnapshot } from '../tests/expectSerialize.js';
import { makeBaseRollupInputs, makeBaseRollupPublicInputs } from '../tests/factories.js';
import { BaseRollupPublicInputs } from './base_rollup.js';
import { BaseOrMergeRollupPublicInputs } from './base_rollup.js';

describe('structs/base_rollup', () => {
it(`serializes and prints BaseRollupInputs`, async () => {
Expand All @@ -16,7 +16,7 @@ describe('structs/base_rollup', () => {

await expectSerializeToMatchSnapshot(
baseRollupPublicInputs.toBuffer(),
'abis__test_roundtrip_serialize_base_rollup_public_inputs',
'abis__test_roundtrip_serialize_base_or_merge_rollup_public_inputs',
);
});

Expand All @@ -25,8 +25,8 @@ describe('structs/base_rollup', () => {

await expectReserializeToMatchObject(
baseRollupPublicInputs,
'abis__test_roundtrip_reserialize_base_rollup_public_inputs',
BaseRollupPublicInputs.fromBuffer,
'abis__test_roundtrip_reserialize_base_or_merge_rollup_public_inputs',
BaseOrMergeRollupPublicInputs.fromBuffer,
);
});
});
14 changes: 10 additions & 4 deletions yarn-project/circuits.js/src/structs/base_rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PRIVATE_DATA_TREE_ROOTS_TREE_HEIGHT,
} from './constants.js';
import { PreviousKernelData } from './kernel.js';
import { AggregationObject, MembershipWitness, UInt32 } from './shared.js';
import { AggregationObject, MembershipWitness, RollupTypes, UInt32 } from './shared.js';

export class NullifierLeafPreimage {
constructor(public leafValue: Fr, public nextValue: Fr, public nextIndex: UInt32) {}
Expand Down Expand Up @@ -168,8 +168,10 @@ export class BaseRollupInputs {
/**
* Output of the base rollup circuit
*/
export class BaseRollupPublicInputs {
export class BaseOrMergeRollupPublicInputs {
constructor(
public rollupType: RollupTypes,
public rollupSubTreeHeight: Fr,
public endAggregationObject: AggregationObject,
public constants: ConstantBaseRollupData,

Expand All @@ -190,9 +192,11 @@ export class BaseRollupPublicInputs {
* Deserializes from a buffer or reader, corresponding to a write in cpp.
* @param bufferReader - Buffer to read from.
*/
static fromBuffer(buffer: Buffer | BufferReader): BaseRollupPublicInputs {
static fromBuffer(buffer: Buffer | BufferReader): BaseOrMergeRollupPublicInputs {
const reader = BufferReader.asReader(buffer);
return new BaseRollupPublicInputs(
return new BaseOrMergeRollupPublicInputs(
reader.readNumber(),
reader.readFr(),
reader.readObject(AggregationObject),
reader.readObject(ConstantBaseRollupData),
reader.readObject(AppendOnlyTreeSnapshot),
Expand All @@ -211,6 +215,8 @@ export class BaseRollupPublicInputs {
*/
toBuffer() {
return serializeToBuffer(
this.rollupType,
this.rollupSubTreeHeight,
this.endAggregationObject,
this.constants,

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuits.js/src/structs/merge_rollup.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Fr } from '@aztec/foundation';
import { FieldsOf } from '../utils/jsUtils.js';
import { serializeToBuffer } from '../utils/serialize.js';
import { AppendOnlyTreeSnapshot, BaseRollupPublicInputs, ConstantBaseRollupData } from './base_rollup.js';
import { AppendOnlyTreeSnapshot, BaseOrMergeRollupPublicInputs, ConstantBaseRollupData } from './base_rollup.js';
import { ROLLUP_VK_TREE_HEIGHT } from './constants.js';
import { AggregationObject, MembershipWitness, RollupTypes, UInt32, UInt8Vector } from './shared.js';
import { VerificationKey } from './verification_key.js';

export class PreviousRollupData {
constructor(
public publicInputs: BaseRollupPublicInputs | MergeRollupPublicInputs,
public publicInputs: BaseOrMergeRollupPublicInputs | MergeRollupPublicInputs,
public proof: UInt8Vector,
public vk: VerificationKey,
/**
Expand Down
7 changes: 5 additions & 2 deletions yarn-project/circuits.js/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import {
AppendOnlyTreeSnapshot,
BaseRollupInputs,
BaseRollupPublicInputs,
BaseOrMergeRollupPublicInputs,
ConstantBaseRollupData,
NullifierLeafPreimage,
} from '../structs/base_rollup.js';
Expand Down Expand Up @@ -57,6 +57,7 @@ import {
ComposerType,
EcdsaSignature,
MembershipWitness,
RollupTypes,
UInt8Vector,
} from '../structs/shared.js';
import { ContractDeploymentData, SignedTxRequest, TxContext, TxRequest } from '../structs/tx.js';
Expand Down Expand Up @@ -264,7 +265,9 @@ export function makeEcdsaSignature(seed = 1): EcdsaSignature {
}

export function makeBaseRollupPublicInputs(seed = 0) {
return new BaseRollupPublicInputs(
return new BaseOrMergeRollupPublicInputs(
RollupTypes.Base,
new Fr(0n),
makeAggregationObject(seed + 0x100),
makeConstantBaseRollupData(seed + 0x200),
makeAppendOnlyTreeSnapshot(seed + 0x300),
Expand Down
Loading

0 comments on commit b84596d

Please sign in to comment.