move impl to field_config

This commit is contained in:
Wanseob Lim
2022-06-08 18:04:09 +09:00
parent 6d05341e9d
commit d49335c967
2 changed files with 68 additions and 65 deletions

View File

@@ -3,15 +3,13 @@ use std::marker::PhantomData;
use halo2_proofs::{
arithmetic::FieldExt,
circuit::Chip,
plonk::{Advice, Column, ConstraintSystem, Fixed, Instance},
poly::Rotation,
};
use super::field_config::FieldConfig;
pub struct FieldChip<F: FieldExt> {
config: FieldConfig,
_marker: PhantomData<F>,
pub config: FieldConfig,
pub _marker: PhantomData<F>,
}
// ANCHOR: chip-impl
@@ -28,62 +26,3 @@ impl<F: FieldExt> Chip<F> for FieldChip<F> {
}
}
impl<F: FieldExt> FieldChip<F> {
pub fn construct(config: <Self as Chip<F>>::Config) -> Self {
Self {
config,
_marker: PhantomData,
}
}
pub fn configure(
meta: &mut ConstraintSystem<F>,
advice: [Column<Advice>; 2],
instance: Column<Instance>,
constant: Column<Fixed>,
) -> <Self as Chip<F>>::Config {
meta.enable_equality(instance);
meta.enable_constant(constant);
for column in &advice {
meta.enable_equality(*column);
}
let s_mul = meta.selector();
// Define our multiplication gate!
meta.create_gate("mul", |meta| {
// To implement multiplication, we need three advice cells and a selector
// cell. We arrange them like so:
//
// | a0 | a1 | s_mul |
// |-----|-----|-------|
// | lhs | rhs | s_mul |
// | out | | |
//
// Gates may refer to any relative offsets we want, but each distinct
// offset adds a cost to the proof. The most common offsets are 0 (the
// current row), 1 (the next row), and -1 (the previous row), for which
// `Rotation` has specific constructors.
let lhs = meta.query_advice(advice[0], Rotation::cur());
let rhs = meta.query_advice(advice[1], Rotation::cur());
let out = meta.query_advice(advice[0], Rotation::next());
let s_mul = meta.query_selector(s_mul);
// Finally, we return the polynomial expressions that constrain this gate.
// For our multiplication gate, we only need a single polynomial constraint.
//
// The polynomial expressions returned from `create_gate` will be
// constrained by the proving system to equal zero. Our expression
// has the following properties:
// - When s_mul = 0, any value is allowed in lhs, rhs, and out.
// - When s_mul != 0, this constrains lhs * rhs = out.
vec![s_mul * (lhs * rhs - out)]
});
FieldConfig {
advice,
instance,
s_mul,
}
}
}
// ANCHOR_END: chip-config

View File

@@ -1,4 +1,8 @@
use halo2_proofs::plonk::{Advice, Column, Instance, Selector};
use std::marker::PhantomData;
use halo2_proofs::{plonk::{Advice, Column, Instance, Selector, ConstraintSystem, Fixed}, arithmetic::FieldExt, circuit::Chip, poly::Rotation};
use super::field_chip::FieldChip;
#[derive(Clone, Debug)]
pub struct FieldConfig {
@@ -15,4 +19,64 @@ pub struct FieldConfig {
// This is important when building larger circuits, where columns are used by
// multiple sets of instructions.
pub s_mul: Selector,
}
}
impl<F: FieldExt> FieldChip<F> {
pub fn construct(config: <Self as Chip<F>>::Config) -> Self {
Self {
config,
_marker: PhantomData,
}
}
pub fn configure(
meta: &mut ConstraintSystem<F>,
advice: [Column<Advice>; 2],
instance: Column<Instance>,
constant: Column<Fixed>,
) -> <Self as Chip<F>>::Config {
meta.enable_equality(instance);
meta.enable_constant(constant);
for column in &advice {
meta.enable_equality(*column);
}
let s_mul = meta.selector();
// Define our multiplication gate!
meta.create_gate("mul", |meta| {
// To implement multiplication, we need three advice cells and a selector
// cell. We arrange them like so:
//
// | a0 | a1 | s_mul |
// |-----|-----|-------|
// | lhs | rhs | s_mul |
// | out | | |
//
// Gates may refer to any relative offsets we want, but each distinct
// offset adds a cost to the proof. The most common offsets are 0 (the
// current row), 1 (the next row), and -1 (the previous row), for which
// `Rotation` has specific constructors.
let lhs = meta.query_advice(advice[0], Rotation::cur());
let rhs = meta.query_advice(advice[1], Rotation::cur());
let out = meta.query_advice(advice[0], Rotation::next());
let s_mul = meta.query_selector(s_mul);
// Finally, we return the polynomial expressions that constrain this gate.
// For our multiplication gate, we only need a single polynomial constraint.
//
// The polynomial expressions returned from `create_gate` will be
// constrained by the proving system to equal zero. Our expression
// has the following properties:
// - When s_mul = 0, any value is allowed in lhs, rhs, and out.
// - When s_mul != 0, this constrains lhs * rhs = out.
vec![s_mul * (lhs * rhs - out)]
});
FieldConfig {
advice,
instance,
s_mul,
}
}
}
// ANCHOR_END: chip-config