Ejemplo n.º 1
0
		internal ECKeyPair (Number d, ECPoint Q, ECDomainParameters domain, ECDomainNames domainName)
		{
			_d = d;
			_Q = Q;
			_domain = domain;
			_domainName = domainName;
		}
Ejemplo n.º 2
0
		public ECDomainParameters (ECGroup group, ECPoint G, Number order, uint h, uint bits, IFiniteField fieldN, Uri uri)
		{
			_group = group;
			_G = G;
			_order = order;
			_h = h;
			_bits = bits;
			_fieldN = fieldN;
			_uri = uri;
		}
Ejemplo n.º 3
0
		public void ToByteArrayTest ()
		{
			ECDomainParameters domain = ECDomains.GetDomainParameter (ECDomainNames.secp192r1);
			ECGroup group = domain.Group;
			ECPoint p = domain.Group.FiniteField.GetInfinityPoint (group);
			ECPoint g = domain.G.Export ();
			byte[] tmp = p.ToByteArray (true);
			Assert.IsTrue (tmp.Length == 1, "#1");
			Assert.IsTrue (tmp[0] == 0, "#2");
			p = new ECPoint (group, tmp);
			Assert.IsTrue (p.IsInifinity (), "#3");

			tmp = domain.G.ToByteArray (false);
			Assert.IsTrue (tmp.Length == ((domain.Bits >> 3) + ((domain.Bits & 7) == 0 ? 0 : 1)) * 2 + 1, "#4");
			p = new ECPoint (group, tmp).Export ();
			Assert.IsTrue (p.X.CompareTo (g.X) == 0, "#5");
			Assert.IsTrue (p.Y.CompareTo (g.Y) == 0, "#6");

			tmp = domain.G.ToByteArray (true);
			Assert.IsTrue (tmp.Length == ((domain.Bits >> 3) + ((domain.Bits & 7) == 0 ? 0 : 1)) + 1, "#7");
			p = new ECPoint (group, tmp).Export ();
			Assert.IsTrue (p.X.CompareTo (g.X) == 0, "#8");
			Assert.IsTrue (p.Y.CompareTo (g.Y) == 0, "#9");
		}
Ejemplo n.º 4
0
		internal void CreatePublicKeyFromPrivateKey ()
		{
			_Q = _domain.G.Multiply (_d);
		}
Ejemplo n.º 5
0
		/// <param name="d">Private Key</param>
		/// <param name="Q">Public Key</param>
		internal ECKeyPair (Number d, ECPoint Q, ECDomainParameters domain)
			: this (d, Q, domain, ECDomainNames.none)
		{
			_domainName = ECDomains.GetDomainName (domain);
		}
Ejemplo n.º 6
0
		public ECDomainParameters (ECGroup group, ECPoint G, Number order, uint h, uint bits, IFiniteField fieldN)
			: this (group, G, order, h, bits, fieldN, null)
		{
		}
Ejemplo n.º 7
0
		void PointCompressTest (int bits, ECGroup group, ECPoint p, int repeats, string name)
		{
			int bytes = (bits >> 3) + ((bits & 7) == 0 ? 0 : 1) + 1;
			for (int i = 0; i < repeats; i ++) {
				byte[] tmp = p.ToByteArray (true);
				Assert.IsTrue (tmp.Length == bytes, name + " #1");
				ECPoint x = new ECPoint (group, tmp).Export ();
				ECPoint p2 = p.Export ();
				Assert.IsTrue (x.X.CompareTo (p2.X) == 0, name + " #2");
				Assert.IsTrue (x.Y.CompareTo (p2.Y) == 0, name + " #3");
				p = p.Multiply (Number.CreateRandomElement (group.P));
			}
		}
Ejemplo n.º 8
0
		public ECPoint Invert ()
		{
			if (_inv != null)
				return _inv;
			return _inv = new ECPoint (_group, _x, _field.Subtract (_field.Modulus, _y), _z);;
		}
