public void Test_Shamir_InitiateSecretWith_Y_P_Array()
        {
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            var n = 10;
            var k = 3;
            var secret = 1234;
            //assign
            var shares = shamir.DivideSecret(k, n,secret);
            var initiatedShares = new List<SecretSharingCore.Common.IShareCollection>();
            //assert
            Assert.AreEqual(shares.Count, n);
            int j = 0;
            foreach (var col in shares)
            {
                j++;
                SecretSharingCore.Common.IShareCollection collection = new SecretSharingCore.Common.ShareCollection();
                for (int i = 0; i < col.GetCount(); i++)
                {

                    Assert.IsNotNull(col.GetShare(i).GetY());
                    Assert.IsNotNull(col.GetShare(i).GetP());

                    SecretSharingCore.Common.ShamirShare myshare = new SecretSharingCore.Common.ShamirShare(col.GetShare(i).GetX(), col.GetShare(i).GetY()
                        , col.GetShare(i).GetP());

                    Assert.AreEqual(col.GetShare(i).ToString(), myshare.ToString());
                    collection.SetShare(i, myshare);
                }
                initiatedShares.Add(collection);
                if (j == k) break;
            }

            var reconsecret = shamir.ReconstructSecret(initiatedShares);
            Assert.AreEqual(secret,reconsecret);
        }
        public List <IShareCollection> DivideSecretWithChunkSizeWrapper(int n, int k, int ChunkSize, String Secret, ref List <long> ElapsedTicks
#if calcPrimeTime
                                                                        , ref double PrimeGenerationTime, ref double allTime
#endif
                                                                        )
        {
            if (n == 0)
            {
                n = 1;
            }
            if (k == 0)
            {
                k = 1;
            }
            Stopwatch sw = new Stopwatch();

            sw.Start();
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            var byteSecret = Encoding.UTF8.GetBytes(Secret.ToCharArray());

#if calcPrimeTime
            double primeElapsed = 0;
            var    a            = shamir.DivideSecret(k, n, byteSecret, ChunkSize, ref primeElapsed);
            PrimeGenerationTime += Math.Pow(primeElapsed, 2.0);
            allTime             += Math.Pow((double)sw.ElapsedTicks, 2.0);
#else
            var a = shamir.DivideSecret(k, n, byteSecret, ChunkSize);
#endif
            sw.Stop();
            ElapsedTicks.Add(sw.ElapsedTicks);
            return(a);
        }
 public void Test_Shamir_DivideStringSecret()
 {
     SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
     var n = 10;
     var k = 3;
     var secret = "1234567812345678123456781234567812345678123456781234567812345678";
     //assign
     var shares = shamir.DivideStringSecret(k, n, secret,(byte) 32);
     //assert
     Assert.AreEqual(shares.Count, n);
 }
        public void Test_Shamir_DivideSecret()
        {
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            var n = 10;
            var k = 3;
            var secret = "12345678";

            //assign
            var shares = shamir.DivideSecret(k, n,Encoding.UTF8.GetBytes(secret));
            //assert
            Assert.AreEqual(shares.Count, n);
        }
Exemple #5
0
        public void TestStringDivideSecret()
        {
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            var n      = 10;
            var k      = 3;
            var secret = "1234567812345678123456781234567812345678123456781234567812345678";
            //assign
            var shares = shamir.DivideStringSecret(k, n, secret, (byte)32);

            //assert
            Assert.AreEqual(shares.Count, n);
        }
Exemple #6
0
        public void TestDivideSecret()
        {
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            var n      = 10;
            var k      = 3;
            var secret = "12345678";
            //assign
            var shares = shamir.DivideSecret(k, n, Encoding.UTF8.GetBytes(secret));

            //assert
            Assert.AreEqual(shares.Count, n);
        }
