Skip to content

Commit

Permalink
feat: Add to other contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Aug 11, 2023
1 parent ccd42d8 commit cac043f
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 73 deletions.
31 changes: 17 additions & 14 deletions yarn-project/acir-simulator/src/acvm/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import {
RETURN_VALUES_LENGTH,
} from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { padArrayEnd } from '@aztec/foundation/collection';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { Tuple } from '@aztec/foundation/serialize';

import { getReturnWitness } from 'acvm_js';

import { ACVMField, ACVMWitness, fromACVMField } from './acvm.js';
import { Tuple } from '@aztec/foundation/serialize';
import { padArrayEnd } from '@aztec/foundation/collection';

// Utilities to read TS classes from ACVM Field arrays
// In the order that the ACVM provides them
Expand Down Expand Up @@ -110,7 +110,6 @@ export class PublicInputsReader {
}
return array as Tuple<Fr, N>;
}

}

/**
Expand All @@ -119,7 +118,10 @@ export class PublicInputsReader {
* @param acir - The ACIR bytecode.
* @returns The public inputs.
*/
export function extractPrivateCircuitPublicInputs(partialWitness: ACVMWitness, acir: Buffer): PrivateCircuitPublicInputs {
export function extractPrivateCircuitPublicInputs(
partialWitness: ACVMWitness,
acir: Buffer,
): PrivateCircuitPublicInputs {
const witnessReader = new PublicInputsReader(partialWitness, acir);

const callContext = new CallContext(
Expand Down Expand Up @@ -193,7 +195,6 @@ export function extractPrivateCircuitPublicInputs(partialWitness: ACVMWitness, a
);
}


/**
* Extracts the public circuit public inputs from the ACVM generated partial witness.
* @param partialWitness - The partial witness.
Expand All @@ -213,9 +214,11 @@ export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, ac
);

const argsHash = witnessReader.readField();
const returnValues = padArrayEnd(witnessReader.readFieldArray(RETURN_VALUES_LENGTH),Fr.ZERO, RETURN_VALUES_LENGTH); ;
const returnValues = padArrayEnd(witnessReader.readFieldArray(RETURN_VALUES_LENGTH), Fr.ZERO, RETURN_VALUES_LENGTH);

const contractStorageUpdateRequests = new Array(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL).fill(ContractStorageUpdateRequest.empty());
const contractStorageUpdateRequests = new Array(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL).fill(
ContractStorageUpdateRequest.empty(),
);
for (let i = 0; i < MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL; i++) {
const request = new ContractStorageUpdateRequest(
witnessReader.readField(),
Expand All @@ -226,10 +229,7 @@ export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, ac
}
const contractStorageReads = new Array(MAX_PUBLIC_DATA_READS_PER_CALL).fill(ContractStorageRead.empty());
for (let i = 0; i < MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL; i++) {
const request = new ContractStorageRead(
witnessReader.readField(),
witnessReader.readField(),
);
const request = new ContractStorageRead(witnessReader.readField(), witnessReader.readField());
contractStorageReads[i] = request;
}
// const contractStorageRead = witnessReader.readFieldArray(MAX_PUBLIC_DATA_READS_PER_CALL);
Expand Down Expand Up @@ -260,7 +260,10 @@ export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, ac
argsHash,
returnValues,
// TODO: how remove
contractStorageUpdateRequests as Tuple<ContractStorageUpdateRequest, typeof MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL>,
contractStorageUpdateRequests as Tuple<
ContractStorageUpdateRequest,
typeof MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL
>,
contractStorageReads as Tuple<ContractStorageRead, typeof MAX_PUBLIC_DATA_READS_PER_CALL>,
publicCallStack,
newCommitments,
Expand All @@ -269,6 +272,6 @@ export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, ac
unencryptedLogsHash,
unencryptedLogPreimagesLength,
historicPublicDataTreeRoot,
proverAddress
proverAddress,
);
}
}
4 changes: 0 additions & 4 deletions yarn-project/acir-simulator/src/public/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
acvm,
convertACVMFieldToBuffer,
extractPublicCircuitPublicInputs,
extractReturnWitness,
frToAztecAddress,
frToSelector,
fromACVMField,
Expand All @@ -30,7 +29,6 @@ import { PackedArgsCache } from '../packed_args_cache.js';
import { CommitmentsDB, PublicContractsDB, PublicStateDB } from './db.js';
import { PublicExecution, PublicExecutionResult } from './execution.js';
import { ContractStorageActionsCollector } from './state_actions.js';
import { decodeReturnValues } from '@aztec/foundation/abi';

// Copied from crate::abi at noir-contracts/src/contracts/noir-aztec/src/abi.nr
const NOIR_MAX_RETURN_VALUES = 4;
Expand Down Expand Up @@ -163,9 +161,7 @@ export class PublicExecutor {
});

// TODO: get the rest of everything from here, this should also be used to get the new Commitments, Nullifiers etc.
console.log("partial witness after execution: ", partialWitness);
const publicInputs = extractPublicCircuitPublicInputs(partialWitness, acir);
console.log("decoded public inputs: ", publicInputs);
const { returnValues } = publicInputs;

const [contractStorageReads, contractStorageUpdateRequests] = storageActions.collect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ export class PublicCircuitPublicInputs {
*/
public proverAddress: AztecAddress,
) {
console.log(contractStorageUpdateRequests);
assertMemberLength(this, 'returnValues', RETURN_VALUES_LENGTH);
assertMemberLength(this, 'publicCallStack', MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL);
assertMemberLength(this, 'newCommitments', MAX_NEW_COMMITMENTS_PER_CALL);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/foundation/src/aztec-address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class AztecAddress {
* @param field - The Field element to convert.
* @returns An Address Object from a Field element with the same value.
*/
static fromField(field: Fr) : AztecAddress {
static fromField(field: Fr): AztecAddress {
return new AztecAddress(toBufferBE(field.value, AztecAddress.SIZE_IN_BYTES));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ contract Child {
}

// Returns base_value + 42.
open fn pubValue(inputs: PublicContextInputs, base_value: Field) -> pub Field {
base_value + inputs.public_global_variables.chain_id + inputs.public_global_variables.version + inputs.public_global_variables.block_number + inputs.public_global_variables.timestamp
open fn pubValue(inputs: PublicContextInputs, base_value: Field) -> pub abi::PublicCircuitPublicInputs {
let mut context = PublicContext::new(inputs, abi::hash_args([base_value]));

// TODO: make these available on context
let returnValue = base_value + inputs.public_global_variables.chain_id + inputs.public_global_variables.version + inputs.public_global_variables.block_number + inputs.public_global_variables.timestamp;

context.return_values.push(returnValue);
// TODO(MADDIAA): MAYBE we put the return values inside the finish object? That could have nice UX
context.finish()
}

// Increments `current_value` by `new_value` and returns `new_value` + 1.
open fn pubStoreValue(inputs: PublicContextInputs, new_value: Field) -> pub Field {
let mut _context = PublicContext::new(inputs, abi::hash_args([new_value]));
open fn pubStoreValue(inputs: PublicContextInputs, new_value: Field) -> pub abi::PublicCircuitPublicInputs {
let mut context = PublicContext::new(inputs, abi::hash_args([new_value]));

let storage = Storage::init();
let old_value = storage.current_value.read();
Expand All @@ -47,7 +54,9 @@ contract Child {
// Compiler fails with "we do not allow private ABI inputs to be returned as public outputs" if we try to
// return new_value as-is, but then it also complains if we add `pub` to `new_value` in the args, so we
// just assign it to another variable and tweak it so it's not the same value, and the compiler is happy.
let ret_value = new_value + 1;
ret_value
let return_value = new_value + 1;

context.return_values.push(return_value);
context.finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,27 @@ contract ExamplePublicStateIncrement {
// a = 100;
open internal fn initialise_a(
inputs: PublicContextInputs,
) {
let mut _context = PublicContext::new(inputs, abi::hash_args([]));
) -> pub abi::PublicCircuitPublicInputs {
let mut context = PublicContext::new(inputs, abi::hash_args([]));

let storage = Storage::init();
storage.a.write(100);

context.finish()
}

// a += b;
open fn increment_a(
inputs: PublicContextInputs,
b: Field,
) {
let mut _context = PublicContext::new(inputs, abi::hash_args([b]));
) -> pub abi::PublicCircuitPublicInputs {
let mut context = PublicContext::new(inputs, abi::hash_args([b]));

let storage = Storage::init();
let mut a = storage.a.read();
a += b;
storage.a.write(a);

context.finish()
}
}
Loading

0 comments on commit cac043f

Please sign in to comment.