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

Main #2

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ config/.nroots
config/.root_f
config/gen_roots
circuits/*
*_ff*
*.casm
src/cuda/constants.cpp
19 changes: 16 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ SNARKJS_REPO = https://github.com/druiz0992/snarkjs.git
RUST_CIRCOM_PATH = $(AUX_PATH)/za
RUST_CIRCOM_REPO = http://github.com/iden3/za.git

FFIASM_PATH = $(AUX_PATH)/ffiasm
FFIASM_REPO = http://github.com/iden3/ffiasm.git

CUSNARKS_LIB = libcusnarks.so
CUBIN_NAME = cusnarks.cubin

dirs= $(CUSRC_PATH) \
$(CTSRC_PATH)

aux_cdirs = $(PCG_PATH)
aux_jsdirs = $(SNARKJS_PATH)
aux_jsdirs = $(SNARKJS_PATH) \
$(FFIASM_PATH)
aux_rdirs = $(RUST_CIRCOM_PATH)

test_dirs = $(CTEST_PATH) \
Expand All @@ -80,6 +84,7 @@ test_dirs = $(CTEST_PATH) \

aux_repos = $(PCG_REPO) \
$(SNARKJS_REPO) \
$(FFIASM_REPO) \
$(RUST_CIRCOM_REPO)

config_dirs = $(CONFIG_PATH)
Expand Down Expand Up @@ -140,7 +145,7 @@ build:
(cd $$i; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) build); done


all: third_party_libs build
all: third_party_libs config

force_all: clean third_party_libs_clean all

Expand All @@ -163,7 +168,15 @@ debug_gpu:
@for i in $(CTEST_IDEAS_PATH); do \
echo "make test in $$i..."; \
(cd $$i; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) test); done
config:

config:
ifeq ($(CUSNARKS_CURVE),"")
read -p "Enter Curve <BN256 | BLS12381> " CURVE; \
(cd ${CUSRC_PATH}; ./ff.sh $$CURVE; cd -;)
else
(cd ${CUSRC_PATH}; ./ff.sh $(CUSNARKS_CURVE); cd -;)
endif
make clean build
@for i in $(CONFIG_SUBDIRS); do \
echo "make test in $$i..."; \
(cd $$i; $(MAKE) $(MFLAGS) $(MYMAKEFLAGS) config); done
Expand Down
28 changes: 9 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ The trusted setup is currently very slow. Implementation has been done using a s
## Installation
1. Download repository www.github.com/iden3/cusnarks.git. From now on, the folder where cusnarks is downloaded will be called *$CUSNARKS_HOME*

2. Change to *$CUSNARKS_HOME* directory and execute cusnarks_build.sh (builds Cusnarks) and launch_cusnarks (launches a proof server and requests proofs) scripts
```sh
cd $CUSNARKS_HOME
./cusnarks_build.sh
./launch_cusnarks.sh
```

Alternatively, you can install Cusnarks manually:

2. Ensure that all [dependencies][] are installed.

Expand Down Expand Up @@ -82,22 +74,20 @@ export LD_LIBRARY_PATH=$CUSNARKS_HOME/lib:$LD_LIBRARY_PATH
4. Build cusnarks to generate shared libraries in *$CUSNARKS_HOME/lib*.

```sh
make all
CUSNARKS_NROOTS=20 CUSNARKS_CURVE="BN256" make all
```

*CUSNARKS_NROOTS* limits the maximum number of constraints that can be proved. If *CUSNARKS_NROOTS = 20*, then the
maximum number of constraints is limited to 2<sup>CUSNARKS_NROOTS-1</sup> constraints.

*CUSNARKS_CURVE* Defines the curve to be used. Options inclide **BN256** and **BLS12381**. For **BN256**
*CUSNARKS_NROOTS* is limited to 28, whereas for **BLS12381** *CUSNARKS_NROOTS* is limited to 32.

Two libraries are generated upon compilation of cusnarks
- *libcusnarks.so* : Cusnarks shared library
- *pycusnarks.so* : Cusnarks shared library wrapped with Cython wrapper so that it can be used from Python

5. Generate some metadata required for cusnarks, including :
- Roots of unity for curve [bn128][BN128]. Default option generates 2^20, which allows processing circuits of
up to 2^19 constraints. When prompted, provide the desired number of roots to generate (maximum is 2^28).
- Location of folder to place input/output data. By default, location is *$CUSNARKS_HOME/data*

```sh
make config
```

6. Launch units tests (optional)
5. Launch units tests (optional)

```sh
CUDA_VISIBLE_DEVICES=1,2 make test
Expand Down
1 change: 1 addition & 0 deletions config/.curve
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_BN256
1 change: 1 addition & 0 deletions config/.curve_tmp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_BN256
2 changes: 1 addition & 1 deletion config/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ clean:

##################################################
config: $(OBJ)
python3 cusnarks_config.py ${NROOTS}
python3 cusnarks_config.py ${CUSNARKS_NROOTS} ${CUSNARKS_CURVE}

##################################################
%.o : %.cpp
Expand Down
25 changes: 21 additions & 4 deletions config/cusnarks_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

CUSNARKS_ROOTS_F = CUSNARKS_CONFIG + '.root_f'
CUSNARKS_ROOTS_N = CUSNARKS_CONFIG + '.nroots'
CUSNARKS_CURVE_TMP = CUSNARKS_CONFIG + '.curve_tmp'
CUSNARKS_CURVE = CUSNARKS_CONFIG + '.curve'

snarkjs = {
'url' : 'http://github.com/druiz0992/snarkjs.git',
Expand Down Expand Up @@ -88,6 +90,7 @@ def run():
sys.stdout.write('Configuring cusnarks....\n\n')

#download_repos([snarkjs, rust_circom])
print(sys.argv)
if len(sys.argv) > 1:
generate_roots(nroots = int(sys.argv[1]))
else:
Expand Down Expand Up @@ -139,6 +142,7 @@ def generate_config_f():
print(roots['nbits'],file=f)
f.close()


def get_roots_file():
return parse_configfile(config['folder'],'ROOTS_FILE')

Expand Down Expand Up @@ -170,20 +174,33 @@ def parse_configfile(fname, label):


def generate_roots(nroots=None):
f = open(CUSNARKS_CURVE_TMP,'r')
roots['curve'] = f.read().rstrip()
f.close()

f = open(CUSNARKS_CURVE,'w')
print(roots['curve'],file=f)
f.close()

if roots['curve'] == "_BN256":
roots['max_roots'] = 28
elif roots['curve'] == "_BLS12381":
roots['max_roots'] = 32

if nroots is None:
sys.stdout.write('####################################\n\n')
sys.stdout.write('Generating roots of unity....\n\n')
sys.stdout.write('Generating roots of unity for curve' +roots['curve'] +'....\n\n')
sys.stdout.write('Number of roots of unity imposes a limit on the maximum number of constraints\n')
sys.stdout.write('in your circuit. Default number roots of unity is 2^'+str(roots['nbits'])+'.\n')
sys.stdout.write('Do you want a different number in the range of 2^20 and 2^28 ['+str(roots['nbits'])+']?\n')
sys.stdout.write('Do you want a different number in the range of 2^20 and 2^'+str(roots['max_roots'])+' ['+str(roots['nbits'])+']?\n')
sys.stdout.flush()
b_root= sys.stdin.readline().rstrip()
if b_root != '':
while True:
try :
# Try until input is valid
if int(b_root) > 28 or int(b_root) < 20 :
sys.stdout.write('Do you want a different number in the range of 2^20 and 2^28 ['+str(roots['nbits'])+']?\n')
if int(b_root) > roots['max_roots'] or int(b_root) < 20 :
sys.stdout.write('Do you want a different number in the range of 2^20 and 2^'+str(roots['max_roots'])+' ['+str(roots['nbits'])+']?\n')
b_root= sys.stdin.readline().rstrip()
else:
break
Expand Down
2 changes: 1 addition & 1 deletion config/gen_roots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

void test_gen_roots(uint32_t nbits, char *filename)
{
uint32_t nsamples = (1<<nbits) * NWORDS_256BIT;
uint32_t nsamples = (1<<nbits) * NWORDS_FR;
uint32_t *roots = (uint32_t *)malloc( nsamples * sizeof(uint32_t));

computeRoots_h(roots,nbits);
Expand Down
30 changes: 30 additions & 0 deletions config/test.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<FR> 4026531841, 1138881939, 2042196113, 674490440, 2172737629, 3092268470, 3778125865, 811880050
<FR_NP> 4026531839, 3269588371, 1281954227, 1703315019, 2567316369, 3818559528, 226705842, 1945644829
<FR_R2> 2921426343, 465102405, 3814480355, 1409170097, 1404797061, 2353627965, 2135835813, 35049649
<FR_R2_rdc> 3032416320, 1586813153, 486258360, 709401790, 2711605229, 2302461540, 2144101756, 217602379
<FR_One_rdc> 1342177275, 2895524892, 2673921321, 922515093, 2021213742, 1718526831, 2584207151, 235567041
<FR_One2_rdc> 1342177275, 2895524892, 2673921321, 922515093, 2021213742, 1718526831, 2584207151, 235567041, 0, 0, 0, 0, 0, 0, 0, 0
<FR_IScaler> 1, 0, 0, 0, 0, 0, 0, 0, 4160749569, 2716924617, 1021098056, 2484728868, 1086368814, 3693617883, 1889062932, 405940025, 4093640705, 4075386926, 1531647084, 3727093302, 3777036869, 1245459528, 686110751, 608910038, 1912602625, 459650785, 1786921599, 2200791871, 2974887249, 2168863999, 2232118308, 710395044, 2969567233, 799266362, 4062042504, 1437641155, 426328791, 483082587, 3005122087, 761137547, 1350565889, 3116557799, 904635660, 1056065798, 3447016858, 1787675528, 1244140328, 786508799, 2688548865, 4275203517, 1473415886, 3012761767, 2809877243, 292488351, 363649449, 799194425, 3357540353, 559559080, 3905289648, 1843626103, 343823788, 1692378411, 4218371305, 805537237, 1544552449, 2996704158, 826259232, 3406541920, 3405764356, 2392323440, 1850764937, 808708644, 638058497, 4215276697, 1434227672, 4187999828, 2789250992, 2742295955, 2814445401, 810294347, 2332295169, 529595670, 1738211893, 283761486, 333510663, 2917282213, 1148801985, 811087199, 1031929857, 834238805, 1890204003, 479125963, 3400607794, 3004775341, 315980277, 811483625, 2529230849, 986560372, 4113683706, 2724291849, 639189063, 3048521906, 4194536719, 811681837, 1130397697, 3210204804, 930456261, 1699391145, 1405963346, 3070395188, 1838847644, 811780944, 430981121, 2174543372, 3633809835, 3334424440, 1789350487, 933848181, 2808486755, 811830497, 81272833, 1656712656, 2838002974, 2004457440, 4128527706, 2013058325, 1145822662, 811855274, 4201385985, 3545280945, 2440099543, 3486957588, 1003149019, 405179750, 2461974264, 811867662, 4113958913, 2342081442, 2241147828, 2080724014, 1587943324, 3896207758, 3120050064, 811873856, 1922761729, 3887965339, 2141671970, 3525090875, 1880340476, 1346754466, 3449087965, 811876953, 2974646785, 365939991, 4239417690, 4247274305, 2026539052, 2219511468, 1466123267, 811878502, 3500589313, 2899894613, 993323253, 313398725, 2099638341, 2655889969, 2622124566, 811879276, 3763560577, 2019388276, 3665243331, 493944582, 4283671633, 726595571, 3200125216, 811879663, 1747562561, 1579135108, 2853719722, 584217511, 1080720983, 4056915669, 1341641892, 811879857, 739563553, 3506492172, 300474269, 629353976, 3774212954, 1427108421, 412400231, 811879954, 235564049, 2322687056, 1171335191, 2799405856, 825991643, 2259688446, 2095263048, 811880002, 4278531593, 1730784497, 1606765652, 1736948148, 1499364636, 528494810, 2936694457, 811880026, 4152531717, 3582316866, 1824480882, 3353202942, 1836051132, 1810381640, 3357410161, 811880038, 1942048131, 213115755, 1933338498, 4161330339, 2004394380, 2451325055, 3567768013, 811880044, 2984289986, 2823482495, 4135250953, 270426741, 4236049653, 2771796762, 3672946939, 811880047
<FR_IScalerMont> 1342177275, 2895524892, 2673921321, 922515093, 2021213742, 1718526831, 2584207151, 235567041, 536870910, 2017203416, 210575069, 2945986415, 4244459333, 2405397650, 1033682860, 523723546, 268435455, 3156085356, 2252771182, 3620476855, 2122229666, 1202698825, 516841430, 261861773, 0, 0, 0, 0, 0, 0, 0, 536870912, 0, 0, 0, 0, 0, 0, 0, 268435456, 0, 0, 0, 0, 0, 0, 0, 134217728, 0, 0, 0, 0, 0, 0, 0, 67108864, 0, 0, 0, 0, 0, 0, 0, 33554432, 0, 0, 0, 0, 0, 0, 0, 16777216, 0, 0, 0, 0, 0, 0, 0, 8388608, 0, 0, 0, 0, 0, 0, 0, 4194304, 0, 0, 0, 0, 0, 0, 0, 2097152, 0, 0, 0, 0, 0, 0, 0, 1048576, 0, 0, 0, 0, 0, 0, 0, 524288, 0, 0, 0, 0, 0, 0, 0, 262144, 0, 0, 0, 0, 0, 0, 0, 131072, 0, 0, 0, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 16384, 0, 0, 0, 0, 0, 0, 0, 8192, 0, 0, 0, 0, 0, 0, 0, 4096, 0, 0, 0, 0, 0, 0, 0, 2048, 0, 0, 0, 0, 0, 0, 0, 1024, 0, 0, 0, 0, 0, 0, 0, 512, 0, 0, 0, 0, 0, 0, 0, 256, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 16
<FR_FrRoots_rdc> 1342177275, 2895524892, 2673921321, 922515093, 2021213742, 1718526831, 2584207151, 235567041, 2684354566, 2538324343, 3663242087, 4046942642, 151523886, 1373741639, 1193918714, 576313009, 1361252470, 3295459324, 3362905968, 3371487515, 225962990, 715055933, 2809173850, 86823741, 3718422171, 839371741, 640663292, 2009740710, 2958679608, 1031669727, 851251543, 313072991, 2782049806, 808429447, 3040884407, 2160890191, 3526748987, 1332068531, 1359520094, 182129160, 2084788, 674948881, 4273271594, 99275347, 4195435722, 3418568875, 3682139278, 806243344, 3312810116, 2445334266, 3871294809, 2810348955, 3112695501, 3285062893, 1834557287, 199870661, 478078481, 3036471777, 235793936, 2688900692, 3165989736, 950325226, 146887933, 343222048, 2957341071, 906433325, 419834437, 1145835400, 2847384746, 1318566208, 560996845, 381542712, 1378844484, 1400764043, 1336038289, 4002628589, 2532446747, 2524239413, 3071842516, 100297165, 566044699, 2763636506, 596032766, 713701755, 604842013, 2456040492, 2264715324, 740482141, 2484115749, 1763238488, 2307219501, 2433311310, 2079031774, 2750036547, 1734411876, 52110648, 2866432738, 1204437980, 1219068114, 3792014971, 2288193035, 2233618078, 1082100474, 411474472, 2607702858, 3027752343, 4273080555, 1688373939, 3286738374, 3031346375, 3062038040, 300536456, 3050922057, 1379431612, 1715819742, 2202644496, 2384721285, 1747130755, 1174262077, 71437120, 1762911349, 256262463, 41801470, 1048350381, 4046694136, 4055309226, 3635683432, 4985195, 4231436152, 2508194812, 1691228322, 3266513799, 2734468569, 3972558889, 2498065460, 102599226, 1663553846, 1447669747, 646811944, 4087669992, 3946508122, 1134373069, 3748855121, 312301536, 1155884220, 610070301, 1284122868, 3435135939, 899868951, 3218749292, 2111158058, 276765315, 1271224695, 1809614327, 2091177166, 9421434, 799876720, 3300149683, 2159252406, 738140125, 3511864287, 3059188583, 2358508108, 2742309257, 1028499886, 297653269, 1912209802, 32700002, 671854903, 1122487497, 1970510094, 2763342977, 542713177, 3534557307, 2466396080, 354263110, 1057907419, 1939397391, 3999283509, 3427711227, 3880890035, 1126166362, 531604111, 164274642, 741724105, 1540283322, 352528123, 2689184862, 2779538624, 1738706695, 4259900953, 503775857, 590811720, 3578000784, 1428855913, 4270554544, 2532913917, 1111778333, 2899314593, 86385812, 1862345222, 1118493264, 399567957, 3196788705, 76660490, 2784657892, 509910055, 351851300, 3651394190, 3502470067, 2180821901, 1257785242, 1819425766, 3913537646, 1438129372, 110926525, 2309774724, 3445133061, 2753492306, 2863171575, 1803752443, 4056712894, 3397191501, 2998045, 3318016071, 837128342, 2270645498, 1128921006, 1214547208, 4086364158, 3319683703, 462598102
<FR_One> 1, 0, 0, 0, 0, 0, 0, 0
<FR_Zero> 0, 0, 0, 0, 0, 0, 0, 0
<FP> 3632069959, 1008765974, 1752287885, 2541841041, 2172737629, 3092268470, 3778125865, 811880050
<FP_NP> 3834012553, 2278688642, 516582089, 2665381221, 406051456, 3635399632, 2441645163, 4118422199
<FP_R2> 1401617033, 4079811675, 3561292283, 3051821329, 172064758, 1202396927, 3401069855, 114859889
<FP_R2_rdc> 3658821855, 2983030191, 2804432310, 1660031206, 182061819, 4018080524, 760816964, 553479824
<FP_One_rdc> 3314486685, 3546104717, 4123462461, 175696680, 2021213740, 1718526831, 2584207151, 235567041
<FP_One2_rdc> 3314486685, 3546104717, 4123462461, 175696680, 2021213740, 1718526831, 2584207151, 235567041, 0, 0, 0, 0, 0, 0, 0, 0
<FP_G1_inf_rdc> 0, 0, 0, 0, 0, 0, 0, 0, 3314486685, 3546104717, 4123462461, 175696680, 2021213740, 1718526831, 2584207151, 235567041, 0, 0, 0, 0, 0, 0, 0, 0
<FP_G2_inf_rdc> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3314486685, 3546104717, 4123462461, 175696680, 2021213740, 1718526831, 2584207151, 235567041, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
<FP_G1x_rdc> 3314486685, 3546104717, 4123462461, 175696680, 2021213740, 1718526831, 2584207151, 235567041
<FP_G1y_rdc> 2334006074, 2797242139, 3951957627, 351393361, 4042427480, 3437053662, 873447006, 471134083
<FP_B_rdc> 1353525463, 2048379561, 3780452793, 527090042, 1768673924, 860613198, 3457654158, 706701124
<FP_G2x1_rdc> 45883430, 2390996433, 1232798066, 3706394933, 2541820639, 4223149639, 2945863739, 425146433
<FP_G2x2_rdc> 2823577920, 2947838845, 1476581572, 1615060314, 1386229638, 166285564, 988445547, 352252035
<FP_G2y1_rdc> 2288773622, 1637743261, 4120812408, 4269789847, 589004286, 4288551522, 2929607174, 687701739
<FP_G2y2_rdc> 3340261102, 1678334806, 847068347, 3696752930, 859115638, 1442395582, 2482857090, 228892902
<FP_B2_1_rdc> 2008548008, 1006188771, 909333341, 34282279, 1232425568, 649588208, 1132767341, 622118450
<FP_B2_2_rdc> 3520921447, 954723532, 2479754558, 1710273405, 581697706, 3611939037, 1248365901, 21084622
<FP_One> 1, 0, 0, 0, 0, 0, 0, 0
<FP_Zero> 0, 0, 0, 0, 0, 0, 0, 0
Loading