Skip to content

Commit

Permalink
feat: support tfhe remainder
Browse files Browse the repository at this point in the history
  • Loading branch information
youben11 committed Oct 23, 2023
1 parent 762deb0 commit f518a9d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
46 changes: 46 additions & 0 deletions fhevm/tfhe.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,39 @@ void* scalar_div_fhe_uint32(void* ct, uint32_t pt, void* sks)
return result;
}
void* scalar_rem_fhe_uint8(void* ct, uint8_t pt, void* sks)
{
FheUint8* result = NULL;
checked_set_server_key(sks);
const int r = fhe_uint8_scalar_rem(ct, pt, &result);
if(r != 0) return NULL;
return result;
}
void* scalar_rem_fhe_uint16(void* ct, uint16_t pt, void* sks)
{
FheUint16* result = NULL;
checked_set_server_key(sks);
const int r = fhe_uint16_scalar_rem(ct, pt, &result);
if(r != 0) return NULL;
return result;
}
void* scalar_rem_fhe_uint32(void* ct, uint32_t pt, void* sks)
{
FheUint32* result = NULL;
checked_set_server_key(sks);
const int r = fhe_uint32_scalar_rem(ct, pt, &result);
if(r != 0) return NULL;
return result;
}
void* bitand_fhe_uint8(void* ct1, void* ct2, void* sks)
{
FheUint8* result = NULL;
Expand Down Expand Up @@ -1994,6 +2027,19 @@ func (lhs *tfheCiphertext) scalarDiv(rhs uint64) (*tfheCiphertext, error) {
})
}

func (lhs *tfheCiphertext) scalarRem(rhs uint64) (*tfheCiphertext, error) {
return lhs.executeBinaryScalarOperation(rhs,
func(lhs unsafe.Pointer, rhs C.uint8_t) unsafe.Pointer {
return C.scalar_rem_fhe_uint8(lhs, rhs, sks)
},
func(lhs unsafe.Pointer, rhs C.uint16_t) unsafe.Pointer {
return C.scalar_rem_fhe_uint16(lhs, rhs, sks)
},
func(lhs unsafe.Pointer, rhs C.uint32_t) unsafe.Pointer {
return C.scalar_rem_fhe_uint32(lhs, rhs, sks)
})
}

func (lhs *tfheCiphertext) bitand(rhs *tfheCiphertext) (*tfheCiphertext, error) {
return lhs.executeBinaryCiphertextOperation(rhs,
func(lhs unsafe.Pointer, rhs unsafe.Pointer) unsafe.Pointer {
Expand Down
35 changes: 35 additions & 0 deletions fhevm/tfhe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,29 @@ func TfheScalarDiv(t *testing.T, fheUintType fheUintType) {
}
}

func TfheScalarRem(t *testing.T, fheUintType fheUintType) {
var a, b big.Int
switch fheUintType {
case FheUint8:
a.SetUint64(4)
b.SetUint64(2)
case FheUint16:
a.SetUint64(49)
b.SetUint64(144)
case FheUint32:
a.SetUint64(70)
b.SetInt64(17)
}
expected := new(big.Int).Rem(&a, &b)
ctA := new(tfheCiphertext)
ctA.encrypt(a, fheUintType)
ctRes, _ := ctA.scalarRem(b.Uint64())
res, err := ctRes.decrypt()
if err != nil || res.Uint64() != expected.Uint64() {
t.Fatalf("%d != %d", expected.Uint64(), res.Uint64())
}
}

func TfheBitAnd(t *testing.T, fheUintType fheUintType) {
var a, b big.Int
switch fheUintType {
Expand Down Expand Up @@ -1216,6 +1239,18 @@ func TestTfheScalarDiv32(t *testing.T) {
TfheScalarDiv(t, FheUint32)
}

func TestTfheScalarRem8(t *testing.T) {
TfheScalarRem(t, FheUint8)
}

func TestTfheScalarRem16(t *testing.T) {
TfheScalarRem(t, FheUint16)
}

func TestTfheScalarRem32(t *testing.T) {
TfheScalarRem(t, FheUint32)
}

func TestTfheBitAnd8(t *testing.T) {
TfheBitAnd(t, FheUint8)
}
Expand Down

0 comments on commit f518a9d

Please sign in to comment.