Skip to content

Commit

Permalink
chore: refactor code.
Browse files Browse the repository at this point in the history
  • Loading branch information
VanhGer committed Feb 3, 2025
1 parent 4a41b74 commit 072ddfa
Show file tree
Hide file tree
Showing 24 changed files with 1,701 additions and 859 deletions.
60 changes: 41 additions & 19 deletions emulator/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,43 +541,58 @@ impl InstrumentedState {
log::debug!("syscall {} {} {} {}", syscall_num, a0, a1, a2);

match syscall_num {
0x300105 => { // SHA_EXTEND
0x300105 => {
// SHA_EXTEND
let w_ptr = a0;
assert!(a1 == 0, "arg2 must be 0");

for i in 16..64 {
// Read w[i-15].
let w_i_minus_15 = self.state.memory.get_memory(w_ptr + (i - 15) * 4);
let w_i_minus_15 = self.state.memory.get_memory(w_ptr + (i - 15) * 4);
// Compute `s0`.
let s0 =
w_i_minus_15.rotate_right(7) ^ w_i_minus_15.rotate_right(18) ^ (w_i_minus_15 >> 3);
let s0 = w_i_minus_15.rotate_right(7)
^ w_i_minus_15.rotate_right(18)
^ (w_i_minus_15 >> 3);

// Read w[i-2].
let w_i_minus_2 = self.state.memory.get_memory(w_ptr + (i - 2) * 4);
let w_i_minus_2 = self.state.memory.get_memory(w_ptr + (i - 2) * 4);
// Compute `s1`.
let s1 =
w_i_minus_2.rotate_right(17) ^ w_i_minus_2.rotate_right(19) ^ (w_i_minus_2 >> 10);
let s1 = w_i_minus_2.rotate_right(17)
^ w_i_minus_2.rotate_right(19)
^ (w_i_minus_2 >> 10);

// Read w[i-16].
let w_i_minus_16 = self.state.memory.get_memory(w_ptr + (i - 16) * 4);
let w_i_minus_16 = self.state.memory.get_memory(w_ptr + (i - 16) * 4);

// Read w[i-7].
let w_i_minus_7 = self.state.memory.get_memory(w_ptr + (i - 7) * 4);
let w_i_minus_7 = self.state.memory.get_memory(w_ptr + (i - 7) * 4);

// Compute `w_i`.
let w_i = s1.wrapping_add(w_i_minus_16).wrapping_add(s0).wrapping_add(w_i_minus_7);
let w_i = s1
.wrapping_add(w_i_minus_16)
.wrapping_add(s0)
.wrapping_add(w_i_minus_7);

// Write w[i].
log::debug!("{:X}, {:X}, {:X} {:X} {:X} {:X}", s1, s0, w_i_minus_16, w_i_minus_7, w_i_minus_15, w_i_minus_2);
log::debug!(
"{:X}, {:X}, {:X} {:X} {:X} {:X}",
s1,
s0,
w_i_minus_16,
w_i_minus_7,
w_i_minus_15,
w_i_minus_2
);
self.state.memory.set_memory(w_ptr + i * 4, w_i);
log::debug!("extend write {:X} {:X}", w_ptr + i * 4, w_i);
}
},
0x010106 => { // SHA_COMPRESS
}
0x010106 => {
// SHA_COMPRESS
let w_ptr = a0;
let h_ptr = a1;
let mut hx = [0u32; 8];
for i in 0..8 {
for i in 0..hx.len() {
hx[i] = self.state.memory.get_memory(h_ptr + i as u32 * 4);
}

Expand Down Expand Up @@ -617,11 +632,18 @@ impl InstrumentedState {
// Execute the "finalize" phase.
let v = [a, b, c, d, e, f, g, h];
for i in 0..8 {
self.state.memory.set_memory(h_ptr + i as u32 * 4, hx[i].wrapping_add(v[i]));
log::debug!("write {:X} {:X}", h_ptr + i as u32 * 4, hx[i].wrapping_add(v[i]));
self.state
.memory
.set_memory(h_ptr + i as u32 * 4, hx[i].wrapping_add(v[i]));
log::debug!(
"write {:X} {:X}",
h_ptr + i as u32 * 4,
hx[i].wrapping_add(v[i])
);
}
},
0x010109 => { //keccak
}
0x010109 => {
//keccak
assert!((a0 & 3) == 0);
assert!((a2 & 3) == 0);
let bytes = (0..a1)
Expand Down Expand Up @@ -664,7 +686,7 @@ impl InstrumentedState {
log::debug!("input: {:?}", vec);
assert_eq!(a0 % 4, 0, "hint read address not aligned to 4 bytes");
if a1 >= 1 {
self.state.cycle += (a1 as u64 + 31) / 32;
self.state.cycle += (a1 as u64).div_ceil(32);
}
for i in (0..a1).step_by(4) {
// Get each byte in the chunk
Expand Down
11 changes: 7 additions & 4 deletions prover/src/all_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ use crate::poseidon_sponge::poseidon_sponge_stark::PoseidonSpongeStark;
use crate::sha_compress::sha_compress_stark;
use crate::sha_compress::sha_compress_stark::ShaCompressStark;
use crate::sha_compress_sponge::sha_compress_sponge_stark;
use crate::sha_compress_sponge::sha_compress_sponge_stark::{ShaCompressSpongeStark, SHA_COMPRESS_SPONGE_READ_BITS};
use crate::sha_compress_sponge::sha_compress_sponge_stark::{
ShaCompressSpongeStark, SHA_COMPRESS_SPONGE_READ_BITS,
};
use crate::sha_extend::sha_extend_stark;
use crate::sha_extend::sha_extend_stark::ShaExtendStark;
use crate::sha_extend_sponge::columns::SHA_EXTEND_SPONGE_READ_BITS;
Expand Down Expand Up @@ -81,9 +83,11 @@ impl<F: RichField + Extendable<D>, const D: usize> AllStark<F, D> {
self.keccak_stark.num_lookup_helper_columns(config),
self.keccak_sponge_stark.num_lookup_helper_columns(config),
self.sha_extend_stark.num_lookup_helper_columns(config),
self.sha_extend_sponge_stark.num_lookup_helper_columns(config),
self.sha_extend_sponge_stark
.num_lookup_helper_columns(config),
self.sha_compress_stark.num_lookup_helper_columns(config),
self.sha_compress_sponge_stark.num_lookup_helper_columns(config),
self.sha_compress_sponge_stark
.num_lookup_helper_columns(config),
self.logic_stark.num_lookup_helper_columns(config),
self.memory_stark.num_lookup_helper_columns(config),
]
Expand Down Expand Up @@ -292,7 +296,6 @@ fn ctl_sha_extend_sponge<F: Field>() -> CrossTableLookup<F> {
CrossTableLookup::new(vec![cpu_looking], sha_extend_sponge_looked)
}


fn ctl_sha_compress_inputs<F: Field>() -> CrossTableLookup<F> {
let sha_compress_sponge_looking = TableWithColumns::new(
Table::ShaCompressSponge,
Expand Down
8 changes: 4 additions & 4 deletions prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ pub mod poseidon_sponge;
pub mod proof;
pub mod prover;
pub mod recursive_verifier;
pub mod sha_compress;
pub mod sha_compress_sponge;
pub mod sha_extend;
pub mod sha_extend_sponge;
pub mod stark;
pub mod stark_testing;
pub mod util;
pub mod vanishing_poly;
pub mod verifier;
pub mod witness;
pub mod sha_extend;
pub mod sha_extend_sponge;
pub mod sha_compress;
pub mod sha_compress_sponge;
45 changes: 21 additions & 24 deletions prover/src/sha_compress/columns.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::util::{indices_arr, transmute_no_compile_time_size_checks};
use std::borrow::{Borrow, BorrowMut};
use std::intrinsics::transmute;
use crate::util::{indices_arr, transmute_no_compile_time_size_checks};
#[derive(Clone)]
pub(crate) struct ShaCompressColumnsView<T: Copy> {


/// input state: a,b,c,d,e,f,g,h in binary form
pub input_state: [T; 256],
/// Out
Expand All @@ -20,37 +18,36 @@ pub(crate) struct ShaCompressColumnsView<T: Copy> {
pub s_1: [T; 32],
pub e_and_f: [T; 32],
pub not_e_and_g: [T; 32],
pub ch: [T;32],
pub ch: [T; 32],
// h.wrapping_add(s1)
pub inter_1: [T;32],
pub carry_1: [T;32],
pub inter_1: [T; 32],
pub carry_1: [T; 32],
// inter_1.wrapping_add(ch)
pub inter_2: [T;32],
pub carry_2: [T;32],
pub inter_2: [T; 32],
pub carry_2: [T; 32],
// inter_2.wrapping_add(SHA_COMPRESS_K[i])
pub inter_3: [T;32],
pub carry_3: [T;32],
pub inter_3: [T; 32],
pub carry_3: [T; 32],
// inter_3.wrapping_add(w_i)
pub temp1: [T;32],
pub carry_4: [T;32],

pub a_rr_2: [T;32],
pub a_rr_13: [T;32],
pub a_rr_22: [T;32],
pub s_0: [T;32],
pub a_and_b: [T;32],
pub a_and_c: [T;32],
pub b_and_c: [T;32],
pub maj: [T;32],
pub temp2: [T;32],
pub carry_5: [T;32],
pub temp1: [T; 32],
pub carry_4: [T; 32],

pub a_rr_2: [T; 32],
pub a_rr_13: [T; 32],
pub a_rr_22: [T; 32],
pub s_0: [T; 32],
pub a_and_b: [T; 32],
pub a_and_c: [T; 32],
pub b_and_c: [T; 32],
pub maj: [T; 32],
pub temp2: [T; 32],
pub carry_5: [T; 32],
pub carry_a: [T; 32],
pub carry_e: [T; 32],

/// The timestamp at which inputs should be read from memory.
pub timestamp: T,
pub is_normal_round: T,

}

pub const NUM_SHA_COMPRESS_COLUMNS: usize = size_of::<ShaCompressColumnsView<u8>>();
Expand Down
40 changes: 25 additions & 15 deletions prover/src/sha_compress/logic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::keccak::logic::andn_gen_circuit;
use plonky2::field::extension::Extendable;
use plonky2::field::packed::PackedField;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use crate::keccak::logic::andn_gen_circuit;

pub(crate) fn and_op<F: RichField + Extendable<D>, const D: usize, const N: usize>(
x: [F; N],
y: [F; N]
y: [F; N],
) -> [F; N] {
let mut result = [F::ZERO; N];
for i in 0..N {
Expand All @@ -21,7 +21,7 @@ pub(crate) fn and_op<F: RichField + Extendable<D>, const D: usize, const N: usiz
pub(crate) fn and_op_packed_constraints<P: PackedField, const N: usize>(
x: [P; N],
y: [P; N],
out: [P; N]
out: [P; N],
) -> Vec<P> {
let mut result = vec![];
for i in 0..N {
Expand All @@ -31,15 +31,18 @@ pub(crate) fn and_op_packed_constraints<P: PackedField, const N: usize>(
result
}

pub(crate) fn and_op_ext_circuit_constraints<F: RichField + Extendable<D>, const D: usize, const N: usize>(
pub(crate) fn and_op_ext_circuit_constraints<
F: RichField + Extendable<D>,
const D: usize,
const N: usize,
>(
builder: &mut CircuitBuilder<F, D>,
x: [ExtensionTarget<D>; N],
y: [ExtensionTarget<D>; N],
out: [ExtensionTarget<D>; N]
out: [ExtensionTarget<D>; N],
) -> Vec<ExtensionTarget<D>> {
let mut result = vec![];
for i in 0..N {

let expected_out = builder.mul_extension(x[i], y[i]);
let out_constraint = builder.sub_extension(expected_out, out[i]);
result.push(out_constraint);
Expand All @@ -49,7 +52,7 @@ pub(crate) fn and_op_ext_circuit_constraints<F: RichField + Extendable<D>, const

pub(crate) fn andn_op<F: RichField + Extendable<D>, const D: usize, const N: usize>(
x: [F; N],
y: [F; N]
y: [F; N],
) -> [F; N] {
let mut result = [F::ZERO; N];
for i in 0..N {
Expand All @@ -63,7 +66,7 @@ pub(crate) fn andn_op<F: RichField + Extendable<D>, const D: usize, const N: usi
pub(crate) fn andn_op_packed_constraints<P: PackedField, const N: usize>(
x: [P; N],
y: [P; N],
out: [P; N]
out: [P; N],
) -> Vec<P> {
let mut result = vec![];
for i in 0..N {
Expand All @@ -73,15 +76,18 @@ pub(crate) fn andn_op_packed_constraints<P: PackedField, const N: usize>(
result
}

pub(crate) fn andn_op_ext_circuit_constraints<F: RichField + Extendable<D>, const D: usize, const N: usize>(
pub(crate) fn andn_op_ext_circuit_constraints<
F: RichField + Extendable<D>,
const D: usize,
const N: usize,
>(
builder: &mut CircuitBuilder<F, D>,
x: [ExtensionTarget<D>; N],
y: [ExtensionTarget<D>; N],
out: [ExtensionTarget<D>; N]
out: [ExtensionTarget<D>; N],
) -> Vec<ExtensionTarget<D>> {
let mut result = vec![];
for i in 0..N {

let expected_out = andn_gen_circuit(builder, x[i], y[i]);
let out_constraint = builder.sub_extension(expected_out, out[i]);
result.push(out_constraint);
Expand All @@ -91,7 +97,7 @@ pub(crate) fn andn_op_ext_circuit_constraints<F: RichField + Extendable<D>, cons

pub(crate) fn xor_op<F: RichField + Extendable<D>, const D: usize, const N: usize>(
x: [F; N],
y: [F; N]
y: [F; N],
) -> [F; N] {
let mut result = [F::ZERO; N];
for i in 0..N {
Expand All @@ -113,7 +119,11 @@ pub(crate) fn equal_packed_constraint<P: PackedField, const N: usize>(
result
}

pub(crate) fn equal_ext_circuit_constraints<F: RichField + Extendable<D>, const D: usize, const N: usize>(
pub(crate) fn equal_ext_circuit_constraints<
F: RichField + Extendable<D>,
const D: usize,
const N: usize,
>(
builder: &mut CircuitBuilder<F, D>,
x: [ExtensionTarget<D>; N],
y: [ExtensionTarget<D>; N],
Expand All @@ -126,10 +136,10 @@ pub(crate) fn equal_ext_circuit_constraints<F: RichField + Extendable<D>, const
result
}

pub(crate) fn from_be_bits_to_u32( bits: [u8; 32]) -> u32 {
pub(crate) fn from_be_bits_to_u32(bits: [u8; 32]) -> u32 {
let mut result = 0;
for i in 0..32 {
result |= (bits[i] as u32) << i;
}
result
}
}
2 changes: 1 addition & 1 deletion prover/src/sha_compress/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod columns;
pub mod logic;
pub mod sha_compress_stark;
pub mod logic;
Loading

0 comments on commit 072ddfa

Please sign in to comment.