Skip to content

Commit

Permalink
fix innersum bgv
Browse files Browse the repository at this point in the history
  • Loading branch information
lehugueni committed Nov 19, 2024
1 parent 3662ccc commit 529dcd5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions schemes/bgv/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,43 @@ func (eval Evaluator) RotateHoistedLazyNew(level int, rotations []int, op0 *rlwe
return
}

// InnerSum computes the inner sum of the underlying slots (see [rlwe.Evaluator.InnerSum]).
// NB: in the slot encoding of BGV/BFV, the underlying N slots are arranged as 2 rows of N/2 slots.
// If n*batchSize < N/2, InnerSum computes the [rlwe.Evaluator.InnerSum] of each row separately.
// If n*batchSize = N, InnerSum computes the [rlwe.Evaluator.InnerSum] on the concatenation of both rows.
// NOTE: In this case, InnerSum performs an addition and a [Evaluator.RotateRowsNew] on top
// Otherwise, InnerSum returns an error.
func (eval Evaluator) InnerSum(ctIn *rlwe.Ciphertext, batchSize, n int, opOut *rlwe.Ciphertext) (err error) {
N := eval.parameters.N()
halfN := N >> 1
l := n * batchSize

if l > halfN {
if l != N {
return fmt.Errorf("innersum: n*batchSize=%d > N/2=%d and n*batchSize != N=%d", l, halfN, N)
}

if err = eval.Evaluator.InnerSum(ctIn, batchSize, n/2, opOut); err != nil {
return
}

var ctRot *rlwe.Ciphertext
ctRot, err = eval.RotateRowsNew(opOut)
if err != nil {
return
}

if err = eval.Add(opOut, ctRot, opOut); err != nil {
return
}

return
}

err = eval.Evaluator.InnerSum(ctIn, batchSize, n, opOut)
return
}

// MatchScalesAndLevel updates the both input ciphertexts to ensures that their scale matches.
// To do so it computes t0 * a = opOut * b such that:
// - ct0.Scale * a = opOut.Scale: make the scales match.
Expand Down
2 changes: 1 addition & 1 deletion schemes/bgv/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (p Parameters) GaloisElementForRowRotation() uint64 {
// InnerSum operation with parameters batch and n.
func (p Parameters) GaloisElementsForInnerSum(batch, n int) (galEls []uint64) {
galEls = rlwe.GaloisElementsForInnerSum(p, batch, n)
if n > p.N()>>1 {
if n*batch > p.N()>>1 {
galEls = append(galEls, p.GaloisElementForRowRotation())
}
return
Expand Down

0 comments on commit 529dcd5

Please sign in to comment.