当前位置: 首页>>代码示例>>C#>>正文


C# IBigInteger.ShiftRight方法代码示例

本文整理汇总了C#中IBigInteger.ShiftRight方法的典型用法代码示例。如果您正苦于以下问题:C# IBigInteger.ShiftRight方法的具体用法?C# IBigInteger.ShiftRight怎么用?C# IBigInteger.ShiftRight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IBigInteger的用法示例。


在下文中一共展示了IBigInteger.ShiftRight方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ApproximateDivisionByN

        /**
        * Approximate division by <code>n</code>. For an integer
        * <code>k</code>, the value <code>&#955; = s k / n</code> is
        * computed to <code>c</code> bits of accuracy.
        * @param k The parameter <code>k</code>.
        * @param s The curve parameter <code>s<sub>0</sub></code> or
        * <code>s<sub>1</sub></code>.
        * @param vm The Lucas Sequence element <code>V<sub>m</sub></code>.
        * @param a The parameter <code>a</code> of the elliptic curve.
        * @param m The bit length of the finite field
        * <code><b>F</b><sub>m</sub></code>.
        * @param c The number of bits of accuracy, i.e. the scale of the returned
        * <code>SimpleBigDecimal</code>.
        * @return The value <code>&#955; = s k / n</code> computed to
        * <code>c</code> bits of accuracy.
        */
        public static SimpleBigDecimal ApproximateDivisionByN(IBigInteger k,
            IBigInteger s, IBigInteger vm, sbyte a, int m, int c)
        {
            int _k = (m + 5)/2 + c;
            IBigInteger ns = k.ShiftRight(m - _k - 2 + a);

            IBigInteger gs = s.Multiply(ns);

            IBigInteger hs = gs.ShiftRight(m);

            IBigInteger js = vm.Multiply(hs);

            IBigInteger gsPlusJs = gs.Add(js);
            IBigInteger ls = gsPlusJs.ShiftRight(_k - c);
            if (gsPlusJs.TestBit(_k-c-1))
            {
                // round up
                ls = ls.Add(BigInteger.One);
            }

            return new SimpleBigDecimal(ls, c);
        }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:38,代码来源:Tnaf.cs

