use slop_air::AirBuilder;
use slop_algebra::{AbstractField, Field};
use sp1_core_executor::{events::ByteRecord, ByteOpcode};
use sp1_derive::AlignedBorrow;
use sp1_hypercube::air::SP1AirBuilder;
use sp1_primitives::consts::u32_to_u16_limbs;
#[derive(AlignedBorrow, Default, Debug, Clone, Copy)]
#[repr(C)]
pub struct FixedRotateRightOperation<T> {
pub value: [T; 2],
pub higher_limb: [T; 2],
}
impl<F: Field> FixedRotateRightOperation<F> {
pub const fn nb_limbs_to_rotate(rotation: usize) -> usize {
rotation / 16
}
pub const fn nb_bits_to_rotate(rotation: usize) -> usize {
rotation % 16
}
pub const fn carry_multiplier(rotation: usize) -> u32 {
let nb_bits_to_rotate = Self::nb_bits_to_rotate(rotation);
1 << (16 - nb_bits_to_rotate)
}
pub fn populate(&mut self, record: &mut impl ByteRecord, input: u32, rotation: usize) -> u32 {
let input_limbs = u32_to_u16_limbs(input);
let expected = input.rotate_right(rotation as u32);
self.value = [
F::from_canonical_u16((expected & 0xFFFF) as u16),
F::from_canonical_u16((expected >> 16) as u16),
];
let nb_limbs_to_rotate = Self::nb_limbs_to_rotate(rotation);
let nb_bits_to_rotate = Self::nb_bits_to_rotate(rotation);
let input_limbs_rotated =
[input_limbs[nb_limbs_to_rotate % 2], input_limbs[(1 + nb_limbs_to_rotate) % 2]];
for i in (0..2).rev() {
let limb = input_limbs_rotated[i];
let lower_limb = (limb & ((1 << nb_bits_to_rotate) - 1)) as u16;
let higher_limb = (limb >> nb_bits_to_rotate) as u16;
self.higher_limb[i] = F::from_canonical_u16(higher_limb);
record.add_bit_range_check(lower_limb, nb_bits_to_rotate as u8);
record.add_bit_range_check(higher_limb, (16 - nb_bits_to_rotate) as u8);
}
expected
}
pub fn eval<AB: SP1AirBuilder>(
builder: &mut AB,
input: [AB::Var; 2],
rotation: usize,
cols: FixedRotateRightOperation<AB::Var>,
is_real: AB::Var,
) {
builder.assert_bool(is_real);
let nb_limbs_to_rotate = Self::nb_limbs_to_rotate(rotation);
let nb_bits_to_rotate = Self::nb_bits_to_rotate(rotation);
let carry_multiplier = AB::F::from_canonical_u32(Self::carry_multiplier(rotation));
let input_limbs_rotated =
[input[nb_limbs_to_rotate % 2], input[(1 + nb_limbs_to_rotate) % 2]];
let mut lower_limb = [AB::Expr::zero(), AB::Expr::zero()];
for i in 0..2 {
let limb = input_limbs_rotated[i];
lower_limb[i] =
limb - cols.higher_limb[i] * AB::Expr::from_canonical_u32(1 << nb_bits_to_rotate);
builder.send_byte(
AB::F::from_canonical_u32(ByteOpcode::Range as u32),
lower_limb[i].clone(),
AB::F::from_canonical_u32(nb_bits_to_rotate as u32),
AB::Expr::zero(),
is_real,
);
builder.send_byte(
AB::F::from_canonical_u32(ByteOpcode::Range as u32),
cols.higher_limb[i],
AB::Expr::from_canonical_u32(16 - nb_bits_to_rotate as u32),
AB::Expr::zero(),
is_real,
);
}
builder.when(is_real).assert_eq(
cols.value[1],
cols.higher_limb[1] + lower_limb[0].clone() * carry_multiplier,
);
builder.when(is_real).assert_eq(
cols.value[0],
cols.higher_limb[0] + lower_limb[1].clone() * carry_multiplier,
);
}
}