akosba / jsnark

A Java library for zk-SNARK circuits
MIT License
207 stars 85 forks source link

How to initialize a number on the prime field? #23

Closed w1001766 closed 4 years ago

w1001766 commented 4 years ago

I was testing the circuit evaluation, and I am trying to initialize some random number on the prime field by doing this in jsnark/libsnark/src/CircuitReader.cpp:

FieldT a = 10000000000000;

When I do a.print(), I got some garbage value. But when I run it on a real circuit and trying to print out some of the wire values, it actually works. Did I do something wrong?

akosba commented 4 years ago

I was testing the circuit evaluation, and I am trying to initialize some random number on the prime field by doing this in jsnark/libsnark/src/CircuitReader.cpp: FieldT a = 10000000000000; When I do a.print(), I got some garbage value.

This seems to be related to libsnark (or libff, to be more accurate). I tried the case you mentioned:

FieldT a = 10000000000000;
a.print()

and it is working fine. Was that the same test case you tried? What is the range of the random number you are trying to assign?

Note that if you try much greater numbers, the result will seem unexpected to you, as I think this is expected to fit into a long. If the range of the random value you're trying to assign is greater than the allowed range, you could try to look into other ways to assign the value to a FieldT variable. (I think you could find this by inspecting the code of Fp in libff. If I recall correctly, you should be able to provide the number as a string, e.g., FieldT("..."), where the argument is the decimal representation of the string. See Fp and Bigint.)