From 27fee8bebbb5ee600d69086f0b5a8ff9a6c8e24e Mon Sep 17 00:00:00 2001 From: Jean-Philippe Bossuat Date: Mon, 21 Feb 2022 19:26:00 +0100 Subject: [PATCH] fixed sparse ternary sampler to wipe poly before sampling (#179) --- ring/ring_sampler_ternary.go | 7 +++++++ ring/ring_test.go | 26 +++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/ring/ring_sampler_ternary.go b/ring/ring_sampler_ternary.go index 0e6fb66f..7311af93 100644 --- a/ring/ring_sampler_ternary.go +++ b/ring/ring_sampler_ternary.go @@ -213,6 +213,13 @@ func (ts *TernarySampler) sampleSparse(lvl int, pol *Poly) { pointer = 0 } } + + for _, i := range index { + for k := 0; k < lvl+1; k++ { + pol.Coeffs[k][i] = 0 + } + + } } // kysampling uses the binary expansion and random bytes matrix to sample a discrete Gaussian value and its sign. diff --git a/ring/ring_test.go b/ring/ring_test.go index b4ad34ab..08f663cf 100644 --- a/ring/ring_test.go +++ b/ring/ring_test.go @@ -391,17 +391,25 @@ func testTernarySampler(testContext *testParams, t *testing.T) { ternarySampler := NewTernarySamplerWithHammingWeight(prng, testContext.ringQ, p, false) + checkPoly := func(pol *Poly) { + for i := range testContext.ringQ.Modulus { + hw := 0 + for _, c := range pol.Coeffs[i] { + if c != 0 { + hw++ + } + } + require.True(t, hw == p) + } + } + pol := ternarySampler.ReadNew() - for i := range testContext.ringQ.Modulus { - hw := 0 - for _, c := range pol.Coeffs[i] { - if c != 0 { - hw++ - } - } - require.True(t, hw == p) - } + checkPoly(pol) + + ternarySampler.Read(pol) + + checkPoly(pol) }) } }