forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.go
38 lines (25 loc) · 877 Bytes
/
algorithms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package ckks
import (
"github.com/jzhchu/lattigo/rlwe"
)
// InverseNew computes 1/op and returns the result on a new element, iterating for n steps and consuming n levels. The algorithm requires the encrypted values to be in the range
// [-1.5 - 1.5i, 1.5 + 1.5i] or the result will be wrong. Each iteration increases the precision.
func (eval *evaluator) InverseNew(op *rlwe.Ciphertext, steps int) (opOut *rlwe.Ciphertext, err error) {
cbar := eval.NegNew(op)
eval.AddConst(cbar, 1, cbar)
tmp := eval.AddConstNew(cbar, 1)
opOut = tmp.CopyNew()
for i := 1; i < steps; i++ {
eval.MulRelin(cbar, cbar, cbar)
if err = eval.Rescale(cbar, op.Scale, cbar); err != nil {
return
}
tmp = eval.AddConstNew(cbar, 1)
eval.MulRelin(tmp, opOut, tmp)
if err = eval.Rescale(tmp, op.Scale, tmp); err != nil {
return
}
opOut = tmp.CopyNew()
}
return
}