Use UtilitiesInstructions::Var instead of internal associated type.

Co-authored-by: Jack Grigg <jack@electriccoin.co>
This commit is contained in:
therealyingtong 2021-06-08 00:20:09 +08:00
parent 40599144bf
commit 0f2dfc5508
3 changed files with 19 additions and 51 deletions

View File

@ -8,27 +8,15 @@ use pasta_curves::arithmetic::FieldExt;
use std::marker::PhantomData;
pub trait CondSwapInstructions<F: FieldExt>: UtilitiesInstructions<F> {
/// Variable representing cell with a certain value in the circuit.
type Var;
#[allow(clippy::type_complexity)]
/// Given an input pair (x,y) and a `swap` boolean flag, return
/// (y,x) if `swap` is set, else (x,y) if `swap` is not set.
fn swap(
&self,
layouter: impl Layouter<F>,
pair: (
<Self as CondSwapInstructions<F>>::Var,
<Self as CondSwapInstructions<F>>::Var,
),
pair: (Self::Var, Self::Var),
swap: Option<bool>,
) -> Result<
(
<Self as CondSwapInstructions<F>>::Var,
<Self as CondSwapInstructions<F>>::Var,
),
Error,
>;
) -> Result<(Self::Var, Self::Var), Error>;
}
/// A chip implementing a conditional swap.
@ -67,24 +55,13 @@ impl<F: FieldExt> UtilitiesInstructions<F> for CondSwapChip<F> {
}
impl<F: FieldExt> CondSwapInstructions<F> for CondSwapChip<F> {
type Var = CellValue<F>;
#[allow(clippy::type_complexity)]
fn swap(
&self,
mut layouter: impl Layouter<F>,
pair: (
<Self as CondSwapInstructions<F>>::Var,
<Self as CondSwapInstructions<F>>::Var,
),
pair: (Self::Var, Self::Var),
swap: Option<bool>,
) -> Result<
(
<Self as CondSwapInstructions<F>>::Var,
<Self as CondSwapInstructions<F>>::Var,
),
Error,
> {
) -> Result<(Self::Var, Self::Var), Error> {
let config = self.config();
layouter.assign_region(

View File

@ -1,4 +1,4 @@
use super::{copy, CellValue, UtilitiesInstructions, Var};
use super::{copy, CellValue, UtilitiesInstructions};
use halo2::{
circuit::{Chip, Layouter},
plonk::{Advice, Column, ConstraintSystem, Error, Expression, Permutation, Selector},
@ -8,15 +8,12 @@ use pasta_curves::arithmetic::FieldExt;
use std::marker::PhantomData;
pub trait EnableFlagInstructions<F: FieldExt>: UtilitiesInstructions<F> {
/// Variable representing cell with a certain value in the circuit.
type Var: Var<F>;
/// Given a `value` and an `enable_flag`, check that either `value = 0`
/// or `enable_flag = 1`.
fn enable_flag(
&self,
layouter: impl Layouter<F>,
value: <Self as EnableFlagInstructions<F>>::Var,
value: Self::Var,
enable_flag: Option<bool>,
) -> Result<(), Error>;
}
@ -54,12 +51,10 @@ impl<F: FieldExt> UtilitiesInstructions<F> for EnableFlagChip<F> {
}
impl<F: FieldExt> EnableFlagInstructions<F> for EnableFlagChip<F> {
type Var = CellValue<F>;
fn enable_flag(
&self,
mut layouter: impl Layouter<F>,
value: <Self as EnableFlagInstructions<F>>::Var,
value: Self::Var,
enable_flag: Option<bool>,
) -> Result<(), Error> {
let config = self.config().clone();

View File

@ -10,15 +10,13 @@ use std::marker::PhantomData;
#[allow(clippy::upper_case_acronyms)]
#[allow(clippy::too_many_arguments)]
pub trait PLONKInstructions<F: FieldExt>: UtilitiesInstructions<F> {
type Var;
// Checks that a * sm * b = c * sc
fn mul(
&self,
layouter: impl Layouter<F>,
a: <Self as PLONKInstructions<F>>::Var,
b: <Self as PLONKInstructions<F>>::Var,
c: <Self as PLONKInstructions<F>>::Var,
a: Self::Var,
b: Self::Var,
c: Self::Var,
sc: Option<F>,
sm: Option<F>,
) -> Result<(), Error>;
@ -26,9 +24,9 @@ pub trait PLONKInstructions<F: FieldExt>: UtilitiesInstructions<F> {
fn add(
&self,
layouter: impl Layouter<F>,
a: <Self as PLONKInstructions<F>>::Var,
b: <Self as PLONKInstructions<F>>::Var,
c: <Self as PLONKInstructions<F>>::Var,
a: Self::Var,
b: Self::Var,
c: Self::Var,
sa: Option<F>,
sb: Option<F>,
sc: Option<F>,
@ -77,14 +75,12 @@ impl<F: FieldExt> UtilitiesInstructions<F> for PLONKChip<F> {
#[allow(clippy::upper_case_acronyms)]
impl<F: FieldExt> PLONKInstructions<F> for PLONKChip<F> {
type Var = CellValue<F>;
fn mul(
&self,
mut layouter: impl Layouter<F>,
a: <Self as PLONKInstructions<F>>::Var,
b: <Self as PLONKInstructions<F>>::Var,
c: <Self as PLONKInstructions<F>>::Var,
a: Self::Var,
b: Self::Var,
c: Self::Var,
sc: Option<F>,
sm: Option<F>,
) -> Result<(), Error> {
@ -129,9 +125,9 @@ impl<F: FieldExt> PLONKInstructions<F> for PLONKChip<F> {
fn add(
&self,
mut layouter: impl Layouter<F>,
a: <Self as PLONKInstructions<F>>::Var,
b: <Self as PLONKInstructions<F>>::Var,
c: <Self as PLONKInstructions<F>>::Var,
a: Self::Var,
b: Self::Var,
c: Self::Var,
sa: Option<F>,
sb: Option<F>,
sc: Option<F>,