From 0c6730f9e42a52435baf6fc3d1c42100e7859688 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Fri, 14 Jul 2023 15:21:05 -0300 Subject: [PATCH] Add noir debug call for arrays with text --- yarn-project/acir-simulator/src/acvm/acvm.ts | 3 ++- yarn-project/acir-simulator/src/client/debug.ts | 6 +++--- .../acir-simulator/src/client/private_execution.ts | 6 +++++- .../src/account_impl/stored_key_account_contract.ts | 10 ++++++++-- .../src/contracts/ecdsa_account_contract/src/main.nr | 8 +++++++- .../src/libs/noir-aztec/src/oracle/debug_log.nr | 8 ++++++++ 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/yarn-project/acir-simulator/src/acvm/acvm.ts b/yarn-project/acir-simulator/src/acvm/acvm.ts index dee64b6aec11..eddf99c15b0e 100644 --- a/yarn-project/acir-simulator/src/acvm/acvm.ts +++ b/yarn-project/acir-simulator/src/acvm/acvm.ts @@ -40,7 +40,8 @@ type ORACLE_NAMES = | 'emitEncryptedLog' | 'emitUnencryptedLog' | 'getPublicKey' - | 'debugLog'; + | 'debugLog' + | 'debugLogWithPrefix'; /** * A type that does not require all keys to be present. diff --git a/yarn-project/acir-simulator/src/client/debug.ts b/yarn-project/acir-simulator/src/client/debug.ts index 7c1b3d70bdb6..2f8c98583728 100644 --- a/yarn-project/acir-simulator/src/client/debug.ts +++ b/yarn-project/acir-simulator/src/client/debug.ts @@ -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); @@ -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(', ') + ']'; diff --git a/yarn-project/acir-simulator/src/client/private_execution.ts b/yarn-project/acir-simulator/src/client/private_execution.ts index 86f20d1cad15..63d582982f94 100644 --- a/yarn-project/acir-simulator/src/client/private_execution.ts +++ b/yarn-project/acir-simulator/src/client/private_execution.ts @@ -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. @@ -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)), diff --git a/yarn-project/aztec.js/src/account_impl/stored_key_account_contract.ts b/yarn-project/aztec.js/src/account_impl/stored_key_account_contract.ts index b37d97a94c47..360e21e04b32 100644 --- a/yarn-project/aztec.js/src/account_impl/stored_key_account_contract.ts +++ b/yarn-project/aztec.js/src/account_impl/stored_key_account_contract.ts @@ -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; @@ -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); diff --git a/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/main.nr index 2593fb4839ca..a95d993b39cb 100644 --- a/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/main.nr @@ -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() diff --git a/yarn-project/noir-contracts/src/libs/noir-aztec/src/oracle/debug_log.nr b/yarn-project/noir-contracts/src/libs/noir-aztec/src/oracle/debug_log.nr index 8f5b3f9b79b9..5be0cc39ffc5 100644 --- a/yarn-project/noir-contracts/src/libs/noir-aztec/src/oracle/debug_log.nr +++ b/yarn-project/noir-contracts/src/libs/noir-aztec/src/oracle/debug_log.nr @@ -9,6 +9,8 @@ fn debug_log_format_oracle(_msg: T, _args: [Field; N], _num_args: Field) - fn debug_log_field_oracle(_field: Field) -> Field {} #[oracle(debugLog)] fn debug_log_array_oracle(_arbitrary_array: [T;N]) -> Field {} +#[oracle(debugLogWithPrefix)] +fn debug_log_array_with_prefix_oracle(_prefix: S, _arbitrary_array: [T;N]) -> Field {} /// NOTE: call this with a str msg of length > 1 /// Example: @@ -38,3 +40,9 @@ unconstrained fn debug_log_field(field: Field) { unconstrained fn debug_log_array(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(prefix: S, arbitrary_array: [T; N]) { + assert(debug_log_array_with_prefix_oracle(prefix, arbitrary_array) == 0); +}