Skip to content

Commit

Permalink
feat(avm): enable contract testing with bb binary (#5584)
Browse files Browse the repository at this point in the history
Please read [contributing guidelines](CONTRIBUTING.md) and remove this
line.

---------

Co-authored-by: Facundo <fcarreiro@users.noreply.github.com>
  • Loading branch information
IlyasRidhuan and fcarreiro authored Apr 10, 2024
1 parent 3287aa2 commit d007d79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace bb::avm_trace {
class AvmMemTraceBuilder {

public:
static const size_t MEM_SIZE = 1024;
static const uint32_t SUB_CLK_IND_LOAD_A = 0;
static const uint32_t SUB_CLK_IND_LOAD_B = 1;
static const uint32_t SUB_CLK_IND_LOAD_C = 2;
Expand Down Expand Up @@ -124,4 +123,4 @@ class AvmMemTraceBuilder {
AvmMemoryTag r_in_tag,
AvmMemoryTag w_in_tag);
};
} // namespace bb::avm_trace
} // namespace bb::avm_trace
38 changes: 14 additions & 24 deletions yarn-project/simulator/src/public/avm_executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AvmTestContractArtifact } from '@aztec/noir-contracts.js';

import { type MockProxy, mock } from 'jest-mock-extended';

import { initContext, initExecutionEnvironment } from '../avm/fixtures/index.js';
import { type CommitmentsDB, type PublicContractsDB, type PublicStateDB } from './db.js';
import { type PublicExecution } from './execution.js';
import { PublicExecutor } from './executor.js';
Expand Down Expand Up @@ -35,34 +36,23 @@ describe('AVM WitGen and Proof Generation', () => {
header = makeHeader(randomInt(1000000));
}, 10000);

it('Should prove valid execution of bytecode that performs addition', async () => {
const args: Fr[] = [new Fr(1), new Fr(2)];
// Bytecode for the following contract is encoded:
// const bytecode = encodeToBytecode([
// new CalldataCopy(/*indirect=*/ 0, /*cdOffset=*/ 0, /*copySize=*/ 2, /*dstOffset=*/ 0),
// new Add(/*indirect=*/ 0, TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2),
// new Return(/*indirect=*/ 0, /*returnOffset=*/ 2, /*copySize=*/ 1),
// ]);
const bytecode: Buffer = Buffer.from('IAAAAAAAAAAAAgAAAAAAAAYAAAAAAAAAAQAAAAI5AAAAAAIAAAAB', 'base64');
publicContracts.getBytecode.mockResolvedValue(bytecode);
const executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);
const functionData = FunctionData.empty();
const execution: PublicExecution = { contractAddress, functionData, args, callContext };
const [proof, vk] = await executor.getAvmProof(execution);
const valid = await executor.verifyAvmProof(vk, proof);
expect(valid).toBe(true);
});

// This is skipped as we require MOV to be implemented in the AVM
it.skip('Should prove valid execution contract function that performs addition', async () => {
const args: Fr[] = [new Fr(1), new Fr(2)];

it('Should prove valid execution contract function that performs addition', async () => {
const addArtifact = AvmTestContractArtifact.functions.find(f => f.name === 'add_args_return')!;
const bytecode = addArtifact.bytecode;
publicContracts.getBytecode.mockResolvedValue(bytecode);
const functionData = FunctionData.fromAbi(addArtifact);
const execution: PublicExecution = { contractAddress, functionData, args, callContext };

const functionData = FunctionData.fromAbi(addArtifact);
const args: Fr[] = [new Fr(99), new Fr(12)];
// We call initContext here to load up a AvmExecutionEnvironment that prepends the calldata with the function selector
// and the args hash. In reality, we should simulate here and get this from the output of the simulation call.
// For now, the interfaces for the PublicExecutor don't quite line up, so we are doing this.
const context = initContext({ env: initExecutionEnvironment({ calldata: args }) });
const execution: PublicExecution = {
contractAddress,
functionData,
args: context.environment.calldata,
callContext,
};
const executor = new PublicExecutor(publicState, publicContracts, commitmentsDb, header);
const [proof, vk] = await executor.getAvmProof(execution);
const valid = await executor.verifyAvmProof(vk, proof);
Expand Down

0 comments on commit d007d79

Please sign in to comment.