Exemple #7
0
        public void TestStringReconstructSecret()
        {
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            var n      = 5;
            var k      = 1;
            var secret = "12345678123456781234567812345678";    //12345678123456781234567812345678";
            //assign
            var shares      = shamir.DivideStringSecret(k, n, secret, (byte)16);
            var kShares     = shares.GetRange(0, k);
            var reconsecret = shamir.ReconstructStringSecret(kShares, (byte)16);

            //assert
            Assert.AreEqual(shares.Count, n);
            Assert.AreEqual(secret, reconsecret);
        }
Exemple #8
0
        public void TestReconstruct_NoChunk()
        {
            //arrange
            var shamir = new SecretSharingCore.Algorithms.Shamir();;
            var n      = 10;
            var k      = 3;
            var secret = "32456";
            //assign
            var shares = shamir.DivideSecret(k, n, Encoding.UTF8.GetBytes(secret));

            var reconSecret = Encoding.UTF8.GetString(shamir.ReconstructSecret(shares));

            //assert
            //Assert.AreEqual(k, kPortionOfShares.Count);
            //Assert.AreEqual(shares.GetCount(), n);
            Assert.AreEqual(secret, reconSecret);
        }
 public void Test_Shamir_DivideSecretWithChunkSize()
 {
     SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
     var n = 10;
     var k = 3;
     var secret = "1234567890";
     var byteSecret = Encoding.UTF8.GetBytes(secret.ToCharArray());
     byte chunkSize = 5;
     //double a = 0;
     //assign
     var shares = shamir.DivideSecret(k, n, byteSecret,chunkSize
     #if calcPrimeTime
         ,ref a
     #endif
         );
     //assert
     Assert.AreEqual(shares.Count, n);
 }
        public void Test_Shamir_FailReconstructSecret()
        {
            //arrange
            var shamir = new SecretSharingCore.Algorithms.Shamir(); ;
            var n = 10;
            var k = 3;
            var secret = 2345;
            //assign
            var shares = shamir.DivideSecret(k,n,secret);

            var kPortionOfShares = shares.GetRange(0, k-1);

            var reconSecret = shamir.ReconstructSecret(kPortionOfShares);
            ////assert
            Assert.AreNotEqual(k, kPortionOfShares.Count);
            Assert.AreEqual(shares.Count, n);
            Assert.AreNotEqual(secret, reconSecret);
        }
Exemple #11
0
        public void FailReconstructSecretTest()
        {
            //arrange
            var shamir = new SecretSharingCore.Algorithms.Shamir();;
            var n      = 10;
            var k      = 3;
            var secret = 2345;
            //assign
            var shares = shamir.DivideSecret(k, n, secret);

            var kPortionOfShares = shares.GetRange(0, k - 1);

            var reconSecret = shamir.ReconstructSecret(kPortionOfShares);

            ////assert
            Assert.AreNotEqual(k, kPortionOfShares.Count);
            Assert.AreEqual(shares.Count, n);
            Assert.AreNotEqual(secret, reconSecret);
        }
Exemple #12
0
        public void TestDivideSecretWithChunkSize()
        {
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            var    n          = 10;
            var    k          = 3;
            var    secret     = "1234567890";
            var    byteSecret = Encoding.UTF8.GetBytes(secret.ToCharArray());
            byte   chunkSize  = 5;
            double a          = 0;
            //assign
            var shares = shamir.DivideSecret(k, n, byteSecret, chunkSize
#if calcPrimeTime
                                             , ref a
#endif
                                             );

            //assert
            Assert.AreEqual(shares.Count, n);
        }
        public void ReconstructSecretWithChunkSizeWrapper(List <IShareCollection> shares, int k, int chunkSize, ref List <long> elapsedTicks, string secret)
        {
            if (k == 0)
            {
                k = 1;
            }
            Stopwatch sw = new Stopwatch();

            sw.Start();
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            //assign
            var reconSecret = Encoding.UTF8.GetString(shamir.ReconstructSecret(shares.GetRange(0, k), chunkSize));

            sw.Stop();
            if (reconSecret != secret)
            {
                throw new Exception("Not able to recon secret!");
            }
            elapsedTicks.Add(sw.ElapsedTicks);
        }
