BytesToDigits(byte[] bytes, int bytesI, Digits digits, int bitN) { if (bitN == 0) { Debug.Assert(false, "untested code"); return; } int digitN = (bitN + (Digit.BitN - 1)) / Digit.BitN; digits._SetAll(0, digitN); for (int iDigit = 0; iDigit != digitN; iDigit++) { int byteNLeft = (bitN + 7) / 8 - 4 * iDigit; Digit digit = 0; for ( int iByte = 0; iByte != (4 > byteNLeft ? byteNLeft : 4); iByte++ ) { digit ^= (Digit)bytes[bytesI + byteNLeft - 1 - iByte] << 8 * iByte; } digits[iDigit] = digit; } digits[digitN - 1] &= Digit.MaxValue >> Digit.BitN * digitN - bitN; }
Mul(Digits a, int aN, Digits b, int bN, Digits c) { Debug.Assert(!c._Overlaps(a) && !c._Overlaps(b) , "overlapping arguments"); Digits p1, p2; int i, n1, n2; if (aN > bN) { p1 = a; p2 = b; n1 = aN; n2 = bN; } else { p2 = a; p1 = b; n2 = aN; n1 = bN; } if (n2 == 0) { Debug.Assert(false, "untested code"); c._SetAll(0, n1); return; } c[n1] = Mul(p1, p2[0], c, n1); for (i = 1; i != n2; i++) { c[i + n1] = Accumulate(p1, p2[i], c + i, n1); } }