/// <summary>
 /// Returns the domain for the 'domain_type' and 'fork_version'
 /// </summary>
 public Domain ComputeDomain(DomainType domainType, ForkVersion forkVersion = new ForkVersion())
 {
     Span<byte> combined = stackalloc byte[Domain.Length];
     domainType.AsSpan().CopyTo(combined);
     forkVersion.AsSpan().CopyTo(combined.Slice(DomainType.Length));
     return new Domain(combined);
 }
Beispiel #2
0
        /// <summary>
        /// Returns the domain for the 'domain_type' and 'fork_version'
        /// </summary>
        public Domain ComputeDomain(DomainType domainType, ForkVersion forkVersion = new ForkVersion())
        {
            var combined = new Span <byte>(new byte[Domain.Length]);

            domainType.AsSpan().CopyTo(combined);
            forkVersion.AsSpan().CopyTo(combined.Slice(DomainType.Length));
            return(new Domain(combined));
        }
        /// <summary>
        /// Returns the domain for the 'domain_type' and 'fork_version'
        /// </summary>
        public Domain ComputeDomain(DomainType domainType, ForkVersion?forkVersion = null)
        {
            forkVersion ??= _initialValueOptions.CurrentValue.GenesisForkVersion;

            Span <byte> combined = stackalloc byte[Domain.Length];

            domainType.AsSpan().CopyTo(combined);
            forkVersion.Value.AsSpan().CopyTo(combined.Slice(DomainType.Length));
            return(new Domain(combined));
        }
        /// <summary>
        /// Returns the domain for the 'domain_type' and 'fork_version'
        /// </summary>
        public Domain ComputeDomain(DomainType domainType, ForkVersion?forkVersion)
        {
            // TODO: Duplicate of BeaconChainUtility

            if (forkVersion == null)
            {
                forkVersion = _initialValueOptions.CurrentValue.GenesisForkVersion;
            }

            Span <byte> combined = stackalloc byte[Domain.Length];

            domainType.AsSpan().CopyTo(combined);
            forkVersion.Value.AsSpan().CopyTo(combined.Slice(DomainType.Length));
            return(new Domain(combined));
        }
Beispiel #5
0
        /// <summary>
        /// Return the seed at ``epoch``.
        /// </summary>
        public Bytes32 GetSeed(BeaconState state, Epoch epoch, DomainType domainType)
        {
            Epoch mixEpoch = (Epoch)(epoch + _stateListLengthOptions.CurrentValue.EpochsPerHistoricalVector
                                     - _timeParameterOptions.CurrentValue.MinimumSeedLookahead - 1UL);
            // # Avoid underflow
            Bytes32 mix = GetRandaoMix(state, mixEpoch);

            Span <byte> seedHashInput = stackalloc byte[DomainType.Length + sizeof(ulong) + Bytes32.Length];

            domainType.AsSpan().CopyTo(seedHashInput);
            BinaryPrimitives.WriteUInt64LittleEndian(seedHashInput.Slice(DomainType.Length), epoch);
            mix.AsSpan().CopyTo(seedHashInput.Slice(DomainType.Length + sizeof(ulong)));
            Bytes32 seed = _cryptographyService.Hash(seedHashInput);

            return(seed);
        }
        /// <summary>
        /// Return the seed at ``epoch``.
        /// </summary>
        public Hash32 GetSeed(BeaconState state, Epoch epoch, DomainType domainType)
        {
            var mixEpoch = epoch + _stateListLengthOptions.CurrentValue.EpochsPerHistoricalVector
                           - _timeParameterOptions.CurrentValue.MinimumSeedLookahead - new Epoch(1);
            // # Avoid underflow
            var mix           = GetRandaoMix(state, mixEpoch);
            var seedHashInput = new Span <byte>(new byte[4 + 8 + 32]);

            domainType.AsSpan().CopyTo(seedHashInput);
            var epochBytes = BitConverter.GetBytes((ulong)epoch);

            if (!BitConverter.IsLittleEndian)
            {
                epochBytes = epochBytes.Reverse().ToArray();
            }
            epochBytes.CopyTo(seedHashInput.Slice(4));
            mix.AsSpan().CopyTo(seedHashInput.Slice(12));
            var seed = _cryptographyService.Hash(seedHashInput);

            return(seed);
        }