@@ -6,23 +6,18 @@ use openvm_stark_backend::p3_field::FieldAlgebra;
6
6
7
7
use crate :: ShaConfig ;
8
8
9
- /// In each SHA256 block:
10
- /// - First 16 rows use Sha256RoundCols
11
- /// - Final row uses Sha256DigestCols
9
+ /// In each SHA block:
10
+ /// - First C::ROUND_ROWS rows use ShaRoundCols
11
+ /// - Final row uses ShaDigestCols
12
12
///
13
- /// Note that for soundness, we require that there is always a padding row after the last digest row in the trace.
14
- /// Right now, this is true because the unpadded height is a multiple of 17, and thus not a power of 2.
15
- ///
16
- /// Sha256RoundCols and Sha256DigestCols share the same first 3 fields:
13
+ /// ShaRoundCols and ShaDigestCols share the same first 3 fields:
17
14
/// - flags
18
15
/// - work_vars/hash (same type, different name)
19
16
/// - schedule_helper
20
17
///
21
18
/// This design allows for:
22
19
/// 1. Common constraints to work on either struct type by accessing these shared fields
23
20
/// 2. Specific constraints to use the appropriate struct, with flags helping to do conditional constraints
24
- ///
25
- /// Note that the `Sha256WorkVarsCols` field it is used for different purposes in the two structs.
26
21
#[ repr( C ) ]
27
22
#[ derive( Clone , Copy , Debug , ColsRef ) ]
28
23
pub struct ShaRoundCols <
@@ -35,7 +30,6 @@ pub struct ShaRoundCols<
35
30
const ROW_VAR_CNT : usize ,
36
31
> {
37
32
pub flags : ShaFlagsCols < T , ROW_VAR_CNT > ,
38
- /// Stores the current state of the working variables
39
33
pub work_vars : ShaWorkVarsCols < T , WORD_BITS , ROUNDS_PER_ROW , WORD_U16S > ,
40
34
pub schedule_helper :
41
35
ShaMessageHelperCols < T , WORD_U16S , ROUNDS_PER_ROW , ROUNDS_PER_ROW_MINUS_ONE > ,
@@ -55,11 +49,7 @@ pub struct ShaDigestCols<
55
49
const ROW_VAR_CNT : usize ,
56
50
> {
57
51
pub flags : ShaFlagsCols < T , ROW_VAR_CNT > ,
58
- /// Will serve as previous hash values for the next block.
59
- /// - on non-last blocks, this is the final hash of the current block
60
- /// - on last blocks, this is the initial state constants, SHA256_H.
61
- /// The work variables constraints are applied on all rows, so `carry_a` and `carry_e`
62
- /// must be filled in with dummy values to ensure these constraints hold.
52
+ /// Will serve as previous hash values for the next block
63
53
pub hash : ShaWorkVarsCols < T , WORD_BITS , ROUNDS_PER_ROW , WORD_U16S > ,
64
54
pub schedule_helper :
65
55
ShaMessageHelperCols < T , WORD_U16S , ROUNDS_PER_ROW , ROUNDS_PER_ROW_MINUS_ONE > ,
@@ -79,11 +69,10 @@ pub struct ShaMessageScheduleCols<
79
69
const ROUNDS_PER_ROW : usize ,
80
70
const WORD_U8S : usize ,
81
71
> {
82
- /// The message schedule words as C::WORD_BITS-bit integers
83
- /// The first 16 rows will be the message data
72
+ /// The message schedule words as 32-bit intergers
84
73
pub w : [ [ T ; WORD_BITS ] ; ROUNDS_PER_ROW ] ,
85
74
/// Will be message schedule carries for rows 4..C::ROUND_ROWS and a buffer for rows 0..4 to be used freely by wrapper chips
86
- /// Note: carries are 2 bit numbers represented using 2 cells as individual bits
75
+ /// Note: carries are represented as 2 bit numbers
87
76
pub carry_or_buffer : [ [ T ; WORD_U8S ] ; ROUNDS_PER_ROW ] ,
88
77
}
89
78
@@ -114,12 +103,10 @@ pub struct ShaMessageHelperCols<
114
103
const ROUNDS_PER_ROW_MINUS_ONE : usize ,
115
104
> {
116
105
/// The following are used to move data forward to constrain the message schedule additions
117
- /// The value of `w` (message schedule word) from 3 rounds ago
118
- /// In general, `w_i` means `w` from `i` rounds ago
106
+ /// The value of `w` from 3 rounds ago
119
107
pub w_3 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW_MINUS_ONE ] ,
120
108
/// Here intermediate(i) = w_i + sig_0(w_{i+1})
121
109
/// Intermed_t represents the intermediate t rounds ago
122
- /// This is needed to constrain the message schedule, since we can only constrain on two rows at a time
123
110
pub intermed_4 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
124
111
pub intermed_8 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
125
112
pub intermed_12 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
@@ -128,38 +115,27 @@ pub struct ShaMessageHelperCols<
128
115
#[ repr( C ) ]
129
116
#[ derive( Clone , Copy , Debug , ColsRef ) ]
130
117
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.
132
118
pub is_round_row : T ,
133
- /// A flag that indicates if the current row is among the first 4 rows of a block.
119
+ /// A flag that indicates if the current row is among the first 4 rows of a block
134
120
pub is_first_4_rows : T ,
135
- /// A flag that indicates if the current row is the last (17th) row of a block.
136
121
pub is_digest_row : T ,
137
- // A flag that indicates if the current row is the last block of the message.
138
- // This flag is only used in digest rows.
139
122
pub is_last_block : T ,
140
123
/// We will encode the row index [0..17) using 5 cells
141
124
//#[length(ROW_VAR_CNT)]
142
125
pub row_idx : [ T ; ROW_VAR_CNT ] ,
143
- /// The index of the current block in the trace starting at 1.
144
- /// Set to 0 on padding rows.
126
+ /// The global index of the current block
145
127
pub global_block_idx : T ,
146
- /// The index of the current block in the current message starting at 0.
147
- /// Resets after every message.
148
- /// Set to 0 on padding rows.
128
+ /// Will store the index of the current block in the current message starting from 0
149
129
pub local_block_idx : T ,
150
130
}
151
131
152
132
impl < O , T : Copy + core:: ops:: Add < Output = O > , const ROW_VAR_CNT : usize >
153
133
ShaFlagsCols < T , ROW_VAR_CNT >
154
134
{
155
- // This refers to the padding rows that are added to the air to make the trace length a power of 2.
156
- // Not to be confused with the padding added to messages as part of the SHA hash function.
157
135
pub fn is_not_padding_row ( & self ) -> O {
158
136
self . is_round_row + self . is_digest_row
159
137
}
160
138
161
- // This refers to the padding rows that are added to the air to make the trace length a power of 2.
162
- // Not to be confused with the padding added to messages as part of the SHA hash function.
163
139
pub fn is_padding_row ( & self ) -> O
164
140
where
165
141
O : FieldAlgebra ,
@@ -168,14 +144,11 @@ impl<O, T: Copy + core::ops::Add<Output = O>, const ROW_VAR_CNT: usize>
168
144
}
169
145
}
170
146
171
- // We need to implement this for the ColsRef type as well
172
147
impl < ' a , O , T : Copy + core:: ops:: Add < Output = O > > ShaFlagsColsRef < ' a , T > {
173
148
pub fn is_not_padding_row ( & self ) -> O {
174
149
* self . is_round_row + * self . is_digest_row
175
150
}
176
151
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
152
pub fn is_padding_row ( & self ) -> O
180
153
where
181
154
O : FieldAlgebra ,
0 commit comments