Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Proving the rollup circuits #5599

Merged
merged 50 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e38f055
WIP
PhilWindle Mar 26, 2024
0abfe1c
Merge branch 'master' into pw/generate-keys
PhilWindle Mar 26, 2024
78b36ca
Merge fix
PhilWindle Mar 26, 2024
0a07ffb
WIP
PhilWindle Mar 26, 2024
029912a
WIP
PhilWindle Mar 26, 2024
04e5bcb
WIP
PhilWindle Mar 26, 2024
dce3bbf
Merge branch 'master' into pw/generate-keys
PhilWindle Mar 27, 2024
43022c1
WIP
PhilWindle Mar 28, 2024
c50d853
Merge branch 'master' into pw/generate-keys
PhilWindle Mar 28, 2024
e16c245
WIP
PhilWindle Mar 28, 2024
41cd872
WIP
PhilWindle Mar 28, 2024
5050114
WIP
PhilWindle Apr 8, 2024
86fd349
Merge branch 'master' into pw/generate-keys
PhilWindle Apr 8, 2024
64e6a39
WIP
PhilWindle Apr 10, 2024
660a21b
Fixes
PhilWindle Apr 10, 2024
d0af6b7
Fixes
PhilWindle Apr 10, 2024
5bb7de2
Cleanup
PhilWindle Apr 10, 2024
529ec78
Merge branch 'master' into pw/generate-keys
PhilWindle Apr 10, 2024
18b93b5
Revert bb changes
PhilWindle Apr 10, 2024
2966163
Revert further bb changes
PhilWindle Apr 10, 2024
cb54097
Some more cleanup
PhilWindle Apr 10, 2024
2352c24
Further cleanup
PhilWindle Apr 10, 2024
e0b33ce
Comments and cleanup
PhilWindle Apr 10, 2024
ff861d1
Fixes
PhilWindle Apr 10, 2024
4d263c8
Test fixes
PhilWindle Apr 11, 2024
2ea7278
Cleanup and breaking up test files
PhilWindle Apr 11, 2024
1ba8845
Formatting
PhilWindle Apr 11, 2024
3533c3a
Review fixes
PhilWindle Apr 11, 2024
ef6f80f
Review fixes
PhilWindle Apr 11, 2024
868a94f
Review changes
PhilWindle Apr 11, 2024
a6986f5
Reverted breakage
PhilWindle Apr 11, 2024
80aa850
Merge branch 'pw/generate-keys' of github.com:AztecProtocol/aztec-pac…
PhilWindle Apr 11, 2024
eba7378
Verify proof in test
PhilWindle Apr 11, 2024
9b07deb
Verify proof in test
PhilWindle Apr 11, 2024
ed3db50
Merge branch 'pw/generate-keys' of github.com:AztecProtocol/aztec-pac…
PhilWindle Apr 11, 2024
1a0bc82
Merge branch 'master' into pw/generate-keys
PhilWindle Apr 11, 2024
1d2cd6c
Merge fixes
PhilWindle Apr 11, 2024
82035e3
Merge fixes
PhilWindle Apr 11, 2024
cfc6ea1
Copy the ACVM in test dockerfile
PhilWindle Apr 11, 2024
cfeb8e1
Fixed dockerfile
PhilWindle Apr 11, 2024
6bb1942
Increased test timeouts
PhilWindle Apr 11, 2024
3838a0c
Test fixes
PhilWindle Apr 11, 2024
6080635
Debugging CI
PhilWindle Apr 12, 2024
e6a9bd3
Move prover client testing to it's own job
PhilWindle Apr 12, 2024
960b709
Fixes
PhilWindle Apr 12, 2024
0a049bb
Further debugging
PhilWindle Apr 12, 2024
dcf6755
More debugging
PhilWindle Apr 12, 2024
98755f4
Merge branch 'master' into pw/generate-keys
PhilWindle Apr 12, 2024
460c4e4
Update build manifest dependencies
PhilWindle Apr 12, 2024
f28abe8
Cleanup
PhilWindle Apr 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"bbfree",
"bbmalloc",
"benesjan",
"Bincode",
"bleurgh",
"bodyparser",
"bootnode",
Expand Down
7 changes: 3 additions & 4 deletions noir/noir-repo/tooling/acvm_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use bn254_blackbox_solver::Bn254BlackBoxSolver;
use clap::Args;

