variableNatEncode static method

BuBytes variableNatEncode(
  1. BigInt val
)

LEB128-style variable-length encoding of a non-negative val.

Implementation

static BuBytes variableNatEncode(BigInt val) {
  if (val.isNegative) {
    throw CoreErrors.invalidOperationArguments(
      "variableNatEncode",
      name: "val",
      reason: "Negative value cannot be nat-encoded.",
    );
  }
  BigInt num = val;
  List<int> output = [(num & BinaryOps.maskBig8).toInt() & 0x7F];
  num ~/= BigInt.from(128);
  while (num > BigInt.zero) {
    output.add(((num & BinaryOps.maskBig8).toInt() & 0x7F) | 0x80);
    num ~/= BigInt.from(128);
  }
  output = output.reversed.toList();
  return BuBytes(output);
}