<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.0" />

Hrss1373Polynomial

using Org.BouncyCastle.Pqc.Crypto.Ntru.ParameterSets; using System; namespace Org.BouncyCastle.Pqc.Crypto.Ntru.Polynomials { internal class Hrss1373Polynomial : HrssPolynomial { private static readonly int L = 1376; private static readonly int M = L / 4; private static readonly int K = L / 16; internal Hrss1373Polynomial(NtruHrssParameterSet parameters) : base(parameters) { } public unsafe override byte[] SqToBytes(int len) { byte[] array = new byte[len]; Span<short> span = new Span<short>(stackalloc byte[8], 4); int i; for (i = 0; i < ParameterSet.PackDegree() / 4; i++) { for (int j = 0; j < 4; j++) { span[j] = (short)Polynomial.ModQ((uint)(coeffs[4 * i + j] & 65535), (uint)ParameterSet.Q()); } array[7 * i] = (byte)(span[0] & 255); array[7 * i + 1] = (byte)((span[0] >> 8) | ((span[1] & 3) << 6)); array[7 * i + 2] = (byte)((span[1] >> 2) & 255); array[7 * i + 3] = (byte)((span[1] >> 10) | ((span[2] & 15) << 4)); array[7 * i + 4] = (byte)((span[2] >> 4) & 255); array[7 * i + 5] = (byte)((span[2] >> 12) | ((span[3] & 63) << 2)); array[7 * i + 6] = (byte)(span[3] >> 6); } if (ParameterSet.PackDegree() % 4 == 2) { span[0] = (short)Polynomial.ModQ((uint)(coeffs[ParameterSet.PackDegree() - 2] & 65535), (uint)ParameterSet.Q()); span[1] = (short)Polynomial.ModQ((uint)(coeffs[ParameterSet.PackDegree() - 1] & 65535), (uint)ParameterSet.Q()); array[7 * i] = (byte)(span[0] & 255); array[7 * i + 1] = (byte)((span[0] >> 8) | ((span[1] & 3) << 6)); array[7 * i + 2] = (byte)((span[1] >> 2) & 255); array[7 * i + 3] = (byte)(span[1] >> 10); } return array; } public override void SqFromBytes(byte[] a) { int i; for (i = 0; i < ParameterSet.PackDegree() / 4; i++) { coeffs[4 * i] = (ushort)((a[7 * i] & 255) | (((ushort)(a[7 * i + 1] & 255) & 63) << 8)); coeffs[4 * i + 1] = (ushort)(((a[7 * i + 1] & 255) >> 6) | ((ushort)(a[7 * i + 2] & 255) << 2) | ((short)(a[7 * i + 3] & 15) << 10)); coeffs[4 * i + 2] = (ushort)(((a[7 * i + 3] & 255) >> 4) | (((ushort)(a[7 * i + 4] & 255) & 255) << 4) | ((short)(a[7 * i + 5] & 3) << 12)); coeffs[4 * i + 3] = (ushort)(((a[7 * i + 5] & 255) >> 2) | ((ushort)(a[7 * i + 6] & 255) << 6)); } if (ParameterSet.PackDegree() % 4 == 2) { coeffs[4 * i] = (ushort)(a[7 * i] | ((a[7 * i + 1] & 63) << 8)); coeffs[4 * i + 1] = (ushort)((a[7 * i + 1] >> 6) | (a[7 * i + 2] << 2) | ((a[7 * i + 3] & 15) << 10)); } coeffs[ParameterSet.N - 1] = 0; } } }