Skip to content

Add assert to ensure unused bits in 32 byte address are zero#2

Open
eranrund wants to merge 1 commit into
ProvableHQ:masterfrom
eranrund:unused-address-bit-assert
Open

Add assert to ensure unused bits in 32 byte address are zero#2
eranrund wants to merge 1 commit into
ProvableHQ:masterfrom
eranrund:unused-address-bit-assert

Conversation

@eranrund
Copy link
Copy Markdown

No description provided.

for i in 0u8..253u8 {
sliced_bits[i] = address_bits[i];
}
assert_eq(address_bytes[31] & 0xE0, 0u8);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0xE0 corresponds to the bit pattern 1110 0000. We use it to check the three most significant bits of the last byte and ensure they are zero.

To verify that this works as expected, I wrote the following test transition:

    transition test(addr: [u8; 32]) -> ([bool; 256], u8) {
        let bits = Serialize::to_bits_raw(addr);
        return (bits, addr[31] & 0xE0);
    }

I then ran it with:

leo run mint_debug.aleo/test '[255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8,31u8]'

Note that the final byte is 31, which in binary is 0001 1111.
This means its three most significant bits are zero.

The result was:

[true, true, ..., true, false, false, false], 0

This confirms that the address [255, 255, ..., 31] sets the first 253 bits to true, while the last three bits remain false. These last three bits are the unused bits we want to ensure are zero, and the mask addr[31] & 0xE0 returning 0 confirms that condition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant