Exemplo n.º 1
0
        public void TestTrivialFixed()
        {
            var sss = new TrivialFixedShamirsSecretSharing();

            var sh = sss.Split(129, 6, 3); /* split the secret value 129 into 6 components - at least 3 of which will be needed to figure out the secret value */

            var newshares = new List <Tuple <int, int> >();

            // newshares.Add(sh[1]);
            // newshares.Add(sh[3]);
            // newshares.Add(sh[4]); //[sh[1], sh[3], sh[4]]; /* pick any selection of 3 shared keys from sh */

            newshares.Add(sh[1]);
            newshares.Add(sh[2]);
            newshares.Add(sh[3]); //[sh[1], sh[3], sh[4]]; /* pick any selection of 3 shared keys from sh */


            Assert.Equal(129, sss.Join(newshares));
        }
Exemplo n.º 2
0
        public void TestTrivialFixed_Matches_Trivial(int available, int needed)
        {
            var sss1 = new TrivialFixedShamirsSecretSharing();
            var sss2 = new TrivialShamirsSecretSharing();

            var secretInt = 129;
            var split1    = sss1.Split(secretInt, available, needed);
            var shares1   = split1.Select(x =>
            {
                var bytes = new byte[sizeof(int) * 2];

                Array.Copy(BitConverter.GetBytes(x.Item1), 0, bytes, 0, sizeof(int));
                Array.Copy(BitConverter.GetBytes(x.Item2), 0, bytes, sizeof(int), sizeof(int));

                return(bytes);
            });

            var split2  = sss2.Split(secretInt, available, needed);
            var shares2 = sss2.Shares;

            //Assert.Equal(shares1, shares2);

            var rand       = new Random();
            var indexOrder = split1.OrderBy(x => rand.Next()).Select(x => x.Item1 - 1).Take(needed);
            // var joinShares1 = split1.Where(x => indexOrder.Contains(x.Item1));
            // var joinShares2 = split2.Where(x => indexOrder.Contains(x.Item1));

            var joinShares1 = indexOrder.Select(x => split1.ElementAt(x));
            var joinShares2 = indexOrder.Select(x => split2.ElementAt(x));

            Console.WriteLine($"Indexes : " + string.Join(",", indexOrder));
            Console.WriteLine($"Indexes1: {joinShares1.Count()} " + string.Join(",", joinShares1.Select(x => x.Item1 - 1)));
            Console.WriteLine($"Indexes2: {joinShares2.Count()} " + string.Join(",", joinShares2.Select(x => x.Item1 - 1)));

            var join1 = sss1.Join(joinShares1.ToList());
            var join2 = sss2.Join(joinShares2.ToArray());

            Assert.Equal(secretInt, join1);
            // Assert.Equal(join1, join2);
        }