Skip to content

Commit

Permalink
feat: debugLog(...) in noir-protocol-circuits (#5568)
Browse files Browse the repository at this point in the history
Until now we didn't allow for easy debug logs from noir protocol circuits because the corresponding foreign call handler was not implemented. In this PR I re-use the debug log functionality from simulator for protocol circuits.
  • Loading branch information
benesjan committed Apr 4, 2024
1 parent bc8211d commit a07bb92
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 18 deletions.
5 changes: 4 additions & 1 deletion noir-projects/aztec-nr/aztec/src/oracle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
mod arguments;
mod call_private_function;
mod context;
mod debug_log;
mod get_contract_instance;
mod get_l1_to_l2_membership_witness;
mod get_nullifier_membership_witness;
Expand All @@ -21,3 +20,7 @@ mod public_call;
mod notes;
mod storage;
mod logs;

// debug_log oracle is used by both noir-protocol-circuits and this crate and for this reason we just re-export it
// here from protocol circuits.
use dep::protocol_types::debug_log;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ contract SchnorrSingleKeyAccount {
use dep::authwit::{entrypoint::{app::AppPayload, fee::FeePayload}, account::AccountActions};

use crate::{util::recover_address, auth_oracle::get_auth_witness};
use dep::aztec::oracle::debug_log::debug_log_format;

global ACCOUNT_ACTIONS_STORAGE_SLOT = 1;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod utils;
mod address;
mod debug_log;
mod grumpkin_point;
mod grumpkin_private_key;
// This is intentionally spelled like this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Placed here as it is used both by simulator and by noir-protocol-circuits-types
import { type ForeignCallInput } from '@noir-lang/acvm_js';

import { type ACVMField } from '../acvm_types.js';
type ACVMField = string;

/**
* Convert an array of ACVMFields to a string.
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './constants.gen.js';
export * from './contract/index.js';
export * from './debug_log.js';
export * from './hints/index.js';
export * from './keys/index.js';
export * from './structs/index.js';
Expand Down
35 changes: 23 additions & 12 deletions yarn-project/noir-protocol-circuits-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type BaseOrMergeRollupPublicInputs,
type BaseParityInputs,
type BaseRollupInputs,
Fr,
type KernelCircuitPublicInputs,
type MergeRollupInputs,
type ParityPublicInputs,
Expand All @@ -16,10 +17,14 @@ import {
type RootParityInputs,
type RootRollupInputs,
type RootRollupPublicInputs,
acvmFieldMessageToString,
oracleDebugCallToFormattedStr,
} from '@aztec/circuits.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { type NoirCompiledCircuit } from '@aztec/types/noir';

import {
type ForeignCallInput,
type WasmBlackBoxFunctionSolver,
createBlackBoxSolver,
executeCircuitWithBlackBoxSolver,
Expand Down Expand Up @@ -453,9 +458,7 @@ async function executePrivateKernelInitWithACVM(input: InitInputType): Promise<I
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
foreignCallHandler,
);

// Decode the witness map into two fields, the return values and the inputs
Expand All @@ -481,9 +484,7 @@ async function executePrivateKernelInnerWithACVM(input: InnerInputType): Promise
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
foreignCallHandler,
);

// Decode the witness map into two fields, the return values and the inputs
Expand All @@ -509,9 +510,7 @@ async function executePrivateKernelTailWithACVM(input: TailInputType): Promise<T
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
foreignCallHandler,
);

// Decode the witness map into two fields, the return values and the inputs
Expand All @@ -536,9 +535,7 @@ async function executePrivateKernelTailToPublicWithACVM(
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
foreignCallHandler,
);

// Decode the witness map into two fields, the return values and the inputs
Expand All @@ -547,3 +544,17 @@ async function executePrivateKernelTailToPublicWithACVM(
// Cast the inputs as the return type
return decodedInputs.return_value as PublicPublicPreviousReturnType;
}

const foreignCallHandler = (name: string, args: ForeignCallInput[]) => {
const log = createDebugLogger('aztec:noir-protocol-circuits:oracle');

if (name === 'debugLog') {
log(oracleDebugCallToFormattedStr(args));
} else if (name === 'debugLogWithPrefix') {
log(`${acvmFieldMessageToString(args[0])}: ${oracleDebugCallToFormattedStr(args.slice(1))}`);
} else {
throw Error(`unexpected oracle during execution: ${name}`);
}

return Promise.resolve([`0x${Buffer.alloc(Fr.SIZE_IN_BYTES).toString('hex')}`]);
};
1 change: 0 additions & 1 deletion yarn-project/simulator/src/acvm/oracle/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type Oracle } from './oracle.js';

export * from './debug.js';
export * from './oracle.js';
export * from './typed_oracle.js';

Expand Down
3 changes: 1 addition & 2 deletions yarn-project/simulator/src/acvm/oracle/oracle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MerkleTreeId, UnencryptedL2Log } from '@aztec/circuit-types';
import { RETURN_VALUES_LENGTH } from '@aztec/circuits.js';
import { RETURN_VALUES_LENGTH, acvmFieldMessageToString, oracleDebugCallToFormattedStr } from '@aztec/circuits.js';
import { EventSelector, FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { padArrayEnd } from '@aztec/foundation/collection';
Expand All @@ -9,7 +9,6 @@ import { createDebugLogger } from '@aztec/foundation/log';
import { type ACVMField } from '../acvm_types.js';
import { frToBoolean, frToNumber, fromACVMField } from '../deserialize.js';
import { toACVMField, toAcvmEnqueuePublicFunctionResult } from '../serialize.js';
import { acvmFieldMessageToString, oracleDebugCallToFormattedStr } from './debug.js';
import { type TypedOracle } from './typed_oracle.js';

/**
Expand Down

0 comments on commit a07bb92

Please sign in to comment.