Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of cx_bn_cnt_bits function is broken? #460

Open
aido opened this issue Feb 25, 2024 · 0 comments
Open

Implementation of cx_bn_cnt_bits function is broken? #460

aido opened this issue Feb 25, 2024 · 0 comments

Comments

@aido
Copy link
Contributor

aido commented Feb 25, 2024

Hi,

I think the implementation of cx_bn_cnt_bits in Speculos may be broken. It is exhibiting some strange behaviour.

I have written the following three tests:

cx_bn_t bn_temp;
CX_CHECK(cx_bn_alloc(&bn_temp, 16));

uint32_t nbits, temp_u32 = 0;

for (uint8_t i = 0; i <= 8; i++) {
    CX_CHECK(cx_bn_set_u32(bn_temp, (uint32_t) i));
    CX_CHECK(cx_bn_cnt_bits(bn_temp, &nbits));
    PRINTF("Test 1: Number of bits set to 1 in number %d is %d\n", i, nbits);
}

for (uint8_t i = 8; i >= 0; i--) {
    CX_CHECK(cx_bn_set_u32(bn_temp, (uint32_t) i));
    CX_CHECK(cx_bn_cnt_bits(bn_temp, &nbits));
    PRINTF("Test 2: Number of bits set to 1 in number %d is %d\n", i, nbits);
}

CX_CHECK(cx_bn_set_u32(bn_temp, (uint32_t) 8));
CX_CHECK(cx_bn_cnt_bits(bn_temp, &nbits));

while (nbits > 0) {
    CX_CHECK(cx_bn_get_u32(bn_temp, &temp_u32));
    PRINTF("Test 3: Number of bits set to 1 in number %d is %d\n", temp_u32, nbits);
    CX_CHECK(cx_bn_shr(bn_temp, 1));
}

Each test results in an infinite loop. It would seem that rather than count the set bits in a Big Num cx_bn_cnt_bits instead calculates the position of the MSB.

