Ejemplo n.º 1
0
        /// <summary>
        /// h += m
        /// h *= r
        /// </summary>
        private void Block(ReadOnlySpan <byte> m)
        {
            var h01 = IntrinsicsUtils.CreateTwoUInt(_h0, _h1);
            var h23 = IntrinsicsUtils.CreateTwoUInt(_h2, _h3);
            var h44 = IntrinsicsUtils.CreateTwoUInt(_h4, _h4);

            var m06 = IntrinsicsUtils.CreateTwoUInt(BinaryPrimitives.ReadUInt32LittleEndian(m) & 0x3ffffff, BinaryPrimitives.ReadUInt32LittleEndian(m.Slice(3)) >> 2 & 0x3ffffff);

            h01 = Sse2.Add(h01, m06);
            var m612 = IntrinsicsUtils.CreateTwoUInt(BinaryPrimitives.ReadUInt32LittleEndian(m.Slice(6)) >> 4 & 0x3ffffff, BinaryPrimitives.ReadUInt32LittleEndian(m.Slice(9)) >> 6 & 0x3ffffff);

            h23 = Sse2.Add(h23, m612);
            var m4 = BinaryPrimitives.ReadUInt32LittleEndian(m.Slice(12)) >> 8 | 1u << 24;

            h44 = Sse2.Add(h44, IntrinsicsUtils.CreateTwoUInt(m4));

            MultiplyR(ref h01, ref h23, ref h44, out var d0, out var d1, out var d2, out var d3, out var d4);

            _h0  = (uint)d0 & 0x3ffffff;
            d1  += (uint)(d0 >> 26);
            _h1  = (uint)d1 & 0x3ffffff;
            d2  += (uint)(d1 >> 26);
            _h2  = (uint)d2 & 0x3ffffff;
            d3  += (uint)(d2 >> 26);
            _h3  = (uint)d3 & 0x3ffffff;
            d4  += (uint)(d3 >> 26);
            _h4  = (uint)d4 & 0x3ffffff;
            _h0 += (uint)(d4 >> 26) * 5;
            _h1 += _h0 >> 26;
            _h0 &= 0x3ffffff;
        }
Ejemplo n.º 2
0
        /// <summary>
        ///  a *= r
        /// </summary>
        private void MultiplyR(ref uint a0, ref uint a1, ref uint a2, ref uint a3, ref uint a4)
        {
            var h01 = IntrinsicsUtils.CreateTwoUInt(a0, a1);
            var h23 = IntrinsicsUtils.CreateTwoUInt(a2, a3);
            var h44 = IntrinsicsUtils.CreateTwoUInt(a4, a4);

            MultiplyR(ref h01, ref h23, ref h44, out var d0, out var d1, out var d2, out var d3, out var d4);

            a0  = (uint)d0 & 0x3ffffff;
            d1 += (uint)(d0 >> 26);
            a1  = (uint)d1 & 0x3ffffff;
            d2 += (uint)(d1 >> 26);
            a2  = (uint)d2 & 0x3ffffff;
            d3 += (uint)(d2 >> 26);
            a3  = (uint)d3 & 0x3ffffff;
            d4 += (uint)(d3 >> 26);
            a4  = (uint)d4 & 0x3ffffff;
            a0 += (uint)(d4 >> 26) * 5;
            a1 += a0 >> 26;
            a0 &= 0x3ffffff;
        }
