relic-toolkit / relic

Code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong square root computation

guidovranken opened this issue · comments

#include <relic_conf.h>
#include <relic.h>

int main(void)
{
    if ( core_init() != RLC_OK ) abort();

    bn_t A, R;

    bn_null(A); bn_new(A);
    bn_null(R); bn_new(R);

    //const char* s = "16";
    const char* s = "64";
    /* noret */ bn_read_str(A, s, strlen(s), 10);

    /* noret */ bn_srt(R, A);

    int outsize = bn_size_str(R, 10);
    char* s2 = malloc(outsize);
    /* noret */ bn_write_str(s2, outsize, R, 10);
    printf("%s\n", s2);
    free(s2);

    bn_free(A);
    bn_free(R);
    return 0;
}

It says the square root of 16 is 5, and the square root of 64 is 9. Is this a bug or an internal rounding effect that's deemed acceptable?

Embarrasing. :)

The function itself was only used in the library to approximate square roots for half-gcd computations, but there is no reason for blatant errors there.

Thanks for the quick fix.

@dfaranha

Sqrt(7) now hangs

#include <relic_conf.h>
#include <relic.h>

int main(void)
{
    if ( core_init() != RLC_OK ) abort();

    bn_t A, R;

    bn_null(A); bn_new(A);
    bn_null(R); bn_new(R);

    const char* s = "7";
    /* noret */ bn_read_str(A, s, strlen(s), 10);

    /* noret */ bn_srt(R, A);

    int outsize = bn_size_str(R, 10);
    char* s2 = malloc(outsize);
    /* noret */ bn_write_str(s2, outsize, R, 10);
    printf("%s\n", s2);
    free(s2);

    bn_free(A);
    bn_free(R);
    return 0;
}

Just pushed a commit hopefully fixing this in a more robust way, with more unit tests.

Thanks, I'll test.