Skip to content

Commit

Permalink
feat: generating Noir constants (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Jul 14, 2023
1 parent c4ad54e commit 26a02f7
Show file tree
Hide file tree
Showing 30 changed files with 298 additions and 151 deletions.
2 changes: 2 additions & 0 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ using aztec3::circuits::abis::NewContractData;
using aztec3::circuits::abis::Point;
using aztec3::circuits::abis::PrivateStateNoteGeneratorIndexPacker;
using aztec3::circuits::abis::PrivateStateTypePacker;
using aztec3::circuits::abis::StorageSlotGeneratorIndexPacker;
using aztec3::circuits::abis::TxContext;
using aztec3::circuits::abis::TxRequest;
using NT = aztec3::utils::types::NativeTypes;
Expand Down Expand Up @@ -586,4 +587,5 @@ WASM_EXPORT const char* abis__test_roundtrip_serialize_function_leaf_preimage(ui
CBIND_NOSCHEMA(get_circuit_constants, [] { return ConstantsPacker{}; });
CBIND_NOSCHEMA(get_circuit_generator_index, [] { return GeneratorIndexPacker{}; });
CBIND_NOSCHEMA(get_circuit_private_state_note_generator_index, [] { return PrivateStateNoteGeneratorIndexPacker{}; });
CBIND_NOSCHEMA(get_circuit_storage_slot_generator_index, [] { return StorageSlotGeneratorIndexPacker{}; });
CBIND_NOSCHEMA(get_circuit_private_state_type, [] { return PrivateStateTypePacker{}; });
5 changes: 5 additions & 0 deletions yarn-project/circuits.js/src/cbind/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export enum GeneratorIndex {
PUBLIC_CIRCUIT_PUBLIC_INPUTS = 43,
FUNCTION_ARGS = 44,
}
export enum StorageSlotGeneratorIndex {
BASE_SLOT = 0,
MAPPING_SLOT = 1,
MAPPING_SLOT_PLACEHOLDER = 2,
}
export enum PrivateStateNoteGeneratorIndex {
VALUE = 1,
OWNER = 2,
Expand Down
69 changes: 58 additions & 11 deletions yarn-project/circuits.js/src/cbind/constants.in.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
import * as fs from 'fs';
import { dirname } from 'path';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { callCbind } from './cbind.js';
import { CircuitsWasm } from '../wasm/circuits_wasm.js';

/**
* Convert the C++ constants to TypeScript.
* Convert the C++ constants to TypeScript and Noir.
*/
async function main(): Promise<void> {
const wasm = await CircuitsWasm.get();
const constants = callCbind(wasm, 'get_circuit_constants', []);
const generatorIndexEnum = callCbind(wasm, 'get_circuit_generator_index', []);
const storageSlotGeneratorIndexEnum = callCbind(wasm, 'get_circuit_storage_slot_generator_index', []);
const privateStateNoteGeneratorIndexEnum = callCbind(wasm, 'get_circuit_private_state_note_generator_index', []);
const privateStateTypeEnum = callCbind(wasm, 'get_circuit_private_state_type', []);

const result: string =
const __dirname = dirname(fileURLToPath(import.meta.url));

// Typescript
const resultTS: string =
'/* eslint-disable */\n// GENERATED FILE - DO NOT EDIT, RUN yarn remake-constants\n' +
processConstants(constants) +
processEnum('GeneratorIndex', generatorIndexEnum) +
processEnum('PrivateStateNoteGeneratorIndex', privateStateNoteGeneratorIndexEnum) +
processEnum('PrivateStateType', privateStateTypeEnum);
processConstantsTS(constants) +
processEnumTS('GeneratorIndex', generatorIndexEnum) +
processEnumTS('StorageSlotGeneratorIndex', storageSlotGeneratorIndexEnum) +
processEnumTS('PrivateStateNoteGeneratorIndex', privateStateNoteGeneratorIndexEnum) +
processEnumTS('PrivateStateType', privateStateTypeEnum);

const __dirname = dirname(fileURLToPath(import.meta.url));
fs.writeFileSync(__dirname + '/constants.gen.ts', result);
fs.writeFileSync(__dirname + '/constants.gen.ts', resultTS);

// Noir
const resultNoir: string =
'// GENERATED FILE - DO NOT EDIT, RUN yarn remake-constants in circuits.js\n' +
processConstantsNoir(constants) +
processEnumNoir(generatorIndexEnum, 'GENERATOR_INDEX__') +
processEnumNoir(storageSlotGeneratorIndexEnum, 'STORAGE_SLOT_GENERATOR_INDEX__') +
processEnumNoir(privateStateNoteGeneratorIndexEnum, 'PRIVATE_STATE_NOTE_GENERATOR_INDEX__') +
processEnumNoir(privateStateTypeEnum, 'PRIVATE_STATE_TYPE__');

const noirTargetPath = join(__dirname, '../../../noir-contracts/src/libs/noir-aztec/src/constants_gen.nr');
fs.writeFileSync(noirTargetPath, resultNoir);
}

/**
Expand All @@ -31,7 +47,7 @@ async function main(): Promise<void> {
* @param constants - An object containing key-value pairs representing constants.
* @returns A string containing code that exports the constants as TypeScript constants.
*/
function processConstants(constants: { [key: string]: number }): string {
function processConstantsTS(constants: { [key: string]: number }): string {
const code: string[] = [];
Object.entries(constants).forEach(([key, value]) => {
code.push(`export const ${key} = ${value};`);
Expand All @@ -46,7 +62,7 @@ function processConstants(constants: { [key: string]: number }): string {
* @param enumValues - An object containing key-value pairs representing enum values.
* @returns A string containing code that exports the enum as a TypeScript enum.
*/
function processEnum(enumName: string, enumValues: { [key: string]: number }): string {
function processEnumTS(enumName: string, enumValues: { [key: string]: number }): string {
const code: string[] = [];

code.push(`export enum ${enumName} {`);
Expand All @@ -60,5 +76,36 @@ function processEnum(enumName: string, enumValues: { [key: string]: number }): s
return code.join('\n');
}

/**
* Processes a collection of constants and generates code to export them as Noir constants.
*
* @param constants - An object containing key-value pairs representing constants.
* @param prefix - A prefix to add to the constant names.
* @returns A string containing code that exports the constants as Noir constants.
*/
function processConstantsNoir(constants: { [key: string]: number }, prefix = ''): string {
const code: string[] = [];
Object.entries(constants).forEach(([key, value]) => {
code.push(`global ${prefix}${key}: comptime Field = ${value};`);
});
return code.join('\n') + '\n';
}

/**
* Processes a collection of enums and generates code to export them as Noir constants prefixed with enum name.
*
* @param enumValues - An object containing key-value pairs representing enum values.
* @param enumPrefix - A prefix to add to the names of resulting Noir constants to communicate the constant was part
* of C++ enum.
* @returns A string containing code that exports the enums as Noir constants prefixed with enum name.
*/
function processEnumNoir(enumValues: { [key: string]: number }, enumPrefix: string): string {
const code: string[] = [];
Object.entries(enumValues).forEach(([key, value]) => {
code.push(`global ${enumPrefix}${key} = ${value};`);
});
return code.join('\n') + '\n';
}

// eslint-disable-next-line no-console
main().catch(console.error);
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contract PendingCommitments {

use crate::storage::Storage;

use dep::aztec::constants_gen::ARGS_LENGTH;
use dep::aztec::abi;
use dep::aztec::abi::PrivateContextInputs;
use dep::aztec::context::Context;
Expand Down Expand Up @@ -197,7 +198,7 @@ contract PendingCommitments {
let mut context = Context::new(inputs, abi::hash_args([amount, owner.x, owner.y, createFnSelector, getAndCheckFnSelector]));

// args for nested calls
let mut args = [0; abi::MAX_ARGS];
let mut args = [0; ARGS_LENGTH];
args[0] = amount;
args[1] = owner.x;
args[2] = owner.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract SchnorrAccount {
use dep::aztec::types::vec::BoundedVec;
use dep::aztec::types::point::Point;
use dep::aztec::oracle::debug_log::debug_log_format;
use dep::aztec::generators;
use dep::aztec::constants_gen::GENERATOR_INDEX__CONTRACT_ADDRESS;

fn entrypoint(
inputs: pub PrivateContextInputs,
Expand Down Expand Up @@ -55,7 +55,7 @@ contract SchnorrAccount {
assert(verification == true);

// Verify public key against address
let reproduced_address = dep::std::hash::pedersen_with_separator([x, y, partial_address], generators::ContractAddress)[0];
let reproduced_address = dep::std::hash::pedersen_with_separator([x, y, partial_address], GENERATOR_INDEX__CONTRACT_ADDRESS)[0];
assert(reproduced_address == inputs.call_context.storage_contract_address);

// Execute calls
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit 26a02f7

Please sign in to comment.