Skip to content

Commit

Permalink
fix: stash code
Browse files Browse the repository at this point in the history
  • Loading branch information
eigmax committed Nov 26, 2023
1 parent dd730d1 commit 1e8b75a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/cpu/columns/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub(crate) struct CpuIOView<T: Copy> {
pub(crate) rs_le: [T; 32],
pub(crate) rt_le: [T; 32],
pub(crate) mem_le: [T; 32],
// Offset to shift, must be constant
pub(crate) shift: T,
}

Expand Down
60 changes: 36 additions & 24 deletions src/cpu/memio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ fn sign_extend_ext<F: RichField + Extendable<D>, const D: usize, const N: usize>
}
}

/// Binary shift limbs by offset
/// Binary shift limbs by offset. Indicate out = limbs >> offset, out
///
#[inline]
fn shift<P: PackedField>(limbs: &mut [P; 32], offset: &P, left: bool) {
let mut tmp = limbs.clone();
Expand All @@ -85,28 +86,11 @@ fn shift<P: PackedField>(limbs: &mut [P; 32], offset: &P, left: bool) {

/// Binary shift limbs by offset
#[inline]
fn shift_ext<F: RichField + Extendable<D>, const D: usize>(builder: &mut CircuitBuilder<F, D>, limbs: &mut [ExtensionTarget<D>; 32], offset: &F, left: bool) {
fn shift_ext<F: RichField + Extendable<D>, const D: usize>(builder: &mut CircuitBuilder<F, D>, limbs: &mut [ExtensionTarget<D>; 32], offset: &ExtensionTarget<D>, left: bool) {
let zeros = builder.zero_extension();
let mut tmp = limbs.clone();
let offset = F::as_slice(offset)[0].to_string();
let offset: usize = offset.parse().unwrap();
if left {
for i in 0..32 {
if i + offset < 32 {
limbs[i] = tmp[i+offset];
} else {
limbs[i] = zeros;
}
}
} else {
for i in 0..32 {
if i - offset >= 0 {
limbs[i] = tmp[i - offset];
} else {
limbs[i] = zeros;
}
}
}
let shift = builder.target_as_constant_ext(*offset).unwrap();
let shift = shift.as_slice()[0];
}

#[inline]
Expand Down Expand Up @@ -243,8 +227,9 @@ fn eval_packed_load<P: PackedField>(
let mut mem_tmp = lv.general.io().mem_le.clone();

rs_tmp[30] = P::ONES - rs_tmp[30];
shift(&mut rs_tmp, & {P::Scalar::from_canonical_u32(16).into()}, true);
shift(&mut rs_tmp, &{P::Scalar::from_canonical_u32(4).into()}, true);
shift(&mut mem_tmp, &limb_from_bits_le(rs_tmp.into_iter()), false);

and(&mut mem_tmp, &bits_ffff);
sign_extend::<_, 16>(&mut mem_tmp);
let mem_value = limb_from_bits_le(mem_tmp.into_iter());
Expand Down Expand Up @@ -336,8 +321,8 @@ fn eval_ext_circuit_load<F: RichField + Extendable<D>, const D: usize>(
let mut mem_tmp = lv.general.io().mem_le.clone();

rs_tmp[30] = builder.sub_extension(ones, rs_tmp[30]);
let _16 = F::from_canonical_u32(16);
shift_ext(builder, &mut rs_tmp, &_16, true);
let _4 = builder.constant_extension(F::Extension::from_canonical_u32(4));
shift_ext(builder, &mut rs_tmp, &_4, true);
let tmp = limb_from_bits_le_recursive(builder, rs_tmp.into_iter());
shift_ext(builder, &mut mem_tmp, &tmp, false);

Expand Down Expand Up @@ -600,6 +585,11 @@ mod tests {
use plonky2::field::types::Field;
use plonky2::plonk::config::{GenericConfig, PoseidonGoldilocksConfig};
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::iop::target::{BoolTarget, Target};

#[test]
fn test_binary_shift() {
Expand All @@ -608,4 +598,26 @@ mod tests {
let offset = GoldilocksField::from_canonical_u32(31);
shift(&mut bin, &offset, true);
}

#[test]
fn test_binary_shift_ext() {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;

let mut bin = [GoldilocksField::ONE; 32];
bin[31] = GoldilocksField::ONE;
let offset = GoldilocksField::from_canonical_u32(31);

let config = CircuitConfig::standard_ecc_config();
let mut builder: CircuitBuilder<F, D> = CircuitBuilder::new(config);

let offset = builder.constant_extension(F::from_canonical_u16(16).into());
let shift: <C as GenericConfig<D>>::FE = builder.target_as_constant_ext(offset).unwrap();
let shift = shift.as_slice()[0].0[0].to_string();
println!("shift: {:?}", shift);
let shift: u32 = shift.parse().unwrap();
println!("shift: {:?}", shift);
//shift_ext(&mut builder, &mut bin.into(), &offset.into(), true);
}
}

0 comments on commit 1e8b75a

Please sign in to comment.