Skip to content

Commit d743bdc

Browse files
Updated names
1 parent dc8938d commit d743bdc

File tree

21 files changed

+50
-78
lines changed

21 files changed

+50
-78
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ openvm-stark-sdk = { git = "https://github.com/openvm-org/stark-backend.git", ta
112112
openvm-sdk = { path = "crates/sdk", default-features = false }
113113
openvm-mod-circuit-builder = { path = "crates/circuits/mod-builder", default-features = false }
114114
openvm-poseidon2-air = { path = "crates/circuits/poseidon2-air", default-features = false }
115-
openvm-sha256-air = { path = "crates/circuits/sha256-air", default-features = false }
115+
openvm-sha-air = { path = "crates/circuits/sha-air", default-features = false }
116116
openvm-sha-macros = { path = "crates/circuits/sha-macros", default-features = false }
117117
openvm-circuit-primitives = { path = "crates/circuits/primitives", default-features = false }
118118
openvm-circuit-primitives-derive = { path = "crates/circuits/primitives/derive", default-features = false }

crates/circuits/sha256-air/Cargo.toml renamed to crates/circuits/sha-air/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "openvm-sha256-air"
2+
name = "openvm-sha-air"
33
version.workspace = true
44
authors.workspace = true
55
edition.workspace = true
File renamed without changes.

crates/circuits/sha256-air/src/columns.rs renamed to crates/circuits/sha-air/src/columns.rs

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@ use openvm_stark_backend::p3_field::FieldAlgebra;
66

77
use crate::ShaConfig;
88

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
1212
///
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:
1714
/// - flags
1815
/// - work_vars/hash (same type, different name)
1916
/// - schedule_helper
2017
///
2118
/// This design allows for:
2219
/// 1. Common constraints to work on either struct type by accessing these shared fields
2320
/// 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.
2621
#[repr(C)]
2722
#[derive(Clone, Copy, Debug, ColsRef)]
2823
pub struct ShaRoundCols<
@@ -35,7 +30,6 @@ pub struct ShaRoundCols<
3530
const ROW_VAR_CNT: usize,
3631
> {
3732
pub flags: ShaFlagsCols<T, ROW_VAR_CNT>,
38-
/// Stores the current state of the working variables
3933
pub work_vars: ShaWorkVarsCols<T, WORD_BITS, ROUNDS_PER_ROW, WORD_U16S>,
4034
pub schedule_helper:
4135
ShaMessageHelperCols<T, WORD_U16S, ROUNDS_PER_ROW, ROUNDS_PER_ROW_MINUS_ONE>,
@@ -55,11 +49,7 @@ pub struct ShaDigestCols<
5549
const ROW_VAR_CNT: usize,
5650
> {
5751
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
6353
pub hash: ShaWorkVarsCols<T, WORD_BITS, ROUNDS_PER_ROW, WORD_U16S>,
6454
pub schedule_helper:
6555
ShaMessageHelperCols<T, WORD_U16S, ROUNDS_PER_ROW, ROUNDS_PER_ROW_MINUS_ONE>,
@@ -79,11 +69,10 @@ pub struct ShaMessageScheduleCols<
7969
const ROUNDS_PER_ROW: usize,
8070
const WORD_U8S: usize,
8171
> {
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
8473
pub w: [[T; WORD_BITS]; ROUNDS_PER_ROW],
8574
/// 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
8776
pub carry_or_buffer: [[T; WORD_U8S]; ROUNDS_PER_ROW],
8877
}
8978

@@ -114,12 +103,10 @@ pub struct ShaMessageHelperCols<
114103
const ROUNDS_PER_ROW_MINUS_ONE: usize,
115104
> {
116105
/// 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
119107
pub w_3: [[T; WORD_U16S]; ROUNDS_PER_ROW_MINUS_ONE],
120108
/// Here intermediate(i) = w_i + sig_0(w_{i+1})
121109
/// 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
123110
pub intermed_4: [[T; WORD_U16S]; ROUNDS_PER_ROW],
124111
pub intermed_8: [[T; WORD_U16S]; ROUNDS_PER_ROW],
125112
pub intermed_12: [[T; WORD_U16S]; ROUNDS_PER_ROW],
@@ -128,38 +115,27 @@ pub struct ShaMessageHelperCols<
128115
#[repr(C)]
129116
#[derive(Clone, Copy, Debug, ColsRef)]
130117
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.
132118
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
134120
pub is_first_4_rows: T,
135-
/// A flag that indicates if the current row is the last (17th) row of a block.
136121
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.
139122
pub is_last_block: T,
140123
/// We will encode the row index [0..17) using 5 cells
141124
//#[length(ROW_VAR_CNT)]
142125
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
145127
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
149129
pub local_block_idx: T,
150130
}
151131

152132
impl<O, T: Copy + core::ops::Add<Output = O>, const ROW_VAR_CNT: usize>
153133
ShaFlagsCols<T, ROW_VAR_CNT>
154134
{
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.
157135
pub fn is_not_padding_row(&self) -> O {
158136
self.is_round_row + self.is_digest_row
159137
}
160138

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.
163139
pub fn is_padding_row(&self) -> O
164140
where
165141
O: FieldAlgebra,
@@ -168,14 +144,11 @@ impl<O, T: Copy + core::ops::Add<Output = O>, const ROW_VAR_CNT: usize>
168144
}
169145
}
170146

171-
// We need to implement this for the ColsRef type as well
172147
impl<'a, O, T: Copy + core::ops::Add<Output = O>> ShaFlagsColsRef<'a, T> {
173148
pub fn is_not_padding_row(&self) -> O {
174149
*self.is_round_row + *self.is_digest_row
175150
}
176151

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.
179152
pub fn is_padding_row(&self) -> O
180153
where
181154
O: FieldAlgebra,

crates/circuits/sha256-air/src/config.rs renamed to crates/circuits/sha-air/src/config.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ pub trait ShaConfig: Send + Sync + Clone {
8181
};
8282
}
8383

84+
/// We can notice that `carry_a`'s and `carry_e`'s are always the same on invalid rows
85+
/// To optimize the trace generation of invalid rows, we precompute those values.
86+
/// This trait also stores the constants K and H for the given SHA config.
8487
pub trait ShaPrecomputedValues<T> {
8588
// these should be appropirately sized for the config
8689
fn get_invalid_carry_a(round_num: usize) -> &'static [u32];
@@ -111,8 +114,6 @@ impl ShaConfig for Sha256Config {
111114
const ROW_VAR_CNT: usize = 5;
112115
}
113116

114-
/// We can notice that `carry_a`'s and `carry_e`'s are always the same on invalid rows
115-
/// To optimize the trace generation of invalid rows, we have those values precomputed here
116117
pub const SHA256_INVALID_CARRY_A: [[u32; Sha256Config::WORD_U16S]; Sha256Config::ROUNDS_PER_ROW] = [
117118
[1230919683, 1162494304],
118119
[266373122, 1282901987],
@@ -179,8 +180,6 @@ impl ShaConfig for Sha512Config {
179180
const ROW_VAR_CNT: usize = 6;
180181
}
181182

182-
/// We can notice that `carry_a`'s and `carry_e`'s are always the same on invalid rows
183-
/// To optimize the trace generation of invalid rows, we have those values precomputed here
184183
pub(crate) const SHA512_INVALID_CARRY_A: [[u32; Sha512Config::WORD_U16S];
185184
Sha512Config::ROUNDS_PER_ROW] = [
186185
[55971842, 827997017, 993005918, 512731953],
@@ -308,7 +307,7 @@ impl ShaPrecomputedValues<u64> for Sha512Config {
308307
}
309308

310309
// Needed to avoid compile errors in utils.rs
311-
// don't ask me why this doesn't inf loop
310+
// not sure why this doesn't inf loop
312311
pub trait RotateRight {
313312
fn rotate_right(self, n: u32) -> Self;
314313
}

crates/circuits/sha256-air/src/lib.rs renamed to crates/circuits/sha-air/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Implementation of the SHA256 compression function without padding
1+
//! Implementation of the SHA256/SHA512 compression function without padding
22
//! This this AIR doesn't constrain any of the message padding
33
44
mod air;
File renamed without changes.

crates/circuits/sha256-air/src/trace.rs renamed to crates/circuits/sha-air/src/trace.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
ShaRoundColsRef, WrappingAdd,
2020
};
2121

22-
/// The trace generation of SHA256 should be done in two passes.
22+
/// The trace generation of SHA should be done in two passes.
2323
/// The first pass should do `get_block_trace` for every block and generate the invalid rows through `get_default_row`
2424
/// The second pass should go through all the blocks and call `generate_missing_cells`
2525
impl<C: ShaConfig + ShaPrecomputedValues<C::Word>> ShaAir<C> {
@@ -455,7 +455,7 @@ impl<C: ShaConfig + ShaPrecomputedValues<C::Word>> ShaAir<C> {
455455
/// This function should be called only after `generate_block_trace` was called for all blocks
456456
/// And [`Self::generate_default_row`] is called for all invalid rows
457457
/// Will populate the missing values of `trace`, where the width of the trace is `trace_width`
458-
/// and the starting column for the `Sha256Air` is `trace_start_col`.
458+
/// and the starting column for the `ShaAir` is `trace_start_col`.
459459
/// Note: `trace` needs to be the rows 1..C::ROWS_PER_BLOCK of a block and the first row of the next block
460460
pub fn generate_missing_cells<F: PrimeField32>(
461461
&self,
@@ -748,11 +748,8 @@ pub fn generate_trace<F: PrimeField32, C: ShaConfig + ShaPrecomputedValues<C::Wo
748748
debug_assert!(input.len() == C::BLOCK_U8S);
749749
}
750750

751-
println!("records.len(): {}", records.len());
752751
let non_padded_height = records.len() * C::ROWS_PER_BLOCK;
753-
println!("non_padded_height: {}", non_padded_height);
754752
let height = next_power_of_two_or_zero(non_padded_height);
755-
println!("height: {}", height);
756753
let width = <ShaAir<C> as BaseAir<F>>::width(sub_air);
757754
let mut values = F::zero_vec(height * width);
758755

crates/circuits/sha256-air/src/utils.rs renamed to crates/circuits/sha-air/src/utils.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub(crate) fn xor_bit<F: FieldAlgebra + Clone>(
102102
+ (not::<F>(x) * not::<F>(y) * z)
103103
}
104104

105-
/// Computes x ^ y ^ z, where x, y, z are [SHA256_WORD_BITS] bit numbers
105+
/// Computes x ^ y ^ z, where x, y, z are [C::WORD_BITS] bit numbers
106106
#[inline]
107107
pub(crate) fn xor<F: FieldAlgebra + Clone>(
108108
x: &[impl Into<F> + Clone],
@@ -114,13 +114,13 @@ pub(crate) fn xor<F: FieldAlgebra + Clone>(
114114
.collect()
115115
}
116116

117-
/// Choose function from SHA256
117+
/// Choose function from the SHA spec
118118
#[inline]
119119
pub fn ch<C: ShaConfig>(x: C::Word, y: C::Word, z: C::Word) -> C::Word {
120120
(x & y) ^ ((!x) & z)
121121
}
122122

123-
/// Computes Ch(x,y,z), where x, y, z are [SHA256_WORD_BITS] bit numbers
123+
/// Computes Ch(x,y,z), where x, y, z are [C::WORD_BITS] bit numbers
124124
#[inline]
125125
pub(crate) fn ch_field<F: FieldAlgebra>(
126126
x: &[impl Into<F> + Clone],
@@ -132,12 +132,12 @@ pub(crate) fn ch_field<F: FieldAlgebra>(
132132
.collect()
133133
}
134134

135-
/// Majority function from SHA256
135+
/// Majority function from the SHA spec
136136
pub fn maj<C: ShaConfig>(x: C::Word, y: C::Word, z: C::Word) -> C::Word {
137137
(x & y) ^ (x & z) ^ (y & z)
138138
}
139139

140-
/// Computes Maj(x,y,z), where x, y, z are [SHA256_WORD_BITS] bit numbers
140+
/// Computes Maj(x,y,z), where x, y, z are [C::WORD_BITS] bit numbers
141141
#[inline]
142142
pub(crate) fn maj_field<F: FieldAlgebra + Clone>(
143143
x: &[impl Into<F> + Clone],
@@ -157,7 +157,7 @@ pub(crate) fn maj_field<F: FieldAlgebra + Clone>(
157157
.collect()
158158
}
159159

160-
/// Big sigma_0 function from SHA256
160+
/// Big sigma_0 function from the SHA spec
161161
pub fn big_sig0<C: ShaConfig>(x: C::Word) -> C::Word {
162162
if C::WORD_BITS == 32 {
163163
x.rotate_right(2) ^ x.rotate_right(13) ^ x.rotate_right(22)
@@ -178,7 +178,7 @@ pub(crate) fn big_sig0_field<F: FieldAlgebra + Clone, C: ShaConfig>(
178178
}
179179
}
180180

181-
/// Big sigma_1 function from SHA256
181+
/// Big sigma_1 function from the SHA spec
182182
pub fn big_sig1<C: ShaConfig>(x: C::Word) -> C::Word {
183183
if C::WORD_BITS == 32 {
184184
x.rotate_right(6) ^ x.rotate_right(11) ^ x.rotate_right(25)
@@ -187,7 +187,7 @@ pub fn big_sig1<C: ShaConfig>(x: C::Word) -> C::Word {
187187
}
188188
}
189189

190-
/// Computes BigSigma1(x), where x is a [SHA256_WORD_BITS] bit number in little-endian
190+
/// Computes BigSigma1(x), where x is a [C::WORD_BITS] bit number in little-endian
191191
#[inline]
192192
pub(crate) fn big_sig1_field<F: FieldAlgebra + Clone, C: ShaConfig>(
193193
x: &[impl Into<F> + Clone],
@@ -199,7 +199,7 @@ pub(crate) fn big_sig1_field<F: FieldAlgebra + Clone, C: ShaConfig>(
199199
}
200200
}
201201

202-
/// Small sigma_0 function from SHA256
202+
/// Small sigma_0 function from the SHA spec
203203
pub fn small_sig0<C: ShaConfig>(x: C::Word) -> C::Word {
204204
if C::WORD_BITS == 32 {
205205
x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3)
@@ -208,7 +208,7 @@ pub fn small_sig0<C: ShaConfig>(x: C::Word) -> C::Word {
208208
}
209209
}
210210

211-
/// Computes SmallSigma0(x), where x is a [SHA256_WORD_BITS] bit number in little-endian
211+
/// Computes SmallSigma0(x), where x is a [C::WORD_BITS] bit number in little-endian
212212
#[inline]
213213
pub(crate) fn small_sig0_field<F: FieldAlgebra + Clone, C: ShaConfig>(
214214
x: &[impl Into<F> + Clone],
@@ -220,7 +220,7 @@ pub(crate) fn small_sig0_field<F: FieldAlgebra + Clone, C: ShaConfig>(
220220
}
221221
}
222222

223-
/// Small sigma_1 function from SHA256
223+
/// Small sigma_1 function from the SHA spec
224224
pub fn small_sig1<C: ShaConfig>(x: C::Word) -> C::Word {
225225
if C::WORD_BITS == 32 {
226226
x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10)
@@ -229,7 +229,7 @@ pub fn small_sig1<C: ShaConfig>(x: C::Word) -> C::Word {
229229
}
230230
}
231231

232-
/// Computes SmallSigma1(x), where x is a [SHA256_WORD_BITS] bit number in little-endian
232+
/// Computes SmallSigma1(x), where x is a [C::WORD_BITS] bit number in little-endian
233233
#[inline]
234234
pub(crate) fn small_sig1_field<F: FieldAlgebra + Clone, C: ShaConfig>(
235235
x: &[impl Into<F> + Clone],
@@ -253,7 +253,7 @@ pub fn get_flag_pt_array(encoder: &Encoder, flag_idx: usize) -> Vec<u32> {
253253
encoder.get_flag_pt(flag_idx)
254254
}
255255

256-
/// Constrain the addition of [SHA256_WORD_BITS] bit words in 16-bit limbs
256+
/// Constrain the addition of [C::WORD_BITS] bit words in 16-bit limbs
257257
/// It takes in the terms some in bits some in 16-bit limbs,
258258
/// the expected sum in bits and the carries
259259
pub fn constraint_word_addition<AB: AirBuilder, C: ShaConfig>(

0 commit comments

Comments
 (0)