public void MaximumExceeded()
        {
            const string longSecret =
                "-----BEGIN EC PRIVATE KEY-----MIIBUQIBAQQgxq7AWG9L6uleuTB9q5FGqnHjXF+kD4y9154SLYYKMDqggeMwgeACAQEwLAYHKoZIzj0BAQIhAP////////////////////////////////////7///wvMEQEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABwRBBHm+Zn753LusVaBilc6HCwcCm/zbLc4o2VnygVsW+BeYSDradyajxGVdpPv8DhEIqP0XtEimhVQZnEfQj/sQ1LgCIQD////////////////////+uq7c5q9IoDu/0l6M0DZBQQIBAaFEA0IABE0XO6I8lZYzXqRQnHP/knSwLex7q77g4J2AN0cVyrADicGlUr6QjVIlIu9NXCHxD2i++ToWjO1zLVdxgNJbUUc=-----END EC PRIVATE KEY-----";
            var split            = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var combine          = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var shares           = split.MakeShares(3, 7, longSecret, 1024);
            var subSet1          = shares.Where(p => p.X.IsEven).ToList();
            var recoveredSecret1 = combine.Reconstruction(subSet1.ToArray());
            var subSet2          = shares.Where(p => !p.X.IsEven).ToList();
            var recoveredSecret2 = combine.Reconstruction(subSet2.ToArray());

            Assert.Equal(longSecret, recoveredSecret1);
            Assert.Equal(longSecret, recoveredSecret2);
        }
        public void ReconstructionFailsAtRnd(int byteArraySize)
        {
            int       ok    = 0;
            const int total = 1000;
            var       sss   = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var       rng   = new Random();

            for (int i = 0; i < total; i++)
            {
                var message = new byte[byteArraySize];
                rng.NextBytes(message);
                const int n             = 5;
                var       s             = Convert.ToBase64String(message);
                var       secret        = new Secret <BigInteger>(s);
                var       shares        = sss.MakeShares((n + 1) / 2, n, secret);
                var       reconstructed =
                    Convert.FromBase64String(sss.Reconstruction(shares.Take((n + 1) / 2).ToArray())
                                             .ToBase64());
                if (message.SequenceEqual(reconstructed))
                {
                    ok++;
                }
            }

            Assert.Equal(1.0, (double)ok / total);
        }
        public void TestReconstructFromStringArray()
        {
            var combine = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var secret  = combine.Reconstruction(TestData.GetPredefinedShares());

            Assert.Equal(TestData.DefaultTestPassword, secret);
        }
        public void TestMinimumSharedSecretsReconstruction()
        {
            var sss    = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var shares = sss.MakeShares(2, 7, 13);
            var subSet = shares.Where(p => p.X == Calculator <BigInteger> .One).ToList();

            Assert.Throws <ArgumentOutOfRangeException>(() => sss.Reconstruction(subSet.ToArray()));
        }
        public void TestShareThreshold()
        {
            var sss    = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var shares = sss.MakeShares(3, 7, 51);
            var subSet = shares.Take(2).ToList();
            var secret = sss.Reconstruction(subSet.ToArray());

            Assert.NotEqual(shares.OriginalSecret, secret);
        }
        public void TestWithRandomSecret(int splitSecurityLevel, int expectedSecurityLevel)
        {
            var split   = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var combine = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var shares  = split.MakeShares(3, 7, splitSecurityLevel);

            Assert.True(shares.OriginalSecretExists);
            Assert.NotNull(shares.OriginalSecret);
            var secret           = shares.OriginalSecret;
            var subSet1          = shares.Where(p => p.X.IsEven).ToList();
            var recoveredSecret1 = combine.Reconstruction(subSet1.ToArray());
            var subSet2          = shares.Where(p => !p.X.IsEven).ToList();
            var recoveredSecret2 = combine.Reconstruction(subSet2.ToArray());

            Assert.Equal(secret, recoveredSecret1);
            Assert.Equal(secret, recoveredSecret2);
            Assert.Equal(expectedSecurityLevel, split.SecurityLevel);
        }
        public void TestSecurityLevelAutoDetection(object secret, int expectedSecurityLevel)
        {
            var split   = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var combine = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());

            Shares <BigInteger> shares = null;

            switch (secret)
            {
            case string password:
                shares = split.MakeShares(3, 7, password);
                break;

            case BigInteger number:
                shares = split.MakeShares(3, 7, number);
                break;

            case null:
                return;
            }

            Assert.NotNull(shares);
            Assert.True(shares.OriginalSecretExists);
            Assert.NotNull(shares.OriginalSecret);
            var subSet1          = shares.Where(p => p.X.IsEven).ToList();
            var recoveredSecret1 = combine.Reconstruction(subSet1.ToArray());
            var subSet2          = shares.Where(p => !p.X.IsEven).ToList();
            var recoveredSecret2 = combine.Reconstruction(subSet2.ToArray());

            switch (secret)
            {
            case string password:
                Assert.Equal(password, recoveredSecret1);
                break;

            case BigInteger number:
                Assert.Equal(number, (BigInteger)recoveredSecret1);
                break;
            }

            Assert.Equal(shares.OriginalSecret, recoveredSecret1);
            Assert.Equal(shares.OriginalSecret, recoveredSecret2);
            Assert.Equal(expectedSecurityLevel, split.SecurityLevel);
        }
Exemplo n.º 8
0
        public static string combineSharedSecrets(List <string> secrets)
        {
            List <FinitePoint <BigInteger> > points = new List <FinitePoint <BigInteger> >();

            foreach (var secret in secrets)
            {
                points.Add(FinitePoint <BigInteger> .Parse(secret));
            }

            var combine = new ShamirsSecretSharing <BigInteger>(gcd);

            return(combine.Reconstruction(points.ToArray()).ToString());
        }
        public void TestReconstructFromString()
        {
            StringBuilder sharesChunk = new StringBuilder();

            foreach (var share in TestData.GetPredefinedShares())
            {
                sharesChunk.AppendLine(share);
            }

            var combine = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            var secret  = combine.Reconstruction(sharesChunk.ToString());

            Assert.Equal(TestData.DefaultTestPassword, secret);
        }
 public void TestReconstructFromStringArrayLegacy()
 {
     Secret.LegacyMode.Value = true;
     try
     {
         var combine = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
         var secret  = combine.Reconstruction(TestData.GetPredefinedSharesLegacy());
         Assert.Equal(TestData.DefaultTestPassword, secret);
     }
     finally
     {
         Secret.LegacyMode.Value = false;
     }
 }