Skip to content

Commit 92dc96f

Browse files
Merge ShaPrecomputedValues trait into ShaConfig trait for simplicity
1 parent eee18cd commit 92dc96f

File tree

5 files changed

+54
-64
lines changed

5 files changed

+54
-64
lines changed

crates/circuits/sha-air/src/air.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ use super::{
1919
small_sig1_field,
2020
};
2121
use crate::{
22-
constraint_word_addition, word_into_u16_limbs, ShaConfig, ShaDigestColsRef,
23-
ShaPrecomputedValues, ShaRoundColsRef,
22+
constraint_word_addition, word_into_u16_limbs, ShaConfig, ShaDigestColsRef, ShaRoundColsRef,
2423
};
2524

2625
/// Expects the message to be padded to a multiple of C::BLOCK_WORDS * C::WORD_BITS bits
2726
#[derive(Clone, Debug)]
28-
pub struct ShaAir<C: ShaConfig + ShaPrecomputedValues<C::Word>> {
27+
pub struct ShaAir<C: ShaConfig> {
2928
pub bitwise_lookup_bus: BitwiseOperationLookupBus,
3029
pub row_idx_encoder: Encoder,
3130
/// Internal bus for self-interactions in this AIR.
3231
bus: PermutationCheckBus,
3332
_phantom: PhantomData<C>,
3433
}
3534

36-
impl<C: ShaConfig + ShaPrecomputedValues<C::Word>> ShaAir<C> {
35+
impl<C: ShaConfig> ShaAir<C> {
3736
pub fn new(bitwise_lookup_bus: BitwiseOperationLookupBus, self_bus_idx: BusIndex) -> Self {
3837
Self {
3938
bitwise_lookup_bus,
@@ -44,15 +43,13 @@ impl<C: ShaConfig + ShaPrecomputedValues<C::Word>> ShaAir<C> {
4443
}
4544
}
4645

47-
impl<F, C: ShaConfig + ShaPrecomputedValues<C::Word>> BaseAir<F> for ShaAir<C> {
46+
impl<F, C: ShaConfig> BaseAir<F> for ShaAir<C> {
4847
fn width(&self) -> usize {
4948
max(C::ROUND_WIDTH, C::DIGEST_WIDTH)
5049
}
5150
}
5251

53-
impl<AB: InteractionBuilder, C: ShaConfig + ShaPrecomputedValues<C::Word>> SubAir<AB>
54-
for ShaAir<C>
55-
{
52+
impl<AB: InteractionBuilder, C: ShaConfig> SubAir<AB> for ShaAir<C> {
5653
/// The start column for the sub-air to use
5754
type AirContext<'a>
5855
= usize
@@ -72,7 +69,7 @@ impl<AB: InteractionBuilder, C: ShaConfig + ShaPrecomputedValues<C::Word>> SubAi
7269
}
7370
}
7471

75-
impl<C: ShaConfig + ShaPrecomputedValues<C::Word>> ShaAir<C> {
72+
impl<C: ShaConfig> ShaAir<C> {
7673
/// Implements the single row constraints (i.e. imposes constraints only on local)
7774
/// Implements some sanity constraints on the row index, flags, and work variables
7875
fn eval_row<AB: InteractionBuilder>(&self, builder: &mut AB, start_col: usize) {

crates/circuits/sha-air/src/columns.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::ShaConfig;
2020
/// 2. Specific constraints to use the appropriate struct, with flags helping to do conditional constraints
2121
#[repr(C)]
2222
#[derive(Clone, Copy, Debug, ColsRef)]
23+
#[config(ShaConfig)]
2324
pub struct ShaRoundCols<
2425
T,
2526
const WORD_BITS: usize,
@@ -38,6 +39,7 @@ pub struct ShaRoundCols<
3839

3940
#[repr(C)]
4041
#[derive(Clone, Copy, Debug, ColsRef)]
42+
#[config(ShaConfig)]
4143
pub struct ShaDigestCols<
4244
T,
4345
const WORD_BITS: usize,
@@ -63,6 +65,7 @@ pub struct ShaDigestCols<
6365

6466
#[repr(C)]
6567
#[derive(Clone, Copy, Debug, ColsRef)]
68+
#[config(ShaConfig)]
6669
pub struct ShaMessageScheduleCols<
6770
T,
6871
const WORD_BITS: usize,
@@ -78,6 +81,7 @@ pub struct ShaMessageScheduleCols<
7881

7982
#[repr(C)]
8083
#[derive(Clone, Copy, Debug, ColsRef)]
84+
#[config(ShaConfig)]
8185
pub struct ShaWorkVarsCols<
8286
T,
8387
const WORD_BITS: usize,
@@ -96,6 +100,7 @@ pub struct ShaWorkVarsCols<
96100
/// Note: these need to be correctly assigned for every row even on padding rows
97101
#[repr(C)]
98102
#[derive(Clone, Copy, Debug, ColsRef)]
103+
#[config(ShaConfig)]
99104
pub struct ShaMessageHelperCols<
100105
T,
101106
const WORD_U16S: usize,
@@ -114,6 +119,7 @@ pub struct ShaMessageHelperCols<
114119

115120
#[repr(C)]
116121
#[derive(Clone, Copy, Debug, ColsRef)]
122+
#[config(ShaConfig)]
117123
pub struct ShaFlagsCols<T, const ROW_VAR_CNT: usize> {
118124
pub is_round_row: T,
119125
/// A flag that indicates if the current row is among the first 4 rows of a block

crates/circuits/sha-air/src/config.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ pub trait ShaPrecomputedValues<T> {
8686
// these should be appropriately sized for the config
8787
fn get_invalid_carry_a(round_num: usize) -> &'static [u32];
8888
fn get_invalid_carry_e(round_num: usize) -> &'static [u32];
89-
fn get_k() -> &'static [T];
90-
fn get_h() -> &'static [T];
89+
fn get_k() -> &'static [Self::Word];
90+
fn get_h() -> &'static [Self::Word];
9191
}
9292

9393
#[derive(Clone)]
@@ -110,6 +110,19 @@ impl ShaConfig for Sha256Config {
110110
const HASH_WORDS: usize = 8;
111111
/// Number of vars needed to encode the row index with [Encoder]
112112
const ROW_VAR_CNT: usize = 5;
113+
114+
fn get_invalid_carry_a(round_num: usize) -> &'static [u32] {
115+
&SHA256_INVALID_CARRY_A[round_num]
116+
}
117+
fn get_invalid_carry_e(round_num: usize) -> &'static [u32] {
118+
&SHA256_INVALID_CARRY_E[round_num]
119+
}
120+
fn get_k() -> &'static [u32] {
121+
&SHA256_K
122+
}
123+
fn get_h() -> &'static [u32] {
124+
&SHA256_H
125+
}
113126
}
114127

115128
pub const SHA256_INVALID_CARRY_A: [[u32; Sha256Config::WORD_U16S]; Sha256Config::ROUNDS_PER_ROW] = [
@@ -141,21 +154,6 @@ pub const SHA256_H: [u32; 8] = [
141154
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
142155
];
143156

144-
impl ShaPrecomputedValues<u32> for Sha256Config {
145-
fn get_invalid_carry_a(round_num: usize) -> &'static [u32] {
146-
&SHA256_INVALID_CARRY_A[round_num]
147-
}
148-
fn get_invalid_carry_e(round_num: usize) -> &'static [u32] {
149-
&SHA256_INVALID_CARRY_E[round_num]
150-
}
151-
fn get_k() -> &'static [u32] {
152-
&SHA256_K
153-
}
154-
fn get_h() -> &'static [u32] {
155-
&SHA256_H
156-
}
157-
}
158-
159157
#[derive(Clone)]
160158
pub struct Sha512Config;
161159

@@ -176,6 +174,19 @@ impl ShaConfig for Sha512Config {
176174
const HASH_WORDS: usize = 8;
177175
/// Number of vars needed to encode the row index with [Encoder]
178176
const ROW_VAR_CNT: usize = 6;
177+
178+
fn get_invalid_carry_a(round_num: usize) -> &'static [u32] {
179+
&SHA512_INVALID_CARRY_A[round_num]
180+
}
181+
fn get_invalid_carry_e(round_num: usize) -> &'static [u32] {
182+
&SHA512_INVALID_CARRY_E[round_num]
183+
}
184+
fn get_k() -> &'static [u64] {
185+
&SHA512_K
186+
}
187+
fn get_h() -> &'static [u64] {
188+
&SHA512_H
189+
}
179190
}
180191

