예제 #1
0
        public void Regenerate20()
        {
            var           rng    = ChaChaIntrinsics.Create(new UInt32[] { 0, 0, 1, 0, 2, 0, 3, 0 }, 0, 0, 10);
            Span <UInt32> buffer = stackalloc UInt32[64];

            rng.Regenerate(buffer);
            Assert.Equal(137206642u, buffer[0]);
            Assert.Equal(0ul, rng.Stream);
        }
예제 #2
0
        public void Generate8()
        {
            var           rng    = ChaChaIntrinsics.Create(new UInt32[] { 0, 0, 1, 0, 2, 0, 3, 0 }, UInt64.MaxValue, 0, 4);
            Span <UInt32> buffer = stackalloc UInt32[64];

            rng.Generate(buffer);
            Assert.Equal(3680296248u, buffer[0]);
            Assert.Equal(0ul, rng.Stream);
        }
예제 #3
0
        public void StreamModification()
        {
            var           rng     = ChaChaIntrinsics.Create(new UInt32[] { 0, 0, 1, 0, 2, 0, 3, 0 }, 0, 0, 4);
            Span <UInt32> buffer  = stackalloc UInt32[64];
            Span <UInt32> buffer2 = stackalloc UInt32[64];

            rng.Regenerate(buffer);
            rng.Stream = 1ul;
            rng.Regenerate(buffer2);

            for (Int32 i = 0; i < buffer.Length; i++)
            {
                Assert.NotEqual(buffer[i], buffer2[i]);
            }
        }
예제 #4
0
        /// <summary>
        /// Creates a ChaCha RNG using the given seed and number of double rounds.
        /// </summary>
        /// <param name="seed">A seed containing the key and stream.</param>
        /// <param name="doubleRounds">
        /// The number of double rounds to perform. Half the total number of rounds,
        /// ex. ChaCha20 has 10 double rounds and ChaCha8 has 4 double rounds.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Thrown if <paramref name="doubleRounds"/> is equal to 0.
        /// </exception>
        public static ChaCha Create(Seed seed, UInt32 doubleRounds)
        {
            if (doubleRounds == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(doubleRounds));
            }

            var key = seed.Key.Length != 0 ? seed.Key.Span : stackalloc UInt32[KeyLength];

#if X86_INTRINSICS
            var core        = ChaChaIntrinsics.Create(key, UInt64.MaxValue, seed.Stream, doubleRounds);
            var blockBuffer = new BlockBuffer32 <ChaChaIntrinsics, UInt64>(core);
#else
            var core        = ChaChaSoftware.Create(key, UInt64.MaxValue, seed.Stream, doubleRounds);
            var blockBuffer = new BlockBuffer32 <ChaChaSoftware, UInt64>(core);
#endif
            return(new ChaCha(blockBuffer));
        }
예제 #5
0
        public void BlockLength()
        {
            var rng = ChaChaIntrinsics.Create(new UInt32[] { 0, 0, 1, 0, 2, 0, 3, 0 }, UInt64.MaxValue, 0, 4);

            Assert.Equal(ChaCha.BufferLength, rng.BlockLength);
        }