operator << method

Uint256 operator <<(
  1. int n
)

Logical left shift, generalized across limb boundaries. Every step is a plain Uint64 shift/bitwise op — already proven web-safe in Uint64 itself — so no additional safety trick is needed at this width.

Implementation

Uint256 operator <<(int n) {
  final shift = n & 255;
  if (shift == 0) return this;
  final limbShift = shift ~/ 64;
  final bitShift = shift % 64;
  final src = [_d0, _d1, _d2, _d3];
  final out = List<Uint64>.filled(4, Uint64.zero);
  for (var i = 3; i >= 0; i--) {
    final srcIndex = i - limbShift;
    if (srcIndex < 0) continue;
    var value = src[srcIndex] << bitShift;
    if (bitShift > 0 && srcIndex - 1 >= 0) {
      value = value | (src[srcIndex - 1] >> (64 - bitShift));
    }
    out[i] = value;
  }
  return Uint256._(out[3], out[2], out[1], out[0]);
}