Skip to content

Commit

Permalink
chore: Cleanup some code in the co-brillig MPC trait
Browse files Browse the repository at this point in the history
  • Loading branch information
rw0x0 committed Nov 28, 2024
1 parent 11e0b03 commit aeae315
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 383 deletions.
6 changes: 2 additions & 4 deletions co-noir/co-brillig/src/mpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ pub trait BrilligDriver<F: PrimeField> {
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType> {
let gt = self.lt(lhs, rhs)?;
self.not(gt)
self.lt(rhs, lhs)
}

/// Checks whether `lhs >= rhs`. The result
Expand All @@ -197,8 +196,7 @@ pub trait BrilligDriver<F: PrimeField> {
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType> {
let gt = self.lt(lhs, rhs)?;
self.not(gt)
self.le(rhs, lhs)
}

/// Converts the provided value to a binary representation, depending
Expand Down
23 changes: 0 additions & 23 deletions co-noir/co-brillig/src/mpc/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,29 +329,6 @@ impl<F: PrimeField> BrilligDriver<F> for PlainBrilligDriver<F> {
}
}

fn gt(
&mut self,
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType> {
match (lhs, rhs) {
(PlainBrilligType::Field(lhs), PlainBrilligType::Field(rhs)) => {
let result = u128::from(lhs > rhs);
Ok(PlainBrilligType::Int(result, IntegerBitSize::U1))
}
(
PlainBrilligType::Int(lhs, lhs_bit_size),
PlainBrilligType::Int(rhs, rhs_bit_size),
) if lhs_bit_size == rhs_bit_size => {
let result = u128::from(lhs > rhs);
Ok(PlainBrilligType::Int(result, IntegerBitSize::U1))
}
x => eyre::bail!(
"type mismatch! Can only do bin ops on same types, but tried with {x:?}"
),
}
}

fn to_radix(
&mut self,
val: Self::BrilligType,
Expand Down
328 changes: 2 additions & 326 deletions co-noir/co-brillig/src/mpc/rep3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,8 @@ impl<F: PrimeField, N: Rep3Network> BrilligDriver<F> for Rep3BrilligDriver<F, N>
cast_ring(rep3_ring_share, integer_bit_size, &mut self.io_context)
}
},
(Rep3BrilligType::Public(public), BitSize::Field) => {
let casted = self.plain_driver.cast(public, bit_size)?;
Ok(Rep3BrilligType::Public(casted))
}
(Rep3BrilligType::Public(public), BitSize::Integer(integer_bit_size)) => {
let casted = self
.plain_driver
.cast(public, BitSize::Integer(integer_bit_size))?;
(Rep3BrilligType::Public(public), bits) => {
let casted = self.plain_driver.cast(public, bits)?;
Ok(Rep3BrilligType::Public(casted))
}
}
Expand Down Expand Up @@ -1249,324 +1243,6 @@ impl<F: PrimeField, N: Rep3Network> BrilligDriver<F> for Rep3BrilligDriver<F, N>
Ok(result)
}

