Exemple #1
0
        public void learn_precomputed()
        {
            #region doc_precomputed
            // As an example, we will try to learn a decision machine
            // that can replicate the "exclusive-or" logical function:

            double[][] inputs =
            {
                new double[] { 0, 0 }, // the XOR function takes two booleans
                new double[] { 0, 1 }, // and computes their exclusive or: the
                new double[] { 1, 0 }, // output is true only if the two booleans
                new double[] { 1, 1 }  // are different
            };

            int[] xor = // this is the output of the xor function
            {
                0,      // 0 xor 0 = 0 (inputs are equal)
                1,      // 0 xor 1 = 1 (inputs are different)
                1,      // 1 xor 0 = 1 (inputs are different)
                0,      // 1 xor 1 = 0 (inputs are equal)
            };

            // Let's use a Gaussian kernel
            var kernel = new Gaussian(0.1);

            // Create a pre-computed Gaussian kernel matrix
            var precomputed = new Precomputed(kernel.ToJagged(inputs));

            // Now, we can create the sequential minimal optimization teacher
            var learn = new SequentialMinimalOptimization <Precomputed, int>()
            {
                Kernel = precomputed // set the precomputed kernel we created
            };

            // And then we can obtain the SVM by using Learn
            var svm = learn.Learn(precomputed.Indices, xor);

            // Finally, we can obtain the decisions predicted by the machine:
            bool[] prediction = svm.Decide(precomputed.Indices);

            // We can also compute the machine prediction to new samples
            double[][] sample =
            {
                new double[] { 0, 1 }
            };

            // Update the precomputed kernel with the new samples
            precomputed = new Precomputed(kernel.ToJagged2(inputs, sample));

            // Update the SVM kernel
            svm.Kernel = precomputed;

            // Compute the predictions to the new samples
            bool[] newPrediction = svm.Decide(precomputed.Indices);
            #endregion

            Assert.AreEqual(prediction, Classes.Decide(xor));
            Assert.AreEqual(newPrediction.Length, 1);
            Assert.AreEqual(newPrediction[0], true);
        }
Exemple #2
0
        private static Precomputed FlippedMillerLoopDoubling(BN128G2 g2)
        {
            Fp2 x = g2.x, y = g2.y, z = g2.z;

            Fp2 a  = Fp._2_INV.Mul(x.Mul(y));             // a = x * y / 2
            Fp2 b  = y.Squared();                         // b = y^2
            Fp2 c  = z.Squared();                         // c = z^2
            Fp2 d  = c.Add(c).Add(c);                     // d = 3 * c
            Fp2 e  = Parameters.B_Fp2.Mul(d);             // e = twist_b * d
            Fp2 f  = e.Add(e).Add(e);                     // f = 3 * e
            Fp2 g  = Fp._2_INV.Mul(b.Add(f));             // g = (b + f) / 2
            Fp2 h  = y.Add(z).Squared().Sub(b.Add(c));    // h = (y + z)^2 - (b + c)
            Fp2 i  = e.Sub(b);                            // i = e - b
            Fp2 j  = x.Squared();                         // j = x^2
            Fp2 e2 = e.Squared();                         // e2 = e^2

            Fp2 rx = a.Mul(b.Sub(f));                     // rx = a * (b - f)
            Fp2 ry = g.Squared().Sub(e2.Add(e2).Add(e2)); // ry = g^2 - 3 * e^2
            Fp2 rz = b.Mul(h);                            // rz = b * h

            Fp2 ell0  = Parameters.TWIST.Mul(i);          // ell_0 = twist * i
            Fp2 ellVW = h.Negate();                       // ell_VW = -h
            Fp2 ellVV = j.Add(j).Add(j);                  // ell_VV = 3 * j

            return(Precomputed.Of(
                       new BN128G2(rx, ry, rz),
                       new EllCoeffs(ell0, ellVW, ellVV)
                       ));
        }
