Exemple #1
0
        public void McElieseGenericFormTest(int n, int k, int d, int t, int fieldPower, MatrixInt message, MatrixInt errorVector, MatrixInt scrambler, int[] permutation, int[] mask)
        {
            var galoisField = new GaloisField(2, fieldPower);
            var generator   = new ParityCheckMatrixGeneratorEllyptic(2);
            var linearCode  = new LinearCode(n, k, d, t, galoisField);

            while (true)
            {
                linearCode.ParityCheckMatrix = generator.Generate(linearCode);

                if (Helper.Weight(linearCode.ParityCheckMatrix) < Math.Ceiling(linearCode.ParityCheckMatrix.RowCount * linearCode.ParityCheckMatrix.ColumnCount * 0.7))
                {
                    continue;
                }

                try
                {
                    linearCode.GeneratorMatrix = GeneratorMatrixCalculator.CalculateGeneratorMatrixAlt(linearCode);
                }
                catch (LinearCodeException ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }

                if (Helper.Weight(MatrixAlgorithms.DotMultiplication(linearCode.GeneratorMatrix, linearCode.ParityCheckMatrix.Transpose(), galoisField)) == 0)
                {
                    linearCode.GeneratorMatrix = linearCode.GeneratorMatrix;
                    break;
                }
            }

            var crytptogram      = McElieseGenericForm.Encrypt(linearCode, scrambler, permutation, mask, generator, message, errorVector);
            var decryptedMessage = McElieseGenericForm.Decrypt(linearCode, permutation, mask, scrambler, generator, crytptogram);

            Assert.True(message == decryptedMessage);
        }
        public McElieseEllyptic(int n, int k, int d, int t, GaloisField galoisField, MatrixInt scramblerMatrix = null, IList <int> permutation = null, IList <int> mask = null)
        {
            _generator = new ParityCheckMatrixGeneratorEllyptic(2);
            LinearCode = new LinearCode(n, k, d, t, galoisField);

            MatrixInt parityCheckMatrix = null;
            MatrixInt generatorMatrix   = null;

            while (true)
            {
                parityCheckMatrix            = _generator.Generate(LinearCode);
                LinearCode.ParityCheckMatrix = parityCheckMatrix;
                Debug.WriteLine(parityCheckMatrix);

                var minValueFillPercentage = 0.7;

                if (Helper.Weight(parityCheckMatrix) < Math.Ceiling(parityCheckMatrix.RowCount * parityCheckMatrix.ColumnCount * minValueFillPercentage))
                {
                    continue;
                }

                try
                {
                    generatorMatrix = GeneratorMatrixCalculator.CalculateGeneratorMatrixAlt(LinearCode);
                    Debug.WriteLine(generatorMatrix);
                }
                catch (LinearCodeException ex)
                {
                    Debug.WriteLine(ex.Message);
                    continue;
                }

                if (IsGeneratorMatrixValid(parityCheckMatrix, generatorMatrix, galoisField))
                {
                    LinearCode.GeneratorMatrix = generatorMatrix;
                    break;
                }
            }
            if (scramblerMatrix is null)
            {
                scramblerMatrix = Helper.GenerateScramblerMatrix(LinearCode.K);
                while (true)
                {
                    try
                    {
                        MatrixAlgorithms.MatrixInverse(scramblerMatrix, galoisField);
                        break;
                    }
                    catch (SolveMatrixException)
                    {
                        Debug.WriteLine("Reattempting to generate scrambler matrix");
                    }
                }
            }
            if (permutation is null)
            {
                permutation = Helper.GeneratePermutaionList(LinearCode.N);
            }
            if (mask is null)
            {
                mask = Helper.GenerateMask(LinearCode.N, LinearCode.GaloisField);
            }
            var inverseMask = new List <int>(LinearCode.N);

            for (int i = 0; i < LinearCode.N; i++)
            {
                inverseMask.Add(LinearCode.GaloisField.GetMultiplicativeInverse(mask[i]));
            }
            PrivateKey = new PrivateKey
            {
                GeneratorMatrix        = LinearCode.GeneratorMatrix,
                ScramblerMatrix        = scramblerMatrix,
                InverseScramblerMatrix = MatrixAlgorithms.MatrixInverse(scramblerMatrix, galoisField),
                Permutation            = permutation,
                InversePermutation     = Helper.InversePermutation(permutation),
                Mask        = mask,
                InverseMask = inverseMask
            };

            var encryptionMatrix = MatrixAlgorithms.DotMultiplication(PrivateKey.ScramblerMatrix, generatorMatrix, LinearCode.GaloisField);

            Debug.WriteLine(encryptionMatrix);
            encryptionMatrix = encryptionMatrix.PermuteColumns(PrivateKey.Permutation);
            Debug.WriteLine(encryptionMatrix);
            for (int col = 0; col < encryptionMatrix.ColumnCount; col++)
            {
                for (int row = 0; row < encryptionMatrix.RowCount; row++)
                {
                    encryptionMatrix[row, col] = LinearCode.GaloisField.MultiplyWords(encryptionMatrix[row, col], PrivateKey.Mask[col]);
                }
            }
            Debug.WriteLine(encryptionMatrix);

            PublicKey = new PublicKey
            {
                EncryptionMatrix = encryptionMatrix,
            };
        }