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
@@ -21,82 +19,119 @@ use super::{
21
19
/// 1. Common constraints to work on either struct type by accessing these shared fields
22
20
/// 2. Specific constraints to use the appropriate struct, with flags helping to do conditional constraints
23
21
#[ repr( C ) ]
24
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
25
- pub struct Sha256RoundCols < T > {
26
- pub flags : Sha256FlagsCols < T > ,
27
- pub work_vars : Sha256WorkVarsCols < T > ,
28
- pub schedule_helper : Sha256MessageHelperCols < T > ,
29
- pub message_schedule : Sha256MessageScheduleCols < T > ,
22
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
23
+ pub struct ShaRoundCols <
24
+ T ,
25
+ const WORD_BITS : usize ,
26
+ const WORD_U8S : usize ,
27
+ const WORD_U16S : usize ,
28
+ const ROUNDS_PER_ROW : usize ,
29
+ const ROUNDS_PER_ROW_MINUS_ONE : usize ,
30
+ const ROW_VAR_CNT : usize ,
31
+ > {
32
+ pub flags : ShaFlagsCols < T , ROW_VAR_CNT > ,
33
+ pub work_vars : ShaWorkVarsCols < T , WORD_BITS , ROUNDS_PER_ROW , WORD_U16S > ,
34
+ pub schedule_helper :
35
+ ShaMessageHelperCols < T , WORD_U16S , ROUNDS_PER_ROW , ROUNDS_PER_ROW_MINUS_ONE > ,
36
+ pub message_schedule : ShaMessageScheduleCols < T , WORD_BITS , ROUNDS_PER_ROW , WORD_U8S > ,
30
37
}
31
38
32
39
#[ repr( C ) ]
33
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
34
- pub struct Sha256DigestCols < T > {
35
- pub flags : Sha256FlagsCols < T > ,
40
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
41
+ pub struct ShaDigestCols <
42
+ T ,
43
+ const WORD_BITS : usize ,
44
+ const WORD_U8S : usize ,
45
+ const WORD_U16S : usize ,
46
+ const HASH_WORDS : usize ,
47
+ const ROUNDS_PER_ROW : usize ,
48
+ const ROUNDS_PER_ROW_MINUS_ONE : usize ,
49
+ const ROW_VAR_CNT : usize ,
50
+ > {
51
+ pub flags : ShaFlagsCols < T , ROW_VAR_CNT > ,
36
52
/// Will serve as previous hash values for the next block
37
- pub hash : Sha256WorkVarsCols < T > ,
38
- pub schedule_helper : Sha256MessageHelperCols < T > ,
53
+ pub hash : ShaWorkVarsCols < T , WORD_BITS , ROUNDS_PER_ROW , WORD_U16S > ,
54
+ pub schedule_helper :
55
+ ShaMessageHelperCols < T , WORD_U16S , ROUNDS_PER_ROW , ROUNDS_PER_ROW_MINUS_ONE > ,
39
56
/// The actual final hash values of the given block
40
57
/// Note: the above `hash` will be equal to `final_hash` unless we are on the last block
41
- pub final_hash : [ [ T ; SHA256_WORD_U8S ] ; SHA256_HASH_WORDS ] ,
58
+ pub final_hash : [ [ T ; WORD_U8S ] ; HASH_WORDS ] ,
42
59
/// The final hash of the previous block
43
60
/// Note: will be constrained using interactions with the chip itself
44
- pub prev_hash : [ [ T ; SHA256_WORD_U16S ] ; SHA256_HASH_WORDS ] ,
61
+ pub prev_hash : [ [ T ; WORD_U16S ] ; HASH_WORDS ] ,
45
62
}
46
63
47
64
#[ repr( C ) ]
48
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
49
- pub struct Sha256MessageScheduleCols < T > {
65
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
66
+ pub struct ShaMessageScheduleCols <
67
+ T ,
68
+ const WORD_BITS : usize ,
69
+ const ROUNDS_PER_ROW : usize ,
70
+ const WORD_U8S : usize ,
71
+ > {
50
72
/// The message schedule words as 32-bit intergers
51
- pub w : [ [ T ; SHA256_WORD_BITS ] ; SHA256_ROUNDS_PER_ROW ] ,
73
+ pub w : [ [ T ; WORD_BITS ] ; ROUNDS_PER_ROW ] ,
52
74
/// Will be message schedule carries for rows 4..16 and a buffer for rows 0..4 to be used freely by wrapper chips
53
75
/// Note: carries are represented as 2 bit numbers
54
- pub carry_or_buffer : [ [ T ; SHA256_WORD_U8S ] ; SHA256_ROUNDS_PER_ROW ] ,
76
+ pub carry_or_buffer : [ [ T ; WORD_U8S ] ; ROUNDS_PER_ROW ] ,
55
77
}
56
78
57
79
#[ repr( C ) ]
58
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
59
- pub struct Sha256WorkVarsCols < T > {
80
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
81
+ pub struct ShaWorkVarsCols <
82
+ T ,
83
+ const WORD_BITS : usize ,
84
+ const ROUNDS_PER_ROW : usize ,
85
+ const WORD_U16S : usize ,
86
+ > {
60
87
/// `a` and `e` after each iteration as 32-bits
61
- pub a : [ [ T ; SHA256_WORD_BITS ] ; SHA256_ROUNDS_PER_ROW ] ,
62
- pub e : [ [ T ; SHA256_WORD_BITS ] ; SHA256_ROUNDS_PER_ROW ] ,
88
+ pub a : [ [ T ; WORD_BITS ] ; ROUNDS_PER_ROW ] ,
89
+ pub e : [ [ T ; WORD_BITS ] ; ROUNDS_PER_ROW ] ,
63
90
/// The carry's used for addition during each iteration when computing `a` and `e`
64
- pub carry_a : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
65
- pub carry_e : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
91
+ pub carry_a : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
92
+ pub carry_e : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
66
93
}
67
94
68
95
/// These are the columns that are used to help with the message schedule additions
69
96
/// Note: these need to be correctly assigned for every row even on padding rows
70
97
#[ repr( C ) ]
71
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
72
- pub struct Sha256MessageHelperCols < T > {
98
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
99
+ pub struct ShaMessageHelperCols <
100
+ T ,
101
+ const WORD_U16S : usize ,
102
+ const ROUNDS_PER_ROW : usize ,
103
+ const ROUNDS_PER_ROW_MINUS_ONE : usize ,
104
+ > {
73
105
/// The following are used to move data forward to constrain the message schedule additions
74
106
/// The value of `w` from 3 rounds ago
75
- pub w_3 : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW - 1 ] ,
107
+ pub w_3 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW_MINUS_ONE ] ,
76
108
/// Here intermediate(i) = w_i + sig_0(w_{i+1})
77
109
/// Intermed_t represents the intermediate t rounds ago
78
- pub intermed_4 : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
79
- pub intermed_8 : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
80
- pub intermed_12 : [ [ T ; SHA256_WORD_U16S ] ; SHA256_ROUNDS_PER_ROW ] ,
110
+ pub intermed_4 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
111
+ pub intermed_8 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
112
+ pub intermed_12 : [ [ T ; WORD_U16S ] ; ROUNDS_PER_ROW ] ,
81
113
}
82
114
83
115
#[ repr( C ) ]
84
- #[ derive( Clone , Copy , Debug , AlignedBorrow ) ]
85
- pub struct Sha256FlagsCols < T > {
116
+ #[ derive( Clone , Copy , Debug , ColsRef ) ]
117
+ pub struct ShaFlagsCols < T , const ROW_VAR_CNT : usize > {
86
118
pub is_round_row : T ,
87
119
/// A flag that indicates if the current row is among the first 4 rows of a block
88
120
pub is_first_4_rows : T ,
89
121
pub is_digest_row : T ,
90
122
pub is_last_block : T ,
91
123
/// We will encode the row index [0..17) using 5 cells
92
- pub row_idx : [ T ; SHA256_ROW_VAR_CNT ] ,
124
+ //#[length(ROW_VAR_CNT)]
125
+ pub row_idx : [ T ; ROW_VAR_CNT ] ,
93
126
/// The global index of the current block
94
127
pub global_block_idx : T ,
95
128
/// Will store the index of the current block in the current message starting from 0
96
129
pub local_block_idx : T ,
97
130
}
98
131
99
- impl < O , T : Copy + core:: ops:: Add < Output = O > > Sha256FlagsCols < T > {
132
+ impl < O , T : Copy + core:: ops:: Add < Output = O > , const ROW_VAR_CNT : usize >
133
+ ShaFlagsCols < T , ROW_VAR_CNT >
134
+ {
100
135
pub fn is_not_padding_row ( & self ) -> O {
101
136
self . is_round_row + self . is_digest_row
102
137
}
@@ -108,3 +143,16 @@ impl<O, T: Copy + core::ops::Add<Output = O>> Sha256FlagsCols<T> {
108
143
not ( self . is_not_padding_row ( ) )
109
144
}
110
145
}
146
+
147
+ impl < ' a , O , T : Copy + core:: ops:: Add < Output = O > > ShaFlagsColsRef < ' a , T > {
148
+ pub fn is_not_padding_row ( & self ) -> O {
149
+ * self . is_round_row + * self . is_digest_row
150
+ }
151
+
152
+ pub fn is_padding_row ( & self ) -> O
153
+ where
154
+ O : FieldAlgebra ,
155
+ {
156
+ not ( self . is_not_padding_row ( ) )
157
+ }
158
+ }
0 commit comments