1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::cmp::min;
use std::convert::TryInto;
use std::fmt;
use halo2_proofs::{
arithmetic::FieldExt,
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: FieldExt>: 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: FieldExt, CS: Sha256Instructions<F>> {
chip: CS,
state: CS::State,
cur_block: Vec<CS::BlockWord>,
length: usize,
}
impl<F: FieldExt, 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"))
}
}