181192
pub(crate) const SHA512_INVALID_CARRY_A: [[u32; Sha512Config::WORD_U16S];
@@ -289,21 +300,6 @@ pub const SHA512_H: [u64; 8] = [
289300
0x5be0cd19137e2179,
290301
];
291302

292-
impl ShaPrecomputedValues<u64> for Sha512Config {
293-
fn get_invalid_carry_a(round_num: usize) -> &'static [u32] {
294-
&SHA512_INVALID_CARRY_A[round_num]
295-
}
296-
fn get_invalid_carry_e(round_num: usize) -> &'static [u32] {
297-
&SHA512_INVALID_CARRY_E[round_num]
298-
}
299-
fn get_k() -> &'static [u64] {
300-
&SHA512_K
301-
}
302-
fn get_h() -> &'static [u64] {
303-
&SHA512_H
304-
}
305-
}
306-
307303
// Needed to avoid compile errors in utils.rs
308304
// not sure why this doesn't inf loop
309305
pub trait RotateRight {

crates/circuits/sha-air/src/tests.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,37 @@ use openvm_stark_sdk::utils::create_seeded_rng;
2323
use rand::Rng;
2424

2525
use crate::{
26-
compose, small_sig0_field, Sha256Config, Sha512Config, ShaAir, ShaConfig, ShaPrecomputedValues,
26+
compose, small_sig0_field, Sha256Config, Sha512Config, ShaAir, ShaConfig,
2727
};
2828

2929
// A wrapper AIR purely for testing purposes
3030
#[derive(Clone, Debug)]
31-
pub struct ShaTestAir<C: ShaConfig + ShaPrecomputedValues<C::Word>> {
31+
pub struct ShaTestAir<C: ShaConfig> {
3232
pub sub_air: ShaAir<C>,
3333
}
3434

35-
impl<F: Field, C: ShaConfig + ShaPrecomputedValues<C::Word>> BaseAirWithPublicValues<F>
36-
for ShaTestAir<C>
37-
{
38-
}
39-
impl<F: Field, C: ShaConfig + ShaPrecomputedValues<C::Word>> PartitionedBaseAir<F>
40-
for ShaTestAir<C>
41-
{
42-
}
43-
impl<F: Field, C: ShaConfig + ShaPrecomputedValues<C::Word>> BaseAir<F> for ShaTestAir<C> {
35+
impl<F: Field, C: ShaConfig> BaseAirWithPublicValues<F> for ShaTestAir<C> {}
36+
impl<F: Field, C: ShaConfig> PartitionedBaseAir<F> for ShaTestAir<C> {}
37+
impl<F: Field, C: ShaConfig> BaseAir<F> for ShaTestAir<C> {
4438
fn width(&self) -> usize {
4539
<ShaAir<C> as BaseAir<F>>::width(&self.sub_air)
4640
}
4741
}
4842

49-
impl<AB: InteractionBuilder, C: ShaConfig + ShaPrecomputedValues<C::Word>> Air<AB>
50-
for ShaTestAir<C>
51-
{
43+
impl<AB: InteractionBuilder, C: ShaConfig> Air<AB> for ShaTestAir<C> {
5244
fn eval(&self, builder: &mut AB) {
5345
self.sub_air.eval(builder, 0);
5446
}
5547
}
5648

5749
// A wrapper Chip purely for testing purposes
58-
pub struct ShaTestChip<C: ShaConfig + ShaPrecomputedValues<C::Word>> {
50+
pub struct ShaTestChip<C: ShaConfig> {
5951
pub air: ShaTestAir<C>,
6052
pub bitwise_lookup_chip: SharedBitwiseOperationLookupChip<8>,
6153
pub records: Vec<(Vec<u8>, bool)>, // length of inner vec is BLOCK_U8S
6254
}
6355

64-
impl<SC: StarkGenericConfig, C: ShaConfig + ShaPrecomputedValues<C::Word> + 'static> Chip<SC>
65-
for ShaTestChip<C>
56+
impl<SC: StarkGenericConfig, C: ShaConfig + 'static> Chip<SC> for ShaTestChip<C>
6657
where
6758
Val<SC>: PrimeField32,
6859
{
@@ -80,7 +71,7 @@ where
8071
}
8172
}
8273

83-
impl<C: ShaConfig + ShaPrecomputedValues<C::Word>> ChipUsageGetter for ShaTestChip<C> {
74+
impl<C: ShaConfig> ChipUsageGetter for ShaTestChip<C> {
8475
fn air_name(&self) -> String {
8576
get_air_name(&self.air)
8677
}
@@ -94,7 +85,7 @@ impl<C: ShaConfig + ShaPrecomputedValues<C::Word>> ChipUsageGetter for ShaTestCh
9485
}
9586

9687
const SELF_BUS_IDX: BusIndex = 28;
97-
fn rand_sha_test<C: ShaConfig + ShaPrecomputedValues<C::Word> + 'static>() {
88+
fn rand_sha_test<C: ShaConfig + 'static>() {
9889
let mut rng = create_seeded_rng();
9990
let tester = VmChipTestBuilder::default();
10091
let bitwise_bus = BitwiseOperationLookupBus::new(BITWISE_OP_LOOKUP_BUS);

crates/circuits/sha-air/src/trace.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ use super::{
1515
};
1616
use crate::{
1717
big_sig0, big_sig1, ch, limbs_into_word, maj, small_sig0, small_sig1, word_into_bits,
18-
word_into_u16_limbs, word_into_u8_limbs, ShaConfig, ShaDigestColsRefMut, ShaPrecomputedValues,
19-
ShaRoundColsRef, WrappingAdd,
18+
word_into_u16_limbs, word_into_u8_limbs, ShaConfig, ShaDigestColsRefMut, ShaRoundColsRef,
19+
WrappingAdd,
2020
};
2121

2222
/// 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`
25-
impl<C: ShaConfig + ShaPrecomputedValues<C::Word>> ShaAir<C> {
25+
impl<C: ShaConfig> ShaAir<C> {
2626
/// This function takes the input_message (padding not handled), the previous hash,
2727
/// and returns the new hash after processing the block input
2828
pub fn get_block_hash(prev_hash: &[C::Word], input: Vec<u8>) -> Vec<C::Word> {
@@ -749,7 +749,7 @@ impl<C: ShaConfig + ShaPrecomputedValues<C::Word>> ShaAir<C> {
749749
}
750750

751751
/// `records` consists of pairs of `(input_block, is_last_block)`.
752-
pub fn generate_trace<F: PrimeField32, C: ShaConfig + ShaPrecomputedValues<C::Word>>(
752+
pub fn generate_trace<F: PrimeField32, C: ShaConfig>(
753753
sub_air: &ShaAir<C>,
754754
bitwise_lookup_chip: SharedBitwiseOperationLookupChip<8>,
755755
records: Vec<(Vec<u8>, bool)>,

0 commit comments

Comments
 (0)