public static BigInteger Hash(LotteryTicket lt)
        {
            var serialized = JsonConvert.SerializeObject(lt);
            var hash       = Cryptography.GenericHashNoKey(serialized).ToList();

            return(new BigInteger(1, hash.ToArray()));
        }
Example #2
0
        public async Task <SignedLotteryTicket> GenerateSignedLotteryTicket(ulong round)
        {
            var ticket     = LotteryTicket.Generate(round);
            var serialized = JsonConvert.SerializeObject(ticket);
            var hash       = Cryptography.GenericHashNoKey(serialized);

            var signedResponse = await OnionServiceClient.SignHashAsync(hash);

            var signedTicket = new SignedLotteryTicket()
            {
                Hash = hash, PublicKey = signedResponse.PublicKey, Signature = signedResponse.Signature
            };

            return(signedTicket);
        }
        public static LotteryTicket GenerateValidTarget(ulong round)
        {
            LotteryTicket lotteryTicket = null;
            object        ticketLock    = new object();

            Parallel.For(
                0,
                Environment.ProcessorCount,
                new ParallelOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            },
                (i, state) => {
                LotteryTicket lt = null;
                BigInteger hash  = BigInteger.Zero;
                int ct           = 1;

                do
                {
                    lt   = Generate(round);
                    hash = Hash(lt);
                    ct   = hash.CompareTo(PoWTarget);
                } while (ct == 1);

                lock (ticketLock)
                {
                    if (lotteryTicket == null)
                    {
                        lotteryTicket = lt;
                    }

                    state.Stop();
                }
            });

            return(lotteryTicket);
        }