mirror of https://github.com/zcash/halo2.git
Merge pull request #475 from zcash/patch-gadgets-import
`halo2_gadgets` import: Visibility fixes and Clippy fixes.
This commit is contained in:
commit
f3502421cd
|
@ -130,7 +130,7 @@ impl<FixedPoints: super::FixedPoints<pallas::Affine>> Config<FixedPoints> {
|
||||||
fn running_sum_coords_gate(&self, meta: &mut ConstraintSystem<pallas::Base>) {
|
fn running_sum_coords_gate(&self, meta: &mut ConstraintSystem<pallas::Base>) {
|
||||||
meta.create_gate("Running sum coordinates check", |meta| {
|
meta.create_gate("Running sum coordinates check", |meta| {
|
||||||
let q_mul_fixed_running_sum =
|
let q_mul_fixed_running_sum =
|
||||||
meta.query_selector(self.running_sum_config.q_range_check);
|
meta.query_selector(self.running_sum_config.q_range_check());
|
||||||
|
|
||||||
let z_cur = meta.query_advice(self.window, Rotation::cur());
|
let z_cur = meta.query_advice(self.window, Rotation::cur());
|
||||||
let z_next = meta.query_advice(self.window, Rotation::next());
|
let z_next = meta.query_advice(self.window, Rotation::next());
|
||||||
|
|
|
@ -190,7 +190,7 @@ impl<Fixed: FixedPoints<pallas::Affine>> Config<Fixed> {
|
||||||
offset,
|
offset,
|
||||||
&(&scalar).into(),
|
&(&scalar).into(),
|
||||||
base,
|
base,
|
||||||
self.super_config.running_sum_config.q_range_check,
|
self.super_config.running_sum_config.q_range_check(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok((scalar, acc, mul_b))
|
Ok((scalar, acc, mul_b))
|
||||||
|
|
|
@ -195,7 +195,7 @@ pub mod tests {
|
||||||
test_single_base(
|
test_single_base(
|
||||||
chip.clone(),
|
chip.clone(),
|
||||||
layouter.namespace(|| "full_width"),
|
layouter.namespace(|| "full_width"),
|
||||||
FixedPoint::from_inner(chip.clone(), test_base.clone()),
|
FixedPoint::from_inner(chip, test_base.clone()),
|
||||||
test_base.generator(),
|
test_base.generator(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
|
@ -117,7 +117,7 @@ impl<Fixed: FixedPoints<pallas::Affine>> Config<Fixed> {
|
||||||
offset,
|
offset,
|
||||||
&(&scalar).into(),
|
&(&scalar).into(),
|
||||||
base,
|
base,
|
||||||
self.super_config.running_sum_config.q_range_check,
|
self.super_config.running_sum_config.q_range_check(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok((scalar, acc, mul_b))
|
Ok((scalar, acc, mul_b))
|
||||||
|
|
|
@ -978,10 +978,9 @@ mod tests {
|
||||||
let compression = config.compression.clone();
|
let compression = config.compression.clone();
|
||||||
let initial_state = compression.initialize_with_iv(&mut layouter, IV)?;
|
let initial_state = compression.initialize_with_iv(&mut layouter, IV)?;
|
||||||
|
|
||||||
let state =
|
let state = config
|
||||||
config
|
|
||||||
.compression
|
.compression
|
||||||
.compress(&mut layouter, initial_state.clone(), w_halves)?;
|
.compress(&mut layouter, initial_state, w_halves)?;
|
||||||
|
|
||||||
let digest = config.compression.digest(&mut layouter, state)?;
|
let digest = config.compression.digest(&mut layouter, state)?;
|
||||||
for (idx, digest_word) in digest.iter().enumerate() {
|
for (idx, digest_word) in digest.iter().enumerate() {
|
||||||
|
|
|
@ -443,7 +443,7 @@ pub(crate) mod tests {
|
||||||
|
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
pub(crate) const PERSONALIZATION: &'static str = "MerkleCRH";
|
pub(crate) const PERSONALIZATION: &str = "MerkleCRH";
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref COMMIT_DOMAIN: sinsemilla::CommitDomain =
|
static ref COMMIT_DOMAIN: sinsemilla::CommitDomain =
|
||||||
|
|
|
@ -13,7 +13,7 @@ use crate::utilities::{
|
||||||
};
|
};
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
pub(crate) mod chip;
|
pub mod chip;
|
||||||
|
|
||||||
/// SWU hash-to-curve personalization for the Merkle CRH generator
|
/// SWU hash-to-curve personalization for the Merkle CRH generator
|
||||||
pub const MERKLE_CRH_PERSONALIZATION: &str = "z.cash:Orchard-MerkleCRH";
|
pub const MERKLE_CRH_PERSONALIZATION: &str = "z.cash:Orchard-MerkleCRH";
|
||||||
|
@ -58,12 +58,40 @@ pub struct MerklePath<
|
||||||
> where
|
> where
|
||||||
MerkleChip: MerkleInstructions<C, PATH_LENGTH, K, MAX_WORDS> + Clone,
|
MerkleChip: MerkleInstructions<C, PATH_LENGTH, K, MAX_WORDS> + Clone,
|
||||||
{
|
{
|
||||||
pub(crate) chip_1: MerkleChip,
|
chip_1: MerkleChip,
|
||||||
pub(crate) chip_2: MerkleChip,
|
chip_2: MerkleChip,
|
||||||
pub(crate) domain: MerkleChip::HashDomains,
|
domain: MerkleChip::HashDomains,
|
||||||
pub(crate) leaf_pos: Option<u32>,
|
leaf_pos: Option<u32>,
|
||||||
// The Merkle path is ordered from leaves to root.
|
// The Merkle path is ordered from leaves to root.
|
||||||
pub(crate) path: Option<[C::Base; PATH_LENGTH]>,
|
path: Option<[C::Base; PATH_LENGTH]>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<
|
||||||
|
C: CurveAffine,
|
||||||
|
MerkleChip,
|
||||||
|
const PATH_LENGTH: usize,
|
||||||
|
const K: usize,
|
||||||
|
const MAX_WORDS: usize,
|
||||||
|
> MerklePath<C, MerkleChip, PATH_LENGTH, K, MAX_WORDS>
|
||||||
|
where
|
||||||
|
MerkleChip: MerkleInstructions<C, PATH_LENGTH, K, MAX_WORDS> + Clone,
|
||||||
|
{
|
||||||
|
/// Constructs a [`MerklePath`].
|
||||||
|
pub fn construct(
|
||||||
|
chip_1: MerkleChip,
|
||||||
|
chip_2: MerkleChip,
|
||||||
|
domain: MerkleChip::HashDomains,
|
||||||
|
leaf_pos: Option<u32>,
|
||||||
|
path: Option<[C::Base; PATH_LENGTH]>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
chip_1,
|
||||||
|
chip_2,
|
||||||
|
domain,
|
||||||
|
leaf_pos,
|
||||||
|
path,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
|
@ -78,7 +106,7 @@ where
|
||||||
MerkleChip: MerkleInstructions<C, PATH_LENGTH, K, MAX_WORDS> + Clone,
|
MerkleChip: MerkleInstructions<C, PATH_LENGTH, K, MAX_WORDS> + Clone,
|
||||||
{
|
{
|
||||||
/// Calculates the root of the tree containing the given leaf at this Merkle path.
|
/// Calculates the root of the tree containing the given leaf at this Merkle path.
|
||||||
pub(crate) fn calculate_root(
|
pub fn calculate_root(
|
||||||
&self,
|
&self,
|
||||||
mut layouter: impl Layouter<C::Base>,
|
mut layouter: impl Layouter<C::Base>,
|
||||||
leaf: MerkleChip::Var,
|
leaf: MerkleChip::Var,
|
||||||
|
@ -253,7 +281,7 @@ pub mod tests {
|
||||||
|
|
||||||
let leaf = chip_1.load_private(
|
let leaf = chip_1.load_private(
|
||||||
layouter.namespace(|| ""),
|
layouter.namespace(|| ""),
|
||||||
config.0.cond_swap_config.a,
|
config.0.cond_swap_config.a(),
|
||||||
self.leaf,
|
self.leaf,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
//! Chip implementing a Merkle hash using Sinsemilla as the hash function.
|
||||||
|
|
||||||
use halo2_proofs::{
|
use halo2_proofs::{
|
||||||
circuit::{AssignedCell, Chip, Layouter},
|
circuit::{AssignedCell, Chip, Layouter},
|
||||||
plonk::{Advice, Column, ConstraintSystem, Error, Selector},
|
plonk::{Advice, Column, ConstraintSystem, Error, Selector},
|
||||||
|
@ -25,6 +27,7 @@ use crate::{
|
||||||
use group::ff::PrimeField;
|
use group::ff::PrimeField;
|
||||||
use std::array;
|
use std::array;
|
||||||
|
|
||||||
|
/// Configuration for the `MerkleChip` implementation.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct MerkleConfig<Hash, Commit, Fixed>
|
pub struct MerkleConfig<Hash, Commit, Fixed>
|
||||||
where
|
where
|
||||||
|
@ -38,6 +41,7 @@ where
|
||||||
pub(super) sinsemilla_config: SinsemillaConfig<Hash, Commit, Fixed>,
|
pub(super) sinsemilla_config: SinsemillaConfig<Hash, Commit, Fixed>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Chip implementing `MerkleInstructions`.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct MerkleChip<Hash, Commit, Fixed>
|
pub struct MerkleChip<Hash, Commit, Fixed>
|
||||||
where
|
where
|
||||||
|
@ -72,6 +76,7 @@ where
|
||||||
F: FixedPoints<pallas::Affine>,
|
F: FixedPoints<pallas::Affine>,
|
||||||
Commit: CommitDomains<pallas::Affine, F, Hash>,
|
Commit: CommitDomains<pallas::Affine, F, Hash>,
|
||||||
{
|
{
|
||||||
|
/// Configures the [`MerkleChip`].
|
||||||
pub fn configure(
|
pub fn configure(
|
||||||
meta: &mut ConstraintSystem<pallas::Base>,
|
meta: &mut ConstraintSystem<pallas::Base>,
|
||||||
sinsemilla_config: SinsemillaConfig<Hash, Commit, F>,
|
sinsemilla_config: SinsemillaConfig<Hash, Commit, F>,
|
||||||
|
@ -175,6 +180,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructs a [`MerkleChip`] given a [`MerkleConfig`].
|
||||||
pub fn construct(config: MerkleConfig<Hash, Commit, F>) -> Self {
|
pub fn construct(config: MerkleConfig<Hash, Commit, F>) -> Self {
|
||||||
MerkleChip { config }
|
MerkleChip { config }
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,9 @@ use halo2_proofs::{
|
||||||
use pasta_curves::arithmetic::FieldExt;
|
use pasta_curves::arithmetic::FieldExt;
|
||||||
use std::{array, ops::Range};
|
use std::{array, ops::Range};
|
||||||
|
|
||||||
pub(crate) mod cond_swap;
|
pub mod cond_swap;
|
||||||
pub(crate) mod decompose_running_sum;
|
pub mod decompose_running_sum;
|
||||||
pub(crate) mod lookup_range_check;
|
pub mod lookup_range_check;
|
||||||
|
|
||||||
/// Trait for a variable in the circuit.
|
/// Trait for a variable in the circuit.
|
||||||
pub trait Var<F: FieldExt>: Clone + std::fmt::Debug + From<AssignedCell<F, F>> {
|
pub trait Var<F: FieldExt>: Clone + std::fmt::Debug + From<AssignedCell<F, F>> {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
//! Gadget and chip for a conditional swap utility.
|
||||||
|
|
||||||
use super::{bool_check, ternary, UtilitiesInstructions};
|
use super::{bool_check, ternary, UtilitiesInstructions};
|
||||||
use halo2_proofs::{
|
use halo2_proofs::{
|
||||||
circuit::{AssignedCell, Chip, Layouter},
|
circuit::{AssignedCell, Chip, Layouter},
|
||||||
|
@ -7,6 +9,7 @@ use halo2_proofs::{
|
||||||
use pasta_curves::arithmetic::FieldExt;
|
use pasta_curves::arithmetic::FieldExt;
|
||||||
use std::{array, marker::PhantomData};
|
use std::{array, marker::PhantomData};
|
||||||
|
|
||||||
|
/// Instructions for a conditional swap gadget.
|
||||||
pub trait CondSwapInstructions<F: FieldExt>: UtilitiesInstructions<F> {
|
pub trait CondSwapInstructions<F: FieldExt>: UtilitiesInstructions<F> {
|
||||||
#[allow(clippy::type_complexity)]
|
#[allow(clippy::type_complexity)]
|
||||||
/// Given an input pair (a,b) and a `swap` boolean flag, returns
|
/// Given an input pair (a,b) and a `swap` boolean flag, returns
|
||||||
|
@ -42,14 +45,22 @@ impl<F: FieldExt> Chip<F> for CondSwapChip<F> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configuration for the [`CondSwapChip`].
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct CondSwapConfig {
|
pub struct CondSwapConfig {
|
||||||
pub q_swap: Selector,
|
q_swap: Selector,
|
||||||
pub a: Column<Advice>,
|
a: Column<Advice>,
|
||||||
pub b: Column<Advice>,
|
b: Column<Advice>,
|
||||||
pub a_swapped: Column<Advice>,
|
a_swapped: Column<Advice>,
|
||||||
pub b_swapped: Column<Advice>,
|
b_swapped: Column<Advice>,
|
||||||
pub swap: Column<Advice>,
|
swap: Column<Advice>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
impl CondSwapConfig {
|
||||||
|
pub(crate) fn a(&self) -> Column<Advice> {
|
||||||
|
self.a
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: FieldExt> UtilitiesInstructions<F> for CondSwapChip<F> {
|
impl<F: FieldExt> UtilitiesInstructions<F> for CondSwapChip<F> {
|
||||||
|
@ -185,6 +196,7 @@ impl<F: FieldExt> CondSwapChip<F> {
|
||||||
config
|
config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructs a [`CondSwapChip`] given a [`CondSwapConfig`].
|
||||||
pub fn construct(config: CondSwapConfig) -> Self {
|
pub fn construct(config: CondSwapConfig) -> Self {
|
||||||
CondSwapChip {
|
CondSwapChip {
|
||||||
config,
|
config,
|
||||||
|
|
|
@ -34,6 +34,7 @@ use pasta_curves::arithmetic::FieldExt;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
/// The running sum $[z_0, ..., z_W]$. If created in strict mode, $z_W = 0$.
|
/// The running sum $[z_0, ..., z_W]$. If created in strict mode, $z_W = 0$.
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct RunningSum<F: FieldExt + PrimeFieldBits>(Vec<AssignedCell<F, F>>);
|
pub struct RunningSum<F: FieldExt + PrimeFieldBits>(Vec<AssignedCell<F, F>>);
|
||||||
impl<F: FieldExt + PrimeFieldBits> std::ops::Deref for RunningSum<F> {
|
impl<F: FieldExt + PrimeFieldBits> std::ops::Deref for RunningSum<F> {
|
||||||
type Target = Vec<AssignedCell<F, F>>;
|
type Target = Vec<AssignedCell<F, F>>;
|
||||||
|
@ -43,16 +44,22 @@ impl<F: FieldExt + PrimeFieldBits> std::ops::Deref for RunningSum<F> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configuration that provides methods for running sum decomposition.
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
|
||||||
pub struct RunningSumConfig<F: FieldExt + PrimeFieldBits, const WINDOW_NUM_BITS: usize> {
|
pub struct RunningSumConfig<F: FieldExt + PrimeFieldBits, const WINDOW_NUM_BITS: usize> {
|
||||||
pub q_range_check: Selector,
|
q_range_check: Selector,
|
||||||
pub z: Column<Advice>,
|
z: Column<Advice>,
|
||||||
_marker: PhantomData<F>,
|
_marker: PhantomData<F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: FieldExt + PrimeFieldBits, const WINDOW_NUM_BITS: usize>
|
impl<F: FieldExt + PrimeFieldBits, const WINDOW_NUM_BITS: usize>
|
||||||
RunningSumConfig<F, WINDOW_NUM_BITS>
|
RunningSumConfig<F, WINDOW_NUM_BITS>
|
||||||
{
|
{
|
||||||
|
/// Returns the q_range_check selector of this [`RunningSumConfig`].
|
||||||
|
pub(crate) fn q_range_check(&self) -> Selector {
|
||||||
|
self.q_range_check
|
||||||
|
}
|
||||||
|
|
||||||
/// `perm` MUST include the advice column `z`.
|
/// `perm` MUST include the advice column `z`.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
|
|
|
@ -23,12 +23,13 @@ impl<F: FieldExt + PrimeFieldBits> std::ops::Deref for RunningSum<F> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configuration that provides methods for a lookup range check.
|
||||||
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
|
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
|
||||||
pub struct LookupRangeCheckConfig<F: FieldExt + PrimeFieldBits, const K: usize> {
|
pub struct LookupRangeCheckConfig<F: FieldExt + PrimeFieldBits, const K: usize> {
|
||||||
pub q_lookup: Selector,
|
q_lookup: Selector,
|
||||||
pub q_running: Selector,
|
q_running: Selector,
|
||||||
pub q_bitshift: Selector,
|
q_bitshift: Selector,
|
||||||
pub running_sum: Column<Advice>,
|
running_sum: Column<Advice>,
|
||||||
table_idx: TableColumn,
|
table_idx: TableColumn,
|
||||||
_marker: PhantomData<F>,
|
_marker: PhantomData<F>,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue