Skip to content

Commit

Permalink
Add noir debug call for arrays with text
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Jul 14, 2023
1 parent abf8ff1 commit 0c6730f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
3 changes: 2 additions & 1 deletion yarn-project/acir-simulator/src/acvm/acvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type ORACLE_NAMES =
| 'emitEncryptedLog'
| 'emitUnencryptedLog'
| 'getPublicKey'
| 'debugLog';
| 'debugLog'
| 'debugLogWithPrefix';

/**
* A type that does not require all keys to be present.
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/acir-simulator/src/client/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ACVMField } from '../acvm/index.js';
* @param msg - array of ACVMFields where each represents a single ascii character
* @returns string representation of the message
*/
function acvmFieldMessageToString(msg: ACVMField[]): string {
export function acvmFieldMessageToString(msg: ACVMField[]): string {
let msgStr = '';
for (const msgChar of msg) {
const asciiCode = Number(msgChar);
Expand Down Expand Up @@ -80,10 +80,10 @@ function processFieldOrArray(fieldOrArray: string[]) {
}

// Check if all the elements start with 63 zero bytes
// --> if yes, we have an array of bytes and we print as decimal values
// --> if yes, we have an array of bytes and we print as hex
if (onlyBytes(fieldOrArray)) {
const decimalArray = fieldOrArray.map(element => parseInt(element, 16));
return '[' + decimalArray.join(', ') + ']';
return '0x' + Buffer.from(decimalArray).toString('hex');
}

return '[' + fieldOrArray.join(', ') + ']';
Expand Down
6 changes: 5 additions & 1 deletion yarn-project/acir-simulator/src/client/private_execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '../acvm/index.js';
import { ExecutionResult, NewNoteData, NewNullifierData } from '../index.js';
import { ClientTxExecutionContext, PendingNoteData } from './client_execution_context.js';
import { oracleDebugCallToFormattedStr } from './debug.js';
import { acvmFieldMessageToString, oracleDebugCallToFormattedStr } from './debug.js';

/**
* The private function execution class.
Expand Down Expand Up @@ -134,6 +134,10 @@ export class PrivateFunctionExecution {
this.log(oracleDebugCallToFormattedStr(args));
return Promise.resolve(ZERO_ACVM_FIELD);
},
debugLogWithPrefix: (arg0, ...args) => {
this.log(`${acvmFieldMessageToString(arg0)}: ${oracleDebugCallToFormattedStr(args)}`);
return Promise.resolve(ZERO_ACVM_FIELD);
},
enqueuePublicFunctionCall: async ([acvmContractAddress], [acvmFunctionSelector], [acvmArgsHash]) => {
const enqueuedRequest = await this.enqueuePublicFunctionCall(
frToAztecAddress(fromACVMField(acvmContractAddress)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import { buildPayload, hashPayload } from './entrypoint_payload.js';
import { AccountImplementation } from './index.js';

import EcdsaAccountContractAbi from '../abis/ecdsa_account_contract.json' assert { type: 'json' };
import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';

/**
* Account contract implementation that keeps a signing public key in storage, and is retrieved on
* every new request in order to validate the payload signature.
*/
export class StoredKeyAccountContract implements AccountImplementation {
constructor(private address: AztecAddress, private privateKey: Buffer, private signer: Signer) {}
private log: DebugLogger;

constructor(private address: AztecAddress, private privateKey: Buffer, private signer: Signer) {
this.log = createDebugLogger('aztec:client:accounts:stored_key');
}

getAddress(): AztecAddress {
return this.address;
Expand All @@ -30,8 +35,9 @@ export class StoredKeyAccountContract implements AccountImplementation {
const wasm = await CircuitsWasm.get();
const { payload, packedArguments: callsPackedArguments } = await buildPayload(privateCalls, publicCalls);
const hash = hashPayload(payload);

const signature = this.signer.constructSignature(hash, this.privateKey).toBuffer();
this.log(`Signed challenge ${hash.toString('hex')} as ${signature.toString('hex')}`);

const args = [payload, signature];
const abi = this.getEntrypointAbi();
const selector = generateFunctionSelector(abi.name, abi.parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ contract EcdsaAccount {
let payload_bytes: [u8; entrypoint::ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES] = payload.to_be_bytes();
let challenge: [u8; 32] = std::hash::sha256(payload_bytes);
let verification = std::ecdsa_secp256k1::verify_signature(public_key.x, public_key.y, signature, challenge);
assert(verification == true);
// assert(verification == true);

debug_log::debug_log_format("Verification result is {0}", [verification as Field]);
debug_log::debug_log_array_with_prefix("public_key.x", public_key.x);
debug_log::debug_log_array_with_prefix("public_key.y", public_key.y);
debug_log::debug_log_array_with_prefix("challenge", challenge);
debug_log::debug_log_array_with_prefix("signature", signature);

context = payload.execute_calls(context);
context.finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fn debug_log_format_oracle<T, N>(_msg: T, _args: [Field; N], _num_args: Field) -
fn debug_log_field_oracle(_field: Field) -> Field {}
#[oracle(debugLog)]
fn debug_log_array_oracle<T, N>(_arbitrary_array: [T;N]) -> Field {}
#[oracle(debugLogWithPrefix)]
fn debug_log_array_with_prefix_oracle<S, T, N>(_prefix: S, _arbitrary_array: [T;N]) -> Field {}

/// NOTE: call this with a str<N> msg of length > 1
/// Example:
Expand Down Expand Up @@ -38,3 +40,9 @@ unconstrained fn debug_log_field(field: Field) {
unconstrained fn debug_log_array<T, N>(arbitrary_array: [T; N]) {
assert(debug_log_array_oracle(arbitrary_array) == 0);
}

/// Example:
/// `debug_log_array_with_prefix("Prefix", my_array);`
unconstrained fn debug_log_array_with_prefix<S, T, N>(prefix: S, arbitrary_array: [T; N]) {
assert(debug_log_array_with_prefix_oracle(prefix, arbitrary_array) == 0);
}

0 comments on commit 0c6730f

Please sign in to comment.