Skip to content

Commit

Permalink
chore: share state manager and side effect tracer for all enqueued ca…
Browse files Browse the repository at this point in the history
…lls in tx
  • Loading branch information
dbanks12 committed Nov 6, 2024
1 parent 4c51c75 commit 21635e6
Show file tree
Hide file tree
Showing 29 changed files with 1,668 additions and 1,169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ namespace bb::avm_trace {
using FF = AvmFlavorSettings::FF;
using AffinePoint = grumpkin::g1::affine_element;

struct EnqueuedCallHint {
FF contract_address;
std::vector<FF> calldata;

MSGPACK_FIELDS(contract_address, calldata);
};

struct ExternalCallHint {
FF success;
std::vector<FF> return_data;
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ library Constants {
uint256 internal constant DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
14061769416655647708490531650437236735160113654556896985372298487345;
uint256 internal constant DEFAULT_GAS_LIMIT = 1000000000;
uint256 internal constant DEFAULT_TEARDOWN_GAS_LIMIT = 100000000;
uint256 internal constant DEFAULT_TEARDOWN_GAS_LIMIT = 12000000;
uint256 internal constant MAX_L2_GAS_PER_ENQUEUED_CALL = 12000000;
uint256 internal constant DEFAULT_MAX_FEE_PER_GAS = 10;
uint256 internal constant DEFAULT_INCLUSION_FEE = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl EnqueuedCallDataValidator {
) {
self.validate_global_variables(previous_kernel);
self.validate_against_call_request(previous_kernel);
self.validate_counters(previous_kernel);
self.validate_start_gas(previous_kernel);
self.validate_transaction_fee(previous_kernel);
self.validate_array_lengths(
Expand Down Expand Up @@ -86,14 +85,6 @@ impl EnqueuedCallDataValidator {
);
}

fn validate_counters(self, previous_kernel: PublicKernelCircuitPublicInputs) {
assert_eq(
self.enqueued_call.data.start_side_effect_counter,
previous_kernel.end_side_effect_counter + 1,
"enqueued call must start from the end counter of the previous call",
);
}

// Validates that the start gas injected into the vm circuit matches the remaining gas.
fn validate_start_gas(self, previous_kernel: PublicKernelCircuitPublicInputs) {
let enqueued_call_start_gas = self.enqueued_call.data.start_gas_left;
Expand Down Expand Up @@ -165,7 +156,7 @@ impl EnqueuedCallDataValidator {
assert_eq(
self.enqueued_call.data.previous_accumulated_data_array_lengths,
prev_lengths,
"mismatch previoius_accumulated_data_array_lengths",
"mismatch previous_accumulated_data_array_lengths",
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn propagate_public_data_writes(
let writes = public_call.contract_storage_update_requests;
for i in 0..writes.len() {
let write = writes[i];
// WARNING: What does this check accomplish?
if write.counter != 0 {
data.public_data_update_requests.push(
PublicDataUpdateRequest::from_contract_storage_update_request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ global DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =

// GAS DEFAULTS
global DEFAULT_GAS_LIMIT: u32 = 1_000_000_000;
global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 100_000_000;
global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 12_000_000;
global MAX_L2_GAS_PER_ENQUEUED_CALL: u32 = 12_000_000;
global DEFAULT_MAX_FEE_PER_GAS: Field = 10;
global DEFAULT_INCLUSION_FEE: Field = 0;
Expand Down
62 changes: 62 additions & 0 deletions noir-projects/noir-protocol-circuits/pubkern.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash
set -eu

cd "$(dirname "$0")"

CMD=${1:-}

if [ -n "$CMD" ]; then
if [ "$CMD" = "clean" ]; then
git clean -fdx
exit 0
else
echo "Unknown command: $CMD"
exit 1
fi
fi

yarn
node ./scripts/generate_variants.js

NARGO=${NARGO:-../../noir/noir-repo/target/release/nargo}
echo "Compiling protocol circuits with ${RAYON_NUM_THREADS:-1} threads"
RAYON_NUM_THREADS=${RAYON_NUM_THREADS:-1} $NARGO compile --silence-warnings --package public_kernel_merge_simulated

BB_HASH=${BB_HASH:-$(cd ../../ && git ls-tree -r HEAD | grep 'barretenberg/cpp' | awk '{print $3}' | git hash-object --stdin)}
echo Using BB hash $BB_HASH
mkdir -p "./target/keys"

#AVAILABLE_MEMORY=0
#
#case "$(uname)" in
# Linux*)
# # Check available memory on Linux
# AVAILABLE_MEMORY=$(awk '/MemTotal/ { printf $2 }' /proc/meminfo)
# ;;
# *)
# echo "Parallel vk generation not supported on this operating system"
# ;;
#esac
## This value may be too low.
## If vk generation fail with an amount of free memory greater than this value then it should be increased.
#MIN_PARALLEL_VK_GENERATION_MEMORY=500000000
#PARALLEL_VK=${PARALLEL_VK:-true}
#
#if [[ AVAILABLE_MEMORY -gt MIN_PARALLEL_VK_GENERATION_MEMORY ]] && [[ $PARALLEL_VK == "true" ]]; then
# echo "Generating vks in parallel..."
# for pathname in "./target"/*.json; do
# BB_HASH=$BB_HASH node ../scripts/generate_vk_json.js "$pathname" "./target/keys" &
# done
#
# for job in $(jobs -p); do
# wait $job || exit 1
# done
#
#else
# echo "System does not have enough memory for parallel vk generation, falling back to sequential"
echo "generating vk"

# for pathname in "./target"/*.json; do
BB_HASH=$BB_HASH node ../scripts/generate_vk_json.js "./target/public_kernel_merge_simulated.json" "./target/keys"
# done
#fi
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_MAGIC_VALUE =
export const DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
14061769416655647708490531650437236735160113654556896985372298487345n;
export const DEFAULT_GAS_LIMIT = 1000000000;
export const DEFAULT_TEARDOWN_GAS_LIMIT = 100000000;
export const DEFAULT_TEARDOWN_GAS_LIMIT = 12000000;
export const MAX_L2_GAS_PER_ENQUEUED_CALL = 12000000;
export const DEFAULT_MAX_FEE_PER_GAS = 10;
export const DEFAULT_INCLUSION_FEE = 0;
Expand Down
114 changes: 112 additions & 2 deletions yarn-project/circuits.js/src/structs/avm/avm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,103 @@ import { Fr } from '@aztec/foundation/fields';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { type FieldsOf } from '@aztec/foundation/types';

//import { Decoder, Encoder, addExtension } from 'msgpackr';
import { type ContractClassIdPreimage } from '../../contract/contract_class_id.js';
import { PublicKeys } from '../../types/public_keys.js';
import { Gas } from '../gas.js';
import { PublicCircuitPublicInputs } from '../public_circuit_public_inputs.js';
import { Vector } from '../shared.js';

//addExtension({
// Class: Fr,
// write: fr => fr.toBuffer(),
//});
//
//const encoder = new Encoder({
// // always encode JS objects as MessagePack maps
// // this makes it compatible with other MessagePack decoders
// useRecords: false,
// int64AsType: 'bigint',
//});
//
///** A long-lived msgpack decoder */
//const decoder = new Decoder({
// useRecords: false,
// int64AsType: 'bigint',
//});

export class AvmEnqueuedCallHint {
public readonly contractAddress: Fr;
public readonly calldata: Vector<Fr>;

constructor(contractAddress: Fr, calldata: Fr[]) {
this.contractAddress = contractAddress;
this.calldata = new Vector(calldata);
}

/* Serializes the inputs to a buffer.
* @returns - The inputs serialized to a buffer.
*/
toBuffer() {
//return encoder.encode(this);
return serializeToBuffer(...AvmEnqueuedCallHint.getFields(this));
}

/**
* Serializes the inputs to a hex string.
* @returns The instance serialized to a hex string.
*/
toString() {
return this.toBuffer().toString('hex');
}

/**
* Is the struct empty?
* @returns whether all members are empty.
*/
isEmpty(): boolean {
return this.contractAddress.isEmpty() && this.calldata.items.length == 0;
}

/**
* Creates a new instance from fields.
* @param fields - Fields to create the instance from.
* @returns A new AvmExecutionHints instance.
*/
static from(fields: FieldsOf<AvmEnqueuedCallHint>): AvmEnqueuedCallHint {
return new AvmEnqueuedCallHint(fields.contractAddress, fields.calldata.items);
}

/**
* Extracts fields from an instance.
* @param fields - Fields to create the instance from.
* @returns An array of fields.
*/
static getFields(fields: FieldsOf<AvmEnqueuedCallHint>) {
return [fields.contractAddress, fields.calldata] as const;
}

/**
* Deserializes from a buffer or reader.
* @param buffer - Buffer or reader to read from.
* @returns The deserialized instance.
*/
static fromBuffer(buff: Buffer | BufferReader) {
const reader = BufferReader.asReader(buff);
return new AvmEnqueuedCallHint(Fr.fromBuffer(reader), reader.readVector(Fr));
//return decoder.decode(buff);
}

/**
* Deserializes from a hex string.
* @param str - Hex string to read from.
* @returns The deserialized instance.
*/
static fromString(str: string): AvmEnqueuedCallHint {
return AvmEnqueuedCallHint.fromBuffer(Buffer.from(str, 'hex'));
}
}

// TODO: Consider just using Tuple.
export class AvmKeyValueHint {
constructor(public readonly key: Fr, public readonly value: Fr) {}
Expand Down Expand Up @@ -366,6 +457,7 @@ export class AvmContractBytecodeHints {

// TODO(dbanks12): rename AvmCircuitHints
export class AvmExecutionHints {
public readonly enqueuedCalls: Vector<AvmEnqueuedCallHint>;
public readonly storageValues: Vector<AvmKeyValueHint>;
public readonly noteHashExists: Vector<AvmKeyValueHint>;
public readonly nullifierExists: Vector<AvmKeyValueHint>;
Expand All @@ -375,6 +467,7 @@ export class AvmExecutionHints {
public readonly contractBytecodeHints: Vector<AvmContractBytecodeHints>;

constructor(
enqueuedCalls: AvmEnqueuedCallHint[],
storageValues: AvmKeyValueHint[],
noteHashExists: AvmKeyValueHint[],
nullifierExists: AvmKeyValueHint[],
Expand All @@ -383,6 +476,7 @@ export class AvmExecutionHints {
contractInstances: AvmContractInstanceHint[],
contractBytecodeHints: AvmContractBytecodeHints[],
) {
this.enqueuedCalls = new Vector(enqueuedCalls);
this.storageValues = new Vector(storageValues);
this.noteHashExists = new Vector(noteHashExists);
this.nullifierExists = new Vector(nullifierExists);
Expand All @@ -397,7 +491,7 @@ export class AvmExecutionHints {
* @returns an empty instance.
*/
empty() {
return new AvmExecutionHints([], [], [], [], [], [], []);
return new AvmExecutionHints([], [], [], [], [], [], [], []);
}

/**
Expand All @@ -406,6 +500,14 @@ export class AvmExecutionHints {
*/
toBuffer() {
return serializeToBuffer(...AvmExecutionHints.getFields(this));
//this.storageValues.items,
//this.noteHashExists.items,
//this.nullifierExists.items,
//this.l1ToL2MessageExists.items,
//this.externalCalls.items,
//this.contractInstances.items,
//this.contractBytecodeHints.items,
//);
}

/**
Expand All @@ -422,6 +524,7 @@ export class AvmExecutionHints {
*/
isEmpty(): boolean {
return (
this.enqueuedCalls.items.length == 0 &&
this.storageValues.items.length == 0 &&
this.noteHashExists.items.length == 0 &&
this.nullifierExists.items.length == 0 &&
Expand All @@ -439,6 +542,8 @@ export class AvmExecutionHints {
*/
static from(fields: FieldsOf<AvmExecutionHints>): AvmExecutionHints {
return new AvmExecutionHints(
//fields.enqueuedCalls.items,
new Array<AvmEnqueuedCallHint>(),
fields.storageValues.items,
fields.noteHashExists.items,
fields.nullifierExists.items,
Expand All @@ -456,6 +561,7 @@ export class AvmExecutionHints {
*/
static getFields(fields: FieldsOf<AvmExecutionHints>) {
return [
//fields.enqueuedCalls,
fields.storageValues,
fields.noteHashExists,
fields.nullifierExists,
Expand All @@ -474,6 +580,9 @@ export class AvmExecutionHints {
static fromBuffer(buff: Buffer | BufferReader): AvmExecutionHints {
const reader = BufferReader.asReader(buff);
return new AvmExecutionHints(
//encoder.decode(buff),
//reader.readVector(AvmEnqueuedCallHint),
new Array<AvmEnqueuedCallHint>(),
reader.readVector(AvmKeyValueHint),
reader.readVector(AvmKeyValueHint),
reader.readVector(AvmKeyValueHint),
Expand All @@ -498,7 +607,7 @@ export class AvmExecutionHints {
* @returns The empty instance.
*/
static empty() {
return new AvmExecutionHints([], [], [], [], [], [], []);
return new AvmExecutionHints([], [], [], [], [], [], [], []);
}
}

Expand Down Expand Up @@ -577,6 +686,7 @@ export class AvmCircuitInputs {
/*calldata=*/ reader.readVector(Fr),
PublicCircuitPublicInputs.fromBuffer(reader),
AvmExecutionHints.fromBuffer(reader),
//decoder.decode(reader.readBuffer()),
);
}

Expand Down
23 changes: 16 additions & 7 deletions yarn-project/circuits.js/src/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ import { GlobalVariables } from '../structs/global_variables.js';
import { Header } from '../structs/header.js';
import {
AvmContractBytecodeHints,
AvmEnqueuedCallHint,
AvmProofData,
BaseRollupHints,
EnqueuedCallData,
Expand Down Expand Up @@ -1490,6 +1491,13 @@ export function makeAvmContractInstanceHint(seed = 0): AvmContractInstanceHint {
);
}

export function makeAvmEnqueuedCallHint(seed = 0): AvmEnqueuedCallHint {
return AvmEnqueuedCallHint.from({
contractAddress: new Fr(seed),
calldata: makeVector((seed % 20) + 4, i => new Fr(i), seed + 0x1000),
});
}

/**
* Creates arbitrary AvmExecutionHints.
* @param seed - The seed to use for generating the hints.
Expand All @@ -1504,13 +1512,14 @@ export function makeAvmExecutionHints(
const baseLength = lengthOffset + (seed % lengthSeedMod);

return AvmExecutionHints.from({
storageValues: makeVector(baseLength, makeAvmKeyValueHint, seed + 0x4200),
noteHashExists: makeVector(baseLength + 1, makeAvmKeyValueHint, seed + 0x4300),
nullifierExists: makeVector(baseLength + 2, makeAvmKeyValueHint, seed + 0x4400),
l1ToL2MessageExists: makeVector(baseLength + 3, makeAvmKeyValueHint, seed + 0x4500),
externalCalls: makeVector(baseLength + 4, makeAvmExternalCallHint, seed + 0x4600),
contractInstances: makeVector(baseLength + 5, makeAvmContractInstanceHint, seed + 0x4700),
contractBytecodeHints: makeVector(baseLength + 6, makeAvmBytecodeHints, seed + 0x4800),
enqueuedCalls: makeVector(baseLength, makeAvmEnqueuedCallHint, seed + 0x4100),
storageValues: makeVector(baseLength + 1, makeAvmKeyValueHint, seed + 0x4200),
noteHashExists: makeVector(baseLength + 2, makeAvmKeyValueHint, seed + 0x4300),
nullifierExists: makeVector(baseLength + 3, makeAvmKeyValueHint, seed + 0x4400),
l1ToL2MessageExists: makeVector(baseLength + 4, makeAvmKeyValueHint, seed + 0x4500),
externalCalls: makeVector(baseLength + 5, makeAvmExternalCallHint, seed + 0x4600),
contractInstances: makeVector(baseLength + 6, makeAvmContractInstanceHint, seed + 0x4700),
contractBytecodeHints: makeVector(baseLength + 7, makeAvmBytecodeHints, seed + 0x4800),
...overrides,
});
}
Expand Down
Loading

0 comments on commit 21635e6

Please sign in to comment.