Skip to content

Commit

Permalink
Merge branch 'minor_fixes' into oneshot
Browse files Browse the repository at this point in the history
  • Loading branch information
richajaindce committed Oct 25, 2023
2 parents 8fa6745 + 0c5f464 commit c348906
Show file tree
Hide file tree
Showing 8 changed files with 520 additions and 98 deletions.
21 changes: 18 additions & 3 deletions src/ff/galois_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use bitvec::prelude::{bitarr, BitArr, Lsb0};
use generic_array::GenericArray;
use typenum::{Unsigned, U1, U3, U4, U5};
use typenum::{Unsigned, U1, U2, U3, U4, U5};

use crate::{
ff::{Field, Serializable},
Expand All @@ -25,6 +25,7 @@ pub trait GaloisField:

// Bit store type definitions
type U8_1 = BitArr!(for 8, in u8, Lsb0);
type U8_2 = BitArr!(for 9, in u8, Lsb0);
type U8_3 = BitArr!(for 24, in u8, Lsb0);
type U8_4 = BitArr!(for 32, in u8, Lsb0);
type U8_5 = BitArr!(for 40, in u8, Lsb0);
Expand All @@ -33,6 +34,10 @@ impl Block for U8_1 {
type Size = U1;
}

impl Block for U8_2 {
type Size = U2;
}

impl Block for U8_3 {
type Size = U3;
}
Expand Down Expand Up @@ -561,8 +566,8 @@ bit_array_impl!(
U8_3,
20,
bitarr!(const u8, Lsb0; 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
// x^20 + x^17 + x^15 + x^14 + x^11 + x^10 + x^9 + x^7 + x^6 + x^5 + 1
0b1_0010_1100_1110_1110_0001_u128
// x^20 + x^7 + x^3 + x^2 + 1
0b1000_0000_0000_1000_1101_u128
);

bit_array_impl!(
Expand All @@ -575,6 +580,16 @@ bit_array_impl!(
0b1_0001_1011_u128
);

bit_array_impl!(
bit_array_9,
Gf9Bit,
U8_2,
9,
bitarr!(const u8, Lsb0; 1, 0, 0, 0, 0, 0, 0, 0, 0),
// x^9 + x^4 + x^3 + x + 1
0b10_0001_1011_u128
);

bit_array_impl!(
bit_array_5,
Gf5Bit,
Expand Down
4 changes: 3 additions & 1 deletion src/ff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ mod prime_field;
use std::ops::{Add, AddAssign, Sub, SubAssign};

pub use field::{Field, FieldType};
pub use galois_field::{GaloisField, Gf2, Gf20Bit, Gf32Bit, Gf3Bit, Gf40Bit, Gf5Bit, Gf8Bit};
pub use galois_field::{
GaloisField, Gf2, Gf20Bit, Gf32Bit, Gf3Bit, Gf40Bit, Gf5Bit, Gf8Bit, Gf9Bit,
};
use generic_array::{ArrayLength, GenericArray};
#[cfg(any(test, feature = "weak-field"))]
pub use prime_field::Fp31;
Expand Down
6 changes: 3 additions & 3 deletions src/protocol/boolean/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ipa_macros::Step;
use super::or::or;
use crate::{
error::Error,
ff::PrimeField,
ff::{Field, PrimeField},
protocol::{
boolean::random_bits_generator::RandomBitsGenerator,
context::{Context, UpgradedContext},
Expand Down Expand Up @@ -205,7 +205,7 @@ pub async fn bitwise_less_than_constant<F, C, S>(
c: u128,
) -> Result<S, Error>
where
F: PrimeField,
F: Field,
C: Context,
S: LinearSecretSharing<F> + BasicProtocols<C, F>,
for<'a> &'a S: LinearRefOps<'a, S, F>,
Expand Down Expand Up @@ -236,7 +236,7 @@ async fn first_differing_bit<F, C, S>(
b: u128,
) -> Result<Vec<S>, Error>
where
F: PrimeField,
F: Field,
C: Context,
S: LinearSecretSharing<F> + BasicProtocols<C, F>,
for<'a> &'a S: LinearRefOps<'a, S, F>,
Expand Down
6 changes: 4 additions & 2 deletions src/protocol/boolean/saturating_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<S: LinearSecretSharing<Gf2>> SaturatingSum<S> {
/// If one of the multiplications errors
///
/// # Panics
/// If something try to add a bit decomposed value larger than this `SaturatingSum` can accomodate
/// If something try to add a bit decomposed value larger than this `SaturatingSum` can accommodate
pub async fn add<C>(
&self,
ctx: C,
Expand Down Expand Up @@ -175,7 +175,9 @@ where
///
/// If `compute_carry_out` is set to `true`, then the mutable refernce to `carry_in` is mutated to take on the value of the `carry_out` bit
///
async fn one_bit_subtractor<C, SB>(
/// # Errors
/// If one of the multiplications errors
pub async fn one_bit_subtractor<C, SB>(
ctx: C,
record_id: RecordId,
x: &SB,
Expand Down
19 changes: 13 additions & 6 deletions src/protocol/prf_sharding/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ pub enum BucketStep {
Bit(usize),
}

impl From<u32> for BucketStep {
fn from(v: u32) -> Self {
Self::Bit(usize::try_from(v).unwrap())
impl TryFrom<u32> for BucketStep {
type Error = String;

fn try_from(v: u32) -> Result<Self, Self::Error> {
let val = usize::try_from(v);
let val = match val {
Ok(val) => Self::Bit(val),
Err(error) => panic!("{error:?}"),
};
Ok(val)
}
}

Expand Down Expand Up @@ -114,7 +121,7 @@ pub mod tests {
use rand::thread_rng;

use crate::{
ff::{Field, Fp32BitPrime, Gf8Bit},
ff::{Field, Fp32BitPrime, Gf8Bit, Gf9Bit},
protocol::{
context::{Context, UpgradableContext, Validator},
prf_sharding::bucket::move_single_value_to_bucket,
Expand Down Expand Up @@ -225,7 +232,7 @@ pub mod tests {
#[should_panic]
fn move_out_of_range_too_many_buckets_steps() {
run(move || async move {
let breakdown_key_bits = get_bits::<Fp32BitPrime>(0, Gf8Bit::BITS);
let breakdown_key_bits = get_bits::<Fp32BitPrime>(0, Gf9Bit::BITS);
let value = Fp32BitPrime::truncate_from(VALUE);

_ = TestWorld::default()
Expand All @@ -234,7 +241,7 @@ pub mod tests {
|ctx, (breakdown_key_share, value_share)| async move {
let validator = ctx.validator();
let ctx = validator.context();
move_single_value_to_bucket::<Gf8Bit, _, _, Fp32BitPrime>(
move_single_value_to_bucket::<Gf9Bit, _, _, Fp32BitPrime>(
ctx.set_total_records(1),
RecordId::from(0),
breakdown_key_share,
Expand Down
Loading

0 comments on commit c348906

Please sign in to comment.