Exemplo n.º 1
0
        private AbstractF2mPoint MultiplyWTnaf(AbstractF2mPoint p, ZTauElement lambda, PreCompInfo preCompInfo, sbyte a, sbyte mu)
        {
            ZTauElement[] alpha = ((a == 0) ? Tnaf.Alpha0 : Tnaf.Alpha1);
            BigInteger    tw    = Tnaf.GetTw(mu, 4);

            sbyte[] u = Tnaf.TauAdicWNaf(mu, lambda, 4, BigInteger.ValueOf(16L), tw, alpha);
            return(MultiplyFromWTnaf(p, u, preCompInfo));
        }
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
         * <code>&#964;</code>
         * -adic NAF (TNAF) method.
         * @param p The AbstractF2mPoint to multiply.
         * @param lambda The element
         * <code>&#955;</code>
         * of
         * <code><b>Z</b>[&#964;]</code>
         * of which to compute the
         * <code>[&#964;]</code>
         * -adic NAF.
         * @return
         * <code>p</code>
         * multiplied by
         * <code>&#955;</code>
         * .
         */
        AbstractF2mPoint MultiplyWTnaf(AbstractF2mPoint p, ZTauElement lambda,
                                       PreCompInfo preCompInfo, sbyte a, sbyte mu)
        {
            var alpha = a == 0 ? Tnaf.Alpha0 : Tnaf.Alpha1;

            var tw = Tnaf.GetTw(mu, Tnaf.Width);

            var u = Tnaf.TauAdicWNaf(mu, lambda, Tnaf.Width,
                                     BigInteger.ValueOf(Tnaf.Pow2Width), tw, alpha);

            return(MultiplyFromWTnaf(p, u, preCompInfo));
        }
Exemplo n.º 3
0
            public PreCompInfo Precompute(PreCompInfo existing)
            {
                if (existing is WTauNafPreCompInfo)
                {
                    return(existing);
                }

                WTauNafPreCompInfo result = new WTauNafPreCompInfo();

                result.PreComp = Tnaf.GetPreComp(m_p, m_a);
                return(result);
            }
Exemplo n.º 4
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>
         */
        static AbstractF2mPoint MultiplyFromWTnaf(AbstractF2mPoint p, sbyte[] u, PreCompInfo preCompInfo)
        {
            var curve = (AbstractF2mCurve)p.Curve;
            var 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 (var i = 0; i < pu.Length; ++i)
            {
                puNeg[i] = (AbstractF2mPoint)pu[i].Negate();
            }


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

            var tauCount = 0;

            for (var 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.º 5
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 <code>&#964;</code>-adic NAF (TNAF) method.
         * @param p The AbstractF2mPoint to multiply.
         * @param lambda The element <code>&#955;</code> of
         * <code><b>Z</b>[&#964;]</code> of which to compute the
         * <code>[&#964;]</code>-adic NAF.
         * @return <code>p</code> multiplied by <code>&#955;</code>.
         */
        private AbstractF2mPoint MultiplyWTnaf(AbstractF2mPoint p, ZTauElement lambda,
                                               sbyte a, sbyte mu)
        {
            ZTauElement[] alpha = (a == 0) ? Tnaf.Alpha0 : Tnaf.Alpha1;

            BigInteger tw = Tnaf.GetTw(mu, Tnaf.Width);

            sbyte[] u = Tnaf.TauAdicWNaf(mu, lambda, Tnaf.Width,
                                         BigInteger.ValueOf(Tnaf.Pow2Width), tw, alpha);

            return(MultiplyFromWTnaf(p, u));
        }
Exemplo n.º 6
0
 /**
  * @return the auxiliary values <code>s<sub>0</sub></code> and
  * <code>s<sub>1</sub></code> used for partial modular reduction for
  * Koblitz curves.
  */
 internal virtual BigInteger[] GetSi()
 {
     if (si == null)
     {
         lock (this) {
             if (si == null)
             {
                 si = Tnaf.GetSi(this);
             }
         }
     }
     return(si);
 }
Exemplo n.º 7
0
        /**
         * Returns the parameter <code>&#956;</code> of the elliptic curve.
         * @return <code>&#956;</code> of the elliptic curve.
         * @throws ArgumentException if the given ECCurve is not a
         * Koblitz curve.
         */
        internal virtual sbyte GetMu()
        {
            if (mu == 0)
            {
                lock (this) {
                    if (mu == 0)
                    {
                        mu = Tnaf.GetMu(this);
                    }
                }
            }

            return(mu);
        }
Exemplo n.º 8
0
 /**
  * @return the auxiliary values <code>s<sub>0</sub></code> and
  * <code>s<sub>1</sub></code> used for partial modular reduction for
  * Koblitz curves.
  */
 internal IBigInteger[] GetSi()
 {
     if (_si == null)
     {
         lock (this)
         {
             if (_si == null)
             {
                 _si = Tnaf.GetSi(this);
             }
         }
     }
     return(_si);
 }
Exemplo n.º 9
0
 internal virtual BigInteger[] GetSi()
 {
     if (this.si == null)
     {
         object obj2 = this;
         lock (obj2)
         {
             if (this.si == null)
             {
                 this.si = Tnaf.GetSi(this);
             }
         }
     }
     return(this.si);
 }
Exemplo n.º 10
0
        /**
         * Returns the parameter <code>&#956;</code> of the elliptic curve.
         * @return <code>&#956;</code> of the elliptic curve.
         * @throws ArgumentException if the given ECCurve is not a
         * Koblitz curve.
         */
        internal sbyte GetMu()
        {
            if (_mu == 0)
            {
                lock (this)
                {
                    if (_mu == 0)
                    {
                        _mu = Tnaf.GetMu(this);
                    }
                }
            }

            return(_mu);
        }
Exemplo n.º 11
0
        protected override ECPoint MultiplyPositive(ECPoint point, BigInteger k)
        {
            if (!(point is AbstractF2mPoint))
            {
                throw new ArgumentException("Only AbstractF2mPoint can be used in WTauNafMultiplier");
            }
            AbstractF2mPoint p     = (AbstractF2mPoint)point;
            AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve;
            int   fieldSize        = curve.FieldSize;
            sbyte intValue         = (sbyte)curve.A.ToBigInteger().IntValue;
            sbyte mu = Tnaf.GetMu((int)intValue);

            BigInteger[] si     = curve.GetSi();
            ZTauElement  lambda = Tnaf.PartModReduction(k, fieldSize, intValue, si, mu, 10);

            return(this.MultiplyWTnaf(p, lambda, curve.GetPreCompInfo(p, PRECOMP_NAME), intValue, mu));
        }
Exemplo n.º 12
0
        protected override ECPoint MultiplyPositive(ECPoint point, BigInteger k)
        {
            //IL_000d: Unknown result type (might be due to invalid IL or missing references)
            if (!(point is AbstractF2mPoint))
            {
                throw new ArgumentException("Only AbstractF2mPoint can be used in WTauNafMultiplier");
            }
            AbstractF2mPoint abstractF2mPoint = (AbstractF2mPoint)point;
            AbstractF2mCurve abstractF2mCurve = (AbstractF2mCurve)abstractF2mPoint.Curve;
            int   fieldSize = abstractF2mCurve.FieldSize;
            sbyte b         = (sbyte)abstractF2mCurve.A.ToBigInteger().IntValue;
            sbyte mu        = Tnaf.GetMu(b);

            BigInteger[] si     = abstractF2mCurve.GetSi();
            ZTauElement  lambda = Tnaf.PartModReduction(k, fieldSize, b, si, mu, 10);

            return(MultiplyWTnaf(abstractF2mPoint, lambda, abstractF2mCurve.GetPreCompInfo(abstractF2mPoint, PRECOMP_NAME), b, mu));
        }
Exemplo n.º 13
0
        /**
         * Multiplies a {@link NBitcoin.BouncyCastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
         * by
         * <code>k</code>
         * using the reduced
         * <code>&#964;</code>
         * -adic NAF (RTNAF)
         * method.
         * @param p The AbstractF2mPoint to multiply.
         * @param k The integer by which to multiply
         * <code>k</code>
         * .
         * @return
         * <code>p</code>
         * multiplied by
         * <code>k</code>
         * .
         */
        protected override ECPoint MultiplyPositive(ECPoint point, BigInteger k)
        {
            if (!(point is AbstractF2mPoint))
            {
                throw new ArgumentException("Only AbstractF2mPoint can be used in WTauNafMultiplier");
            }

            var p     = (AbstractF2mPoint)point;
            var curve = (AbstractF2mCurve)p.Curve;
            var m     = curve.FieldSize;
            var a     = (sbyte)curve.A.ToBigInteger().IntValue;
            var mu    = Tnaf.GetMu(a);
            var s     = curve.GetSi();

            var rho = Tnaf.PartModReduction(k, m, a, s, mu, 10);

            return(MultiplyWTnaf(p, rho, curve.GetPreCompInfo(p, PRECOMP_NAME), a, mu));
        }
Exemplo n.º 14
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);
        }
Exemplo n.º 15
0
        /**
         * Multiplies a {@link org.bouncycastle.math.ec.AbstractF2mPoint AbstractF2mPoint}
         * by <code>k</code> using the reduced <code>&#964;</code>-adic NAF (RTNAF)
         * method.
         * @param p The AbstractF2mPoint to multiply.
         * @param k The integer by which to multiply <code>k</code>.
         * @return <code>p</code> multiplied by <code>k</code>.
         */
        protected override ECPoint MultiplyPositive(ECPoint point, BigInteger k)
        {
            if (!(point is AbstractF2mPoint))
            {
                throw new ArgumentException("Only AbstractF2mPoint can be used in WTauNafMultiplier");
            }

            AbstractF2mPoint p     = (AbstractF2mPoint)point;
            AbstractF2mCurve curve = (AbstractF2mCurve)p.Curve;
            int   m  = curve.FieldSize;
            sbyte a  = (sbyte)curve.A.ToBigInteger().IntValue;
            sbyte mu = Tnaf.GetMu(a);

            BigInteger[] s = curve.GetSi();

            ZTauElement rho = Tnaf.PartModReduction(k, m, a, s, mu, (sbyte)10);

            return(MultiplyWTnaf(p, rho, a, mu));
        }
Exemplo n.º 16
0
        /**
         * Multiplies a {@link org.bouncycastle.math.ec.F2mPoint F2mPoint}
         * by <code>k</code> using the reduced <code>&#964;</code>-adic NAF (RTNAF)
         * method.
         * @param p The F2mPoint to multiply.
         * @param k The integer by which to multiply <code>k</code>.
         * @return <code>p</code> multiplied by <code>k</code>.
         */
        protected override ECPoint MultiplyPositive(ECPoint point, BigInteger k)
        {
            if (!(point is F2mPoint))
            {
                throw new ArgumentException("Only F2mPoint can be used in WTauNafMultiplier");
            }

            F2mPoint p     = (F2mPoint)point;
            F2mCurve curve = (F2mCurve)p.Curve;
            int      m     = curve.M;
            sbyte    a     = (sbyte)curve.A.ToBigInteger().IntValue;
            sbyte    mu    = curve.GetMu();

            BigInteger[] s = curve.GetSi();

            ZTauElement rho = Tnaf.PartModReduction(k, m, a, s, mu, (sbyte)10);

            return(MultiplyWTnaf(p, rho, curve.GetPreCompInfo(p, PRECOMP_NAME), a, mu));
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
0
        /**
         * Multiplies a {@link org.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.º 19
0
        /**
         * Multiplies a {@link Al.Security.math.ec.F2mPoint F2mPoint}
         * by <code>k</code> using the reduced <code>&#964;</code>-adic NAF (RTNAF)
         * method.
         * @param p The F2mPoint to multiply.
         * @param k The integer by which to multiply <code>k</code>.
         * @return <code>p</code> multiplied by <code>k</code>.
         */
        public ECPoint Multiply(ECPoint point, BigInteger k, PreCompInfo preCompInfo)
        {
            if (!(point is F2mPoint))
            {
                throw new ArgumentException("Only F2mPoint can be used in WTauNafMultiplier");
            }

            F2mPoint p = (F2mPoint)point;

            F2mCurve curve = (F2mCurve)p.Curve;
            int      m     = curve.M;
            sbyte    a     = (sbyte)curve.A.ToBigInteger().IntValue;
            sbyte    mu    = curve.GetMu();

            BigInteger[] s = curve.GetSi();

            ZTauElement rho = Tnaf.PartModReduction(k, m, a, s, mu, (sbyte)10);

            return(MultiplyWTnaf(p, rho, preCompInfo, a, mu));
        }
Exemplo n.º 20
0
        /**
         * Multiplies a {@link Al.Security.math.ec.F2mPoint F2mPoint}
         * by an element <code>&#955;</code> of <code><b>Z</b>[&#964;]</code> using
         * the <code>&#964;</code>-adic NAF (TNAF) method.
         * @param p The F2mPoint to multiply.
         * @param lambda The element <code>&#955;</code> of
         * <code><b>Z</b>[&#964;]</code> of which to compute the
         * <code>[&#964;]</code>-adic NAF.
         * @return <code>p</code> multiplied by <code>&#955;</code>.
         */
        private F2mPoint MultiplyWTnaf(F2mPoint p, ZTauElement lambda,
                                       PreCompInfo preCompInfo, sbyte a, sbyte mu)
        {
            ZTauElement[] alpha;
            if (a == 0)
            {
                alpha = Tnaf.Alpha0;
            }
            else
            {
                // a == 1
                alpha = Tnaf.Alpha1;
            }

            BigInteger tw = Tnaf.GetTw(mu, Tnaf.Width);

            sbyte[] u = Tnaf.TauAdicWNaf(mu, lambda, Tnaf.Width,
                                         BigInteger.ValueOf(Tnaf.Pow2Width), tw, alpha);

            return(MultiplyFromWTnaf(p, u, preCompInfo));
        }
        /**
         * Multiplies a {@link org.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,
                                                  IPreCompInfo 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);
                p.PreCompInfo = new WTauNafPreCompInfo(pu);
            }
            else
            {
                pu = ((WTauNafPreCompInfo)preCompInfo).GetPreComp();
            }

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

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

            return(q);
        }