Exemple #1
0
        public K GetWeightedRandom(ConcurrentBag <K> exclude)
        {
            lock ( Wheel )
            {
                var pos = 0.0;

                var subset = Wheel;

                if (exclude != null && exclude.Any())
                {
                    subset = Wheel.Where(one => !exclude.Contains(one.Id));
                }

                var subsetsum = subset.Sum(one => one.Space);
                var target    = BufUtils.RandomDouble(subsetsum);

                foreach (var one in subset)
                {
                    pos += one.Space;
                    if (pos >= target)
                    {
                        Logging.LogDebugData($"Roulette: {one.Id}");
                        return(one.Id);
                    }
                }

                if (subset.Any())
                {
                    return(subset.Random().Id);
                }

                return(Wheel.Random().Id);
            }
        }
Exemple #2
0
        public static uint RandomUintNZ()
        {
            uint result;

            do
            {
                result = BufUtils.RandomUint();
            } while (result == 0);

            return(result);
        }
Exemple #3
0
        public static BufLen Decrypt(BufLen data, I2PPrivateKey pkey, bool zeropad)
        {
            if (data == null || (zeropad && data.Length != 514))
            {
                throw new ArgumentException("ElGamal padded data to decrypt must be exactly 514 bytes!");
            }
            if (!zeropad && data.Length != 512)
            {
                throw new ArgumentException("ElGamal data to decrypt must be exactly 512 bytes!");
            }

            var x = I2PConstants.ElGamalP.Subtract(pkey.ToBigInteger()).Subtract(BigInteger.One);

            BigInteger a, b;
            var        reader = new BufRefLen(data);

            if (zeropad)
            {
                reader.Seek(1);
                a = reader.ReadBigInteger(256);
                reader.Seek(1);
                b = reader.ReadBigInteger(256);
            }
            else
            {
                a = reader.ReadBigInteger(256);
                b = reader.ReadBigInteger(256);
            }

            var m2   = b.Multiply(a.ModPow(x, I2PConstants.ElGamalP));
            var m1   = m2.Mod(I2PConstants.ElGamalP);
            var m    = m1.ToByteArray(255);
            var hash = I2PHashSHA256.GetHash(m, 33, 222);

            if (!BufUtils.Equal(m, 1, hash, 0, 32))
            {
                throw new HashCheckFailException();
            }

            return(new BufLen(m, 33, 222));
        }
        public K GetWeightedRandom()
        {
            lock ( Wheel )
            {
                var pos    = 0f;
                var target = BufUtils.RandomFloat(TotalSpaceSum);

                foreach (var one in Wheel)
                {
                    pos += one.Space;
                    if (pos >= target)
                    {
                        RandomFitEMA = (49 * RandomFitEMA + one.Space + MinFit - 1) / 50f;
                        DebugUtils.LogDebug(() => "Roulette: " + one.Id.ToString());
                        return(one.Id);
                    }
                }

                return(Wheel[BufUtils.RandomInt(Wheel.Count)].Id);
            }
        }
Exemple #5
0
        public static BufLen Decrypt(BufLen data, I2PPrivateKey pkey, bool zeropad)
        {
            if (data == null || zeropad && data.Length != EncryptedPaddedLength)
            {
                throw new ArgumentException($"ElGamal padded data to decrypt must be exactly {EncryptedPaddedLength} bytes!");
            }

            if (!zeropad && data.Length != EncryptedShortLength)
            {
                throw new ArgumentException($"ElGamal data to decrypt must be exactly {EncryptedShortLength} bytes!");
            }

            var x = I2PConstants.ElGamalPMinusOne.Subtract(pkey.ToBigInteger());

            var reader = new BufRefLen(data);

            var readlen = zeropad
                        ? EncryptedPaddedLength / 2
                        : EncryptedShortLength / 2;

            var a = reader.ReadBigInteger(readlen);
            var b = reader.ReadBigInteger(readlen);

            var m2      = b.Multiply(a.ModPow(x, I2PConstants.ElGamalP));
            var m1      = m2.Mod(I2PConstants.ElGamalP);
            var m       = m1.ToByteArrayUnsigned();
            var payload = new BufLen(m, 33, ClearTextLength);
            var hash    = I2PHashSHA256.GetHash(payload);

            if (!BufUtils.Equal(m, 1, hash, 0, 32))
            {
                throw new HashCheckFailException();
            }

            return(payload);
        }
Exemple #6
0
 public static KeyValuePair <K, V> Random <K, V>(this IDictionary <K, V> dic)
 {
     return(dic.Skip(BufUtils.RandomInt(dic.Keys.Count - 1))
            .Take(1).FirstOrDefault());
 }
Exemple #7
0
 public static V RandomValue <K, V>(this IDictionary <K, V> dic)
 {
     return(dic[dic.Keys.Skip(BufUtils.RandomInt(dic.Keys.Count - 1))
                .Take(1).FirstOrDefault()]);
 }