示例2: TauAdicWNaf

        /**
        * Computes the <code>[&#964;]</code>-adic window NAF of an element
        * <code>&#955;</code> of <code><b>Z</b>[&#964;]</code>.
        * @param mu The parameter &#956; of the elliptic curve.
        * @param lambda The element <code>&#955;</code> of
        * <code><b>Z</b>[&#964;]</code> of which to compute the
        * <code>[&#964;]</code>-adic NAF.
        * @param width The window width of the resulting WNAF.
        * @param pow2w 2<sup>width</sup>.
        * @param tw The auxiliary value <code>t<sub>w</sub></code>.
        * @param alpha The <code>&#945;<sub>u</sub></code>'s for the window width.
        * @return The <code>[&#964;]</code>-adic window NAF of
        * <code>&#955;</code>.
        */
        public static sbyte[] TauAdicWNaf(sbyte mu, ZTauElement lambda,
            sbyte width, IBigInteger pow2w, IBigInteger tw, ZTauElement[] alpha)
        {
            if (!((mu == 1) || (mu == -1)))
                throw new ArgumentException("mu must be 1 or -1");

            IBigInteger norm = Norm(mu, lambda);

            // Ceiling of log2 of the norm
            int log2Norm = norm.BitLength;

            // If length(TNAF) > 30, then length(TNAF) < log2Norm + 3.52
            int maxLength = log2Norm > 30 ? log2Norm + 4 + width : 34 + width;

            // The array holding the TNAF
            sbyte[] u = new sbyte[maxLength];

            // 2^(width - 1)
            IBigInteger pow2wMin1 = pow2w.ShiftRight(1);

            // Split lambda into two BigIntegers to simplify calculations
            IBigInteger r0 = lambda.u;
            IBigInteger r1 = lambda.v;
            int i = 0;

            // while lambda <> (0, 0)
            while (!((r0.Equals(BigInteger.Zero))&&(r1.Equals(BigInteger.Zero))))
            {
                // if r0 is odd
                if (r0.TestBit(0))
                {
                    // uUnMod = r0 + r1*tw Mod 2^width
                    IBigInteger uUnMod
                        = r0.Add(r1.Multiply(tw)).Mod(pow2w);

                    sbyte uLocal;
                    // if uUnMod >= 2^(width - 1)
                    if (uUnMod.CompareTo(pow2wMin1) >= 0)
                    {
                        uLocal = (sbyte) uUnMod.Subtract(pow2w).IntValue;
                    }
                    else
                    {
                        uLocal = (sbyte) uUnMod.IntValue;
                    }
                    // uLocal is now in [-2^(width-1), 2^(width-1)-1]

                    u[i] = uLocal;
                    bool s = true;
                    if (uLocal < 0)
                    {
                        s = false;
                        uLocal = (sbyte)-uLocal;
                    }
                    // uLocal is now >= 0

                    if (s)
                    {
                        r0 = r0.Subtract(alpha[uLocal].u);
                        r1 = r1.Subtract(alpha[uLocal].v);
                    }
                    else
                    {
                        r0 = r0.Add(alpha[uLocal].u);
                        r1 = r1.Add(alpha[uLocal].v);
                    }
                }
                else
                {
                    u[i] = 0;
                }

                IBigInteger t = r0;

                if (mu == 1)
                {
                    r0 = r1.Add(r0.ShiftRight(1));
                }
                else
                {
                    // mu == -1
                    r0 = r1.Subtract(r0.ShiftRight(1));
                }
                r1 = t.ShiftRight(1).Negate();
                i++;
            }
//.........这里部分代码省略.........
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:101,代码来源:Tnaf.cs

示例3: WindowNaf

        /**
        * Computes the Window NAF (non-adjacent Form) of an integer.
        * @param width The width <code>w</code> of the Window NAF. The width is
        * defined as the minimal number <code>w</code>, such that for any
        * <code>w</code> consecutive digits in the resulting representation, at
        * most one is non-zero.
        * @param k The integer of which the Window NAF is computed.
        * @return The Window NAF of the given width, such that the following holds:
        * <code>k = &#8722;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
        * </code>, where the <code>k<sub>i</sub></code> denote the elements of the
        * returned <code>sbyte[]</code>.
        */
        public sbyte[] WindowNaf(sbyte width, IBigInteger k)
        {
            // The window NAF is at most 1 element longer than the binary
            // representation of the integer k. sbyte can be used instead of short or
            // int unless the window width is larger than 8. For larger width use
            // short or int. However, a width of more than 8 is not efficient for
            // m = log2(q) smaller than 2305 Bits. Note: Values for m larger than
            // 1000 Bits are currently not used in practice.
            sbyte[] wnaf = new sbyte[k.BitLength + 1];

            // 2^width as short and BigInteger
            short pow2wB = (short)(1 << width);
            IBigInteger pow2wBI = BigInteger.ValueOf(pow2wB);

            int i = 0;

            // The actual length of the WNAF
            int length = 0;

            // while k >= 1
            while (k.SignValue > 0)
            {
                // if k is odd
                if (k.TestBit(0))
                {
                    // k Mod 2^width
                    IBigInteger remainder = k.Mod(pow2wBI);

                    // if remainder > 2^(width - 1) - 1
                    if (remainder.TestBit(width - 1))
                    {
                        wnaf[i] = (sbyte)(remainder.IntValue - pow2wB);
                    }
                    else
                    {
                        wnaf[i] = (sbyte)remainder.IntValue;
                    }
                    // wnaf[i] is now in [-2^(width-1), 2^(width-1)-1]

                    k = k.Subtract(BigInteger.ValueOf(wnaf[i]));
                    length = i;
                }
                else
                {
                    wnaf[i] = 0;
                }

                // k = k/2
                k = k.ShiftRight(1);
                i++;
            }

            length++;

            // Reduce the WNAF array to its actual length
            sbyte[] wnafShort = new sbyte[length];
            Array.Copy(wnaf, 0, wnafShort, 0, length);
            return wnafShort;
        }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:71,代码来源:WNafMultiplier.cs


注:本文中的IBigInteger.ShiftRight方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。