diff --git a/crystals-kyber/poly.go b/crystals-kyber/poly.go index 56ed0ab..5502e25 100644 --- a/crystals-kyber/poly.go +++ b/crystals-kyber/poly.go @@ -186,7 +186,7 @@ func polyToMsg(p Poly) []byte { return msg } -//compress packs a polynomial into a byte array using d bits per coefficient +//compress packs a polynomial into a byte array using d bits per coefficient - fixed against https://kyberslash.cr.yp.to/faq.html (cases d=4,5 only for now) func (p *Poly) compress(d int) []byte { c := make([]byte, n*d/8) switch d { @@ -229,18 +229,17 @@ func (p *Poly) compress(d int) []byte { case 5: var t [8]uint16 - var d0 uint32 /* accumulation value for fixing KyberSlash2 */ + var d5 uint32 /* accumulation value for fixing KyberSlash2 */ id := 0 for i := 0; i < n/8; i++ { for j := 0; j < 8; j++ { /* t[j] = uint16(((uint32(p[8*i+j])<<5)+uint32(q)/2)/ uint32(q)) & ((1 << 5) - 1) */ - t[j] = uint16(p[8*i+j]) - d0 = uint32(t[j] << 5) - d0 += 1664 - d0 *= 40318 - d0 >>= 27 - t[j] = uint16(d0 & 0x1f) + d5 = uint32(p[8*i+j] << 5) + d5 += 1664 + d5 *= 40318 + d5 >>= 27 + t[j] = uint16(d5 & 0x1f) } c[id] = byte(t[0]) | byte(t[1]<<5) c[id+1] = byte(t[1]>>3) | byte(t[2]<<2) | byte(t[3]<<7)