1
1
//! WARNING: the order of fields in the structs is important, do not change it
2
2
3
- use openvm_circuit_primitives:: { utils:: not, AlignedBorrow } ;
3
+ use openvm_circuit_primitives:: utils:: not;
4
+ use openvm_sha_macros:: ColsRef ;
4
5
use openvm_stark_backend:: p3_field:: FieldAlgebra ;
5
6
6
- use super :: {
7
- SHA256_HASH_WORDS , SHA256_ROUNDS_PER_ROW , SHA256_ROW_VAR_CNT , SHA256_WORD_BITS ,
8
- SHA256_WORD_U16S , SHA256_WORD_U8S ,
9
- } ;
7
+ use crate :: ShaConfig ;
10
8
11
9
/// In each SHA256 block:
12
10
/// - First 16 rows use Sha256RoundCols
@@ -26,77 +24,111 @@ use super::{
26
24
///
27
25
/// Note that the `Sha256WorkVarsCols` field it is used for different purposes in the two structs.
28
26
#[ repr( C ) ]
29
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
30
- pub struct Sha256RoundCols < T > {
31
- pub flags : Sha256FlagsCols < T > ,
27
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
28
+ pub struct ShaRoundCols <
29
+ T ,
30
+ const WORD_BITS : usize ,
31
+ const WORD_U8S : usize ,
32
+ const WORD_U16S : usize ,
33
+ const ROUNDS_PER_ROW : usize ,
34
+ const ROUNDS_PER_ROW_MINUS_ONE : usize ,
35
+ const ROW_VAR_CNT : usize ,
36
+ > {
37
+ pub flags : ShaFlagsCols < T , ROW_VAR_CNT > ,
32
38
/// Stores the current state of the working variables
33
- pub work_vars : Sha256WorkVarsCols < T > ,
34
- pub schedule_helper : Sha256MessageHelperCols < T > ,
35
- pub message_schedule : Sha256MessageScheduleCols < T > ,
39
+ pub work_vars : ShaWorkVarsCols < T , WORD_BITS , ROUNDS_PER_ROW , WORD_U16S > ,
40
+ pub schedule_helper :
41
+ ShaMessageHelperCols < T , WORD_U16S , ROUNDS_PER_ROW , ROUNDS_PER_ROW_MINUS_ONE > ,
42
+ pub message_schedule : ShaMessageScheduleCols < T , WORD_BITS , ROUNDS_PER_ROW , WORD_U8S > ,
36
43
}
37
44
38
45
#[ repr( C ) ]
39
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
40
- pub struct Sha256DigestCols < T > {
41
- pub flags : Sha256FlagsCols < T > ,
46
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
47
+ pub struct ShaDigestCols <
48
+ T ,
49
+ const WORD_BITS : usize ,
50
+ const WORD_U8S : usize ,
51
+ const WORD_U16S : usize ,
52
+ const HASH_WORDS : usize ,
53
+ const ROUNDS_PER_ROW : usize ,
54
+ const ROUNDS_PER_ROW_MINUS_ONE : usize ,
55
+ const ROW_VAR_CNT : usize ,
56
+ > {
57
+ pub flags : ShaFlagsCols < T , ROW_VAR_CNT > ,
42
58
/// Will serve as previous hash values for the next block.
43
59
/// - on non-last blocks, this is the final hash of the current block
44
60
/// - on last blocks, this is the initial state constants, SHA256_H.
45
61
/// The work variables constraints are applied on all rows, so `carry_a` and `carry_e`
46
62
/// must be filled in with dummy values to ensure these constraints hold.
47
- pub hash : Sha256WorkVarsCols < T > ,
48
- pub schedule_helper : Sha256MessageHelperCols < T > ,
63
+ pub hash : ShaWorkVarsCols < T , WORD_BITS , ROUNDS_PER_ROW , WORD_U16S > ,
64
+ pub schedule_helper :
65
+ ShaMessageHelperCols < T , WORD_U16S , ROUNDS_PER_ROW , ROUNDS_PER_ROW_MINUS_ONE > ,
49
66
/// The actual final hash values of the given block
50
67
/// Note: the above `hash` will be equal to `final_hash` unless we are on the last block
51
- pub final_hash : [ [ T ; SHA256_WORD_U8S ] ; SHA256_HASH_WORDS ] ,
68
+ pub final_hash : [ [ T ; WORD_U8S ] ; HASH_WORDS ] ,
52
69
/// The final hash of the previous block
53
70
/// Note: will be constrained using interactions with the chip itself
54
- pub prev_hash : [ [ T ; SHA256_WORD_U16S ] ; SHA256_HASH_WORDS ] ,
71
+ pub prev_hash : [ [ T ; WORD_U16S ] ; HASH_WORDS ] ,
55
72
}
56
73
57
74
#[ repr( C ) ]
58
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
59
- pub struct Sha256MessageScheduleCols < T > {
60
- /// The message schedule words as 32-bit integers
61
- /// The first 16 words will be the message data
62
- pub w : [ [ T ; SHA256_WORD_BITS ] ; SHA256_ROUNDS_PER_ROW ] ,
75
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
76
+ pub struct ShaMessageScheduleCols <
77
+ T ,
78
+ const WORD_BITS : usize ,
79
+ const ROUNDS_PER_ROW : usize ,
80
+ const WORD_U8S : usize ,
81
+ > {
82
+ /// The message schedule words as C::WORD_BITS-bit integers
83
+ /// The first 16 rows will be the message data
84
+ pub w : [ [ T ; WORD_BITS ] ; ROUNDS_PER_ROW ] ,
63
85
/// Will be message schedule carries for rows 4..16 and a buffer for rows 0..4 to be used freely by wrapper chips
64
86
/// Note: carries are 2 bit numbers represented using 2 cells as individual bits
65
- pub carry_or_buffer : [ [ T ; SHA256_WORD_U8S ] ; SHA256_ROUNDS_PER_ROW ] ,
87
+ pub carry_or_buffer : [ [ T ; WORD_U8S ] ; ROUNDS_PER_ROW ] ,
66
88
}
67
89
68
90
#[ repr( C ) ]
69
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
70
- pub struct Sha256WorkVarsCols < T > {
91
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
92
+ pub struct ShaWorkVarsCols <
93
+ T ,
94
+ const WORD_BITS : usize ,
95
+ const ROUNDS_PER_ROW : usize ,
96
+ const WORD_U16S : usize ,
97
+ > {
71
98
/// `a` and `e` after each iteration as 32-bits
72
- pub a : [ [ T ; SHA256_WORD_BITS ] ; SHA256_ROUNDS_PER_ROW ] ,
73
- pub e : [ [ T ; SHA256_WORD_BITS ] ; SHA256_ROUNDS_PER_ROW ] ,
99
+ pub a : [ [ T ; WORD_BITS ] ; ROUNDS_PER_ROW ] ,
100
+ pub e : [ [ T ; WORD_BITS ] ; ROUNDS_PER_ROW ] ,
74
101
/// The carry's used for addition during each iteration when computing `a` and `e`
75
- pub carry_a : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
76
- pub carry_e : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
102
+ pub carry_a : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
103
+ pub carry_e : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
77
104
}
78
105
79
106
/// These are the columns that are used to help with the message schedule additions
80
107
/// Note: these need to be correctly assigned for every row even on padding rows
81
108
#[ repr( C ) ]
82
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
83
- pub struct Sha256MessageHelperCols < T > {
109
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
110
+ pub struct ShaMessageHelperCols <
111
+ T ,
112
+ const WORD_U16S : usize ,
113
+ const ROUNDS_PER_ROW : usize ,
114
+ const ROUNDS_PER_ROW_MINUS_ONE : usize ,
115
+ > {
84
116
/// The following are used to move data forward to constrain the message schedule additions
85
117
/// The value of `w` (message schedule word) from 3 rounds ago
86
118
/// In general, `w_i` means `w` from `i` rounds ago
87
- pub w_3 : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW - 1 ] ,
119
+ pub w_3 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW_MINUS_ONE ] ,
88
120
/// Here intermediate(i) = w_i + sig_0(w_{i+1})
89
121
/// Intermed_t represents the intermediate t rounds ago
90
122
/// This is needed to constrain the message schedule, since we can only constrain on two rows at a time
91
- pub intermed_4 : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
92
- pub intermed_8 : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
93
- pub intermed_12 : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
123
+ pub intermed_4 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
124
+ pub intermed_8 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
125
+ pub intermed_12 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
94
126
}
95
127
96
128
#[ repr( C ) ]
97
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
98
- pub struct Sha256FlagsCols < T > {
99
- /// A flag that indicates if the current row is among the first 16 rows of a block.
129
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
130
+ pub struct ShaFlagsCols < T , const ROW_VAR_CNT : usize > {
131
+ /// A flag that indicates if the current row is among the first C::ROUND_ROWS rows of a block.
100
132
pub is_round_row : T ,
101
133
/// A flag that indicates if the current row is among the first 4 rows of a block.
102
134
pub is_first_4_rows : T ,
@@ -106,7 +138,8 @@ pub struct Sha256FlagsCols<T> {
106
138
// This flag is only used in digest rows.
107
139
pub is_last_block : T ,
108
140
/// We will encode the row index [0..17) using 5 cells
109
- pub row_idx : [ T ; SHA256_ROW_VAR_CNT ] ,
141
+ //#[length(ROW_VAR_CNT)]
142
+ pub row_idx : [ T ; ROW_VAR_CNT ] ,
110
143
/// The index of the current block in the trace starting at 1.
111
144
/// Set to 0 on padding rows.
112
145
pub global_block_idx : T ,
@@ -116,7 +149,9 @@ pub struct Sha256FlagsCols<T> {
116
149
pub local_block_idx : T ,
117
150
}
118
151
119
- impl < O , T : Copy + core:: ops:: Add < Output = O > > Sha256FlagsCols < T > {
152
+ impl < O , T : Copy + core:: ops:: Add < Output = O > , const ROW_VAR_CNT : usize >
153
+ ShaFlagsCols < T , ROW_VAR_CNT >
154
+ {
120
155
// This refers to the padding rows that are added to the air to make the trace length a power of 2.
121
156
// Not to be confused with the padding added to messages as part of the SHA hash function.
122
157
pub fn is_not_padding_row ( & self ) -> O {
@@ -132,3 +167,19 @@ impl<O, T: Copy + core::ops::Add<Output = O>> Sha256FlagsCols<T> {
132
167
not ( self . is_not_padding_row ( ) )
133
168
}
134
169
}
170
+
171
+ // We need to implement this for the ColsRef type as well
172
+ impl < ' a , O , T : Copy + core:: ops:: Add < Output = O > > ShaFlagsColsRef < ' a , T > {
173
+ pub fn is_not_padding_row ( & self ) -> O {
174
+ * self . is_round_row + * self . is_digest_row
175
+ }
176
+
177
+ // This refers to the padding rows that are added to the air to make the trace length a power of 2.
178
+ // Not to be confused with the padding added to messages as part of the SHA hash function.
179
+ pub fn is_padding_row ( & self ) -> O
180
+ where
181
+ O : FieldAlgebra ,
182
+ {
183
+ not ( self . is_not_padding_row ( ) )
184
+ }
185
+ }
0 commit comments