fn gt(
&mut self,
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType> {
let result = match (lhs, rhs) {
(Rep3BrilligType::Public(lhs), Rep3BrilligType::Public(rhs)) => {
let result = self.plain_driver.gt(lhs, rhs)?;
Rep3BrilligType::Public(result)
}
(Rep3BrilligType::Public(public), Rep3BrilligType::Shared(shared)) => {
match (shared, public) {
(Shared::Field(rhs), Public::Field(lhs)) => {
let le = rep3::arithmetic::ge_public_bit(rhs, lhs, &mut self.io_context)?;
let result = !Rep3RingShare::new(
Bit::cast_from_biguint(&le.a),
Bit::cast_from_biguint(&le.b),
);
Rep3BrilligType::shared_u1(result)
}
(Shared::Ring128(rhs), Public::Int(lhs, IntegerBitSize::U128)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::lt_public(
rhs,
lhs.into(),
&mut self.io_context,
)?)
}
(Shared::Ring64(rhs), Public::Int(lhs, IntegerBitSize::U64)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::lt_public(
rhs,
u64::try_from(lhs).expect("must be u64").into(),
&mut self.io_context,
)?)
}
(Shared::Ring32(rhs), Public::Int(lhs, IntegerBitSize::U32)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::lt_public(
rhs,
u32::try_from(lhs).expect("must be u32").into(),
&mut self.io_context,
)?)
}
(Shared::Ring16(rhs), Public::Int(lhs, IntegerBitSize::U16)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::lt_public(
rhs,
u16::try_from(lhs).expect("must be u16").into(),
&mut self.io_context,
)?)
}
(Shared::Ring8(rhs), Public::Int(lhs, IntegerBitSize::U8)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::lt_public(
rhs,
u8::try_from(lhs).expect("must be u8").into(),
&mut self.io_context,
)?)
}
(Shared::Ring1(rhs), Public::Int(lhs, IntegerBitSize::U1)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::lt_public(
rhs,
bit_from_u128!(lhs),
&mut self.io_context,
)?)
}
x => eyre::bail!(
"type mismatch! Can only do bin ops on same types, but tried with {x:?}"
),
}
}
(Rep3BrilligType::Shared(shared), Rep3BrilligType::Public(public)) => {
match (shared, public) {
(Shared::Field(lhs), Public::Field(rhs)) => {
let le = rep3::arithmetic::le_public_bit(lhs, rhs, &mut self.io_context)?;
let result = !Rep3RingShare::new(
Bit::cast_from_biguint(&le.a),
Bit::cast_from_biguint(&le.b),
);
Rep3BrilligType::shared_u1(result)
}
(Shared::Ring128(lhs), Public::Int(rhs, IntegerBitSize::U128)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::gt_public(
lhs,
rhs.into(),
&mut self.io_context,
)?)
}
(Shared::Ring64(lhs), Public::Int(rhs, IntegerBitSize::U64)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::gt_public(
lhs,
u64::try_from(rhs).expect("must be u64").into(),
&mut self.io_context,
)?)
}
(Shared::Ring32(lhs), Public::Int(rhs, IntegerBitSize::U32)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::gt_public(
lhs,
u32::try_from(rhs).expect("must be u32").into(),
&mut self.io_context,
)?)
}
(Shared::Ring16(lhs), Public::Int(rhs, IntegerBitSize::U16)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::gt_public(
lhs,
u16::try_from(rhs).expect("must be u16").into(),
&mut self.io_context,
)?)
}
(Shared::Ring8(lhs), Public::Int(rhs, IntegerBitSize::U8)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::gt_public(
lhs,
u8::try_from(rhs).expect("must be u8").into(),
&mut self.io_context,
)?)
}
(Shared::Ring1(lhs), Public::Int(rhs, IntegerBitSize::U1)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::gt_public(
lhs,
bit_from_u128!(rhs),
&mut self.io_context,
)?)
}
x => eyre::bail!(
"type mismatch! Can only do bin ops on same types, but tried with {x:?}"
),
}
}
(Rep3BrilligType::Shared(s1), Rep3BrilligType::Shared(s2)) => match (s1, s2) {
(Shared::Field(s1), Shared::Field(s2)) => {
let le = rep3::arithmetic::ge_bit(s2, s1, &mut self.io_context)?;
let result = !Rep3RingShare::new(
Bit::cast_from_biguint(&le.a),
Bit::cast_from_biguint(&le.b),
);
Rep3BrilligType::shared_u1(result)
}
(Shared::Ring128(s1), Shared::Ring128(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::gt(s1, s2, &mut self.io_context)?,
),
(Shared::Ring64(s1), Shared::Ring64(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::gt(s1, s2, &mut self.io_context)?,
),
(Shared::Ring32(s1), Shared::Ring32(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::gt(s1, s2, &mut self.io_context)?,
),
(Shared::Ring16(s1), Shared::Ring16(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::gt(s1, s2, &mut self.io_context)?,
),
(Shared::Ring8(s1), Shared::Ring8(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::gt(s1, s2, &mut self.io_context)?,
),
(Shared::Ring1(s1), Shared::Ring1(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::gt(s1, s2, &mut self.io_context)?,
),
x => eyre::bail!(
"type mismatch! Can only do bin ops on same types, but tried with {x:?}"
),
},
};
Ok(result)
}

fn ge(
&mut self,
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType> {
let result = match (lhs, rhs) {
(Rep3BrilligType::Public(lhs), Rep3BrilligType::Public(rhs)) => {
let result = self.plain_driver.ge(lhs, rhs)?;
Rep3BrilligType::Public(result)
}
(Rep3BrilligType::Public(public), Rep3BrilligType::Shared(shared)) => {
match (shared, public) {
(Shared::Field(rhs), Public::Field(lhs)) => {
let ge = rep3::arithmetic::le_public_bit(rhs, lhs, &mut self.io_context)?;
let result = Rep3RingShare::new(
Bit::cast_from_biguint(&ge.a),
Bit::cast_from_biguint(&ge.b),
);
Rep3BrilligType::shared_u1(result)
}
(Shared::Ring128(rhs), Public::Int(lhs, IntegerBitSize::U128)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::le_public(
rhs,
lhs.into(),
&mut self.io_context,
)?)
}
(Shared::Ring64(rhs), Public::Int(lhs, IntegerBitSize::U64)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::le_public(
rhs,
u64::try_from(lhs).expect("must be u64").into(),
&mut self.io_context,
)?)
}
(Shared::Ring32(rhs), Public::Int(lhs, IntegerBitSize::U32)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::le_public(
rhs,
u32::try_from(lhs).expect("must be u32").into(),
&mut self.io_context,
)?)
}
(Shared::Ring16(rhs), Public::Int(lhs, IntegerBitSize::U16)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::le_public(
rhs,
u16::try_from(lhs).expect("must be u16").into(),
&mut self.io_context,
)?)
}
(Shared::Ring8(rhs), Public::Int(lhs, IntegerBitSize::U8)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::le_public(
rhs,
u8::try_from(lhs).expect("must be u8").into(),
&mut self.io_context,
)?)
}
(Shared::Ring1(rhs), Public::Int(lhs, IntegerBitSize::U1)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::le_public(
rhs,
bit_from_u128!(lhs),
&mut self.io_context,
)?)
}
x => eyre::bail!(
"type mismatch! Can only do bin ops on same types, but tried with {x:?}"
),
}
}
(Rep3BrilligType::Shared(shared), Rep3BrilligType::Public(public)) => {
match (shared, public) {
(Shared::Field(lhs), Public::Field(rhs)) => {
let ge = rep3::arithmetic::ge_public_bit(lhs, rhs, &mut self.io_context)?;
let result = Rep3RingShare::new(
Bit::cast_from_biguint(&ge.a),
Bit::cast_from_biguint(&ge.b),
);
Rep3BrilligType::shared_u1(result)
}
(Shared::Ring128(lhs), Public::Int(rhs, IntegerBitSize::U128)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::ge_public(
lhs,
rhs.into(),
&mut self.io_context,
)?)
}
(Shared::Ring64(lhs), Public::Int(rhs, IntegerBitSize::U64)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::ge_public(
lhs,
u64::try_from(rhs).expect("must be u64").into(),
&mut self.io_context,
)?)
}
(Shared::Ring32(lhs), Public::Int(rhs, IntegerBitSize::U32)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::ge_public(
lhs,
u32::try_from(rhs).expect("must be u32").into(),
&mut self.io_context,
)?)
}
(Shared::Ring16(lhs), Public::Int(rhs, IntegerBitSize::U16)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::ge_public(
lhs,
u16::try_from(rhs).expect("must be u16").into(),
&mut self.io_context,
)?)
}
(Shared::Ring8(lhs), Public::Int(rhs, IntegerBitSize::U8)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::ge_public(
lhs,
u8::try_from(rhs).expect("must be u8").into(),
&mut self.io_context,
)?)
}
(Shared::Ring1(lhs), Public::Int(rhs, IntegerBitSize::U1)) => {
Rep3BrilligType::shared_u1(rep3_ring::arithmetic::ge_public(
lhs,
bit_from_u128!(rhs),
&mut self.io_context,
)?)
}
x => eyre::bail!(
"type mismatch! Can only do bin ops on same types, but tried with {x:?}"
),
}
}
(Rep3BrilligType::Shared(s1), Rep3BrilligType::Shared(s2)) => match (s1, s2) {
(Shared::Field(s1), Shared::Field(s2)) => {
let ge = rep3::arithmetic::ge_bit(s1, s2, &mut self.io_context)?;
let result = Rep3RingShare::new(
Bit::cast_from_biguint(&ge.a),
Bit::cast_from_biguint(&ge.b),
);
Rep3BrilligType::shared_u1(result)
}
(Shared::Ring128(s1), Shared::Ring128(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::ge(s1, s2, &mut self.io_context)?,
),
(Shared::Ring64(s1), Shared::Ring64(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::ge(s1, s2, &mut self.io_context)?,
),
(Shared::Ring32(s1), Shared::Ring32(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::ge(s1, s2, &mut self.io_context)?,
),
(Shared::Ring16(s1), Shared::Ring16(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::ge(s1, s2, &mut self.io_context)?,
),
(Shared::Ring8(s1), Shared::Ring8(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::ge(s1, s2, &mut self.io_context)?,
),
(Shared::Ring1(s1), Shared::Ring1(s2)) => Rep3BrilligType::shared_u1(
rep3_ring::arithmetic::ge(s1, s2, &mut self.io_context)?,
),
x => eyre::bail!(
"type mismatch! Can only do bin ops on same types, but tried with {x:?}"
),
},
};
Ok(result)
}

fn to_radix(
&mut self,
val: Self::BrilligType,
Expand Down
Loading

0 comments on commit aeae315

Please sign in to comment.