sha256: Use assign_table() API for lookup table.

This commit is contained in:
therealyingtong 2021-10-27 10:48:56 +02:00
parent ee5bc8184a
commit e1c132d664
2 changed files with 21 additions and 67 deletions

View File

@ -331,8 +331,9 @@ trait Table16Assignment<F: FieldExt> {
} }
#[cfg(test)] #[cfg(test)]
#[cfg(feature = "dev-graph")]
mod tests { mod tests {
use super::super::{BlockWord, Sha256, BLOCK_SIZE}; use super::super::{Sha256, BLOCK_SIZE};
use super::{message_schedule::msg_schedule_test_input, Table16Chip, Table16Config}; use super::{message_schedule::msg_schedule_test_input, Table16Chip, Table16Config};
use halo2::{ use halo2::{
arithmetic::FieldExt, arithmetic::FieldExt,
@ -341,9 +342,9 @@ mod tests {
plonk::{Circuit, ConstraintSystem, Error}, plonk::{Circuit, ConstraintSystem, Error},
}; };
#[cfg(feature = "dev-graph")]
#[test] #[test]
fn print_sha256_circuit() { fn print_sha256_circuit() {
use plotters::prelude::*;
struct MyCircuit {} struct MyCircuit {}
impl<F: FieldExt> Circuit<F> for MyCircuit { impl<F: FieldExt> Circuit<F> for MyCircuit {
@ -381,53 +382,8 @@ mod tests {
} }
} }
let circuit: MyCircuit = MyCircuit {};
eprintln!("{}", halo2::dev::circuit_dot_graph::<Fq, _>(&circuit));
}
#[cfg(feature = "dev-graph")]
#[test]
fn print_table16_chip() {
use plotters::prelude::*;
struct MyCircuit {}
impl<F: FieldExt> Circuit<F> for MyCircuit {
type Config = Table16Config;
type FloorPlanner = SimpleFloorPlanner;
fn without_witnesses(&self) -> Self {
MyCircuit {}
}
fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config {
Table16Chip::configure(meta)
}
fn synthesize(
&self,
config: Self::Config,
mut layouter: impl Layouter<F>,
) -> Result<(), Error> {
let table16_chip = Table16Chip::<F>::construct(config.clone());
Table16Chip::<F>::load(config, &mut layouter)?;
// Test vector: "abc"
let test_input = msg_schedule_test_input();
// Create a message of length 2 blocks
let mut input = Vec::with_capacity(2 * BLOCK_SIZE);
for _ in 0..2 {
input.extend_from_slice(&test_input);
}
Sha256::digest(table16_chip, layouter.namespace(|| "'abc' * 2"), &input)?;
Ok(())
}
}
let root = let root =
SVGBackend::new("sha-256-table16-chip-layout.svg", (1024, 20480)).into_drawing_area(); BitMapBackend::new("sha-256-table16-chip-layout.png", (1024, 3480)).into_drawing_area();
root.fill(&WHITE).unwrap(); root.fill(&WHITE).unwrap();
let root = root let root = root
.titled("16-bit Table SHA-256 Chip", ("sans-serif", 60)) .titled("16-bit Table SHA-256 Chip", ("sans-serif", 60))
@ -435,7 +391,7 @@ mod tests {
let circuit = MyCircuit {}; let circuit = MyCircuit {};
halo2::dev::CircuitLayout::default() halo2::dev::CircuitLayout::default()
.render::<Fq, _, _>(&circuit, &root) .render::<Fq, _, _>(17, &circuit, &root)
.unwrap(); .unwrap();
} }
} }

View File

@ -2,7 +2,7 @@ use super::{util::*, CellValue16, CellValue32};
use halo2::{ use halo2::{
arithmetic::FieldExt, arithmetic::FieldExt,
circuit::{Chip, Layouter, Region}, circuit::{Chip, Layouter, Region},
plonk::{Advice, Column, ConstraintSystem, Error, Fixed}, plonk::{Advice, Column, ConstraintSystem, Error, TableColumn},
poly::Rotation, poly::Rotation,
}; };
use std::marker::PhantomData; use std::marker::PhantomData;
@ -135,9 +135,9 @@ pub(super) struct SpreadInputs {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(super) struct SpreadTable { pub(super) struct SpreadTable {
pub(super) tag: Column<Fixed>, pub(super) tag: TableColumn,
pub(super) dense: Column<Fixed>, pub(super) dense: TableColumn,
pub(super) spread: Column<Fixed>, pub(super) spread: TableColumn,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -172,22 +172,19 @@ impl<F: FieldExt> SpreadTableChip<F> {
input_dense: Column<Advice>, input_dense: Column<Advice>,
input_spread: Column<Advice>, input_spread: Column<Advice>,
) -> <Self as Chip<F>>::Config { ) -> <Self as Chip<F>>::Config {
let table_tag = meta.fixed_column(); let table_tag = meta.lookup_table_column();
let table_dense = meta.fixed_column(); let table_dense = meta.lookup_table_column();
let table_spread = meta.fixed_column(); let table_spread = meta.lookup_table_column();
meta.lookup(|meta| { meta.lookup(|meta| {
let tag_cur = meta.query_advice(input_tag, Rotation::cur()); let tag_cur = meta.query_advice(input_tag, Rotation::cur());
let dense_cur = meta.query_advice(input_dense, Rotation::cur()); let dense_cur = meta.query_advice(input_dense, Rotation::cur());
let spread_cur = meta.query_advice(input_spread, Rotation::cur()); let spread_cur = meta.query_advice(input_spread, Rotation::cur());
let table_tag_cur = meta.query_fixed(table_tag, Rotation::cur());
let table_dense_cur = meta.query_fixed(table_dense, Rotation::cur());
let table_spread_cur = meta.query_fixed(table_spread, Rotation::cur());
vec![ vec![
(tag_cur, table_tag_cur), (tag_cur, table_tag),
(dense_cur, table_dense_cur), (dense_cur, table_dense),
(spread_cur, table_spread_cur), (spread_cur, table_spread),
] ]
}); });
@ -209,15 +206,15 @@ impl<F: FieldExt> SpreadTableChip<F> {
config: SpreadTableConfig, config: SpreadTableConfig,
layouter: &mut impl Layouter<F>, layouter: &mut impl Layouter<F>,
) -> Result<<Self as Chip<F>>::Loaded, Error> { ) -> Result<<Self as Chip<F>>::Loaded, Error> {
layouter.assign_region( layouter.assign_table(
|| "spread table", || "spread table",
|mut gate| { |mut table| {
// We generate the row values lazily (we only need them during keygen). // We generate the row values lazily (we only need them during keygen).
let mut rows = SpreadTableConfig::generate::<F>(); let mut rows = SpreadTableConfig::generate::<F>();
for index in 0..(1 << 16) { for index in 0..(1 << 16) {
let mut row = None; let mut row = None;
gate.assign_fixed( table.assign_cell(
|| "tag", || "tag",
config.table.tag, config.table.tag,
index, index,
@ -226,13 +223,13 @@ impl<F: FieldExt> SpreadTableChip<F> {
row.map(|(tag, _, _)| tag).ok_or(Error::SynthesisError) row.map(|(tag, _, _)| tag).ok_or(Error::SynthesisError)
}, },
)?; )?;
gate.assign_fixed( table.assign_cell(
|| "dense", || "dense",
config.table.dense, config.table.dense,
index, index,
|| row.map(|(_, dense, _)| dense).ok_or(Error::SynthesisError), || row.map(|(_, dense, _)| dense).ok_or(Error::SynthesisError),
)?; )?;
gate.assign_fixed( table.assign_cell(
|| "spread", || "spread",
config.table.spread, config.table.spread,
index, index,
@ -242,6 +239,7 @@ impl<F: FieldExt> SpreadTableChip<F> {
}, },
)?; )?;
} }
Ok(()) Ok(())
}, },
) )