Fixed potential bugs related to ciphertext/plaintext allocation

This commit is contained in:
Jean-Philippe Bossuat
2022-05-12 17:21:10 +02:00
parent a4c1be42dd
commit d1e2f2de66
5 changed files with 27 additions and 18 deletions

View File

@@ -45,12 +45,8 @@ func NewPlaintextLvl(params Parameters, level int) *Plaintext {
// where the message is set to the passed poly. No checks are performed on poly and
// the returned Plaintext will share its backing array of coefficient.
func NewPlaintextAtLevelFromPoly(level int, poly *ring.Poly) *Plaintext {
if len(poly.Coeffs) < level+1 {
panic("cannot NewPlaintextAtLevelFromPoly: provided ring.Poly level is too small")
}
v0 := new(ring.Poly)
v0.Coeffs = poly.Coeffs[:level+1]
return &Plaintext{Plaintext: &rlwe.Plaintext{Value: v0}}
pt := rlwe.NewPlaintextAtLevelFromPoly(level, poly)
return &Plaintext{Plaintext: pt}
}
// NewPlaintextRingT creates and allocates a new plaintext in RingT (single modulus T).

View File

@@ -39,10 +39,9 @@ func NewCiphertextRandom(prng utils.PRNG, params Parameters, degree, level int,
// where the message is set to the passed poly. No checks are performed on poly and
// the returned Ciphertext will share its backing array of coefficient.
func NewCiphertextAtLevelFromPoly(level int, poly [2]*ring.Poly) *Ciphertext {
v0, v1 := new(ring.Poly), new(ring.Poly)
v0.IsNTT, v1.IsNTT = true, true
v0.Coeffs, v1.Coeffs = poly[0].Coeffs[:level+1], poly[1].Coeffs[:level+1]
return &Ciphertext{Ciphertext: &rlwe.Ciphertext{Value: []*ring.Poly{v0, v1}}, Scale: 0}
ct := rlwe.NewCiphertextAtLevelFromPoly(level, poly)
ct.Value[0].IsNTT, ct.Value[1].IsNTT = true, true
return &Ciphertext{Ciphertext: ct, Scale: 0}
}
// ScalingFactor returns the scaling factor of the ciphertext

View File

@@ -32,8 +32,7 @@ func (p *Plaintext) SetScalingFactor(scale float64) {
// where the message is set to the passed poly. No checks are performed on poly and
// the returned Plaintext will share its backing array of coefficient.
func NewPlaintextAtLevelFromPoly(level int, poly *ring.Poly) *Plaintext {
v0 := new(ring.Poly)
v0.IsNTT = true
v0.Coeffs = poly.Coeffs[:level+1]
return &Plaintext{Plaintext: &rlwe.Plaintext{Value: v0}, Scale: 0}
pt := rlwe.NewPlaintextAtLevelFromPoly(level, poly)
pt.Value.IsNTT = true
return &Plaintext{Plaintext: pt, Scale: 0}
}

View File

@@ -61,6 +61,19 @@ func NewPlaintext(params Parameters, level int) *Plaintext {
return &Plaintext{Value: ring.NewPoly(params.N(), level)}
}
// NewPlaintextAtLevelFromPoly construct a new Plaintext at a specific level
// where the message is set to the passed poly. No checks are performed on poly and
// the returned Plaintext will share its backing array of coefficient.
func NewPlaintextAtLevelFromPoly(level int, poly *ring.Poly) *Plaintext {
if len(poly.Coeffs) < level+1 {
panic("cannot NewPlaintextAtLevelFromPoly: provided ring.Poly level is too small")
}
v0 := new(ring.Poly)
v0.Coeffs = poly.Coeffs[:level+1]
v0.Buff = poly.Buff[:poly.N()*(level+1)]
return &Plaintext{Value: v0}
}
// Degree returns the degree of the target element.
func (pt Plaintext) Degree() int {
return 0
@@ -105,13 +118,13 @@ func NewCiphertextNTT(params Parameters, degree, level int) *Ciphertext {
return el
}
// NewCiphertextNTTAtLevelFromPoly construct a new Ciphetext at a specific level
// NewCiphertextAtLevelFromPoly construct a new Ciphetext at a specific level
// where the message is set to the passed poly. No checks are performed on poly and
// the returned Ciphertext will share its backing array of coefficient.
func NewCiphertextNTTAtLevelFromPoly(level int, poly [2]*ring.Poly) *Ciphertext {
func NewCiphertextAtLevelFromPoly(level int, poly [2]*ring.Poly) *Ciphertext {
v0, v1 := new(ring.Poly), new(ring.Poly)
v0.IsNTT, v1.IsNTT = true, true
v0.Coeffs, v1.Coeffs = poly[0].Coeffs[:level+1], poly[1].Coeffs[:level+1]
v0.Buff, v1.Buff = poly[0].Buff[:poly[0].N()*(level+1)], poly[1].Buff[:poly[1].N()*(level+1)]
return &Ciphertext{Value: []*ring.Poly{v0, v1}}
}

View File

@@ -177,7 +177,9 @@ func (eval *Evaluator) Trace(ctIn *Ciphertext, logN int, ctOut *Ciphertext) {
ring.MulScalarMontgomeryVec(ctIn.Value[1].Coeffs[i], ctOut.Value[1].Coeffs[i], invN, Q, mredparams)
}
buff := NewCiphertextNTTAtLevelFromPoly(levelQ, [2]*ring.Poly{eval.BuffQP[3].Q, eval.BuffQP[4].Q})
buff := NewCiphertextAtLevelFromPoly(levelQ, [2]*ring.Poly{eval.BuffQP[3].Q, eval.BuffQP[4].Q})
buff.Value[0].IsNTT = true
buff.Value[1].IsNTT = true
for i := logN; i < eval.params.LogN()-1; i++ {
eval.Automorphism(ctOut, eval.params.GaloisElementForColumnRotationBy(1<<i), buff)