public void TestMultBook()
        {
            EllipticCurve curve = new EllipticCurve(a: -1, b: 3231, modulus: 661643);

            Point q = (87, 2);

            q = curve.Multiply(BigInteger.Pow(2, 9), q);
            bool success = q.Equals((Point)(196083, 134895));

            q       = curve.Multiply(BigInteger.Pow(3, 6), q);
            success = success && q.Equals((Point)(470021, 282574));
            Assert.IsTrue(success);
        }
Exemplo n.º 2
0
        public EllipticDiffieHellman(EllipticCurve curve, Point generator, BigInteger order, byte[] priv = null)
        {
            this.curve     = curve;
            this.generator = generator;

            // Generate private key
            if (priv == null)
            {
                byte[] max = order.ToByteArray();
                do
                {
                    byte[] p1 = new byte[5 /*rand.Next(max.Length) + 1*/];

                    rand.NextBytes(p1);

                    if (p1.Length == max.Length)
                    {
                        p1[p1.Length - 1] %= max[max.Length - 1];
                    }
                    else
                    {
                        p1[p1.Length - 1] &= 127;
                    }

                    this.priv = new BigInteger(p1);
                } while (this.priv < 2);
            }
            else
            {
                this.priv = new BigInteger(priv);
            }

            // Generate public key
            pub = curve.Multiply(generator, this.priv);
        }
Exemplo n.º 3
0
        public static Point HashToPoint(byte[] toHash, EllipticCurve ec, BigInt cofactor)
        {
            Point P = HashToPoint(toHash, ec);

            P = ec.Multiply(P, cofactor);
            return(P);
        }
Exemplo n.º 4
0
        public void Multiply()
        {
            int     n = Form.NumericUpDownNValue;
            ECPoint s = (ECPoint)Form.ComboBoxS.SelectedItem;

            multiplicationResult = ellipticCurve.Multiply(n, s);
            Form.TextBoxMultiplicationResult.Text = multiplicationResult.ToString();
            Form.MultiplicationLogButtonEnabled   = true;
            DrawCurve();
        }
Exemplo n.º 5
0
        public byte[] GetSharedSecret(byte[] pK)
        {
            BitReader reader = new BitReader(pK);

            byte[] x            = reader.ReadByteArray();
            Point  remotePublic = new Point(
                new BigInteger(x),
                new BigInteger(reader.ReadByteArray(pK.Length - BinaryHelpers.VarIntSize(x.Length) - x.Length))
                );

            return(curve.Multiply(remotePublic, priv).X.ToByteArray()); // Use the x-coordinate as the shared secret
        }
Exemplo n.º 6
0
        public void MultiplyTest()
        {
            BigInt xA = new BigInt("489a03c58dcf7fcfc97e99ffef0bb4634", 16);
            BigInt yA = new BigInt("510c6972d795ec0c2b081b81de767f808", 16);
            BigInt l  = new BigInt("b8bbbc0089098f2769b32373ade8f0daf", 16);
            BigInt xE = new BigInt("073734b32a882cc97956b9f7e54a2d326", 16);
            BigInt yE = new BigInt("9c4b891aab199741a44a5b6b632b949f7", 16);
            Point  e  = new Point(xE, yE);
            Point  a  = new Point(xA, yA);

            Assert.AreEqual(e, curve.Multiply(a, l));
        }
Exemplo n.º 7
0
        public byte[] GetSharedSecretRaw(byte[] pK)
        {
            byte[] p1 = new byte[pK[0] | (pK[1] << 8) | (pK[2] << 16) | (pK[3] << 24)]; // Reconstruct x-axis size
            byte[] p2 = new byte[pK.Length - p1.Length - 4];
            Array.Copy(pK, 4, p1, 0, p1.Length);
            Array.Copy(pK, 4 + p1.Length, p2, 0, p2.Length);

            CurvePoint remotePublic = new CurvePoint(new BigInteger(p1), new BigInteger(p2));

            byte[] secret = curve.Multiply(remotePublic, priv).X.GetBytes(); // Use the x-coordinate as the shared secret

            return(secret);
        }
Exemplo n.º 8
0
        public void BillinearTest()
        {
            //using a predefined pairing
            Pairing e = Predefined.nssTate();

            //get P, which is a random point in group G1
            BigInt xP = new BigInt("6489939838247988945871981900040296813593014269345452667904622838482534881503698483366430292883248221510827517646942621360563883893977498988822397615847035", 10);
            BigInt yP = new BigInt("4183869719038127850866054323652097154062917818644838834002147757841525900395398794914246812190615424669832349278263049648914095684913634131502517423626958", 10);
            Point P = new Point(xP, yP);

            //get Q, which is a random point in group G2
            BigInt xQ = new BigInt("2954273822533893703877488768696651085799471510788466547935020721095428933759242911655447662730930250257332528194261492443088328780492269310876627460775540", 10);
            BigInt yQ = new BigInt("5618108911398484778214611016375606480759218944436047801309324726445485710683489022960503194997792712985027904009701406998265308597942813750269945637462064", 10);
            Point Q = new Point(xQ, yQ);

            //compute e(P,Q)
            FieldElement epq = e.Compute(P, Q);

            //the curve on which G1 is defined
            EllipticCurve g1 = e.Curve;
            //a is a 160-bit random integer
            BigInt a = new BigInt(160, new Random());
            //Point aP is computed over G1
            Point aP = g1.Multiply(P, a);

            //The curve on which G2 is defined
            EllipticCurve g2 = e.Curve2;
            //b is a 160-bit random integer
            BigInt b = new BigInt(160, new Random());
            //bQ is computed over G2
            Point bQ = g2.Multiply(Q, b);

            //compute e(aP,bQ)
            FieldElement res = e.Compute(aP, bQ);

            //compute e(P,Q)^ab, this is done in group Gt
            BigInt ab = a.Multiply(b);
            //get the field on which Gt is defined
            Field gt = e.Gt;
            FieldElement res2 = gt.Pow(epq, ab);

            //compare these two
            Assert.AreEqual(res, res2);
        }
Exemplo n.º 9
0
        public ECDiffieHellman(EllipticCurve curve, CurvePoint generator, BigInteger order, byte[] priv = null)
        {
            this.curve     = curve;
            this.generator = generator;

            // Generate private key
            if (priv == null)
            {
                this.priv = new BigInteger();
                this.priv.GenRandomBits(order.DataLength, rand);
            }
            else
            {
                this.priv = new BigInteger(priv);
            }

            // Generate public key
            pub = curve.Multiply(generator, this.priv);
        }
        public void TestMult()
        {
            EllipticCurve curve = new EllipticCurve(a: 1, b: 1, modulus: 23);

            Point[] expected = new Point[] {
                (0, 1), (6, 19), (3, 13), (13, 16), (18, 3), (7, 11),
                (11, 3), (5, 19), (19, 18), (12, 4), (1, 16), (17, 20),
                (9, 16), (4, 0), (9, 7), (17, 3), (1, 7), (12, 19),
                (19, 5), (5, 4), (11, 20), (7, 12), (18, 20), (13, 7),
                (3, 10), (6, 4), (0, 22), null
            };

            Point p = (0, 1);

            Point[] actual = new Point[expected.Length];

            for (int i = 0; i < expected.Length; i++)
            {
                actual[i] = curve.Multiply(i + 1, p);
            }

            CollectionAssert.AreEqual(expected, actual);
        }