Right, I changed that to match your value but there is no need for it. Use tx output = 1 then. The point is that w gets incremented after y was chosen based on w=1.
See my BIP above. After increasing w, you should go back to step 3. In your example, you only go back to step 4
Well, I have a hard time seeing what you are implementing. I thought you meant to go to step 4 because if I have to go to step 3 the case isn't that useful.
If in step 3, y = y2 because you have enough bits left then you use them all. Next in step 4, there won't be any left. So unless utxo index = 0, you will have to increase w and go back to the beginning. What's the idea behind this case then?
This is different than what I think you meant in English.
In English:
If there is enough space, I'll use y1 bit to encode TxIndex. Otherwise, I'll use y2, given that it's enough to encode my interested tx. Otherwise, we need more words
If there is enough space after y is fixed, I'll use z1 bit to encode OutputIndex. Otherwise, I'll use z2, given that it's enough to encode my interested output. Otherwise, we need more words
By the bold sentence, I mean to re-evaluate the y with more words. It may not be clear enough.
Anyway, I try to redo your example:
Using the same values for `x=3`, `c=4` and the blockchain:
I want to encode a tx at block 5, index 3, output index = 1
Therefore, `y
min=2` and `z
min=1`
Now I go through the encoding steps.
- `w = ceil(3+2+1+4+1)/11 = 1`
- There are 40 tx, so `y1=6`
- `y2=11-1-3-4 = 3`
- `y=min(3,6)=3`
- `y
min>y`? no. don't increase `w` but use 3 bits. txindex is encoded as `011`.
Next phase. Let's say there are 2 outputs in that tx.
- `z1=1`
- `z2=11-1-3-3-4 = 0`
- `z=min(1,0) = 0`
- `z
min>z` ? yes. increase w. `w=2` and retry
- `y2=22-1-3-4 = 14`
- `y=min(14,6)=6`
- `y
min>y`? no. don't increase `w` but use 6 bits. txindex is encoded as `000011`.
- `z2=22-1-3-6-4 = 8`
- `z=min(1,8)=1`
- `z
min>z` ? no. use `z=1`. utxo index is encoded as `1`.
- checksum takes `22-1-3-6-1 = 11` bits
Result is
`1010000111x-xxxxxxxxxx0`
----------------
decode:
x=3, so it's block 101, i.e. block 5
Because it is block 5, we know y1=6; because it has 2 words, we know y2=14. Therefore, we know y=6. Therefore, txindex is 000011=3
Because it is index 3, we know z1=1; because it has 2 words, we know z2=8. Therefore, we know z=1. Therefore, outputindex is 1=1
The rest is 11 bit checksum + 1 bit version