예제 #1
0
파일: Tnaf.cs 프로젝트: Lokad/BitcoinNet
        /**
         * Multiplies a {@link BitcoinNet.BouncyCastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
         * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>
         * using the <code>&#964;</code>-adic NAF (TNAF) method, given the TNAF
         * of <code>&#955;</code>.
         * @param p The AbstractF2mPoint to Multiply.
         * @param u The the TNAF of <code>&#955;</code>..
         * @return <code>&#955; * p</code>
         */
        public static AbstractF2mPoint MultiplyFromTnaf(AbstractF2mPoint p, sbyte[] u)
        {
            ECCurve          curve = p.Curve;
            AbstractF2mPoint q     = (AbstractF2mPoint)curve.Infinity;
            AbstractF2mPoint pNeg  = (AbstractF2mPoint)p.Negate();
            int tauCount           = 0;

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

                    ECPoint x = ui > 0 ? p : pNeg;
                    q = (AbstractF2mPoint)q.Add(x);
                }
            }
            if (tauCount > 0)
            {
                q = q.TauPow(tauCount);
            }
            return(q);
        }
예제 #2
0
    public static AbstractF2mPoint MultiplyFromTnaf(AbstractF2mPoint p, sbyte[] u)
    {
        ECCurve          curve             = p.Curve;
        AbstractF2mPoint abstractF2mPoint  = (AbstractF2mPoint)curve.Infinity;
        AbstractF2mPoint abstractF2mPoint2 = (AbstractF2mPoint)p.Negate();
        int num = 0;

        for (int num2 = u.Length - 1; num2 >= 0; num2--)
        {
            num++;
            sbyte b = u[num2];
            if (b != 0)
            {
                abstractF2mPoint = abstractF2mPoint.TauPow(num);
                num = 0;
                ECPoint b2 = (b > 0) ? p : abstractF2mPoint2;
                abstractF2mPoint = (AbstractF2mPoint)abstractF2mPoint.Add(b2);
            }
        }
        if (num > 0)
        {
            abstractF2mPoint = abstractF2mPoint.TauPow(num);
        }
        return(abstractF2mPoint);
    }
예제 #3
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);
        }
예제 #4
0
        private static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u, PreCompInfo preCompInfo)
        {
            AbstractF2mPoint[] preComp;
            AbstractF2mCurve   curve = (AbstractF2mCurve)p.Curve;
            sbyte intValue           = (sbyte)curve.A.ToBigInteger().IntValue;

            if ((preCompInfo == null) || !(preCompInfo is WTauNafPreCompInfo))
            {
                preComp = Tnaf.GetPreComp(p, intValue);
                WTauNafPreCompInfo info = new WTauNafPreCompInfo {
                    PreComp = preComp
                };
                curve.SetPreCompInfo(p, PRECOMP_NAME, info);
            }
            else
            {
                preComp = ((WTauNafPreCompInfo)preCompInfo).PreComp;
            }
            AbstractF2mPoint[] pointArray2 = new AbstractF2mPoint[preComp.Length];
            for (int i = 0; i < preComp.Length; i++)
            {
                pointArray2[i] = (AbstractF2mPoint)preComp[i].Negate();
            }
            AbstractF2mPoint infinity = (AbstractF2mPoint)p.Curve.Infinity;
            int pow = 0;

            for (int j = u.Length - 1; j >= 0; j--)
            {
                pow++;
                int num5 = u[j];
                if (num5 != 0)
                {
                    infinity = infinity.TauPow(pow);
                    pow      = 0;
                    ECPoint b = (num5 <= 0) ? pointArray2[-num5 >> 1] : preComp[num5 >> 1];
                    infinity = (AbstractF2mPoint)infinity.Add(b);
                }
            }
            if (pow > 0)
            {
                infinity = infinity.TauPow(pow);
            }
            return(infinity);
        }
예제 #5
0
        private static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u, PreCompInfo preCompInfo)
        {
            AbstractF2mCurve abstractF2mCurve = (AbstractF2mCurve)p.Curve;
            sbyte            a = (sbyte)abstractF2mCurve.A.ToBigInteger().IntValue;

            AbstractF2mPoint[] preComp;
            if (preCompInfo == null || !(preCompInfo is WTauNafPreCompInfo))
            {
                preComp = Tnaf.GetPreComp(p, a);
                WTauNafPreCompInfo wTauNafPreCompInfo = new WTauNafPreCompInfo();
                wTauNafPreCompInfo.PreComp = preComp;
                abstractF2mCurve.SetPreCompInfo(p, PRECOMP_NAME, wTauNafPreCompInfo);
            }
            else
            {
                preComp = ((WTauNafPreCompInfo)preCompInfo).PreComp;
            }
            AbstractF2mPoint[] array = new AbstractF2mPoint[preComp.Length];
            for (int i = 0; i < preComp.Length; i++)
            {
                array[i] = (AbstractF2mPoint)preComp[i].Negate();
            }
            AbstractF2mPoint abstractF2mPoint = (AbstractF2mPoint)p.Curve.Infinity;
            int num = 0;

            for (int num2 = u.Length - 1; num2 >= 0; num2--)
            {
                num++;
                int num3 = u[num2];
                if (num3 != 0)
                {
                    abstractF2mPoint = abstractF2mPoint.TauPow(num);
                    num = 0;
                    ECPoint b = ((num3 > 0) ? preComp[num3 >> 1] : array[-num3 >> 1]);
                    abstractF2mPoint = (AbstractF2mPoint)abstractF2mPoint.Add(b);
                }
            }
            if (num > 0)
            {
                abstractF2mPoint = abstractF2mPoint.TauPow(num);
            }
            return(abstractF2mPoint);
        }
예제 #6
0
        /**
         * Multiplies a {@link org.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)
        {
            AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve;
            sbyte            a     = (sbyte)curve.A.ToBigInteger().IntValue;

            WTauNafCallback    callback    = new WTauNafCallback(p, a);
            WTauNafPreCompInfo preCompInfo = (WTauNafPreCompInfo)curve.Precompute(p, PRECOMP_NAME, callback);

            AbstractF2mPoint[] pu = 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);
        }
예제 #7
0
        public static AbstractF2mPoint MultiplyFromTnaf(AbstractF2mPoint p, sbyte[] u)
        {
            AbstractF2mPoint infinity = (AbstractF2mPoint)p.Curve.Infinity;
            AbstractF2mPoint point2   = (AbstractF2mPoint)p.Negate();
            int pow = 0;

            for (int i = u.Length - 1; i >= 0; i--)
            {
                pow++;
                sbyte num3 = u[i];
                if (num3 != 0)
                {
                    infinity = infinity.TauPow(pow);
                    pow      = 0;
                    ECPoint b = (num3 <= 0) ? point2 : p;
                    infinity = (AbstractF2mPoint)infinity.Add(b);
                }
            }
            if (pow > 0)
            {
                infinity = infinity.TauPow(pow);
            }
            return(infinity);
        }