Ejemplo n.º 3
0
        public Poly1305X86(ReadOnlySpan <byte> key)
        {
            if (key.Length < KeySize)
            {
                throw new ArgumentException(@"Key length must be 32 bytes", nameof(key));
            }

            // r &= 0xFFFFFFC0FFFFFFC0FFFFFFC0FFFFFFF
            var r0 = BinaryPrimitives.ReadUInt32LittleEndian(key) & 0x3FFFFFF;
            var r1 = BinaryPrimitives.ReadUInt32LittleEndian(key.Slice(3)) >> 2 & 0x3FFFF03;
            var r2 = BinaryPrimitives.ReadUInt32LittleEndian(key.Slice(6)) >> 4 & 0x3FFC0FF;
            var r3 = BinaryPrimitives.ReadUInt32LittleEndian(key.Slice(9)) >> 6 & 0x3F03FFF;
            var r4 = BinaryPrimitives.ReadUInt32LittleEndian(key.Slice(12)) >> 8 & 0x00FFFFF;

            var s1 = r1 * 5;
            var s2 = r2 * 5;
            var s3 = r3 * 5;
            var s4 = r4 * 5;

            _r0s4 = IntrinsicsUtils.CreateTwoUInt(r0, s4);
            _s3s2 = IntrinsicsUtils.CreateTwoUInt(s3, s2);
            _r1r0 = IntrinsicsUtils.CreateTwoUInt(r1, r0);
            _s4s3 = IntrinsicsUtils.CreateTwoUInt(s4, s3);
            _s1s2 = IntrinsicsUtils.CreateTwoUInt(s1, s2);
            _r2r1 = IntrinsicsUtils.CreateTwoUInt(r2, r1);
            _r3r2 = IntrinsicsUtils.CreateTwoUInt(r3, r2);
            _s3s4 = IntrinsicsUtils.CreateTwoUInt(s3, s4);
            _r4r3 = IntrinsicsUtils.CreateTwoUInt(r4, r3);
            _r0   = Sse2.ConvertScalarToVector128UInt32(r0);

            _x0 = BinaryPrimitives.ReadUInt32LittleEndian(key.Slice(16));
            _x1 = BinaryPrimitives.ReadUInt32LittleEndian(key.Slice(20));
            _x2 = BinaryPrimitives.ReadUInt32LittleEndian(key.Slice(24));
            _x3 = BinaryPrimitives.ReadUInt32LittleEndian(key.Slice(28));

            var u0 = r0;
            var u1 = r1;
            var u2 = r2;
            var u3 = r3;
            var u4 = r4;

            MultiplyR(ref u0, ref u1, ref u2, ref u3, ref u4);

            _ru0 = IntrinsicsUtils.CreateTwoUInt(u0, r0);
            _ru1 = IntrinsicsUtils.CreateTwoUInt(u1, r1);
            _ru2 = IntrinsicsUtils.CreateTwoUInt(u2, r2);
            _ru3 = IntrinsicsUtils.CreateTwoUInt(u3, r3);
            _ru4 = IntrinsicsUtils.CreateTwoUInt(u4, r4);

            _sv1 = _ru1.Multiply5();
            _sv2 = _ru2.Multiply5();
            _sv3 = _ru3.Multiply5();
            _sv4 = _ru4.Multiply5();

            if (Avx2.IsSupported)
            {
                var w0 = u0;
                var w1 = u1;
                var w2 = u2;
                var w3 = u3;
                var w4 = u4;
                MultiplyR(ref w0, ref w1, ref w2, ref w3, ref w4);

                var y0 = w0;
                var y1 = w1;
                var y2 = w2;
                var y3 = w3;
                var y4 = w4;
                MultiplyR(ref y0, ref y1, ref y2, ref y3, ref y4);

                _ruwy0 = IntrinsicsUtils.Create4UInt(y0, w0, u0, r0);
                _ruwy1 = IntrinsicsUtils.Create4UInt(y1, w1, u1, r1);
                _ruwy2 = IntrinsicsUtils.Create4UInt(y2, w2, u2, r2);
                _ruwy3 = IntrinsicsUtils.Create4UInt(y3, w3, u3, r3);
                _ruwy4 = IntrinsicsUtils.Create4UInt(y4, w4, u4, r4);

                _svxz1 = _ruwy1.Multiply5();
                _svxz2 = _ruwy2.Multiply5();
                _svxz3 = _ruwy3.Multiply5();
                _svxz4 = _ruwy4.Multiply5();
            }
        }
