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);
        }
Exemple #5
0
        public void FinitePointToString()
        {
            var split = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());
            FinitePoint <BigInteger> fp = split.MakeShares(3, 7, 500).First();
            string s1 = fp.ToString();
            string s2 = new FinitePoint <BigInteger>(s1).ToString();

            Assert.Equal(s1, s2);
        }
Exemple #6
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;
     }
 }
        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 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);
        }
Exemple #12
0
        public static List <string> createSharedSecret(string secretInput, int minimum, int total)
        {
            // Create Shamir's Secret Sharing instance with BigInteger
            var split = new ShamirsSecretSharing <BigInteger>(gcd);

            string password = secretInput;
            // Minimum number of shared secrets for reconstruction: minimum
            // Maximum number of shared secrets: total
            // Attention: The secret length changes the security level set by the ctor
            var x = split.MakeShares(minimum, total, password);

            List <string> secrets = new List <string>();

            foreach (var item in x.Item2)
            {
                secrets.Add(item.ToString());
            }

            return(secrets);
        }
        public void TestMinimumSharedSecretsMake()
        {
            var sss = new ShamirsSecretSharing <BigInteger>(new ExtendedEuclideanAlgorithm <BigInteger>());

            Assert.Throws <ArgumentOutOfRangeException>(() => sss.MakeShares(1, 7, 5));
        }