Skip to content
32 changes: 16 additions & 16 deletions ext/crates/fp/src/vector/fp_wrapper/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ use crate::field::Field;

impl<F: Field> FqVector<F> {
pub(super) fn scale_helper(&mut self, c: F::ElementContainer) {
self.scale(self.fq.el(c))
self.scale(self.fq().el(c))
}

pub(super) fn entry_helper(&self, index: usize) -> F::ElementContainer {
self.entry(index).val()
}

pub(super) fn set_entry_helper(&mut self, index: usize, value: F::ElementContainer) {
self.set_entry(index, self.fq.el(value))
self.set_entry(index, self.fq().el(value))
}

pub(super) fn add_helper(&mut self, other: &Self, c: F::ElementContainer) {
self.add(other, self.fq.el(c))
self.add(other, self.fq().el(c))
}

pub(super) fn add_offset_helper(
Expand All @@ -40,23 +40,23 @@ impl<F: Field> FqVector<F> {
c: F::ElementContainer,
offset: usize,
) {
self.add_offset(other, self.fq.el(c), offset)
self.add_offset(other, self.fq().el(c), offset)
}

pub(super) fn add_basis_element_helper(&mut self, index: usize, value: F::ElementContainer) {
self.add_basis_element(index, self.fq.el(value))
self.add_basis_element(index, self.fq().el(value))
}

pub(super) fn copy_from_slice_helper(&mut self, other: &[F::ElementContainer]) {
self.copy_from_slice(&other.iter().map(|x| self.fq.el(x.clone())).collect_vec())
self.copy_from_slice(&other.iter().map(|x| self.fq().el(x.clone())).collect_vec())
}

pub(super) fn add_truncate_helper(
&mut self,
other: &Self,
c: F::ElementContainer,
) -> Option<()> {
self.add_truncate(other, self.fq.el(c))
self.add_truncate(other, self.fq().el(c))
}

pub(super) fn add_carry_helper<T>(
Expand All @@ -68,7 +68,7 @@ impl<F: Field> FqVector<F> {
where
for<'a> &'a mut T: TryInto<&'a mut Self>,
{
self.add_carry(other, self.fq.el(c), rest)
self.add_carry(other, self.fq().el(c), rest)
}

pub(super) fn first_nonzero_helper(&self) -> Option<(usize, F::ElementContainer)> {
Expand All @@ -88,11 +88,11 @@ impl<F: Field> FqSlice<'_, F> {

impl<F: Field> FqSliceMut<'_, F> {
pub(super) fn scale_helper(&mut self, c: F::ElementContainer) {
self.scale(self.fq.el(c))
self.scale(self.fq().el(c))
}

pub(super) fn add_helper(&mut self, other: FqSlice<F>, c: F::ElementContainer) {
self.add(other, self.fq.el(c))
self.add(other, self.fq().el(c))
}

pub(super) fn add_offset_helper(
Expand All @@ -101,15 +101,15 @@ impl<F: Field> FqSliceMut<'_, F> {
c: F::ElementContainer,
offset: usize,
) {
self.add_offset(other, self.fq.el(c), offset)
self.add_offset(other, self.fq().el(c), offset)
}

pub(super) fn set_entry_helper(&mut self, index: usize, value: F::ElementContainer) {
self.set_entry(index, self.fq.el(value))
self.set_entry(index, self.fq().el(value))
}

pub(super) fn add_basis_element_helper(&mut self, index: usize, value: F::ElementContainer) {
self.add_basis_element(index, self.fq.el(value))
self.add_basis_element(index, self.fq().el(value))
}

pub(super) fn add_masked_helper(
Expand All @@ -118,7 +118,7 @@ impl<F: Field> FqSliceMut<'_, F> {
c: F::ElementContainer,
mask: &[usize],
) {
self.add_masked(other, self.fq.el(c), mask)
self.add_masked(other, self.fq().el(c), mask)
}

pub(super) fn add_unmasked_helper(
Expand All @@ -127,7 +127,7 @@ impl<F: Field> FqSliceMut<'_, F> {
c: F::ElementContainer,
mask: &[usize],
) {
self.add_unmasked(other, self.fq.el(c), mask)
self.add_unmasked(other, self.fq().el(c), mask)
}

pub(super) fn add_tensor_helper(
Expand All @@ -137,7 +137,7 @@ impl<F: Field> FqSliceMut<'_, F> {
left: FqSlice<F>,
right: FqSlice<F>,
) {
self.add_tensor(offset, self.fq.el(coeff), left, right)
self.add_tensor(offset, self.fq().el(coeff), left, right)
}
}

Expand Down
73 changes: 30 additions & 43 deletions ext/crates/fp/src/vector/impl_fqslice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,16 @@ use crate::{
// Public methods

impl<'a, F: Field> FqSlice<'a, F> {
pub(crate) fn new(fq: F, limbs: &'a [Limb], start: usize, end: usize) -> Self {
FqSlice {
fq,
limbs,
start,
end,
}
}

pub fn prime(&self) -> ValidPrime {
self.fq.characteristic().to_dyn()
self.fq().characteristic().to_dyn()
}

pub fn len(&self) -> usize {
self.end - self.start
self.end() - self.start()
}

pub const fn is_empty(&self) -> bool {
self.start == self.end
self.start() == self.end()
}

pub fn entry(&self, index: usize) -> FieldElement<F> {
Expand All @@ -42,12 +33,12 @@ impl<'a, F: Field> FqSlice<'a, F> {
index,
self.len()
);
let bit_mask = self.fq.bitmask();
let limb_index = self.fq.limb_bit_index_pair(index + self.start);
let mut result = self.limbs[limb_index.limb];
let bit_mask = self.fq().bitmask();
let limb_index = self.fq().limb_bit_index_pair(index + self.start());
let mut result = self.limbs()[limb_index.limb];
result >>= limb_index.bit_index;
result &= bit_mask;
self.fq.decode(result)
self.fq().decode(result)
}

/// TODO: implement prime 2 version
Expand All @@ -69,15 +60,15 @@ impl<'a, F: Field> FqSlice<'a, F> {
return true;
}
let (min_mask, max_mask) = self.limb_masks();
if self.limbs[limb_range.start] & min_mask != 0 {
if self.limbs()[limb_range.start] & min_mask != 0 {
return false;
}

let inner_range = self.limb_range_inner();
if !inner_range.is_empty() && self.limbs[inner_range].iter().any(|&x| x != 0) {
if !inner_range.is_empty() && self.limbs()[inner_range].iter().any(|&x| x != 0) {
return false;
}
if self.limbs[limb_range.end - 1] & max_mask != 0 {
if self.limbs()[limb_range.end - 1] & max_mask != 0 {
return false;
}
true
Expand All @@ -87,24 +78,24 @@ impl<'a, F: Field> FqSlice<'a, F> {
pub fn slice(self, start: usize, end: usize) -> Self {
assert!(start <= end && end <= self.len());

FqSlice {
fq: self.fq,
limbs: self.limbs,
start: self.start + start,
end: self.start + end,
}
FqSlice::new(
self.fq(),
self.into_limbs(),
self.start() + start,
self.start() + end,
)
}

/// Converts a slice to an owned FqVector. This is vastly more efficient if the start of the vector is aligned.
#[must_use]
pub fn to_owned(self) -> FqVector<F> {
let mut new = FqVector::new(self.fq, self.len());
if self.start.is_multiple_of(self.fq.entries_per_limb()) {
let mut new = FqVector::new(self.fq(), self.len());
if self.start().is_multiple_of(self.fq().entries_per_limb()) {
let limb_range = self.limb_range();
new.limbs[0..limb_range.len()].copy_from_slice(&self.limbs[limb_range]);
if !new.limbs.is_empty() {
let len = new.limbs.len();
new.limbs[len - 1] &= self.limb_masks().1;
new.limbs_mut()[0..limb_range.len()].copy_from_slice(&self.limbs()[limb_range]);
if !new.limbs().is_empty() {
let len = new.limbs().len();
new.limbs_mut()[len - 1] &= self.limb_masks().1;
}
} else {
new.as_slice_mut().assign(self);
Expand All @@ -115,23 +106,19 @@ impl<'a, F: Field> FqSlice<'a, F> {

// Limb methods
impl<F: Field> FqSlice<'_, F> {
pub(crate) fn limbs(&self) -> &[Limb] {
self.limbs
}

#[inline]
pub(super) fn offset(&self) -> usize {
let bit_length = self.fq.bit_length();
let entries_per_limb = self.fq.entries_per_limb();
(self.start % entries_per_limb) * bit_length
let bit_length = self.fq().bit_length();
let entries_per_limb = self.fq().entries_per_limb();
(self.start() % entries_per_limb) * bit_length
}

#[inline]
pub(super) fn limb_range(&self) -> std::ops::Range<usize> {
self.fq.range(self.start, self.end)
self.fq().range(self.start(), self.end())
}

/// This function underflows if `self.end == 0`, which happens if and only if we are taking a
/// This function underflows if `self.end() == 0`, which happens if and only if we are taking a
/// slice of width 0 at the start of an `FpVector`. This should be a very rare edge case.
/// Dealing with the underflow properly would probably require using `saturating_sub` or
/// something of that nature, and that has a nontrivial (10%) performance hit.
Expand All @@ -148,8 +135,8 @@ impl<F: Field> FqSlice<'_, F> {

#[inline(always)]
pub(super) fn max_limb_mask(&self) -> Limb {
let num_entries = 1 + (self.end - 1) % self.fq.entries_per_limb();
let bit_max = num_entries * self.fq.bit_length();
let num_entries = 1 + (self.end() - 1) % self.fq().entries_per_limb();
let bit_max = num_entries * self.fq().bit_length();

(!0) >> (constants::BITS_PER_LIMB - bit_max)
}
Expand All @@ -169,7 +156,7 @@ impl<F: Field> FqSlice<'_, F> {

impl<'a, F: Field> From<&'a FqVector<F>> for FqSlice<'a, F> {
fn from(v: &'a FqVector<F>) -> Self {
v.slice(0, v.len)
v.slice(0, v.len())
}
}

Expand Down
Loading