Ejemplo n.º 4
0
        private void Block2(ReadOnlySpan <byte> m)
        {
            var n0  = MemoryMarshal.Cast <byte, uint>(m);
            var hc0 = IntrinsicsUtils.CreateTwoUInt(n0[0], n0[4]);

            hc0 = Sse2.And(hc0, And128);
            hc0 = Sse2.Add(hc0, Sse2.ConvertScalarToVector128UInt32(_h0));

            var n1  = MemoryMarshal.Cast <byte, uint>(m.Slice(3));
            var hc1 = IntrinsicsUtils.CreateTwoUInt(n1[0], n1[4]);

            hc1 = Sse2.ShiftRightLogical(hc1, 2);
            hc1 = Sse2.And(hc1, And128);
            hc1 = Sse2.Add(hc1, Sse2.ConvertScalarToVector128UInt32(_h1));

            var n2  = MemoryMarshal.Cast <byte, uint>(m.Slice(6));
            var hc2 = IntrinsicsUtils.CreateTwoUInt(n2[0], n2[4]);

            hc2 = Sse2.ShiftRightLogical(hc2, 4);
            hc2 = Sse2.And(hc2, And128);
            hc2 = Sse2.Add(hc2, Sse2.ConvertScalarToVector128UInt32(_h2));

            var n3  = MemoryMarshal.Cast <byte, uint>(m.Slice(9));
            var hc3 = IntrinsicsUtils.CreateTwoUInt(n3[0], n3[4]);

            hc3 = Sse2.ShiftRightLogical(hc3, 6);
            hc3 = Sse2.And(hc3, And128);
            hc3 = Sse2.Add(hc3, Sse2.ConvertScalarToVector128UInt32(_h3));

            var n4  = MemoryMarshal.Cast <byte, uint>(m.Slice(12));
            var hc4 = IntrinsicsUtils.CreateTwoUInt(n4[0], n4[4]);

            hc4 = Sse2.ShiftRightLogical(hc4, 8);
            hc4 = Sse2.Xor(hc4, Or128);
            hc4 = Sse2.Add(hc4, Sse2.ConvertScalarToVector128UInt32(_h4));

            var t1 = Sse2.Multiply(_ru0, hc0);

            t1 = Sse2.Add(t1, Sse2.Multiply(_sv4, hc1));
            t1 = Sse2.Add(t1, Sse2.Multiply(_sv3, hc2));
            t1 = Sse2.Add(t1, Sse2.Multiply(_sv2, hc3));
            t1 = Sse2.Add(t1, Sse2.Multiply(_sv1, hc4));
            var d0 = t1.Add2UInt64();

            t1 = Sse2.Multiply(_ru1, hc0);
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru0, hc1));
            t1 = Sse2.Add(t1, Sse2.Multiply(_sv4, hc2));
            t1 = Sse2.Add(t1, Sse2.Multiply(_sv3, hc3));
            t1 = Sse2.Add(t1, Sse2.Multiply(_sv2, hc4));
            var d1 = t1.Add2UInt64();

            t1 = Sse2.Multiply(_ru2, hc0);
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru1, hc1));
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru0, hc2));
            t1 = Sse2.Add(t1, Sse2.Multiply(_sv4, hc3));
            t1 = Sse2.Add(t1, Sse2.Multiply(_sv3, hc4));
            var d2 = t1.Add2UInt64();

            t1 = Sse2.Multiply(_ru3, hc0);
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru2, hc1));
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru1, hc2));
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru0, hc3));
            t1 = Sse2.Add(t1, Sse2.Multiply(_sv4, hc4));
            var d3 = t1.Add2UInt64();

            t1 = Sse2.Multiply(_ru4, hc0);
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru3, hc1));
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru2, hc2));
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru1, hc3));
            t1 = Sse2.Add(t1, Sse2.Multiply(_ru0, hc4));
            var d4 = t1.Add2UInt64();

            _h0  = (uint)d0 & 0x3ffffff;
            d1  += d0 >> 26;
            _h1  = (uint)d1 & 0x3ffffff;
            d2  += d1 >> 26;
            _h2  = (uint)d2 & 0x3ffffff;
            d3  += d2 >> 26;
            _h3  = (uint)d3 & 0x3ffffff;
            d4  += d3 >> 26;
            _h4  = (uint)d4 & 0x3ffffff;
            _h0 += (uint)((d4 >> 26) * 5);
            _h1 += _h0 >> 26;
            _h0 &= 0x3ffffff;
        }