From c90c3b33358d69dd2a10c32b84a53a78623145a6 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Thu, 1 Apr 2021 12:07:17 +0800 Subject: [PATCH] Configure Sinsemilla chip --- src/circuit/gadget/sinsemilla/chip.rs | 83 ++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/src/circuit/gadget/sinsemilla/chip.rs b/src/circuit/gadget/sinsemilla/chip.rs index 876c7dcbb..3582e2859 100644 --- a/src/circuit/gadget/sinsemilla/chip.rs +++ b/src/circuit/gadget/sinsemilla/chip.rs @@ -2,10 +2,15 @@ use super::super::ecc::chip::{EccChip, EccConfig}; use super::{CommitDomains, HashDomains, SinsemillaInstructions}; use crate::constants::OrchardFixedBases; +use crate::primitives::sinsemilla::K; + +use ff::Field; +use group::Curve; use halo2::{ arithmetic::{CurveAffine, FieldExt}, circuit::{Cell, Layouter}, - plonk::{Error, Selector}, + plonk::{Advice, Column, ConstraintSystem, Error, Expression, Selector}, + poly::Rotation, }; mod generator_table; @@ -92,6 +97,82 @@ impl CommitDomains, OrchardHashDomains EccChip { + fn configure_sinsemilla( + meta: &mut ConstraintSystem, + q_sinsemilla: Selector, + bits: Column, + u: Column, + A: (Column, Column), + P: (Column, Column), + lambda: (Column, Column), + add_complete_bool: [Column; 4], + add_complete_inv: [Column; 4], + ) -> SinsemillaConfig { + let ecc_config = EccChip::::configure( + meta, + bits, + u, + A, + P, + lambda, + add_complete_bool, + add_complete_inv, + ); + + // Fixed column for Sinsemilla selector + let sinsemilla_cur = meta.query_selector(q_sinsemilla, Rotation::cur()); + + // m_i = z_{i + 1} - (z_i * 2^k) + let z_cur = meta.query_advice(ecc_config.bits, Rotation::cur()); + let z_next = meta.query_advice(ecc_config.bits, Rotation::next()); + let m = z_next - z_cur * C::Base::from_u64((1 << K) as u64); + + // y_a = (1/2) ⋅ (lambda1 + lambda2) ⋅ (x_a - (lambda1^2 - x_a - x_p)) + let lambda1_cur = meta.query_advice(ecc_config.lambda.0, Rotation::cur()); + let lambda2_cur = meta.query_advice(ecc_config.lambda.1, Rotation::cur()); + let x_a_cur = meta.query_advice(ecc_config.A.0, Rotation::cur()); + let x_p_cur = meta.query_advice(ecc_config.P.0, Rotation::cur()); + let y_a_cur = (lambda1_cur.clone() + lambda2_cur.clone()) + * (x_a_cur.clone() + - (lambda1_cur.clone() * lambda1_cur.clone() - x_a_cur.clone() - x_p_cur.clone())) + * C::Base::TWO_INV; + + // y_p = y_a - lambda1 ⋅ (x_a - x_p) + let y_p = y_a_cur.clone() - lambda1_cur.clone() * (x_a_cur.clone() - x_p_cur.clone()); + + let (x_p_init, y_p_init) = get_s_by_idx::(0).to_affine().get_xy().unwrap(); + + let generator_table = GeneratorTable::configure::( + meta, + sinsemilla_cur.clone() * m + + (Expression::Constant(C::Base::one()) - sinsemilla_cur.clone()) * C::Base::zero(), + sinsemilla_cur.clone() * x_p_cur.clone() + + (Expression::Constant(C::Base::one()) - sinsemilla_cur.clone()) * x_p_init, + sinsemilla_cur.clone() * y_p + + (Expression::Constant(C::Base::one()) - sinsemilla_cur.clone()) * y_p_init, + ); + + // TODO: create gates + + SinsemillaConfig { + ecc_config, + generator_table, + q_sinsemilla, + } + } +} + impl SinsemillaInstructions for EccChip { type Message = Message; type CommitDomains = OrchardCommitDomains;