Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleiserson committed Jan 26, 2024
1 parent 14b87da commit 6f5feda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ipa-core/src/ff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub trait Serializable: Sized {

pub trait ArrayAccess {
type Output;
type Iter<'a>: Iterator<Item = Self::Output> + ExactSizeIterator + Send
type Iter<'a>: ExactSizeIterator<Item = Self::Output> + Send
where
Self: 'a;

Expand Down
32 changes: 30 additions & 2 deletions ipa-core/src/secret_sharing/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ use crate::{
secret_sharing::{FieldArray, SharedValue, SharedValueArray},
};

/// Wrapper around `[V; N]`.
///
/// This wrapper serves two purposes:
/// * It enables us to implement the `std::ops` traits, which the coherence rules
/// don't let us implement for `[V; N]`.
/// * It disables by-index access to individual elements of the array, which
/// should never be necessary in properly vectorized code.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StdArray<V: SharedValue, const N: usize>([V; N]);

Expand Down Expand Up @@ -76,14 +83,16 @@ impl<V: SharedValue, const N: usize> TryFrom<Vec<V>> for StdArray<V, N> {
// Panics if the iterator terminates before producing N items.
impl<V: SharedValue, const N: usize> FromIterator<V> for StdArray<V, N>
where
Self: Serializable,
Self: Serializable, // required for `<Self as SharedValueArray>::ZERO`
{
fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self {
let mut res = Self::ZERO;
let mut iter = iter.into_iter();

for i in 0..N {
res.0[i] = iter.next().unwrap();
res.0[i] = iter
.next()
.unwrap_or_else(|| panic!("Expected iterator to produce {N} items, got only {i}"));
}

res
Expand Down Expand Up @@ -316,6 +325,8 @@ impl<V: SharedValue, const N: usize> Message for StdArray<V, N> where Self: Seri

#[cfg(all(test, unit_test))]
mod test {
use std::iter;

use proptest::{
prelude::{prop, Arbitrary, Strategy},
proptest,
Expand Down Expand Up @@ -408,6 +419,23 @@ mod test {
assert_eq!(copy, expected);
}

#[test]
#[allow(clippy::from_iter_instead_of_collect)]
fn from_iter(a: StdArray<Fp32BitPrime, 32>) {
let iter = a.0.iter().copied();
let copy = StdArray::<Fp32BitPrime, 32>::from_iter(iter);
assert_eq!(copy, a);
}
}

#[test]
#[should_panic(expected = "Expected iterator to produce 32 items, got only 0")]
#[allow(clippy::from_iter_instead_of_collect)]
fn from_short_iter() {
StdArray::<Fp32BitPrime, 32>::from_iter(iter::empty());
}

proptest! {
#[test]
fn get_set(mut a: StdArray<Fp32BitPrime, 1>, b: Fp32BitPrime, c: Fp32BitPrime) {
assert_eq!(a.get(0), a.0[0]);
Expand Down

0 comments on commit 6f5feda

Please sign in to comment.