add bomberman example

This commit is contained in:
Janmajaya Mall
2024-07-01 17:48:35 +05:30
parent 72d8cafa95
commit 00e431484e
4 changed files with 206 additions and 18 deletions

View File

@@ -554,7 +554,8 @@ mod tests {
assert_eq!(
m0_plus_m1,
m0.wrapping_add(m1),
"Expected {} but got {m0_plus_m1} for {i}+{j}",
"Expected {} but got {m0_plus_m1} for
{i}+{j}",
m0.wrapping_add(m1)
);
}
@@ -565,7 +566,8 @@ mod tests {
assert_eq!(
m0_sub_m1,
m0.wrapping_sub(m1),
"Expected {} but got {m0_sub_m1} for {i}-{j}",
"Expected {} but got {m0_sub_m1} for
{i}-{j}",
m0.wrapping_sub(m1)
);
}
@@ -591,22 +593,26 @@ mod tests {
let (q, r) = i.div_rem_euclid(&j);
assert_eq!(
m_quotient, q,
"Expected {} but got {m_quotient} for {i}/{j}",
"Expected {} but got {m_quotient} for
{i}/{j}",
q
);
assert_eq!(
m_remainder, r,
"Expected {} but got {m_remainder} for {i}%{j}",
"Expected {} but got {m_remainder} for
{i}%{j}",
r
);
} else {
assert_eq!(
m_quotient, 255,
"Expected 255 but got {m_quotient}. Case div by zero"
"Expected 255 but got {m_quotient}. Case
div by zero"
);
assert_eq!(
m_remainder, i,
"Expected {i} but got {m_remainder}. Case div by zero"
"Expected {i} but got {m_remainder}. Case
div by zero"
);
let div_by_zero = ck.decrypt(&div_zero_error_flag().unwrap());

View File

@@ -20,15 +20,32 @@ pub fn div_zero_error_flag() -> Option<FheBool> {
mod frontend {
use super::ops::{
arbitrary_bit_adder, arbitrary_bit_division_for_quotient_and_rem, arbitrary_bit_subtractor,
eight_bit_mul,
eight_bit_mul, is_zero,
};
use crate::utils::{Global, WithLocal};
use super::*;
mod arithetic {
/// Set Div by Zero flag after each divison. Div by zero flag is set to true
/// if either 1 of the division executed in circuit evaluation has
/// denominator set to 0.
fn set_div_by_zero_flag(denominator: &FheUint8) {
{
BoolEvaluator::with_local_mut(|e| {
let key = RuntimeServerKey::global();
let is_zero = is_zero(e, denominator.data(), key);
DIV_ZERO_ERROR.with_borrow_mut(|before_is_zero| {
if before_is_zero.is_none() {
*before_is_zero = Some(FheBool { data: is_zero });
} else {
e.or_inplace(before_is_zero.as_mut().unwrap().data_mut(), &is_zero, key);
}
});
})
}
}
use ops::is_zero;
mod arithetic {
use super::*;
use std::ops::{Add, AddAssign, Div, Mul, Rem, Sub};
@@ -76,13 +93,12 @@ mod frontend {
impl Div<&FheUint8> for &FheUint8 {
type Output = FheUint8;
fn div(self, rhs: &FheUint8) -> Self::Output {
// set div by 0 error flag
set_div_by_zero_flag(rhs);
BoolEvaluator::with_local_mut(|e| {
let key = RuntimeServerKey::global();
// set div by 0 error flag
let is_zero = is_zero(e, rhs.data(), key);
DIV_ZERO_ERROR.set(Some(FheBool { data: is_zero }));
let (quotient, _) = arbitrary_bit_division_for_quotient_and_rem(
e,
self.data(),
@@ -141,13 +157,12 @@ mod frontend {
}
pub fn div_rem(&self, rhs: &FheUint8) -> (FheUint8, FheUint8) {
// set div by 0 error flag
set_div_by_zero_flag(rhs);
BoolEvaluator::with_local_mut(|e| {
let key = RuntimeServerKey::global();
// set div by 0 error flag
let is_zero = is_zero(e, rhs.data(), key);
DIV_ZERO_ERROR.set(Some(FheBool { data: is_zero }));
let (quotient, remainder) = arbitrary_bit_division_for_quotient_and_rem(
e,
self.data(),