Skip to content

Commit 8a48aef

Browse files
Add MerkleInstructions
1 parent 27163ec commit 8a48aef

File tree

5 files changed

+125
-5
lines changed

5 files changed

+125
-5
lines changed

src/circuit/gadget.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub(crate) mod ecc;
2+
pub(crate) mod orchard_action;
23
pub(crate) mod sinsemilla;
34
pub(crate) mod utilities;

src/circuit/gadget/orchard_action.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod merkle;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use halo2::{
2+
circuit::{Cell, Chip, Layouter, Region},
3+
plonk::{Advice, Column, ConstraintSystem, Error, Permutation},
4+
};
5+
use pasta_curves::arithmetic::{CurveAffine, FieldExt};
6+
use std::marker::PhantomData;
7+
8+
use crate::circuit::gadget::{
9+
ecc::{
10+
chip::{EccChip, EccConfig},
11+
EccInstructions,
12+
},
13+
sinsemilla::{SinsemillaChip, SinsemillaConfig, SinsemillaInstructions},
14+
utilities::{UtilitiesChip, UtilitiesConfig, UtilitiesInstructions, Var},
15+
};
16+
use std::convert::TryInto;
17+
18+
enum Node<F: FieldExt> {
19+
Leaf(Var<F>),
20+
Inner(Var<F>),
21+
Sibling(Var<F>),
22+
}
23+
struct Root<F: FieldExt>(pub Var<F>);
24+
25+
pub trait MerkleInstructions<F: FieldExt, const MERKLE_DEPTH: usize>:
26+
UtilitiesInstructions<F>
27+
{
28+
/// Check the validity of a Merkle path from a given leaf to a claimed root.
29+
fn merkle_path_check(
30+
&self,
31+
layouter: impl Layouter<F>,
32+
root: Option<[u8; 32]>,
33+
leaf: Option<[u8; 32]>,
34+
merkle_path: [Option<[u8; 32]>; MERKLE_DEPTH],
35+
) -> Result<(), Error> {
36+
Ok(())
37+
}
38+
}
39+
40+
fn layer_hash<C: CurveAffine>(
41+
chip: MerkleChip<C>,
42+
layer: u16,
43+
left: Node<C::Base>,
44+
right: Node<C::Base>,
45+
) -> Result<Var<C::Base>, Error> {
46+
todo!()
47+
}
48+
49+
#[derive(Clone, Debug)]
50+
pub struct MerkleConfig {
51+
config1: (UtilitiesConfig, SinsemillaConfig),
52+
config2: (UtilitiesConfig, SinsemillaConfig),
53+
}
54+
55+
pub struct MerkleChip<C: CurveAffine> {
56+
config: MerkleConfig,
57+
_marker: PhantomData<C>,
58+
}
59+
60+
impl<C: CurveAffine> Chip<C::Base> for MerkleChip<C> {
61+
type Config = MerkleConfig;
62+
type Loaded = ();
63+
64+
fn config(&self) -> &Self::Config {
65+
&self.config
66+
}
67+
68+
fn loaded(&self) -> &Self::Loaded {
69+
&()
70+
}
71+
}
72+
73+
impl<C: CurveAffine> MerkleChip<C> {
74+
pub fn configure(
75+
meta: &mut ConstraintSystem<C::Base>,
76+
advices: [Column<Advice>; 10],
77+
perm: Permutation,
78+
) -> MerkleConfig {
79+
let ecc_config = EccChip::<C>::configure(meta, advices);
80+
81+
let lookup = (
82+
meta.fixed_column(),
83+
meta.fixed_column(),
84+
meta.fixed_column(),
85+
);
86+
let config1 = (
87+
UtilitiesChip::configure(meta, advices.clone()[..5].try_into().unwrap(), perm.clone()),
88+
SinsemillaChip::<C>::configure(
89+
meta,
90+
ecc_config.clone(),
91+
advices.clone()[..5].try_into().unwrap(),
92+
lookup,
93+
perm.clone(),
94+
),
95+
);
96+
let config2 = (
97+
UtilitiesChip::configure(meta, advices.clone()[5..].try_into().unwrap(), perm.clone()),
98+
SinsemillaChip::<C>::configure(
99+
meta,
100+
ecc_config,
101+
advices.clone()[5..].try_into().unwrap(),
102+
lookup,
103+
perm.clone(),
104+
),
105+
);
106+
MerkleConfig { config1, config2 }
107+
}
108+
109+
pub fn construct(config: MerkleConfig) -> Self {
110+
MerkleChip {
111+
config,
112+
_marker: PhantomData,
113+
}
114+
}
115+
}

src/circuit/gadget/utilities.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use halo2::{
55
use pasta_curves::arithmetic::FieldExt;
66
use std::marker::PhantomData;
77

8-
mod cond_swap;
9-
mod plonk;
8+
pub mod cond_swap;
9+
pub mod plonk;
1010

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

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

31-
struct UtilitiesChip<F: FieldExt> {
31+
pub struct UtilitiesChip<F: FieldExt> {
3232
config: UtilitiesConfig,
3333
_marker: PhantomData<F>,
3434
}
@@ -70,7 +70,7 @@ impl<F: FieldExt> UtilitiesChip<F> {
7070
}
7171
}
7272

73-
trait UtilitiesInstructions<F: FieldExt>: CondSwapInstructions<F> {
73+
pub trait UtilitiesInstructions<F: FieldExt>: CondSwapInstructions<F> {
7474
type Var;
7575

7676
/// Load a private input into the circuit.

src/constants.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub mod util;
1818

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

21+
/// $\mathsf{MerkleDepth^{Orchard}}$
22+
pub(crate) const MERKLE_DEPTH_ORCHARD: usize = 32;
23+
2124
/// $\ell^\mathsf{Orchard}_\mathsf{base}$
2225
pub(crate) const L_ORCHARD_BASE: usize = 255;
2326

0 commit comments

Comments
 (0)