openvm_sha2_air/
config.rs

1use std::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
2
3use crate::{Sha2DigestColsRef, Sha2RoundColsRef};
4
5#[repr(u32)]
6#[derive(num_enum::TryFromPrimitive, num_enum::IntoPrimitive, Copy, Clone, Debug)]
7pub enum Sha2Variant {
8    Sha256,
9    Sha512,
10    Sha384,
11}
12
13pub trait Sha2BlockHasherSubairConfig: Send + Sync + Clone {
14    // --- Required ---
15
16    type Word: 'static
17        + Shr<usize, Output = Self::Word>
18        + Shl<usize, Output = Self::Word>
19        + BitAnd<Output = Self::Word>
20        + Not<Output = Self::Word>
21        + BitXor<Output = Self::Word>
22        + BitOr<Output = Self::Word>
23        + RotateRight
24        + WrappingAdd
25        + PartialEq
26        + From<u32>
27        + TryInto<u32, Error: std::fmt::Debug>
28        + Copy
29        + Send
30        + Sync;
31    // Differentiate between the SHA-2 variants
32    const VARIANT: Sha2Variant;
33    /// Number of bits in a SHA word
34    const WORD_BITS: usize;
35    /// Number of words in a SHA block
36    const BLOCK_WORDS: usize;
37    /// Number of rows per block
38    const ROWS_PER_BLOCK: usize;
39    /// Number of rounds per row. Must divide Self::ROUNDS_PER_BLOCK
40    const ROUNDS_PER_ROW: usize;
41    /// Number of rounds per block. Must be a multiple of Self::ROUNDS_PER_ROW
42    const ROUNDS_PER_BLOCK: usize;
43    /// Number of words in a SHA hash
44    const HASH_WORDS: usize;
45    /// Number of vars needed to encode the row index with
46    /// [`Encoder`](openvm_circuit_primitives::encoder::Encoder)
47    const ROW_VAR_CNT: usize;
48
49    /// We also store the SHA constants K and H
50    fn get_k() -> &'static [Self::Word];
51    fn get_h() -> &'static [Self::Word];
52
53    // --- Provided ---
54
55    /// Number of 16-bit limbs in a SHA word
56    const WORD_U16S: usize = Self::WORD_BITS / 16;
57    /// Number of 8-bit limbs in a SHA word
58    const WORD_U8S: usize = Self::WORD_BITS / 8;
59    /// Number of cells in a SHA block
60    const BLOCK_U8S: usize = Self::BLOCK_WORDS * Self::WORD_U8S;
61    /// Number of bits in a SHA block
62    const BLOCK_BITS: usize = Self::BLOCK_WORDS * Self::WORD_BITS;
63    /// Number of rows used for the sha rounds
64    const ROUND_ROWS: usize = Self::ROUNDS_PER_BLOCK / Self::ROUNDS_PER_ROW;
65    /// Number of rows used for the message
66    const MESSAGE_ROWS: usize = Self::BLOCK_WORDS / Self::ROUNDS_PER_ROW;
67    /// Number of rounds per row minus one (needed for one of the column structs)
68    const ROUNDS_PER_ROW_MINUS_ONE: usize = Self::ROUNDS_PER_ROW - 1;
69    /// Width of the Sha2RoundCols
70    const SUBAIR_ROUND_WIDTH: usize = Sha2RoundColsRef::<u8>::width::<Self>();
71    /// Width of the Sha2DigestCols
72    const SUBAIR_DIGEST_WIDTH: usize = Sha2DigestColsRef::<u8>::width::<Self>();
73    /// Width of the Sha2BlockHasherCols
74    const SUBAIR_WIDTH: usize = if Self::SUBAIR_ROUND_WIDTH > Self::SUBAIR_DIGEST_WIDTH {
75        Self::SUBAIR_ROUND_WIDTH
76    } else {
77        Self::SUBAIR_DIGEST_WIDTH
78    };
79}
80
81#[derive(Clone)]
82pub struct Sha256Config;
83
84#[derive(Clone)]
85pub struct Sha512Config;
86
87#[derive(Clone)]
88pub struct Sha384Config;
89
90impl Sha2BlockHasherSubairConfig for Sha256Config {
91    // ==== Do not change these constants! ====
92    const VARIANT: Sha2Variant = Sha2Variant::Sha256;
93    type Word = u32;
94    /// Number of bits in a SHA256 word
95    const WORD_BITS: usize = 32;
96    /// Number of words in a SHA256 block
97    const BLOCK_WORDS: usize = 16;
98    /// Number of rows per block
99    const ROWS_PER_BLOCK: usize = 17;
100    /// Number of rounds per row
101    const ROUNDS_PER_ROW: usize = 4;
102    /// Number of rounds per block
103    const ROUNDS_PER_BLOCK: usize = 64;
104    /// Number of words in a SHA256 hash
105    const HASH_WORDS: usize = 8;
106    /// Number of vars needed to encode the row index with
107    /// [`Encoder`](openvm_circuit_primitives::encoder::Encoder)
108    const ROW_VAR_CNT: usize = 5;
109
110    fn get_k() -> &'static [u32] {
111        &SHA256_K
112    }
113    fn get_h() -> &'static [u32] {
114        &SHA256_H
115    }
116}
117
118/// SHA256 constant K's
119pub const SHA256_K: [u32; 64] = [
120    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
121    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
122    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
123    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
124    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
125    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
126    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
127    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
128];
129/// SHA256 initial hash values
130pub const SHA256_H: [u32; 8] = [
131    0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
132];
133
134impl Sha2BlockHasherSubairConfig for Sha512Config {
135    // ==== Do not change these constants! ====
136    const VARIANT: Sha2Variant = Sha2Variant::Sha512;
137    type Word = u64;
138    /// Number of bits in a SHA512 word
139    const WORD_BITS: usize = 64;
140    /// Number of words in a SHA512 block
141    const BLOCK_WORDS: usize = 16;
142    /// Number of rows per block
143    const ROWS_PER_BLOCK: usize = 21;
144    /// Number of rounds per row
145    const ROUNDS_PER_ROW: usize = 4;
146    /// Number of rounds per block
147    const ROUNDS_PER_BLOCK: usize = 80;
148    /// Number of words in a SHA512 hash
149    const HASH_WORDS: usize = 8;
150    /// Number of vars needed to encode the row index with
151    /// [`Encoder`](openvm_circuit_primitives::encoder::Encoder)
152    const ROW_VAR_CNT: usize = 6;
153
154    fn get_k() -> &'static [u64] {
155        &SHA512_K
156    }
157    fn get_h() -> &'static [u64] {
158        &SHA512_H
159    }
160}
161
162/// SHA512 constant K's
163pub const SHA512_K: [u64; 80] = [
164    0x428a2f98d728ae22,
165    0x7137449123ef65cd,
166    0xb5c0fbcfec4d3b2f,
167    0xe9b5dba58189dbbc,
168    0x3956c25bf348b538,
169    0x59f111f1b605d019,
170    0x923f82a4af194f9b,
171    0xab1c5ed5da6d8118,
172    0xd807aa98a3030242,
173    0x12835b0145706fbe,
174    0x243185be4ee4b28c,
175    0x550c7dc3d5ffb4e2,
176    0x72be5d74f27b896f,
177    0x80deb1fe3b1696b1,
178    0x9bdc06a725c71235,
179    0xc19bf174cf692694,
180    0xe49b69c19ef14ad2,
181    0xefbe4786384f25e3,
182    0x0fc19dc68b8cd5b5,
183    0x240ca1cc77ac9c65,
184    0x2de92c6f592b0275,
185    0x4a7484aa6ea6e483,
186    0x5cb0a9dcbd41fbd4,
187    0x76f988da831153b5,
188    0x983e5152ee66dfab,
189    0xa831c66d2db43210,
190    0xb00327c898fb213f,
191    0xbf597fc7beef0ee4,
192    0xc6e00bf33da88fc2,
193    0xd5a79147930aa725,
194    0x06ca6351e003826f,
195    0x142929670a0e6e70,
196    0x27b70a8546d22ffc,
197    0x2e1b21385c26c926,
198    0x4d2c6dfc5ac42aed,
199    0x53380d139d95b3df,
200    0x650a73548baf63de,
201    0x766a0abb3c77b2a8,
202    0x81c2c92e47edaee6,
203    0x92722c851482353b,
204    0xa2bfe8a14cf10364,
205    0xa81a664bbc423001,
206    0xc24b8b70d0f89791,
207    0xc76c51a30654be30,
208    0xd192e819d6ef5218,
209    0xd69906245565a910,
210    0xf40e35855771202a,
211    0x106aa07032bbd1b8,
212    0x19a4c116b8d2d0c8,
213    0x1e376c085141ab53,
214    0x2748774cdf8eeb99,
215    0x34b0bcb5e19b48a8,
216    0x391c0cb3c5c95a63,
217    0x4ed8aa4ae3418acb,
218    0x5b9cca4f7763e373,
219    0x682e6ff3d6b2b8a3,
220    0x748f82ee5defb2fc,
221    0x78a5636f43172f60,
222    0x84c87814a1f0ab72,
223    0x8cc702081a6439ec,
224    0x90befffa23631e28,
225    0xa4506cebde82bde9,
226    0xbef9a3f7b2c67915,
227    0xc67178f2e372532b,
228    0xca273eceea26619c,
229    0xd186b8c721c0c207,
230    0xeada7dd6cde0eb1e,
231    0xf57d4f7fee6ed178,
232    0x06f067aa72176fba,
233    0x0a637dc5a2c898a6,
234    0x113f9804bef90dae,
235    0x1b710b35131c471b,
236    0x28db77f523047d84,
237    0x32caab7b40c72493,
238    0x3c9ebe0a15c9bebc,
239    0x431d67c49c100d4c,
240    0x4cc5d4becb3e42b6,
241    0x597f299cfc657e2a,
242    0x5fcb6fab3ad6faec,
243    0x6c44198c4a475817,
244];
245/// SHA512 initial hash values
246pub const SHA512_H: [u64; 8] = [
247    0x6a09e667f3bcc908,
248    0xbb67ae8584caa73b,
249    0x3c6ef372fe94f82b,
250    0xa54ff53a5f1d36f1,
251    0x510e527fade682d1,
252    0x9b05688c2b3e6c1f,
253    0x1f83d9abfb41bd6b,
254    0x5be0cd19137e2179,
255];
256
257impl Sha2BlockHasherSubairConfig for Sha384Config {
258    // ==== Do not change these constants! ====
259    const VARIANT: Sha2Variant = Sha2Variant::Sha384;
260    type Word = <Sha512Config as Sha2BlockHasherSubairConfig>::Word;
261    /// Number of bits in a SHA384 word
262    const WORD_BITS: usize = <Sha512Config as Sha2BlockHasherSubairConfig>::WORD_BITS;
263    /// Number of words in a SHA384 block
264    const BLOCK_WORDS: usize = <Sha512Config as Sha2BlockHasherSubairConfig>::BLOCK_WORDS;
265    /// Number of rows per block
266    const ROWS_PER_BLOCK: usize = <Sha512Config as Sha2BlockHasherSubairConfig>::ROWS_PER_BLOCK;
267    /// Number of rounds per row
268    const ROUNDS_PER_ROW: usize = <Sha512Config as Sha2BlockHasherSubairConfig>::ROUNDS_PER_ROW;
269    /// Number of rounds per block
270    const ROUNDS_PER_BLOCK: usize = <Sha512Config as Sha2BlockHasherSubairConfig>::ROUNDS_PER_BLOCK;
271    /// Number of words in a SHA384 hash
272    const HASH_WORDS: usize = <Sha512Config as Sha2BlockHasherSubairConfig>::HASH_WORDS;
273    /// Number of vars needed to encode the row index with
274    /// [`Encoder`](openvm_circuit_primitives::encoder::Encoder)
275    const ROW_VAR_CNT: usize = <Sha512Config as Sha2BlockHasherSubairConfig>::ROW_VAR_CNT;
276
277    fn get_k() -> &'static [u64] {
278        &SHA384_K
279    }
280    fn get_h() -> &'static [u64] {
281        &SHA384_H
282    }
283}
284
285/// SHA384 constant K's
286pub const SHA384_K: [u64; 80] = SHA512_K;
287
288/// SHA384 initial hash values
289pub const SHA384_H: [u64; 8] = [
290    0xcbbb9d5dc1059ed8,
291    0x629a292a367cd507,
292    0x9159015a3070dd17,
293    0x152fecd8f70e5939,
294    0x67332667ffc00b31,
295    0x8eb44a8768581511,
296    0xdb0c2e0d64f98fa7,
297    0x47b5481dbefa4fa4,
298];
299
300// Needed to avoid compile errors in utils.rs
301// not sure why this doesn't inf loop
302pub trait RotateRight {
303    fn rotate_right(self, n: u32) -> Self;
304}
305impl RotateRight for u32 {
306    fn rotate_right(self, n: u32) -> Self {
307        self.rotate_right(n)
308    }
309}
310impl RotateRight for u64 {
311    fn rotate_right(self, n: u32) -> Self {
312        self.rotate_right(n)
313    }
314}
315pub trait WrappingAdd {
316    fn wrapping_add(self, n: Self) -> Self;
317}
318impl WrappingAdd for u32 {
319    fn wrapping_add(self, n: u32) -> Self {
320        self.wrapping_add(n)
321    }
322}
323impl WrappingAdd for u64 {
324    fn wrapping_add(self, n: u64) -> Self {
325        self.wrapping_add(n)
326    }
327}