encodeTo method

  1. @override
void encodeTo(
  1. BigInt value,
  2. Output output
)
override

Convert self to a slice and append it to the destination.

Implementation

@override
void encodeTo(BigInt value, Output output) {
  if (value < BigInt.from(64)) {
    output.pushByte(value.toInt() << 2);
  } else if (value < BigInt.from(16384)) {
    output
      ..pushByte((value.toInt() & 0x3F) << 2 | 1)
      ..pushByte((value.toInt() >> 6).toUnsigned(8));
  } else if (value < BigInt.from(1073741824)) {
    output
      ..pushByte((value.toInt() & 0x3F) << 2 | 2)
      ..pushByte((value.toInt() >> 6).toUnsigned(8))
      ..pushByte((value.toInt() >> 14).toUnsigned(8))
      ..pushByte((value.toInt() >> 22).toUnsigned(8));
  } else {
    assertion(value.bitLength >= 30,
        'Previously checked anyting less than 2^30; qed');
    final bytesNeeded = (value.bitLength + 7) >> 3;
    output.pushByte(((bytesNeeded - 4) << 2).toUnsigned(8) | 3);
    for (var i = 0; i < bytesNeeded; i++) {
      output.pushByte(value.toUnsigned(8).toInt());
      value >>= 8;
    }
    assertion(value == BigInt.zero, 'Value is not fully consumed; qed');
  }
}