Exemplo n.º 1
0
        /**
         * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
         * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>
         * using the window <code>&#964;</code>-adic NAF (TNAF) method, given the
         * WTNAF of <code>&#955;</code>.
         * @param p The AbstractF2mPoint to multiply.
         * @param u The the WTNAF of <code>&#955;</code>..
         * @return <code>&#955; * p</code>
         */
        private static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u, PreCompInfo preCompInfo)
        {
            var   curve = (AbstractF2mCurve)p.Curve;
            sbyte a     = (sbyte)curve.A.ToBigInteger().IntValue;

            AbstractF2mPoint[] pu;
            if ((preCompInfo == null) || !(preCompInfo is WTauNafPreCompInfo))
            {
                pu = Tnaf.GetPreComp(p, a);

                var pre = new WTauNafPreCompInfo();
                pre.PreComp = pu;
                curve.SetPreCompInfo(p, PRECOMP_NAME, pre);
            }
            else
            {
                pu = ((WTauNafPreCompInfo)preCompInfo).PreComp;
            }

            // TODO Include negations in precomp (optionally) and use from here
            var puNeg = new AbstractF2mPoint[pu.Length];

            for (int i = 0; i < pu.Length; ++i)
            {
                puNeg[i] = (AbstractF2mPoint)pu[i].Negate();
            }


            // q = infinity
            var q = (AbstractF2mPoint)p.Curve.Infinity;

            int tauCount = 0;

            for (int i = u.Length - 1; i >= 0; i--)
            {
                ++tauCount;
                int ui = u[i];
                if (ui != 0)
                {
                    q        = q.TauPow(tauCount);
                    tauCount = 0;

                    ECPoint x = ui > 0 ? pu[ui >> 1] : puNeg[(-ui) >> 1];
                    q = (AbstractF2mPoint)q.Add(x);
                }
            }
            if (tauCount > 0)
            {
                q = q.TauPow(tauCount);
            }
            return(q);
        }
Exemplo n.º 2
0
		/**
        * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
        * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>
        * using the window <code>&#964;</code>-adic NAF (TNAF) method, given the
        * WTNAF of <code>&#955;</code>.
        * @param p The AbstractF2mPoint to multiply.
        * @param u The the WTNAF of <code>&#955;</code>..
        * @return <code>&#955; * p</code>
        */
		private static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u, PreCompInfo preCompInfo)
		{
			AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve;
			sbyte a = (sbyte)curve.A.ToBigInteger().IntValue;

			AbstractF2mPoint[] pu;
			if((preCompInfo == null) || !(preCompInfo is WTauNafPreCompInfo))
			{
				pu = Tnaf.GetPreComp(p, a);

				WTauNafPreCompInfo pre = new WTauNafPreCompInfo();
				pre.PreComp = pu;
				curve.SetPreCompInfo(p, PRECOMP_NAME, pre);
			}
			else
			{
				pu = ((WTauNafPreCompInfo)preCompInfo).PreComp;
			}

			// TODO Include negations in precomp (optionally) and use from here
			AbstractF2mPoint[] puNeg = new AbstractF2mPoint[pu.Length];
			for(int i = 0; i < pu.Length; ++i)
			{
				puNeg[i] = (AbstractF2mPoint)pu[i].Negate();
			}


			// q = infinity
			AbstractF2mPoint q = (AbstractF2mPoint)p.Curve.Infinity;

			int tauCount = 0;
			for(int i = u.Length - 1; i >= 0; i--)
			{
				++tauCount;
				int ui = u[i];
				if(ui != 0)
				{
					q = q.TauPow(tauCount);
					tauCount = 0;

					ECPoint x = ui > 0 ? pu[ui >> 1] : puNeg[(-ui) >> 1];
					q = (AbstractF2mPoint)q.Add(x);
				}
			}
			if(tauCount > 0)
			{
				q = q.TauPow(tauCount);
			}
			return q;
		}
Exemplo n.º 3
0
        /**
         * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.F2mPoint F2mPoint}
         * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>
         * using the window <code>&#964;</code>-adic NAF (TNAF) method, given the
         * WTNAF of <code>&#955;</code>.
         * @param p The F2mPoint to multiply.
         * @param u The the WTNAF of <code>&#955;</code>..
         * @return <code>&#955; * p</code>
         */
        private static F2mPoint MultiplyFromWTnaf(F2mPoint p, sbyte[] u, PreCompInfo preCompInfo)
        {
            F2mCurve curve = (F2mCurve)p.Curve;
            sbyte    a     = (sbyte)curve.A.ToBigInteger().IntValue;

            F2mPoint[] pu;
            if ((preCompInfo == null) || !(preCompInfo is WTauNafPreCompInfo))
            {
                pu = Tnaf.GetPreComp(p, a);

                WTauNafPreCompInfo pre = new WTauNafPreCompInfo();
                pre.PreComp = pu;
                curve.SetPreCompInfo(p, PRECOMP_NAME, pre);
            }
            else
            {
                pu = ((WTauNafPreCompInfo)preCompInfo).PreComp;
            }

            // q = infinity
            F2mPoint q = (F2mPoint)curve.Infinity;

            for (int i = u.Length - 1; i >= 0; i--)
            {
                q = Tnaf.Tau(q);
                sbyte ui = u[i];
                if (ui != 0)
                {
                    if (ui > 0)
                    {
                        q = q.AddSimple(pu[ui]);
                    }
                    else
                    {
                        // u[i] < 0
                        q = q.SubtractSimple(pu[-ui]);
                    }
                }
            }

            return(q);
        }
Exemplo n.º 4
0
        /**
        * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.F2mPoint F2mPoint}
        * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>
        * using the window <code>&#964;</code>-adic NAF (TNAF) method, given the
        * WTNAF of <code>&#955;</code>.
        * @param p The F2mPoint to multiply.
        * @param u The the WTNAF of <code>&#955;</code>..
        * @return <code>&#955; * p</code>
        */
        private static F2mPoint MultiplyFromWTnaf(F2mPoint p, sbyte[] u, PreCompInfo preCompInfo)
        {
            F2mCurve curve = (F2mCurve)p.Curve;
            sbyte a = (sbyte)curve.A.ToBigInteger().IntValue;

            F2mPoint[] pu;
            if ((preCompInfo == null) || !(preCompInfo is WTauNafPreCompInfo))
            {
                pu = Tnaf.GetPreComp(p, a);

                WTauNafPreCompInfo pre = new WTauNafPreCompInfo();
                pre.PreComp = pu;
                curve.SetPreCompInfo(p, PRECOMP_NAME, pre);
            }
            else
            {
                pu = ((WTauNafPreCompInfo)preCompInfo).PreComp;
            }

            // q = infinity
            F2mPoint q = (F2mPoint)curve.Infinity;
            for (int i = u.Length - 1; i >= 0; i--)
            {
                q = Tnaf.Tau(q);
                sbyte ui = u[i];
                if (ui != 0)
                {
                    if (ui > 0)
                    {
                        q = q.AddSimple(pu[ui]);
                    }
                    else
                    {
                        // u[i] < 0
                        q = q.SubtractSimple(pu[-ui]);
                    }
                }
            }

            return q;
        }