use crate::cli::fs::inputs::{read_bytecode_from_file, read_inputs_from_file};
use crate::cli::fs::witness::save_witness_to_dir;
use crate::errors::CliError;
use nargo::ops::{execute_program, DefaultForeignCallExecutor};

use super::fs::witness::create_output_witness_string;
use super::fs::witness::{create_output_witness_string, save_witness_to_dir};

/// Executes a circuit to calculate its return value
#[derive(Debug, Clone, Args)]
Expand Down Expand Up @@ -46,9 +45,9 @@ fn run_command(args: ExecuteCommand) -> Result<String, CliError> {
)?;
if args.output_witness.is_some() {
save_witness_to_dir(
&output_witness_string,
&args.working_directory,
output_witness,
&args.output_witness.unwrap(),
&args.working_directory,
)?;
}
Ok(output_witness_string)
Expand Down
47 changes: 34 additions & 13 deletions noir/noir-repo/tooling/acvm_cli/src/cli/fs/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@ use std::{
path::{Path, PathBuf},
};

use acvm::acir::native_types::WitnessMap;
use acvm::acir::native_types::{WitnessMap, WitnessStack};

use crate::errors::{CliError, FilesystemError};

/// Saves the provided output witnesses to a toml file created at the given location
pub(crate) fn save_witness_to_dir<P: AsRef<Path>>(
output_witness: &String,
witness_dir: P,
file_name: &String,
) -> Result<PathBuf, FilesystemError> {
let witness_path = witness_dir.as_ref().join(file_name);
fn create_named_dir(named_dir: &Path, name: &str) -> PathBuf {
std::fs::create_dir_all(named_dir)
.unwrap_or_else(|_| panic!("could not create the `{name}` directory"));

PathBuf::from(named_dir)
}

let mut file = File::create(&witness_path)
.map_err(|_| FilesystemError::OutputWitnessCreationFailed(file_name.clone()))?;
write!(file, "{}", output_witness)
.map_err(|_| FilesystemError::OutputWitnessWriteFailed(file_name.clone()))?;
fn write_to_file(bytes: &[u8], path: &Path) -> String {
let display = path.display();

Ok(witness_path)
let mut file = match File::create(path) {
Err(why) => panic!("couldn't create {display}: {why}"),
Ok(file) => file,
};

match file.write_all(bytes) {
Err(why) => panic!("couldn't write to {display}: {why}"),
Ok(_) => display.to_string(),
}
}

/// Creates a toml representation of the provided witness map
Expand All @@ -34,3 +39,19 @@ pub(crate) fn create_output_witness_string(witnesses: &WitnessMap) -> Result<Str

toml::to_string(&witness_map).map_err(|_| CliError::OutputWitnessSerializationFailed())
}

pub(crate) fn save_witness_to_dir<P: AsRef<Path>>(
witnesses: WitnessStack,
witness_name: &str,
witness_dir: P,
) -> Result<PathBuf, FilesystemError> {
create_named_dir(witness_dir.as_ref(), "witness");
let witness_path = witness_dir.as_ref().join(witness_name).with_extension("gz");

let buf: Vec<u8> = witnesses
.try_into()
.map_err(|_op| FilesystemError::OutputWitnessCreationFailed(witness_name.to_string()))?;
write_to_file(buf.as_slice(), &witness_path);

Ok(witness_path)
}
3 changes: 0 additions & 3 deletions noir/noir-repo/tooling/acvm_cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ pub(crate) enum FilesystemError {

#[error(" Error: failed to create output witness file {0}.")]
OutputWitnessCreationFailed(String),

#[error(" Error: failed to write output witness file {0}.")]
OutputWitnessWriteFailed(String),
}

#[derive(Debug, Error)]
Expand Down
11 changes: 10 additions & 1 deletion noir/noir-repo/tooling/noirc_abi_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// See Cargo.toml for explanation.
use getrandom as _;

use acvm::acir::native_types::WitnessMap;
use acvm::acir::native_types::{WitnessMap, WitnessStack};
use iter_extended::try_btree_map;
use noirc_abi::{
errors::InputParserError,
Expand Down Expand Up @@ -113,3 +113,12 @@ pub fn abi_decode(abi: JsAbi, witness_map: JsWitnessMap) -> Result<JsValue, JsAb
<wasm_bindgen::JsValue as JsValueSerdeExt>::from_serde(&return_struct)
.map_err(|err| err.to_string().into())
}

#[wasm_bindgen(js_name = serializeWitness)]
pub fn serialise_witness(witness_map: JsWitnessMap) -> Result<Vec<u8>, JsAbiError> {
console_error_panic_hook::set_once();
let converted_witness: WitnessMap = witness_map.into();
let witness_stack: WitnessStack = converted_witness.into();
let output = witness_stack.try_into();
output.map_err(|_| JsAbiError::new("Failed to convert to Vec<u8>".to_string()))
}
32 changes: 32 additions & 0 deletions yarn-project/circuit-types/src/tx/processed_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,34 @@ import {
type Header,
KernelCircuitPublicInputs,
type Proof,
type PublicKernelCircuitPrivateInputs,
type PublicKernelCircuitPublicInputs,
type PublicKernelTailCircuitPrivateInputs,
makeEmptyProof,
} from '@aztec/circuits.js';

/**
* Used to communicate to the prover which type of circuit to prove
*/
export enum PublicKernelType {
SETUP,
APP_LOGIC,
TEARDOWN,
TAIL,
}

export type PublicKernelTailRequest = {
type: PublicKernelType.TAIL;
inputs: PublicKernelTailCircuitPrivateInputs;
};

export type PublicKernelNonTailRequest = {
type: PublicKernelType.SETUP | PublicKernelType.APP_LOGIC | PublicKernelType.TEARDOWN;
inputs: PublicKernelCircuitPrivateInputs;
};

export type PublicKernelRequest = PublicKernelTailRequest | PublicKernelNonTailRequest;

/**
* Represents a tx that has been processed by the sequencer public processor,
* so its kernel circuit public inputs are filled in.
Expand All @@ -38,6 +62,11 @@ export type ProcessedTx = Pick<Tx, 'proof' | 'encryptedLogs' | 'unencryptedLogs'
* Reason the tx was reverted.
*/
revertReason: SimulationError | undefined;

/**
* The collection of public kernel circuit inputs for simulation/proving
*/
publicKernelRequests: PublicKernelRequest[];
};

export type RevertedTx = ProcessedTx & {
Expand Down Expand Up @@ -90,6 +119,7 @@ export function makeProcessedTx(
tx: Tx,
kernelOutput: KernelCircuitPublicInputs,
proof: Proof,
publicKernelRequests: PublicKernelRequest[],
revertReason?: SimulationError,
): ProcessedTx {
return {
Expand All @@ -100,6 +130,7 @@ export function makeProcessedTx(
unencryptedLogs: revertReason ? UnencryptedTxL2Logs.empty() : tx.unencryptedLogs,
isEmpty: false,
revertReason,
publicKernelRequests,
};
}

Expand All @@ -123,6 +154,7 @@ export function makeEmptyProcessedTx(header: Header, chainId: Fr, version: Fr):
proof: emptyProof,
isEmpty: true,
revertReason: undefined,
publicKernelRequests: [],
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('L1Publisher integration', () => {
seed + 0x500,
);

const processedTx = makeProcessedTx(tx, kernelOutput, makeProof());
const processedTx = makeProcessedTx(tx, kernelOutput, makeProof(), []);

processedTx.data.end.newNoteHashes = makeTuple(MAX_NEW_NOTE_HASHES_PER_TX, fr, seed + 0x100);
processedTx.data.end.newNullifiers = makeTuple(MAX_NEW_NULLIFIERS_PER_TX, fr, seed + 0x200);
Expand Down
90 changes: 87 additions & 3 deletions yarn-project/noir-protocol-circuits-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
createBlackBoxSolver,
executeCircuitWithBlackBoxSolver,
} from '@noir-lang/acvm_js';
import { type Abi, abiDecode, abiEncode } from '@noir-lang/noirc_abi';
import { type Abi, abiDecode, abiEncode, serializeWitness } from '@noir-lang/noirc_abi';
import { type WitnessMap } from '@noir-lang/types';

import BaseParityJson from './target/parity_base.json' assert { type: 'json' };
Expand All @@ -46,6 +46,7 @@ import PublicKernelAppLogicSimulatedJson from './target/public_kernel_app_logic_
import PublicKernelSetupSimulatedJson from './target/public_kernel_setup_simulated.json' assert { type: 'json' };
import PublicKernelTailSimulatedJson from './target/public_kernel_tail_simulated.json' assert { type: 'json' };
import PublicKernelTeardownSimulatedJson from './target/public_kernel_teardown_simulated.json' assert { type: 'json' };
import BaseRollupJson from './target/rollup_base.json' assert { type: 'json' };
import BaseRollupSimulatedJson from './target/rollup_base_simulated.json' assert { type: 'json' };
import MergeRollupJson from './target/rollup_merge.json' assert { type: 'json' };
import RootRollupJson from './target/rollup_root.json' assert { type: 'json' };
Expand Down Expand Up @@ -126,12 +127,65 @@ export const BaseParityArtifact = BaseParityJson as NoirCompiledCircuit;

export const RootParityArtifact = RootParityJson as NoirCompiledCircuit;

export const BaseRollupArtifact = BaseRollupSimulatedJson as NoirCompiledCircuit;
export const SimulatedBaseRollupArtifact = BaseRollupSimulatedJson as NoirCompiledCircuit;

export const BaseRollupArtifact = BaseRollupJson as NoirCompiledCircuit;

export const MergeRollupArtifact = MergeRollupJson as NoirCompiledCircuit;

export const RootRollupArtifact = RootRollupJson as NoirCompiledCircuit;

export type ServerProtocolArtifact =
| 'PublicKernelSetupArtifact'
| 'PublicKernelAppLogicArtifact'
| 'PublicKernelTeardownArtifact'
| 'PublicKernelTailArtifact'
| 'BaseParityArtifact'
| 'RootParityArtifact'
| 'BaseRollupArtifact'
| 'MergeRollupArtifact'
| 'RootRollupArtifact';

export type ClientProtocolArtifact =
| 'PrivateKernelInitArtifact'
| 'PrivateKernelInnerArtifact'
| 'PrivateKernelTailArtifact';

export type ProtocolArtifact = ServerProtocolArtifact | ClientProtocolArtifact;

export const ServerCircuitArtifacts: Record<ServerProtocolArtifact, NoirCompiledCircuit> = {
PublicKernelSetupArtifact: PublicKernelSetupArtifact,
PublicKernelAppLogicArtifact: PublicKernelAppLogicArtifact,
PublicKernelTeardownArtifact: PublicKernelTeardownArtifact,
PublicKernelTailArtifact: PublicKernelTailArtifact,
BaseParityArtifact: BaseParityArtifact,
RootParityArtifact: RootParityArtifact,
BaseRollupArtifact: BaseRollupArtifact,
MergeRollupArtifact: MergeRollupArtifact,
RootRollupArtifact: RootRollupArtifact,
};

export const ClientCircuitArtifacts: Record<ClientProtocolArtifact, NoirCompiledCircuit> = {
PrivateKernelInitArtifact: PrivateKernelInitArtifact,
PrivateKernelInnerArtifact: PrivateKernelInnerArtifact,
PrivateKernelTailArtifact: PrivateKernelTailArtifact,
};

export const ProtocolCircuitArtifacts: Record<ProtocolArtifact, NoirCompiledCircuit> = {
PrivateKernelInitArtifact: PrivateKernelInitArtifact,
PrivateKernelInnerArtifact: PrivateKernelInnerArtifact,
PrivateKernelTailArtifact: PrivateKernelTailArtifact,
PublicKernelSetupArtifact: PublicKernelSetupArtifact,
PublicKernelAppLogicArtifact: PublicKernelAppLogicArtifact,
PublicKernelTeardownArtifact: PublicKernelTeardownArtifact,
PublicKernelTailArtifact: PublicKernelTailArtifact,
BaseParityArtifact: BaseParityArtifact,
RootParityArtifact: RootParityArtifact,
BaseRollupArtifact: BaseRollupArtifact,
MergeRollupArtifact: MergeRollupArtifact,
RootRollupArtifact: RootRollupArtifact,
};

let solver: Promise<WasmBlackBoxFunctionSolver>;

const getSolver = (): Promise<WasmBlackBoxFunctionSolver> => {
Expand All @@ -141,6 +195,10 @@ const getSolver = (): Promise<WasmBlackBoxFunctionSolver> => {
return solver;
};

export function serializeInputWitness(witness: WitnessMap) {
return serializeWitness(witness);
}

/**
* Executes the init private kernel.
* @param privateKernelInitCircuitPrivateInputs - The private inputs to the initial private kernel.
Expand Down Expand Up @@ -236,6 +294,17 @@ export function convertRootParityInputsToWitnessMap(inputs: RootParityInputs): W
* @returns The witness map
*/
export function convertBaseRollupInputsToWitnessMap(inputs: BaseRollupInputs): WitnessMap {
const mapped = mapBaseRollupInputsToNoir(inputs);
const initialWitnessMap = abiEncode(BaseRollupJson.abi as Abi, { inputs: mapped as any });
return initialWitnessMap;
}

/**
* Converts the inputs of the simulated base rollup circuit into a witness map.
* @param inputs - The base rollup inputs.
* @returns The witness map
*/
export function convertSimulatedBaseRollupInputsToWitnessMap(inputs: BaseRollupInputs): WitnessMap {
const mapped = mapBaseRollupInputsToNoir(inputs);
const initialWitnessMap = abiEncode(BaseRollupSimulatedJson.abi as Abi, { inputs: mapped as any });
return initialWitnessMap;
Expand Down Expand Up @@ -306,14 +375,29 @@ export function convertPublicTailInputsToWitnessMap(inputs: PublicKernelTailCirc
return initialWitnessMap;
}

/**
* Converts the outputs of the simulated base rollup circuit from a witness map.
* @param outputs - The base rollup outputs as a witness map.
* @returns The public inputs.
*/
export function convertSimulatedBaseRollupOutputsFromWitnessMap(outputs: WitnessMap): BaseOrMergeRollupPublicInputs {
// Decode the witness map into two fields, the return values and the inputs
const decodedInputs: DecodedInputs = abiDecode(BaseRollupSimulatedJson.abi as Abi, outputs);

// Cast the inputs as the return type
const returnType = decodedInputs.return_value as BaseRollupReturnType;

return mapBaseOrMergeRollupPublicInputsFromNoir(returnType);
}

/**
* Converts the outputs of the base rollup circuit from a witness map.
* @param outputs - The base rollup outputs as a witness map.
* @returns The public inputs.
*/
export function convertBaseRollupOutputsFromWitnessMap(outputs: WitnessMap): BaseOrMergeRollupPublicInputs {
// Decode the witness map into two fields, the return values and the inputs
const decodedInputs: DecodedInputs = abiDecode(BaseRollupSimulatedJson.abi as Abi, outputs);
const decodedInputs: DecodedInputs = abiDecode(BaseRollupJson.abi as Abi, outputs);

// Cast the inputs as the return type
const returnType = decodedInputs.return_value as BaseRollupReturnType;
Expand Down
7 changes: 7 additions & 0 deletions yarn-project/prover-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.1.0",
"type": "module",
"exports": "./dest/index.js",
"bin": {
"bb-cli": "./dest/bb/index.js"
},
"typedocOptions": {
"entryPoints": [
"./src/index.ts"
Expand All @@ -16,6 +19,7 @@
"clean": "rm -rf ./dest .tsbuildinfo",
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
"formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
"bb": "node --no-warnings ./dest/bb/index.js",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests"
},
"inherits": [
Expand Down Expand Up @@ -44,14 +48,17 @@
"@aztec/noir-protocol-circuits-types": "workspace:^",
"@aztec/simulator": "workspace:^",
"@aztec/world-state": "workspace:^",
"commander": "^9.0.0",
"lodash.chunk": "^4.2.0",
"source-map-support": "^0.5.21",
"tslib": "^2.4.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@types/jest": "^29.5.0",
"@types/memdown": "^3.0.0",
"@types/node": "^18.7.23",
"@types/source-map-support": "^0.5.10",
"jest": "^29.5.0",
"jest-mock-extended": "^3.0.3",
"ts-node": "^10.9.1",
Expand Down
Loading