midnight_circuits/utils/composable.rs
1// This file is part of MIDNIGHT-ZK.
2// Copyright (C) Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14//! Trait for modular, composable chips.
15
16use std::fmt::Debug;
17
18use midnight_proofs::{
19 circuit::{Chip, Layouter},
20 plonk::{ConstraintSystem, Error},
21};
22
23use crate::CircuitField;
24
25/// Provides a common interface for layering chips with shared resources.
26pub trait ComposableChip<F>: Chip<F> + Clone + Debug
27where
28 F: CircuitField,
29{
30 /// Resources that can be used by other chips or gadgets,
31 /// typically sub-chip configurations and columns.
32 type SharedResources;
33
34 /// Instruction set dependencies of the chip.
35 /// This chip will need to be provided with subchips that implement these
36 /// instructions.
37 type InstructionDeps;
38
39 /// Initialize the chip.
40 fn new(config: &Self::Config, sub_chips: &Self::InstructionDeps) -> Self;
41
42 /// Configure the chip.
43 /// Receives the underlying chips and columns it needs via
44 /// Self::SharedResources. This method must not allocate any resource in
45 /// the constraint system that is intended to be shared by other chips.
46 fn configure(
47 meta: &mut ConstraintSystem<F>,
48 shared_resources: &Self::SharedResources,
49 ) -> Self::Config;
50
51 /// Load all tables (including those of underlying chips taken as configs)
52 fn load(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error>;
53}