예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="trits"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public override sbyte[] Squeeze(sbyte[] trits, int offset, int length)
        {
            if (length % 243 != 0)
            {
                throw new IllegalStateException("Illegal length: " + length);
            }

            do
            {
                _keccak.DoFinal(_byteState, 0);

                //convert to trits
                FixedBigIntConverter.FromBytesToTrits(_byteState, _tritState);

                //copy with offset
                _tritState[HASH_LENGTH - 1] = 0;
                Array.Copy(_tritState, 0, trits, offset, HASH_LENGTH);

                //calculate hash again
                for (var i = _byteState.Length; i-- > 0;)
                {
                    _byteState[i] = (byte)(_byteState[i] ^ 0xFF);
                }

                _keccak.BlockUpdate(_byteState, 0, _byteState.Length);
                offset += HASH_LENGTH;
            } while ((length -= HASH_LENGTH) > 0);

            return(trits);
        }
예제 #2
0
        /// <inheritdoc />
        /// <summary>
        /// </summary>
        /// <param name="trits"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        /// <exception cref="T:Iota.Lib.CSharp.Api.Exception.IllegalStateException"></exception>
        public override void Squeeze(int[] trits, int offset, int length)
        {
            if (length % 243 != 0)
            {
                throw new IllegalStateException("Illegal length: " + length);
            }

            do
            {
                _keccak.DoFinal(_byteState, 0);

                //convert to trits
                FixedBigIntConverter.FromBytesToTrits(_byteState, _tritState);
                //BigIntConverter.TritsFromBigInt(
                //    BigIntConverter.BigIntFromBytes(_byteState, 0, ByteHashLength),
                //    _tritState, 0, HashLength);

                //copy with offset
                _tritState[HashLength - 1] = 0;
                Array.Copy(_tritState, 0, trits, offset, HashLength);

                //calculate hash again
                for (var i = _byteState.Length; i-- > 0;)
                {
                    _byteState[i] = (byte)(_byteState[i] ^ 0xFF);
                }

                _keccak.BlockUpdate(_byteState, 0, _byteState.Length);
                offset += HashLength;
            } while ((length -= HashLength) > 0);
        }
예제 #3
0
        public void TestFixedBigInt()
        {
            var inputTrits  = new int[243];
            var outputTrits = new int[243];
            var bytes       = new byte[384 / 8];

            for (var i = 0; i < inputTrits.Length; i++)
            {
                inputTrits[i] = Random.Next(3) - 1;
            }

            inputTrits[inputTrits.Length - 1] = 0;
            FixedBigIntConverter.FromTritsToBytes(inputTrits, bytes);
            FixedBigIntConverter.FromBytesToTrits(bytes, outputTrits);
            outputTrits[outputTrits.Length - 1] = 0;

            for (var i = 0; i < inputTrits.Length; i++)
            {
                Assert.AreEqual(inputTrits[i], outputTrits[i]);
            }
        }
예제 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="trits"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public override ICurl Absorb(sbyte[] trits, int offset, int length)
        {
            if (length % 243 != 0)
            {
                throw new IllegalStateException("Illegal length: " + length);
            }

            do
            {
                Array.Copy(trits, offset, _tritState, 0, HASH_LENGTH);

                //convert to bits
                _tritState[HASH_LENGTH - 1] = 0;
                FixedBigIntConverter.FromTritsToBytes(_tritState, _byteState);

                //run keccak
                _keccak.BlockUpdate(_byteState, 0, _byteState.Length);
                offset += HASH_LENGTH;
            } while ((length -= HASH_LENGTH) > 0);

            return(this);
        }
예제 #5
0
        /// <inheritdoc />
        /// <summary>
        /// </summary>
        /// <param name="trits"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public override void Absorb(int[] trits, int offset, int length)
        {
            if (length % 243 != 0)
            {
                throw new IllegalStateException("Illegal length: " + length);
            }

            do
            {
                //copy trits[offset:offset+length]
                Array.Copy(trits, offset, _tritState, 0, HashLength);

                //convert to bits
                _tritState[HashLength - 1] = 0;
                FixedBigIntConverter.FromTritsToBytes(_tritState, _byteState);
                //BigIntConverter.BytesFromBigInt(
                //    BigIntConverter.BigIntFromTrits(_tritState, 0, HashLength),
                //    _byteState, 0, ByteHashLength);

                //run keccak
                _keccak.BlockUpdate(_byteState, 0, _byteState.Length);
                offset += HashLength;
            } while ((length -= HashLength) > 0);
        }