09:29:27.929:seproxyhal: printf: Test 1: Number of bits set to 1 in number 0 is -127
09:29:27.931:seproxyhal: printf: Test 1: Number of bits set to 1 in number 1 is 1
09:29:27.932:seproxyhal: printf: Test 1: Number of bits set to 1 in number 2 is 2
09:29:27.934:seproxyhal: printf: Test 1: Number of bits set to 1 in number 3 is 2
09:29:27.935:seproxyhal: printf: Test 1: Number of bits set to 1 in number 4 is 3
09:29:27.936:seproxyhal: printf: Test 1: Number of bits set to 1 in number 5 is 3
09:29:27.938:seproxyhal: printf: Test 1: Number of bits set to 1 in number 6 is 3
09:29:27.939:seproxyhal: printf: Test 1: Number of bits set to 1 in number 7 is 3
09:29:27.941:seproxyhal: printf: Test 1: Number of bits set to 1 in number 8 is 4
09:29:27.942:seproxyhal: printf: Test 2: Number of bits set to 1 in number 8 is 4
09:29:27.943:seproxyhal: printf: Test 2: Number of bits set to 1 in number 7 is 3
09:29:27.945:seproxyhal: printf: Test 2: Number of bits set to 1 in number 6 is 3
09:29:27.946:seproxyhal: printf: Test 2: Number of bits set to 1 in number 5 is 3
09:29:27.947:seproxyhal: printf: Test 2: Number of bits set to 1 in number 4 is 3
09:29:27.949:seproxyhal: printf: Test 2: Number of bits set to 1 in number 3 is 2
09:29:27.950:seproxyhal: printf: Test 2: Number of bits set to 1 in number 2 is 2
09:29:27.952:seproxyhal: printf: Test 2: Number of bits set to 1 in number 1 is 1
09:29:27.953:seproxyhal: printf: Test 2: Number of bits set to 1 in number 0 is 0
09:29:27.954:seproxyhal: printf: Test 2: Number of bits set to 1 in number 255 is 8
09:29:27.956:seproxyhal: printf: Test 2: Number of bits set to 1 in number 254 is 8
09:29:27.957:seproxyhal: printf: Test 2: Number of bits set to 1 in number 253 is 8
09:29:27.959:seproxyhal: printf: Test 2: Number of bits set to 1 in number 252 is 8
09:29:27.960:seproxyhal: printf: Test 2: Number of bits set to 1 in number 251 is 8
09:29:27.962:seproxyhal: printf: Test 2: Number of bits set to 1 in number 250 is 8
09:29:27.963:seproxyhal: printf: Test 2: Number of bits set to 1 in number 249 is 8
09:29:27.965:seproxyhal: printf: Test 2: Number of bits set to 1 in number 248 is 8
09:29:27.966:seproxyhal: printf: Test 2: Number of bits set to 1 in number 247 is 8
09:29:27.967:seproxyhal: printf: Test 2: Number of bits set to 1 in number 246 is 8
09:29:27.969:seproxyhal: printf: Test 2: Number of bits set to 1 in number 245 is 8
.
.
.
09:36:07.109:seproxyhal: printf: Test 2: Number of bits set to 1 in number 8 is 4
09:36:07.110:seproxyhal: printf: Test 2: Number of bits set to 1 in number 7 is 3
09:36:07.112:seproxyhal: printf: Test 2: Number of bits set to 1 in number 6 is 3
09:36:07.113:seproxyhal: printf: Test 2: Number of bits set to 1 in number 5 is 3
09:36:07.114:seproxyhal: printf: Test 2: Number of bits set to 1 in number 4 is 3
09:36:07.116:seproxyhal: printf: Test 2: Number of bits set to 1 in number 3 is 2
09:36:07.117:seproxyhal: printf: Test 2: Number of bits set to 1 in number 2 is 2
09:36:07.119:seproxyhal: printf: Test 2: Number of bits set to 1 in number 1 is 1
09:36:07.120:seproxyhal: printf: Test 2: Number of bits set to 1 in number 0 is 0
09:36:07.109:seproxyhal: printf: Test 2: Number of bits set to 1 in number 255 is 8
09:36:07.123:seproxyhal: printf: Test 2: Number of bits set to 1 in number 254 is 8
09:36:07.124:seproxyhal: printf: Test 2: Number of bits set to 1 in number 253 is 8
09:36:07.126:seproxyhal: printf: Test 2: Number of bits set to 1 in number 252 is 8
09:36:07.127:seproxyhal: printf: Test 2: Number of bits set to 1 in number 251 is 8
09:36:07.129:seproxyhal: printf: Test 2: Number of bits set to 1 in number 250 is 8
09:36:07.130:seproxyhal: printf: Test 2: Number of bits set to 1 in number 249 is 8
09:36:07.131:seproxyhal: printf: Test 2: Number of bits set to 1 in number 248 is 8
09:36:07.133:seproxyhal: printf: Test 2: Number of bits set to 1 in number 247 is 8
.
.
.
09:41:28.614:seproxyhal: printf: Test 3: Number of bits set to 1 in number 8 is 4
09:41:28.615:seproxyhal: printf: Test 3: Number of bits set to 1 in number 4 is 4
09:41:28.617:seproxyhal: printf: Test 3: Number of bits set to 1 in number 2 is 4
09:41:28.618:seproxyhal: printf: Test 3: Number of bits set to 1 in number 1 is 4
09:41:28.619:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
09:41:28.609:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
09:41:28.622:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
09:41:28.623:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
09:41:28.625:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
09:41:28.626:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
09:41:28.628:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
09:41:28.629:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
09:41:28.630:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
09:41:28.632:seproxyhal: printf: Test 3: Number of bits set to 1 in number 0 is 4
.
.
.
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

No branches or pull requests

1 participant