Exemple #14
0
        public void TestInitiateSecretWith_Y_P_Array()
        {
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            var n      = 10;
            var k      = 3;
            var secret = 1234;
            //assign
            var shares          = shamir.DivideSecret(k, n, secret);
            var initiatedShares = new List <IShareCollection>();

            //assert
            Assert.AreEqual(shares.Count, n);
            int j = 0;

            foreach (var col in shares)
            {
                j++;
                IShareCollection collection = new ShareCollection();
                for (int i = 0; i < col.GetCount(); i++)
                {
                    Assert.IsNotNull(col.GetShare(i).GetY());
                    Assert.IsNotNull(col.GetShare(i).GetP());

                    ShamirShare myshare = new ShamirShare(col.GetShare(i).GetX(), col.GetShare(i).GetY()
                                                          , col.GetShare(i).GetP());

                    Assert.AreEqual(col.GetShare(i).ToString(), myshare.ToString());
                    collection.SetShare(i, myshare);
                }
                initiatedShares.Add(collection);
                if (j == k)
                {
                    break;
                }
            }

            var reconsecret = shamir.ReconstructSecret(initiatedShares);

            Assert.AreEqual(secret, reconsecret);
        }
Exemple #15
0
        public void TestReconstructSecretWithChunkSize()
        {
            SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
            var  n          = 10;
            var  k          = 3;
            var  secret     = "1234567890";
            var  byteSecret = Encoding.UTF8.GetBytes(secret.ToCharArray());
            byte chunkSize  = 5;
            //assign
            double a      = 0;
            var    shares = shamir.DivideSecret(k, n, byteSecret, chunkSize
#if calcPrimeTime
                                                , ref a
#endif
                                                );
            //if the secret array is not dividable to the chunk we have to truncate null values
            var reconSecret = Encoding.UTF8.GetString(shamir.ReconstructSecret(shares, chunkSize).Where(ch => ch != '\0').ToArray());

            //assert
            Assert.AreEqual(shares.Count, n);
            Assert.AreEqual(secret, reconSecret);
        }
 public void Test_Shamir_ReconstructSecretWithChunkSize()
 {
     SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
     var n = 10;
     var k = 3;
     var secret = "1234567890";
     var byteSecret = Encoding.UTF8.GetBytes(secret.ToCharArray());
     byte chunkSize = 5;
     //assign
     //double a= 0;
     var shares = shamir.DivideSecret(k, n, byteSecret, chunkSize
     #if calcPrimeTime
         ,ref a
     #endif
         );
     //if the secret array is not dividable to the chunk we have to truncate null values
     var reconSecret = Encoding.UTF8.GetString( shamir.ReconstructSecret(shares, chunkSize).Where(ch=>ch !='\0').ToArray());
     //assert
     Assert.AreEqual(shares.Count, n);
     Assert.AreEqual(secret, reconSecret);
 }
 public void Test_Shamir_ReconstructStringSecret()
 {
     SecretSharingCore.Algorithms.Shamir shamir = new SecretSharingCore.Algorithms.Shamir();
         var n = 5;
         var k = 1;
         var secret = "12345678123456781234567812345678";//12345678123456781234567812345678";
         //assign
         var shares = shamir.DivideStringSecret(k, n, secret, (byte)16);
         var kShares = shares.GetRange(0, k);
         var reconsecret = shamir.ReconstructStringSecret(kShares, (byte)16);
         //assert
         Assert.AreEqual(shares.Count, n);
         Assert.AreEqual(secret, reconsecret);
 }
        public void Test_Shamir_ReconstructNoChunk()
        {
            //arrange
            var shamir = new SecretSharingCore.Algorithms.Shamir(); ;
            var n = 10;
            var k = 3;
            var secret = "32456";
            //assign
            var shares = shamir.DivideSecret(k, n,Encoding.UTF8.GetBytes(secret));

            var reconSecret =Encoding.UTF8.GetString(  shamir.ReconstructSecret(shares));
            //assert
            //Assert.AreEqual(k, kPortionOfShares.Count);
            //Assert.AreEqual(shares.GetCount(), n);
            Assert.AreEqual(secret, reconSecret);
        }