use std::cmp::min;
use std::convert::TryInto;
use std::fmt;
use group::ff::Field;
use halo2_proofs::{
circuit::{Chip, Layouter},
plonk::Error,
};
mod table16;
pub use table16::{BlockWord, Table16Chip, Table16Config};
pub const BLOCK_SIZE: usize = 16;
const DIGEST_SIZE: usize = 8;
pub trait Sha256Instructions<F: Field>: Chip<F> {
type State: Clone + fmt::Debug;
type BlockWord: Copy + fmt::Debug + Default;
fn initialization_vector(&self, layouter: &mut impl Layouter<F>) -> Result<Self::State, Error>;
fn initialization(
&self,
layouter: &mut impl Layouter<F>,
init_state: &Self::State,
) -> Result<Self::State, Error>;
fn compress(
&self,
layouter: &mut impl Layouter<F>,
initialized_state: &Self::State,
input: [Self::BlockWord; BLOCK_SIZE],
) -> Result<Self::State, Error>;
fn digest(
&self,
layouter: &mut impl Layouter<F>,
state: &Self::State,
) -> Result<[Self::BlockWord; DIGEST_SIZE], Error>;
}
#[derive(Debug)]
pub struct Sha256Digest<BlockWord>([BlockWord; DIGEST_SIZE]);
#[derive(Debug)]
pub struct Sha256<F: Field, CS: Sha256Instructions<F>> {
chip: CS,
state: CS::State,
cur_block: Vec<CS::BlockWord>,
length: usize,
}
impl<F: Field, Sha256Chip: Sha256Instructions<F>> Sha256<F, Sha256Chip> {
pub fn new(chip: Sha256Chip, mut layouter: impl Layouter<F>) -> Result<Self, Error> {
let state = chip.initialization_vector(&mut layouter)?;
Ok(Sha256 {
chip,
state,
cur_block: Vec::with_capacity(BLOCK_SIZE),
length: 0,
})
}
pub fn update(
&mut self,
mut layouter: impl Layouter<F>,
mut data: &[Sha256Chip::BlockWord],
) -> Result<(), Error> {
self.length += data.len() * 32;
let remaining = BLOCK_SIZE - self.cur_block.len();
let (l, r) = data.split_at(min(remaining, data.len()));
self.cur_block.extend_from_slice(l);
data = r;
if self.cur_block.len() < BLOCK_SIZE {
return Ok(());
}
self.state = self.chip.compress(
&mut layouter,
&self.state,
self.cur_block[..]
.try_into()
.expect("cur_block.len() == BLOCK_SIZE"),
)?;
self.cur_block.clear();
let mut chunks_iter = data.chunks_exact(BLOCK_SIZE);
for chunk in &mut chunks_iter {
self.state = self.chip.initialization(&mut layouter, &self.state)?;
self.state = self.chip.compress(
&mut layouter,
&self.state,
chunk.try_into().expect("chunk.len() == BLOCK_SIZE"),
)?;
}
let rem = chunks_iter.remainder();
self.cur_block.extend_from_slice(rem);
Ok(())
}
pub fn finalize(
mut self,
mut layouter: impl Layouter<F>,
) -> Result<Sha256Digest<Sha256Chip::BlockWord>, Error> {
if !self.cur_block.is_empty() {
let padding = vec![Sha256Chip::BlockWord::default(); BLOCK_SIZE - self.cur_block.len()];
self.cur_block.extend_from_slice(&padding);
self.state = self.chip.initialization(&mut layouter, &self.state)?;
self.state = self.chip.compress(
&mut layouter,
&self.state,
self.cur_block[..]
.try_into()
.expect("cur_block.len() == BLOCK_SIZE"),
)?;
}
self.chip
.digest(&mut layouter, &self.state)
.map(Sha256Digest)
}
pub fn digest(
chip: Sha256Chip,
mut layouter: impl Layouter<F>,
data: &[Sha256Chip::BlockWord],
) -> Result<Sha256Digest<Sha256Chip::BlockWord>, Error> {
let mut hasher = Self::new(chip, layouter.namespace(|| "init"))?;
hasher.update(layouter.namespace(|| "update"), data)?;
hasher.finalize(layouter.namespace(|| "finalize"))
}
}