relic-toolkit / relic

Code
Other
456 stars 179 forks source link

How to convert a fp12_t value into bn_t #260

Closed neuroney closed 1 year ago

neuroney commented 1 year ago

When I want to convert a fp12_t value into a bn_t value, I encountered something like this:

ERROR THROWN in /home/administrator/relic-0.6.0/src/bn/relic_bn_mem.c:133
ERROR THROWN in /home/administrator/relic-0.6.0/src/bn/relic_bn_mem.c:81
ERROR THROWN in /home/administrator/relic-0.6.0/src/bn/relic_bn_mem.c:81
ERROR THROWN in /home/administrator/relic-0.6.0/src/bn/relic_bn_mem.c:81
ERROR THROWN in /home/administrator/relic-0.6.0/src/bn/relic_bn_mem.c:81
ERROR THROWN in /home/administrator/relic-0.6.0/src/bn/relic_bn_mem.c:133
*** stack smashing detected ***: terminated
Aborted (core dumped)

I choose the curve B12_P638 under x86-64 ubuntu with the preset file:

cmake -DCHECK=off -DARITH=gmp  -DFP_PRIME=638 -DFP_QNRES=on -DFP_METHD="BASIC;COMBA;COMBA;MONTY;LOWER;LOWER;SLIDE" -DFPX_METHD="INTEG;INTEG;LAZYR" -DPP_METHD="LAZYR;OATEP" -DCFLAGS="-O2 -funroll-loops -fomit-frame-pointer" $1

My code is as follows:

    // omit some init codes.
    bn_t(q);
    bn_new(q);
    ep_curve_get_ord(q);

    fp12_t a;
    bn_t b;
    fp_new(a);
    bn_new(b);
    fp12_rand(a);
    uint8_t bin[12 * RLC_FP_BYTES];
    fp12_write_bin(bin, sizeof(bin), a, 0);
    bn_read_bin(b, bin, sizeof(bin));
    bn_mod(b,b,q);

Additionally, If I want to add -DALLOC=DYNAMIC into my preset file, I had 10251676643565_ pic

neuroney commented 1 year ago

When I limited the bn_t size, it seems OK without the overflow.

bn_read_bin(b, bin, RLC_FP_BYTES*3); // RLC_FP_BYTES*1~3

But I still want to is it possible to convert the whole fp12_t into bn_t?

dfaranha commented 1 year ago

Hi,

Why would you want to convert an fp12_t into a bn_t? The type fp12_t actually stores 12 integers modulo p.

Your original code would work after you rebuild RELIC with a larger BN_PRECI or with ALLOC=DYNAMIC to store that many bytes in a bn_t type.

neuroney commented 1 year ago

Thanks for your response! Actually I want to do a HASH on a fp12_t object, to map it into one integer modulo p. How can I add ALLOC=DYNAMIC during rebuilding? In a preset file?

When I wrote my preset file with "-DALLOC=DYNAMIC", I got errors during make, as the figure below:

The preset file is here:

cmake -DCHECK=off -DARITH=gmp  -DALLOC=DYNAMIC -DFP_PRIME=638 -DFP_QNRES=on -DFP_METHD="BASIC;COMBA;COMBA;MONTY;LOWER;LOWER;SLIDE" -DFPX_METHD="INTEG;INTEG;LAZYR" -DPP_METHD="LAZYR;OATEP" -DCFLAGS="-O2 -funroll-loops -fomit-frame-pointer" $1
dfaranha commented 1 year ago

If you want to hash an fp12_t element, you already have a byte vector with its contents right there, ready for hashing! After you hash to a smaller representation, then you can encode into an element modulo q.

Are you building the head of the main branch?

neuroney commented 1 year ago

Yes! I have solved the hash like this method. Thanks for your explanation.

I built the code in releases https://github.com/relic-toolkit/relic/archive/refs/tags/0.6.0.tar.gz

What I change is add -DALLOC=DYNAMIC into gmp-pbc-bls381.sh, and then follows the instruction in Wiki. And it had errors like the figure above.

neuroney commented 1 year ago

Oh, when I clone the main branch. No errors happen! Thanks for your time. We can close this issue now.