Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: constrain all unused cells to be zero #193

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions hashes/zkevm/src/sha256/vanilla/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,84 @@ impl<F: Field> Sha256CircuitConfig<F> {
cb.gate(meta.query_fixed(q_squeeze, Rotation::cur()))
});

// Constrain all unused cells to be 0 for safety.
#[allow(clippy::needless_range_loop)]
meta.create_gate("unused is zero", |meta| {
let to_const =
|value: &String| -> &'static str { Box::leak(value.clone().into_boxed_str()) };
let mut cb = BaseConstraintBuilder::new(MAX_DEGREE);

let q_start = meta.query_fixed(q_start, Rotation::cur());
let q_compression = meta.query_fixed(q_compression, Rotation::cur());
let q_input = meta.query_fixed(q_input, Rotation::cur());
let q_extend = meta.query_fixed(q_extend, Rotation::cur());
let q_end = meta.query_fixed(q_end, Rotation::cur());
let q_squeeze = meta.query_fixed(q_squeeze, Rotation::cur());

let is_final = meta.query_advice(is_final, Rotation::cur());
let is_paddings = is_paddings.map(|c| meta.query_advice(c, Rotation::cur()));
let word_value = meta.query_advice(hash_table.word_value, Rotation::cur());
let length = meta.query_advice(hash_table.length, Rotation::cur());
let output_lo = meta.query_advice(hash_table.output.lo(), Rotation::cur());
let output_hi = meta.query_advice(hash_table.output.hi(), Rotation::cur());

// column w.b0-w.b1 at offsets [0-19, 68-91, 140-255]
cb.condition(not::expr(q_extend.clone()), |cb| {
cb.require_zero("if not(q_extend) w.b0 = 0", w_ext[0].clone());
cb.require_zero("if not(q_extend) w.b1 = 0", w_ext[1].clone());
});
// column w.b2-w.b33 at offsets [0-3, 68-75, 140-255]
cb.condition(q_start.clone() + q_end.clone(), |cb| {
for k in 2..=33 {
cb.require_zero(
to_const(&format!("if q_start and q_end w.b{k} = 0")),
w_ext[k].clone(),
);
}
});
// column is_final at offsets [4, 20-70, 92-142, 144-255]
cb.condition(q_compression.clone() + q_end.clone() - q_squeeze.clone(), |cb| {
cb.require_zero(
"if q_compression or (q_end and not(q_squeeze)) is_final = 0",
is_final,
);
});
// column pad0-pad2 at offsets [0-3, 20-75, 92-255]
cb.condition(not::expr(q_input.clone()), |cb| {
for k in 0..=2 {
cb.require_zero(
to_const(&format!("if not(q_input) is_paddings[{k}] = 0")),
is_paddings[k].clone(),
);
}
});
// column pad3 at offsets [20-70, 92-142, 144-255]
cb.condition(q_extend.clone() + q_end.clone() - q_squeeze.clone(), |cb| {
cb.require_zero(
"if q_extend or (q_end and not(q_squeeze)) is_paddings[3] = 0",
is_paddings[3].clone(),
);
});
// column value at offsets [0-3, 20-75, 92-255]
cb.condition(not::expr(q_input.clone()), |cb| {
cb.require_zero("if not(q_input) hash_table.word_value = 0", word_value.clone());
});
// column len at offsets [20-70, 92-142, 144-255]
cb.condition(q_extend.clone() + q_end.clone() - q_squeeze.clone(), |cb| {
cb.require_zero(
"if q_extend or (q_end and not(q_squeeze)) hash_table.lenght = 0",
length.clone(),
);
});
// column out_lo, out_hi at offsets [0-70, 72-142, 144-255]
cb.condition(not::expr(q_squeeze.clone()), |cb| {
cb.require_zero("if not(q_squeeze) output_lo = 0", output_lo.clone());
cb.require_zero("if not(q_squeeze) output_hi = 0", output_hi.clone());
});

cb.gate(meta.query_fixed(q_enable, Rotation::cur()))
});

info!("degree: {}", meta.degree());

Sha256CircuitConfig {
Expand Down
23 changes: 15 additions & 8 deletions hashes/zkevm/src/sha256/vanilla/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,23 @@ pub fn generate_witnesses_sha256(rows: &mut Vec<VirtualShaRow>, input_bytes: &[u
is_final,
length,
is_paddings,
hash_bytes: [u8; NUM_BYTES_TO_SQUEEZE]| {
hash_bytes: [u8; NUM_BYTES_TO_SQUEEZE],
is_input: bool| {
let word_to_bits = |value: u64, num_bits: usize| {
into_be_bits(&value.to_be_bytes())[64 - num_bits..64]
.iter()
.map(|b| *b != 0)
.collect::<Vec<_>>()
};
let mut word_bytes_be = (w as u32).to_be_bytes();
for (byte, is_padding) in word_bytes_be.iter_mut().zip(is_paddings) {
*byte = if is_padding { 0 } else { *byte };
}
let word_value = u32::from_le_bytes(word_bytes_be);
let word_value = if is_input {
let mut word_bytes_be = (w as u32).to_be_bytes();
for (byte, is_padding) in word_bytes_be.iter_mut().zip(is_paddings) {
*byte = if is_padding { 0 } else { *byte };
}
u32::from_le_bytes(word_bytes_be)
} else {
0
};
let hash_lo = u128::from_be_bytes(hash_bytes[16..].try_into().unwrap());
let hash_hi = u128::from_be_bytes(hash_bytes[..16].try_into().unwrap());
rows.push(VirtualShaRow {
Expand All @@ -282,7 +287,7 @@ pub fn generate_witnesses_sha256(rows: &mut Vec<VirtualShaRow>, input_bytes: &[u

// Add start rows
let mut add_row_start = |a: u64, e: u64, is_final| {
add_row(0, a, e, is_final, length, [false, false, false, in_padding], zero_hash)
add_row(0, a, e, is_final, length, [false, false, false, in_padding], zero_hash, false)
};
add_row_start(d, h, idx == 0);
add_row_start(c, g, idx == 0);
Expand Down Expand Up @@ -348,6 +353,7 @@ pub fn generate_witnesses_sha256(rows: &mut Vec<VirtualShaRow>, input_bytes: &[u
if round < NUM_WORDS_TO_ABSORB { length } else { 0 },
is_paddings,
zero_hash,
round < NUM_WORDS_TO_ABSORB,
);

// Truncate the newly calculated values
Expand Down Expand Up @@ -378,7 +384,7 @@ pub fn generate_witnesses_sha256(rows: &mut Vec<VirtualShaRow>, input_bytes: &[u

// Add end rows
let mut add_row_end = |a: u64, e: u64| {
add_row(0, a, e, false, 0, [false; ABSORB_WIDTH_PER_ROW_BYTES], zero_hash)
add_row(0, a, e, false, 0, [false; ABSORB_WIDTH_PER_ROW_BYTES], zero_hash, false)
};
add_row_end(hs[3], hs[7]);
add_row_end(hs[2], hs[6]);
Expand All @@ -391,6 +397,7 @@ pub fn generate_witnesses_sha256(rows: &mut Vec<VirtualShaRow>, input_bytes: &[u
length,
[false, false, false, in_padding],
hash_bytes,
false,
);

// Now truncate the results
Expand Down