ergoplatform / eips

Ergo Improvement Proposals
64 stars 36 forks source link

[EIP-0004] Give reference of algorithm `intToVlq` #13

Open scalahub opened 4 years ago

scalahub commented 4 years ago

In EIP-0004, the encoding in R4 is given as '\x0e' + intToVlq(byteArray.length) + byteArray Would be good to have a link or a code snippet explaining how to implement intToVlq

gagarin55 commented 4 years ago

FYI, it is Base 128 Varints encoding - https://developers.google.com/protocol-buffers/docs/encoding, not VLQ

HazeyOneKenobi commented 2 years ago

From testing and minting NFT's that match anon_real's implementation on auctionhouse, I think it is VLQ. Sample Java below;

public static byte[] vlqEncode(long n) {

  int numRelevantBits = 64 - Long.numberOfLeadingZeros(n);
  int numBytes = (numRelevantBits + 6) / 7;
  if (numBytes == 0)
    numBytes = 1;
  byte[] output = new byte[numBytes];
  for (int i = numBytes - 1; i >= 0; i--)
  {
    int curByte = (int)(n & 0x7F);
    if (i != (numBytes - 1))
      curByte |= 0x80;
    output[i] = (byte)curByte;
    n >>>= 7;
  }
  return output;
}

N.B. If encoding a hex string, halve the value of it's string length that you feed into this.