|
| 1 | +use super::gates::{ |
| 2 | + absorb::{AbsorbConfig, ABSORB_NEXT_INPUTS}, |
| 3 | + iota_b13::IotaB13Config, |
| 4 | + iota_b9::IotaB9Config, |
| 5 | + pi::PiConfig, |
| 6 | + rho::RhoConfig, |
| 7 | + theta::ThetaConfig, |
| 8 | + xi::XiConfig, |
| 9 | +}; |
| 10 | +use crate::{ |
| 11 | + arith_helpers::*, common::{ROUND_CONSTANTS, PERMUTATION, ROTATION_CONSTANTS}, gates::rho_checks::RhoAdvices, |
| 12 | +}; |
| 13 | +use crate::{gates::mixing::MixingConfig, keccak_arith::*}; |
| 14 | +use halo2::{ |
| 15 | + circuit::Region, |
| 16 | + plonk::{Advice, Column, ConstraintSystem, Error, Selector}, |
| 17 | +}; |
| 18 | +use itertools::Itertools; |
| 19 | +use num_bigint::BigUint; |
| 20 | +use pasta_curves::arithmetic::FieldExt; |
| 21 | +use std::{convert::TryInto, marker::PhantomData}; |
| 22 | + |
| 23 | +#[derive(Clone, Debug)] |
| 24 | +pub struct KeccakFConfig<F: FieldExt> { |
| 25 | + theta_config: ThetaConfig<F>, |
| 26 | + rho_config: RhoConfig<F>, |
| 27 | + pi_config: PiConfig<F>, |
| 28 | + xi_config: XiConfig<F>, |
| 29 | + iota_b9_config: IotaB9Config<F>, |
| 30 | + mixing_config: MixingConfig<F>, |
| 31 | + state: [Column<Advice>; 25], |
| 32 | + _marker: PhantomData<F>, |
| 33 | +} |
| 34 | + |
| 35 | +impl<F: FieldExt> KeccakFConfig<F> { |
| 36 | + const B9_ROW: usize = 0; |
| 37 | + const B13_ROW: usize = 1; |
| 38 | + |
| 39 | + // We assume state is recieved in base-9. |
| 40 | + pub fn configure(meta: &mut ConstraintSystem<F>) -> KeccakFConfig<F> { |
| 41 | + let state = (0..25) |
| 42 | + .map(|_| { |
| 43 | + let column = meta.advice_column(); |
| 44 | + meta.enable_equality(column.into()); |
| 45 | + column |
| 46 | + }) |
| 47 | + .collect_vec() |
| 48 | + .try_into() |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + // theta |
| 52 | + let theta_config = ThetaConfig::configure(meta.selector(), meta, state); |
| 53 | + // rho |
| 54 | + let rho_config = { |
| 55 | + let cols: [Column<Advice>; 7] = state[0..7].try_into().unwrap(); |
| 56 | + let adv = RhoAdvices::from(cols); |
| 57 | + let axiliary = [state[8], state[9]]; |
| 58 | + |
| 59 | + let base13_to_9 = [ |
| 60 | + meta.fixed_column(), |
| 61 | + meta.fixed_column(), |
| 62 | + meta.fixed_column(), |
| 63 | + ]; |
| 64 | + let special = [meta.fixed_column(), meta.fixed_column()]; |
| 65 | + RhoConfig::configure( |
| 66 | + meta, |
| 67 | + state, |
| 68 | + &adv, |
| 69 | + axiliary, |
| 70 | + base13_to_9, |
| 71 | + special, |
| 72 | + ) |
| 73 | + }; |
| 74 | + // Pi |
| 75 | + let pi_config = PiConfig::configure(meta.selector(), meta, state); |
| 76 | + // xi |
| 77 | + let xi_config = XiConfig::configure(meta.selector(), meta, state); |
| 78 | + |
| 79 | + // Iotab9 |
| 80 | + // Generate advice and instance column for Round constants in base9 |
| 81 | + let round_ctant_b9 = meta.advice_column(); |
| 82 | + let round_constants_b9 = meta.instance_column(); |
| 83 | + let iota_b9_config = IotaB9Config::configure( |
| 84 | + meta, |
| 85 | + state, |
| 86 | + round_ctant_b9, |
| 87 | + round_constants_b9, |
| 88 | + ); |
| 89 | + let not_mixing_b9_to_13 = StateConversion::configure(meta); |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + let mixing_config = MixingConfig::configure(meta, state); |
| 94 | + // in side mixing let b9_to_13 = StateConversion::configure(meta); |
| 95 | + |
| 96 | + |
| 97 | + KeccakFConfig { |
| 98 | + theta_config, |
| 99 | + rho_config, |
| 100 | + pi_config, |
| 101 | + xi_config, |
| 102 | + iota_b9_config, |
| 103 | + mixing_config, |
| 104 | + state, |
| 105 | + _marker: PhantomData, |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + pub fn assign_all( |
| 110 | + &self, |
| 111 | + region: &mut Region<'_, F>, |
| 112 | + mut offset: usize, |
| 113 | + state: [F; 25], |
| 114 | + out_state: [F;25], |
| 115 | + flag: bool, |
| 116 | + next_mixing: Option<[F; ABSORB_NEXT_INPUTS]>, |
| 117 | + absolute_row_b9: usize, |
| 118 | + absolute_row_b13: usize, |
| 119 | + ) -> Result<[F; 25], Error> { |
| 120 | + // In case is needed |
| 121 | + let mut state = state; |
| 122 | + |
| 123 | + // First 23 rounds |
| 124 | + for round in 0..PERMUTATION { |
| 125 | + // State in base-13 |
| 126 | + // theta |
| 127 | + state = { |
| 128 | + // Apply theta outside circuit |
| 129 | + let out_state = KeccakFArith::theta(&state_to_biguint(state)); |
| 130 | + let out_state = state_bigint_to_pallas(out_state); |
| 131 | + // assignment |
| 132 | + self.theta_config |
| 133 | + .assign_state(region, offset, state, out_state)? |
| 134 | + }; |
| 135 | + |
| 136 | + offset += ThetaConfig::OFFSET; |
| 137 | + |
| 138 | + // rho |
| 139 | + state = { |
| 140 | + // Apply rho outside circuit |
| 141 | + let out_state = KeccakFArith::rho(&state_to_biguint(state)); |
| 142 | + let out_state = state_bigint_to_pallas(out_state); |
| 143 | + // assignment |
| 144 | + self.rho_config |
| 145 | + .assign_region(region, offset, out_state)?; |
| 146 | + out_state |
| 147 | + }; |
| 148 | + // Outputs in base-9 which is what Pi requires. |
| 149 | + offset += RhoConfig::OFFSET; |
| 150 | + |
| 151 | + // pi |
| 152 | + state = { |
| 153 | + // Apply pi outside circuit |
| 154 | + let out_state = KeccakFArith::pi(&state_to_biguint(state)); |
| 155 | + let out_state = state_bigint_to_pallas(out_state); |
| 156 | + // assignment |
| 157 | + self.pi_config |
| 158 | + .assign_state(region, offset, state, out_state)? |
| 159 | + }; |
| 160 | + |
| 161 | + offset += PiConfig::OFFSET; |
| 162 | + |
| 163 | + // xi |
| 164 | + state = { |
| 165 | + // Apply xi outside circuit |
| 166 | + let out_state = KeccakFArith::xi(&state_to_biguint(state)); |
| 167 | + let out_state = state_bigint_to_pallas(out_state); |
| 168 | + // assignment |
| 169 | + self.xi_config |
| 170 | + .assign_state(region, offset, state, out_state)? |
| 171 | + }; |
| 172 | + |
| 173 | + offset += XiConfig::OFFSET; |
| 174 | + |
| 175 | + // iota_b9 |
| 176 | + state = { |
| 177 | + let out_state = KeccakFArith::iota_b9(&state_to_biguint(state), ROUND_CONSTANTS[round]); |
| 178 | + let out_state = state_bigint_to_pallas(out_state); |
| 179 | + self |
| 180 | + .iota_b9_config |
| 181 | + .not_last_round(region, offset, state, out_state, round)?; |
| 182 | + out_state |
| 183 | + }; |
| 184 | + offset += IotaB9Config::OFFSET; |
| 185 | + // The resulting state is in Base-13 now. Which is what Theta |
| 186 | + // requires again at the start of the loop. |
| 187 | + |
| 188 | + self.not_mixing_b9_to_13.not_last_round(); |
| 189 | + } |
| 190 | + |
| 191 | + // Final round. |
| 192 | + let round = PERMUTATION; |
| 193 | + // PERMUTATION'th round |
| 194 | + // State in base-13 |
| 195 | + // theta |
| 196 | + state = { |
| 197 | + // Apply theta outside circuit |
| 198 | + let out_state = KeccakFArith::theta(&state_to_biguint(state)); |
| 199 | + let out_state = state_bigint_to_pallas(out_state); |
| 200 | + // assignment |
| 201 | + self.theta_config |
| 202 | + .assign_state(region, offset, state, out_state)? |
| 203 | + }; |
| 204 | + |
| 205 | + offset += 1; |
| 206 | + |
| 207 | + // rho |
| 208 | + state = { |
| 209 | + // Apply rho outside circuit |
| 210 | + let out_state = KeccakFArith::rho(&state_to_biguint(state)); |
| 211 | + let out_state = state_bigint_to_pallas(out_state); |
| 212 | + // assignment |
| 213 | + self.rho_config |
| 214 | + .assign_state(region, offset, state, out_state)? |
| 215 | + }; |
| 216 | + // Outputs in base-9 which is what Pi requires. |
| 217 | + offset += 1; |
| 218 | + |
| 219 | + // pi |
| 220 | + state = { |
| 221 | + // Apply pi outside circuit |
| 222 | + let out_state = KeccakFArith::pi(&state_to_biguint(state)); |
| 223 | + let out_state = state_bigint_to_pallas(out_state); |
| 224 | + // assignment |
| 225 | + self.pi_config |
| 226 | + .assign_state(region, offset, state, out_state)? |
| 227 | + }; |
| 228 | + |
| 229 | + offset += PiConfig::OFFSET; |
| 230 | + |
| 231 | + // xi |
| 232 | + state = { |
| 233 | + // Apply xi outside circuit |
| 234 | + let out_state = KeccakFArith::xi(&state_to_biguint(state)); |
| 235 | + let out_state = state_bigint_to_pallas(out_state); |
| 236 | + // assignment |
| 237 | + self.xi_config |
| 238 | + .assign_state(region, offset, state, out_state)? |
| 239 | + }; |
| 240 | + |
| 241 | + offset += XiConfig::OFFSET; |
| 242 | + |
| 243 | + // Mixing step |
| 244 | + state = { |
| 245 | + let out_state = KeccakFArith::mixing(&state_to_biguint(state), next_mixing, ROUND_CONSTANTS[round]); |
| 246 | + let out_state = state_bigint_to_pallas(out_state); |
| 247 | + self.mixing_config.assign_state(region, offset, state, out_state, flag, next_mixing, round, round)?; |
| 248 | + out_state |
| 249 | + }; |
| 250 | + |
| 251 | + Ok(state) |
| 252 | + } |
| 253 | +} |
0 commit comments