Understanding Variable-Length Integers in Ethereum
When building a serialized Bitcoin transaction, you will often come across a Variable-Length Integer (VLI) field. In this article, we will explain how VLI works and provide examples of how to use it in building a Bitcoin-style script.
What is a Variable-Length Integer?
A Variable-Length Integer (VLI) is an integer that has a specific number of bits assigned to it. Unlike fixed-length integers, which always have the same number of bytes (such as “int” in C), the length of a VLI can vary depending on the value.
Ethereum VLI Field
In Ethereum, a variable-length integer field is used to represent input values in a script. This field is commonly referred to as an “unsigned 32-bit integer” or simply “VLI”.
There are two main variants of the VLI field:
- 0x00: a single byte representing an unsigned 32-bit integer (0x…).
- 0x01: a short integer (0x…).
How does it work?
When a script needs to store or retrieve an input value, the programmer writes the corresponding VLI field to the transaction data. The length of the VLI field is usually specified when defining the input type.
Here is an example of how you can use VLI fields in a Bitcoin-style script:
// Define an unsigned 32-bit integer (0x000001)
uint256 remainder = 1;
// Input script to send ether to sender
scriptPubKey address = scriptPubKey {
pubkey = pubkey_of_address("0x..."), // VLI field with length 0x00 (one byte)
inputs[0].value = 10; // input value represented by VLI field
};
In this example:
- The variable “balance” is an unsigned 32-bit integer.
- The address scriptPubKey is a VLI field with length 0x00, representing the uint256 variable balance.
Creating a Bitcoin-style script with VLI
When writing a script that requires input values, you will often use multiple inputs and their corresponding VLI fields. Here is an example:
// Define two unsigned 32-bit integers (0x000001)
uint256 amount = 10;
fee uint256 = 1;
// Input script to transfer ether to the sender
scriptPubKey address = scriptPubKey {
pubkey = pubkey_of_address("0x..."), // VLI field with length 0x00 (one byte)
input[0].value = 10; // input value represented by VLI field
};
// Output script to store ether in the recipient's wallet
outputScript {
paytransfer(addressOf(0x...) as recipient);
}
In this example:
- “amount” and “fee” are unsigned 32-bit integers.
- The first input has a VLI field of length 0x00, representing “uint256 amount”.
- The second input has a VLI field of length 0x01, representing “uint256 fee”.
Conclusion
Variable-length integers (VLIs) are a vital part of the Ethereum scripting language. Understanding how they work can help you create efficient and effective scripts that interact with your blockchain. When writing Bitcoin-style scripts for serialization purposes, be sure to specify the lengths of the VLI fields when defining input types or script outputs.
By learning about VLI fields, you will be better equipped to create serialized transactions that meet the requirements of your blockchain ecosystem. Happy coding!

Leave A Comment