Skip to content

Commit

Permalink
Add MerkleInstructions
Browse files Browse the repository at this point in the history
  • Loading branch information
therealyingtong committed Jun 2, 2021
1 parent 27163ec commit 8a48aef
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/circuit/gadget.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod ecc;
pub(crate) mod orchard_action;
pub(crate) mod sinsemilla;
pub(crate) mod utilities;
1 change: 1 addition & 0 deletions src/circuit/gadget/orchard_action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod merkle;
115 changes: 115 additions & 0 deletions src/circuit/gadget/orchard_action/merkle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use halo2::{
circuit::{Cell, Chip, Layouter, Region},
plonk::{Advice, Column, ConstraintSystem, Error, Permutation},
};
use pasta_curves::arithmetic::{CurveAffine, FieldExt};
use std::marker::PhantomData;

use crate::circuit::gadget::{
ecc::{
chip::{EccChip, EccConfig},
EccInstructions,
},
sinsemilla::{SinsemillaChip, SinsemillaConfig, SinsemillaInstructions},
utilities::{UtilitiesChip, UtilitiesConfig, UtilitiesInstructions, Var},
};
use std::convert::TryInto;

enum Node<F: FieldExt> {
Leaf(Var<F>),
Inner(Var<F>),
Sibling(Var<F>),
}
struct Root<F: FieldExt>(pub Var<F>);

pub trait MerkleInstructions<F: FieldExt, const MERKLE_DEPTH: usize>:
UtilitiesInstructions<F>
{
/// Check the validity of a Merkle path from a given leaf to a claimed root.
fn merkle_path_check(
&self,
layouter: impl Layouter<F>,
root: Option<[u8; 32]>,
leaf: Option<[u8; 32]>,
merkle_path: [Option<[u8; 32]>; MERKLE_DEPTH],
) -> Result<(), Error> {
Ok(())
}
}

fn layer_hash<C: CurveAffine>(
chip: MerkleChip<C>,
layer: u16,
left: Node<C::Base>,
right: Node<C::Base>,
) -> Result<Var<C::Base>, Error> {
todo!()
}

#[derive(Clone, Debug)]
pub struct MerkleConfig {
config1: (UtilitiesConfig, SinsemillaConfig),
config2: (UtilitiesConfig, SinsemillaConfig),
}

pub struct MerkleChip<C: CurveAffine> {
config: MerkleConfig,
_marker: PhantomData<C>,
}

impl<C: CurveAffine> Chip<C::Base> for MerkleChip<C> {
type Config = MerkleConfig;
type Loaded = ();

fn config(&self) -> &Self::Config {
&self.config
}

fn loaded(&self) -> &Self::Loaded {
&()
}
}

impl<C: CurveAffine> MerkleChip<C> {
pub fn configure(
meta: &mut ConstraintSystem<C::Base>,
advices: [Column<Advice>; 10],
perm: Permutation,
) -> MerkleConfig {
let ecc_config = EccChip::<C>::configure(meta, advices);

let lookup = (
meta.fixed_column(),
meta.fixed_column(),
meta.fixed_column(),
);
let config1 = (
UtilitiesChip::configure(meta, advices.clone()[..5].try_into().unwrap(), perm.clone()),
SinsemillaChip::<C>::configure(
meta,
ecc_config.clone(),
advices.clone()[..5].try_into().unwrap(),
lookup,
perm.clone(),
),
);
let config2 = (
UtilitiesChip::configure(meta, advices.clone()[5..].try_into().unwrap(), perm.clone()),
SinsemillaChip::<C>::configure(
meta,
ecc_config,
advices.clone()[5..].try_into().unwrap(),
lookup,
perm.clone(),
),
);
MerkleConfig { config1, config2 }
}

pub fn construct(config: MerkleConfig) -> Self {
MerkleChip {
config,
_marker: PhantomData,
}
}
}
10 changes: 5 additions & 5 deletions src/circuit/gadget/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use halo2::{
use pasta_curves::arithmetic::FieldExt;
use std::marker::PhantomData;

mod cond_swap;
mod plonk;
pub mod cond_swap;
pub mod plonk;

use cond_swap::{CondSwapChip, CondSwapConfig, CondSwapInstructions, Pair, Swap};
use plonk::{PLONKChip, PLONKConfig, PLONKInstructions};
Expand All @@ -19,7 +19,7 @@ pub struct Var<F: FieldExt> {
}

#[derive(Clone, Debug)]
struct UtilitiesConfig {
pub struct UtilitiesConfig {
// Column where private inputs are witnessed.
private: Column<Advice>,
// Config to use a conditional swap chip.
Expand All @@ -28,7 +28,7 @@ struct UtilitiesConfig {
plonk_config: PLONKConfig,
}

struct UtilitiesChip<F: FieldExt> {
pub struct UtilitiesChip<F: FieldExt> {
config: UtilitiesConfig,
_marker: PhantomData<F>,
}
Expand Down Expand Up @@ -70,7 +70,7 @@ impl<F: FieldExt> UtilitiesChip<F> {
}
}

trait UtilitiesInstructions<F: FieldExt>: CondSwapInstructions<F> {
pub trait UtilitiesInstructions<F: FieldExt>: CondSwapInstructions<F> {
type Var;

/// Load a private input into the circuit.
Expand Down
3 changes: 3 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub mod util;

pub use load::{OrchardFixedBase, OrchardFixedBasesFull, ValueCommitV};

/// $\mathsf{MerkleDepth^{Orchard}}$
pub(crate) const MERKLE_DEPTH_ORCHARD: usize = 32;

/// $\ell^\mathsf{Orchard}_\mathsf{base}$
pub(crate) const L_ORCHARD_BASE: usize = 255;

Expand Down

0 comments on commit 8a48aef

Please sign in to comment.