Exemple #3
0
        private static Precomputed FlippedMillerLoopMixedAddition(BN128G2 base_val, BN128G2 addend)
        {
            Fp2 x1 = addend.x, y1 = addend.y, z1 = addend.z;
            Fp2 x2 = base_val.x, y2 = base_val.y;

            Fp2 d = x1.Sub(x2.Mul(z1));                                 // d = x1 - x2 * z1
            Fp2 e = y1.Sub(y2.Mul(z1));                                 // e = y1 - y2 * z1
            Fp2 f = d.Squared();                                        // f = d^2
            Fp2 g = e.Squared();                                        // g = e^2
            Fp2 h = d.Mul(f);                                           // h = d * f
            Fp2 i = x1.Mul(f);                                          // i = x1 * f
            Fp2 j = h.Add(z1.Mul(g)).Sub(i.Dbl());                      // j = h + z1 * g - 2 * i

            Fp2 x3 = d.Mul(j);                                          // x3 = d * j
            Fp2 y3 = e.Mul(i.Sub(j)).Sub(h.Mul(y1));                    // y3 = e * (i - j) - h * y1)
            Fp2 z3 = z1.Mul(h);                                         // z3 = Z1*H

            Fp2 ell0  = Parameters.TWIST.Mul(e.Mul(x2).Sub(d.Mul(y2))); // ell_0 = TWIST * (e * x2 - d * y2)
            Fp2 ellVV = e.Negate();                                     // ell_VV = -e
            Fp2 ellVW = d;                                              // ell_VW = d

            return(Precomputed.Of(
                       new BN128G2(x3, y3, z3),
                       new EllCoeffs(ell0, ellVW, ellVV)
                       ));
        }
Exemple #4
0
    public void SetLineOnPath(int line, int from, int to, double s, double t)
    {
        var lineObj = GetManagedObject(line, "SetLineOnPath.Line");
        var lr      = lineObj.GetComponent <LineRenderer>();
        var points  = Precomputed.GetLinePath(from, to, s, t);

        lr.SetWidth(GetNoteScale(s), GetNoteScale(t));
        lr.SetVertexCount(points.Length);
        lr.SetPositions(points);
        if (SceneSettings.Hidden)
        {
            lr.material.color = new Color(1, 1, 1, 1 - (float)s);
        }
    }
        private void GenerateElGamal(Stream outSecret, Stream outPublic)
        {
            // Prepare a strong Secure Random with seed
            SecureRandom secureRandom = PgpEncryptionUtil.GetSecureRandom();

            IAsymmetricCipherKeyPairGenerator dsaKpg = GeneratorUtilities.GetKeyPairGenerator("DSA");
            DsaParametersGenerator            pGen   = new DsaParametersGenerator();

            pGen.Init((int)PublicKeyLength.BITS_1024, 80, new SecureRandom());  // DSA is 1024 even for long 2048+ ElGamal keys
            DsaParameters dsaParams        = pGen.GenerateParameters();
            DsaKeyGenerationParameters kgp = new DsaKeyGenerationParameters(secureRandom, dsaParams);

            dsaKpg.Init(kgp);

            //
            // this takes a while as the key generator has to Generate some DSA parameters
            // before it Generates the key.
            //
            AsymmetricCipherKeyPair           dsaKp  = dsaKpg.GenerateKeyPair();
            IAsymmetricCipherKeyPairGenerator elgKpg = GeneratorUtilities.GetKeyPairGenerator("ELGAMAL");

            Group elgamalGroup = Precomputed.GetElGamalGroup((int)this.publicKeyLength);

            if (elgamalGroup == null)
            {
                throw new ArgumentException("ElGamal Group not found for key length: " + this.publicKeyLength);
            }

            Org.BouncyCastle.Math.BigInteger p = elgamalGroup.GetP();
            Org.BouncyCastle.Math.BigInteger g = elgamalGroup.GetG();

            secureRandom = PgpEncryptionUtil.GetSecureRandom();
            ElGamalParameters elParams           = new ElGamalParameters(p, g);
            ElGamalKeyGenerationParameters elKgp = new ElGamalKeyGenerationParameters(secureRandom, elParams);

            elgKpg.Init(elKgp);

            //
            // this is quicker because we are using preGenerated parameters.
            //
            AsymmetricCipherKeyPair elgKp = elgKpg.GenerateKeyPair();

            DsaElGamalKeyGeneratorUtil.ExportKeyPair(outSecret, outPublic, dsaKp, elgKp, identity, passphrase, true);
        }
Exemple #6
0
        private static List <EllCoeffs> CalcEllCoeffs(Bn128Fp2 baseElement)
        {
            List <EllCoeffs> coeffs = new List <EllCoeffs>();

            Bn128Fp2 addend = baseElement;

            // for each bit except most significant one
            for (int i = LoopCount.BitLength() - 2; i >= 0; i--)
            {
                Precomputed doubling = FlippedMillerLoopDoubling(addend);

                addend = doubling.G2;
                coeffs.Add(doubling.Coeffs);

                if (LoopCount.TestBit(i))
                {
                    Precomputed additionInLoop = FlippedMillerLoopMixedAddition(baseElement, addend);
                    addend = additionInLoop.G2;
                    coeffs.Add(additionInLoop.Coeffs);
                }
            }

            Bn128Fp2 q1 = baseElement.MulByP();
            Bn128Fp2 q2 = q1.MulByP();

            q2 = new Bn128Fp2(q2.X, q2.Y.Negate(), q2.Z); // q2.y = -q2.y

            Precomputed addition = FlippedMillerLoopMixedAddition(q1, addend);

            addend = addition.G2;
            coeffs.Add(addition.Coeffs);

            addition = FlippedMillerLoopMixedAddition(q2, addend);
            coeffs.Add(addition.Coeffs);

            return(coeffs);
        }
Exemple #7
0
        private static List <EllCoeffs> CalcEllCoeffs(BN128G2 base_val)
        {
            List <EllCoeffs> coeffs = new List <EllCoeffs>();

            BN128G2     addend = base_val;
            Precomputed addition;

            // for each bit except most significant one
            for (int i = LOOP_COUNT.BitLength - 2; i >= 0; i--)
            {
                Precomputed doubling = FlippedMillerLoopDoubling(addend);

                addend = doubling.g2;
                coeffs.Add(doubling.coeffs);

                if (LOOP_COUNT.TestBit(i))
                {
                    addition = FlippedMillerLoopMixedAddition(base_val, addend);
                    addend   = addition.g2;
                    coeffs.Add(addition.coeffs);
                }
            }

            BN128G2 q1 = base_val.MulByP();
            BN128G2 q2 = q1.MulByP();

            q2 = new BN128G2(q2.x, q2.y.Negate(), q2.z); // q2.y = -q2.y

            addition = FlippedMillerLoopMixedAddition(q1, addend);
            addend   = addition.g2;
            coeffs.Add(addition.coeffs);

            addition = FlippedMillerLoopMixedAddition(q2, addend);
            coeffs.Add(addition.coeffs);

            return(coeffs);
        }
