|
| 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 | +} |
0 commit comments