Ejemplo n.º 9
0
		public static unsafe ECPoint MultiplyAndAdd (ECPoint p1, Number scaler1, ECPoint p2, Number scaler2)
		{
#if true
			int l = scaler1.BitCount ();
			if (l < scaler2.BitCount ())
				l = scaler2.BitCount ();
			int* b1 = stackalloc int[l >> 2];
			int* b2 = stackalloc int[l >> 2];
			int* e1 = stackalloc int[l >> 2];
			int* e2 = stackalloc int[l >> 2];
			int d1 = ComputeSignedWindowDecomposition (scaler1, b1, e1) - 1;
			int d2 = ComputeSignedWindowDecomposition (scaler2, b2, e2) - 1;
			ECPoint[] p1ary = p1.SetupMultiplyHelperPoints ();
			ECPoint[] p2ary = p2.SetupMultiplyHelperPoints ();
			int lastE;
			ECPoint Q;
			if (e1[d1] == e2[d2]) {
				Q = p1ary[b1[d1]].Add (p2ary[b2[d2--]]);
				lastE = e1[d1--];
			} else if (e1[d1] > e2[d2]) {
				Q = p1ary[b1[d1]];
				lastE = e1[d1--];
			} else {
				Q = p2ary[b2[d2]];
				lastE = e2[d2--];
			}
			while (d1 >= 0 || d2 >= 0) {
				int nextE;
				int nextType = 0;
				if (d1 >= 0 && d2 >= 0 && e1[d1] == e2[d2]) {
					nextE = e1[d1];
					nextType = 0;
				} else if ((d1 >= 0 && d2 < 0) || (d1 >= 0 && e1[d1] > e2[d2])) {
					nextE = e1[d1];
					nextType = 1;
				} else {
					nextE = e2[d2];
					nextType = 2;
				}
				for (int k = 0; k < lastE - nextE; k ++)
					Q = Q.Double ();
				if (nextType == 0 || nextType == 1) {
					if (b1[d1] > 0)
						Q = Q.Add (p1ary[b1[d1--]]);
					else
						Q = Q.Add (p1ary[-b1[d1--]].Invert ());
				}
				if (nextType == 0 || nextType == 2) {
					if (b2[d2] > 0)
						Q = Q.Add (p2ary[b2[d2--]]);
					else
						Q = Q.Add (p2ary[-b2[d2--]].Invert ());
				}
				lastE = nextE;
			}
			for (int k = 0; k < lastE; k++)
				Q = Q.Double ();
			return Q;
#else
			int l = scaler1.BitCount ();
			if (l < scaler2.BitCount ())
				l = scaler2.BitCount ();
			ECPoint Z = p1.Add (p2);
			ECPoint R = p1._field.GetInfinityPoint (p1._group);
			for (int i = l - 1; i >= 0; i --) {
				R = R.Double ();
				uint ki = scaler1.GetBit (i);
				uint li = scaler2.GetBit (i);
				if (ki == 0) {
					if (li == 1)
						R = R.Add (p2);
				} else {
					if (li == 0)
						R = R.Add (p1);
					else
						R = R.Add (Z);
				}
			}
			return R;
#endif
		}
Ejemplo n.º 10
0
		ECPoint[] SetupMultiplyHelperPoints ()
		{
			if (_multiplyHelperPoints != null)
				return _multiplyHelperPoints;
			ECPoint[] P = new ECPoint [1 << (MultiplyWindowSize - 1)];
			P[1] = this;
			P[2] = this.Double ();
			for (int i = 3; i < P.Length; i += 2)
				P[i] = P[i - 2].Add (P[2]);
			_multiplyHelperPoints = P;
			return P;
		}
Ejemplo n.º 11
0
		public ECPoint Add (ECPoint other)
		{
			if (this.IsInifinity ())
				return other;
			if (other.IsInifinity ())
				return this;
			Number z1p2 = this._z2, z2p2 = other._z2, z1p3 = this._z3, z2p3 = other._z3;
			if (z1p2 == null) this._z2 = z1p2 = _field.Multiply (_z, _z);
			if (z2p2 == null) other._z2 = z2p2 = _field.Multiply (other._z, other._z);
			if (z1p3 == null) this._z3 = z1p3 = _field.Multiply (z1p2, this._z);
			if (z2p3 == null) other._z3 = z2p3 = _field.Multiply (z2p2, other._z);

			Number u1 = _field.Multiply (_x, z2p2);
			Number u2 = _field.Multiply (other._x, z1p2);
			Number H = _field.Subtract (u2, u1);

			Number s1 = _field.Multiply (_y, z2p3);
			Number s2 = _field.Multiply (other._y, z1p3);
			Number r = _field.Subtract (s2, s1);
			if (H.IsZero ()) {
				if (r.IsZero ())
					return Double ();
				return _field.GetInfinityPoint (_group);
			}

			Number H2 = _field.Multiply (H, H);
			Number H3 = _field.Multiply (H2, H);
			Number X = _field.Subtract (_field.Subtract (_field.Multiply (r, r), H3), _field.Multiply (_field.Add (u1, u1), H2));
			Number Y = _field.Subtract (_field.Multiply (r, _field.Subtract (_field.Multiply (u1, H2), X)), _field.Multiply (s1, H3));
			Number Z = _field.Multiply (_field.Multiply (_z, other._z), H);

			return new ECPoint (_group, X, Y, Z);
		}