/// <summary>Compute a*b and store the result to r.</summary> /// <returns>r</returns> internal static LongLong Multiplication(LongLong r, long a, long b) { /* * final long x0 = a & LOWER_MASK; * final long x1 = (a & UPPER_MASK) >> MID; * * final long y0 = b & LOWER_MASK; * final long y1 = (b & UPPER_MASK) >> MID; * * final long t = (x0 + x1)*(y0 + y1); * final long u = (x0 - x1)*(y0 - y1); * final long v = x1*y1; * * final long tmp = (t - u)>>>1; * result.d0 = ((t + u)>>>1) - v + ((tmp << MID) & FULL_MASK);; * result.d1 = v + (tmp >> MID); * return result; */ long a_lower = a & LowerMask; long a_upper = (a & UpperMask) >> Mid; long b_lower = b & LowerMask; long b_upper = (b & UpperMask) >> Mid; long tmp = a_lower * b_upper + a_upper * b_lower; r.d0 = a_lower * b_lower + ((tmp << Mid) & FullMask); r.d1 = a_upper * b_upper + (tmp >> Mid); return(r); }
internal static void VerifyMultiplication(long a, long b) { LongLong ll = LongLong.Multiplication(new LongLong(), a, b); BigInteger bi = BigInteger.ValueOf(a).Multiply(BigInteger.ValueOf(b)); string s = string.Format("\na = %x\nb = %x\nll= " + ll + "\nbi= " + bi.ToString(16 ) + "\n", a, b); //System.out.println(s); NUnit.Framework.Assert.AreEqual(s, bi, ll.ToBigInteger()); }
internal virtual long M(long c, long d) { LongLong.Multiplication(this.x, c, d); // a = (x * N')&(R - 1) = ((x & R_1) * N') & R_1 long a = LongLong.Multiplication(this.xN_I, this.x.And(this._enclosing.R1), this. _enclosing.NI).And(this._enclosing.R1); LongLong.Multiplication(this.aN, a, this._enclosing.N); long z = this.aN.PlusEqual(this.x).ShiftRight(this._enclosing.s); return(z < this._enclosing.N ? z : z - this._enclosing.N); }
internal static void VerifyRightShift(long a, long b) { LongLong ll = new LongLong().Set(a, b); BigInteger bi = ll.ToBigInteger(); for (int i = 0; i < LongLong.Size >> 1; i++) { long result = ll.ShiftRight(i) & Mask; long expected = bi.ShiftRight(i) & Mask; string s = string.Format("\na = %x\nb = %x\nll= " + ll + "\nbi= " + bi.ToString(16 ) + "\n", a, b); NUnit.Framework.Assert.AreEqual(s, expected, result); } string s_1 = string.Format("\na = %x\nb = %x\nll= " + ll + "\nbi= " + bi.ToString (16) + "\n", a, b); //System.out.println(s); NUnit.Framework.Assert.AreEqual(s_1, bi, ll.ToBigInteger()); }
/// <summary>Plus equal operation (+=).</summary> internal virtual LongLong PlusEqual(LongLong that) { this.d0 += that.d0; this.d1 += that.d1; return(this); }