Exemple #8
0
        private void PrecomputedAttack(BitArray[] plainTextArray, BitArray[] cipherTextArray)
        {
            var           KeyList = Precomputed.Attack(plainTextArray, cipherTextArray);
            StringBuilder sB      = new StringBuilder("Keys found: ");

            foreach (var key in KeyList)
            {
                sB.Append(key);
                sB.Append(", ");
            }
            sB.Remove(sB.Length - 2, 2);
            Console.WriteLine(sB.ToString());
            var goodKeys = Bruteforce.KeysForAll(KeyList, plainTextArray, cipherTextArray);

            sB.Clear();
            sB.Append("Keys that work for all the pairs: ");
            foreach (var key in goodKeys)
            {
                sB.Append(key);
                sB.Append(", ");
            }
            sB.Remove(sB.Length - 2, 2);
            Console.WriteLine(sB.ToString());
        }
        public void multiclass_precomputed_matrix_smo()
        {
            #region doc_precomputed
            // Let's say we have the following data to be classified
            // into three possible classes. Those are the samples:
            //
            double[][] trainInputs =
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 1, 0 }, //  0
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 1, 1, 1 }, //  2
                new double[] { 1, 0, 1, 1 }, //  2
                new double[] { 1, 1, 0, 1 }, //  2
                new double[] { 0, 1, 1, 1 }, //  2
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] trainOutputs = // those are the training set class labels
            {
                0, 0, 0, 0, 0,
                1, 1, 1, 1, 1,
                2, 2, 2, 2, 2,
            };

            // Let's chose a kernel function
            Polynomial kernel = new Polynomial(2);

            // Get the kernel matrix for the training set
            double[][] K = kernel.ToJagged(trainInputs);

            // Create a pre-computed kernel
            var pre = new Precomputed(K);

            // Create a one-vs-one learning algorithm using SMO
            var teacher = new MulticlassSupportVectorLearning <Precomputed, int>()
            {
                Learner = (p) => new SequentialMinimalOptimization <Precomputed, int>()
                {
                    Kernel = pre
                }
            };

#if DEBUG
            teacher.ParallelOptions.MaxDegreeOfParallelism = 1;
#endif

            // Learn a machine
            var machine = teacher.Learn(pre.Indices, trainOutputs);

            // Compute the machine's prediction for the training set
            int[] trainPrediction = machine.Decide(pre.Indices);

            // Evaluate prediction error for the training set using mean accuracy (mAcc)
            double trainingError = new ZeroOneLoss(trainOutputs).Loss(trainPrediction);

            // Now let's compute the machine's prediction for a test set
            double[][] testInputs = // test-set inputs
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] testOutputs = // those are the test set class labels
            {
                0, 0, 1, 2,
            };

            // Compute precomputed matrix between train and testing
            pre.Values = kernel.ToJagged2(trainInputs, testInputs);

            // Update the kernel
            machine.Kernel = pre;

            // Compute the machine's prediction for the test set
            int[] testPrediction = machine.Decide(pre.Indices);

            // Evaluate prediction error for the training set using mean accuracy (mAcc)
            double testError = new ZeroOneLoss(testOutputs).Loss(testPrediction);
            #endregion


            Assert.AreEqual(0, trainingError);
            Assert.AreEqual(0, testError);

            // Create a one-vs-one learning algorithm using SMO
            var teacher2 = new MulticlassSupportVectorLearning <Polynomial>()
            {
                Learner = (p) => new SequentialMinimalOptimization <Polynomial>()
                {
                    Kernel = kernel
                }
            };

#if DEBUG
            teacher.ParallelOptions.MaxDegreeOfParallelism = 1;
#endif

            // Learn a machine
            var expected = teacher2.Learn(trainInputs, trainOutputs);

            Assert.AreEqual(4, expected.NumberOfInputs);
            Assert.AreEqual(3, expected.NumberOfOutputs);
            Assert.AreEqual(0, machine.NumberOfInputs);
            Assert.AreEqual(3, machine.NumberOfOutputs);

            var machines = Enumerable.Zip(machine, expected, (a, b) => Tuple.Create(a.Value, b.Value));

            foreach (var pair in machines)
            {
                var a = pair.Item1;
                var e = pair.Item2;

                Assert.AreEqual(0, a.NumberOfInputs);
                Assert.AreEqual(2, a.NumberOfOutputs);

                Assert.AreEqual(4, e.NumberOfInputs);
                Assert.AreEqual(2, e.NumberOfOutputs);

                Assert.IsTrue(a.Weights.IsEqual(e.Weights));
            }
        }