dusk-network / kelvin

Merkle tree toolkit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Branch::Level offset values are not always correct

CPerezz opened this issue · comments

After elaborating some tests, I've seen that once I get the Kelvin Branch, not all of the offsets do match with it's corresponding hash positions on the upper Level.

Branch: PoseidonBranch { root: 0x087cfe079397028bd6de80e102628c9783a363ccc57c2ffdf013c0b1e47cfd4b, levels: [PoseidonLevel { upper_lvl_hash: 3, leaves: [0x0000000000000000000000000000000000000000000000000000000000000003, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x000000000000000000000000000000000000000000000000000000000000000d, 0x0000000000000000000000000000000000000000000000000000000000000009] }, 

PoseidonLevel { upper_lvl_hash: 4, leaves: [0x0000000000000000000000000000000000000000000000000000000000000001, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x2b82f82f5c91c1b1860cc7a424536b4aee92b5ffb43d9fb39f27924d79f7d432] }, 

PoseidonLevel { upper_lvl_hash: 4, leaves: [0x0000000000000000000000000000000000000000000000000000000000000001, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x3c73c654ea296a4afbeb9ff93af061e6706fd2da476f25d8443185b48cc0ecb9] }, 

PoseidonLevel { upper_lvl_hash: 4, leaves: [0x0000000000000000000000000000000000000000000000000000000000000001, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x72a5773695750f72f621030fa9cfb75d9c28c7d395036d961d2992fec09d28f5] }, 

PoseidonLevel { upper_lvl_hash: 4, leaves: [0x0000000000000000000000000000000000000000000000000000000000000005, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000011, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x679270b490d6e7cfa44e929178c8fdeee800589ce3bbefb86b7bfb0d7ec9e9ec] }, 

PoseidonLevel { upper_lvl_hash: 3, leaves: [0x0000000000000000000000000000000000000000000000000000000000000002, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x25b1377a614dd96669d61be14dacab609d0a520290e231c07abdc07730dcccff, 0x0000000000000000000000000000000000000000000000000000000000000000] }, 

PoseidonLevel { upper_lvl_hash: 3, leaves: [0x0000000000000000000000000000000000000000000000000000000000000002, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x21d45a0052ba26a952ec18e6e928d075e81e0fcd770cb82a861a92a5c7952f79, 0x0000000000000000000000000000000000000000000000000000000000000000] }, 

PoseidonLevel { upper_lvl_hash: 3, leaves: [0x0000000000000000000000000000000000000000000000000000000000000002, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x175d8fae6690405ca49717d11919f04379c03e2ad3a80786c045f44ee64337d3, 0x0000000000000000000000000000000000000000000000000000000000000000] }, 

PoseidonLevel { upper_lvl_hash: 3, leaves: [0x000000000000000000000000000000000000000000000000000000000000000f, 0x4030657e7b7c68d60872d32a447c95fee900bb365d3e8e5c1ab10b87531af13f, 0x71814581e64527148ed5c4ce3a8c0d477360f05bc5261b9435cc32cfd151d847, 0x468b8743ee998e90309be2f343999c0140c899b297282f4737829044eb6fc3cc, 0x07ae7d964e2c86233cf880fc8531710fd37b640ba43ce522e6a56da386569312] }] }

This is the trace of the PoseidonBranch I import From the kelvin::Branch.
upper_lvl_hash is a field that represents the offset of the next level position of the hash of this level.
This takes in consideration that we compute the bitflags. So I add 1 to ALL of the offsets.

The problem is that there are only two levels that do not match with it's previous offset.

Error on hashing chain. Lvl Hash Announced: 0x2b82f82f5c91c1b1860cc7a424536b4aee92b5ffb43d9fb39f27924d79f7d432, Upper level hash: 0x0000000000000000000000000000000000000000000000000000000000000000
Error on hashing chain. Lvl Hash Announced: 0x25b1377a614dd96669d61be14dacab609d0a520290e231c07abdc07730dcccff, Upper level hash: 0x0000000000000000000000000000000000000000000000000000000000000000

After debugging and checking the offsets of the Branch before I generate my custom data structure, I've been able to see that the offsets indeed do not match on each level.

Could you test this within kelvin @krl ? Since after some hours of debugging I really think that the error is here.

Until we don't clarify what's going on, this is blocking: dusk-network/Poseidon252#11

After discussing it with @krl, it's verified that there's a bug in kelvin affecting the offsets that the Branch structure provides.

After some tests elaborated in a49363a it was verified that there was indeed no bug in the code.

This came from a missunderstanding in what was the offset field of each level referencing.
So closing this.