From b2ac4daaa7be20fde7fcd765c81c23c14053c6f4 Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Sat, 18 Apr 2020 22:33:59 +0200 Subject: [PATCH] Blake to Blake2b and use of native big num --- circuits/pedersen.circom | 22 +- circuits/pedersen_old.circom | 20 +- index.js | 2 - package-lock.json | 406 +++++++---------------------------- package.json | 7 +- src/babyjub.js | 53 ++--- src/eddsa.js | 77 +++---- src/g2_gencontract.js | 3 +- src/mimc7.js | 17 +- src/mimcsponge.js | 22 +- src/pedersenHash.js | 26 +-- src/poseidon.js | 15 +- src/smt.js | 58 ++--- src/smt_hashes_mimc.js | 2 + src/smt_hashes_poseidon.js | 5 +- src/smt_memdb.js | 10 +- src/utils.js | 87 -------- test/aliascheck.js | 22 +- test/babyjub.js | 45 ++-- test/babyjub_js.js | 84 ++++---- test/binsub.js | 18 +- test/binsum.js | 12 +- test/circuits/in.json | 258 ---------------------- test/comparators.js | 147 ++++++------- test/eddsa.js | 6 +- test/eddsa_js.js | 31 ++- test/eddsamimc.js | 13 +- test/eddsaposeidon.js | 12 +- test/escalarmul.js | 23 +- test/escalarmulany.js | 9 +- test/escalarmulfix.js | 14 +- test/montgomery.js | 19 +- test/multiplexer.js | 62 +++--- test/pedersen.js | 14 +- test/pedersen2.js | 4 +- test/poseidoncircuit.js | 1 - test/poseidoncontract.js | 1 - test/sha256.js | 4 +- test/sign.js | 23 +- test/smtjs.js | 67 +++--- test/smtprocessor.js | 24 +-- test/smtverifier.js | 18 +- 42 files changed, 572 insertions(+), 1191 deletions(-) delete mode 100644 src/utils.js delete mode 100644 test/circuits/in.json diff --git a/circuits/pedersen.circom b/circuits/pedersen.circom index 245d5d8..9b335f2 100644 --- a/circuits/pedersen.circom +++ b/circuits/pedersen.circom @@ -177,17 +177,17 @@ template Pedersen(n) { signal output out[2]; var BASE[10][2] = [ - [10457101036533406547632367118273992217979173478358440826365724437999023779287,19824078218392094440610104313265183977899662750282163392862422243483260492317], - [2671756056509184035029146175565761955751135805354291559563293617232983272177,2663205510731142763556352975002641716101654201788071096152948830924149045094], - [5802099305472655231388284418920769829666717045250560929368476121199858275951,5980429700218124965372158798884772646841287887664001482443826541541529227896], - [7107336197374528537877327281242680114152313102022415488494307685842428166594,2857869773864086953506483169737724679646433914307247183624878062391496185654], - [20265828622013100949498132415626198973119240347465898028410217039057588424236,1160461593266035632937973507065134938065359936056410650153315956301179689506], - [1487999857809287756929114517587739322941449154962237464737694709326309567994,14017256862867289575056460215526364897734808720610101650676790868051368668003], - [14618644331049802168996997831720384953259095788558646464435263343433563860015,13115243279999696210147231297848654998887864576952244320558158620692603342236], - [6814338563135591367010655964669793483652536871717891893032616415581401894627,13660303521961041205824633772157003587453809761793065294055279768121314853695], - [3571615583211663069428808372184817973703476260057504149923239576077102575715,11981351099832644138306422070127357074117642951423551606012551622164230222506], - [18597552580465440374022635246985743886550544261632147935254624835147509493269,6753322320275422086923032033899357299485124665258735666995435957890214041481] - ] + [7688621503272331394947188562469131124099290577812125474996268020905176040083,6637287939860384587467947982369268811366630904563077767287326262235485629411], + [11549681895645637778324638856880330712650895608496649854094912415387988201330,5771732722784528537721081267383956005090479808901717812009343940574217488577], + [18790245153471844934157747708238883966079935875787657036767664036124524381945,18300275459419441151064576487317481499516933849631632883767173501999997278432], + [16301069151422548986850494139112207641738464387919729729324473657161689764196,8215273507373494014441104012907835625670941526105528197815397741007626226499], + [12597665704678284488008395353749282149622295037737374782196049599390683534185,4072455241781501621593714139281767473040087753548015968773801065193764079468], + [4729410576230735258214831208080552588881894465489299233097088872252465832672,14367731890670510422926552586486424937476635415639602730590517235570020260326], + [7546420686025050869200393054526306477146836870617678274607971529534032974471,8663210466512842901413293603100781938253817808912549776944118491282484711929], + [6544653022506992755201027646251976600601201151329001772892901529509137954387,5932506509962692832681604586561215780097326378431958035490245111470435106811], + [12376274813795671622507230443130412169480807188767687554607910279743333852725,10116389110458158800073166533660211332390835019644001845057351607297889034557], + [18268098112071835140361074835791174816144587762778386397940339415400583397725,8120955462199046866292537174552276799123029303901205157708576578886090835495] + ]; var nSegments = ((n-1)\200)+1; diff --git a/circuits/pedersen_old.circom b/circuits/pedersen_old.circom index 9ddc387..cf867e5 100644 --- a/circuits/pedersen_old.circom +++ b/circuits/pedersen_old.circom @@ -29,16 +29,16 @@ template Pedersen(n) { component escalarMuls[nexps]; var PBASE[10][2] = [ - [10457101036533406547632367118273992217979173478358440826365724437999023779287,19824078218392094440610104313265183977899662750282163392862422243483260492317], - [2671756056509184035029146175565761955751135805354291559563293617232983272177,2663205510731142763556352975002641716101654201788071096152948830924149045094], - [5802099305472655231388284418920769829666717045250560929368476121199858275951,5980429700218124965372158798884772646841287887664001482443826541541529227896], - [7107336197374528537877327281242680114152313102022415488494307685842428166594,2857869773864086953506483169737724679646433914307247183624878062391496185654], - [20265828622013100949498132415626198973119240347465898028410217039057588424236,1160461593266035632937973507065134938065359936056410650153315956301179689506], - [1487999857809287756929114517587739322941449154962237464737694709326309567994,14017256862867289575056460215526364897734808720610101650676790868051368668003], - [14618644331049802168996997831720384953259095788558646464435263343433563860015,13115243279999696210147231297848654998887864576952244320558158620692603342236], - [6814338563135591367010655964669793483652536871717891893032616415581401894627,13660303521961041205824633772157003587453809761793065294055279768121314853695], - [3571615583211663069428808372184817973703476260057504149923239576077102575715,11981351099832644138306422070127357074117642951423551606012551622164230222506], - [18597552580465440374022635246985743886550544261632147935254624835147509493269,6753322320275422086923032033899357299485124665258735666995435957890214041481] + [7688621503272331394947188562469131124099290577812125474996268020905176040083,6637287939860384587467947982369268811366630904563077767287326262235485629411], + [11549681895645637778324638856880330712650895608496649854094912415387988201330,5771732722784528537721081267383956005090479808901717812009343940574217488577], + [18790245153471844934157747708238883966079935875787657036767664036124524381945,18300275459419441151064576487317481499516933849631632883767173501999997278432], + [16301069151422548986850494139112207641738464387919729729324473657161689764196,8215273507373494014441104012907835625670941526105528197815397741007626226499], + [12597665704678284488008395353749282149622295037737374782196049599390683534185,4072455241781501621593714139281767473040087753548015968773801065193764079468], + [4729410576230735258214831208080552588881894465489299233097088872252465832672,14367731890670510422926552586486424937476635415639602730590517235570020260326], + [7546420686025050869200393054526306477146836870617678274607971529534032974471,8663210466512842901413293603100781938253817808912549776944118491282484711929], + [6544653022506992755201027646251976600601201151329001772892901529509137954387,5932506509962692832681604586561215780097326378431958035490245111470435106811], + [12376274813795671622507230443130412169480807188767687554607910279743333852725,10116389110458158800073166533660211332390835019644001845057351607297889034557], + [18268098112071835140361074835791174816144587762778386397940339415400583397725,8120955462199046866292537174552276799123029303901205157708576578886090835495] ]; var i; diff --git a/index.js b/index.js index 8af9aa9..a0293a9 100644 --- a/index.js +++ b/index.js @@ -7,8 +7,6 @@ exports.pedersenHash = require("./src/pedersenHash"); exports.SMT = require("./src/smt").SMT; exports.SMTMemDB = require("./src/smt_memdb"); exports.poseidon = require("./src/poseidon"); -exports.leInt2Buff = require("./src/utils").leInt2Buff; -exports.leBuff2int = require("./src/utils").leBuff2int; diff --git a/package-lock.json b/package-lock.json index 79691eb..3e83e38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -57,9 +57,9 @@ "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==" }, "@types/node": { - "version": "12.12.31", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.31.tgz", - "integrity": "sha512-T+wnJno8uh27G9c+1T+a1/WYCHzLeDqtsGJkoEdSp2X8RTh3oOCZQcUnjAx90CS8cmmADX51O0FI/tu9s0yssg==" + "version": "12.12.36", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.36.tgz", + "integrity": "sha512-hmmypvyO/uTLFYCYu6Hlb3ydeJ11vXRxg8/WJ0E3wvwmPO0y47VqnfmXFVuWlysO0Zyj+je1Y33rQeuYkZ51GQ==" }, "@web3-js/scrypt-shim": { "version": "0.1.0", @@ -292,16 +292,6 @@ "safe-buffer": "^5.1.1" } }, - "blake-hash": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/blake-hash/-/blake-hash-1.1.0.tgz", - "integrity": "sha512-rNbOFPT7DC/0XnLBJ0noWuzcV+9kHwEKzRGljHMDLQzYv6WZT1vjV3UkWQuNFzyr5tIL7zSsw7A834pgTl75xQ==", - "requires": { - "bindings": "^1.2.1", - "inherits": "^2.0.3", - "nan": "^2.2.1" - } - }, "blake2b": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/blake2b/-/blake2b-2.1.3.tgz", @@ -448,9 +438,9 @@ } }, "buffer": { - "version": "5.5.0", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.5.0.tgz", - "integrity": "sha512-9FTEDjLjwoAkEwyMGDjYJQN2gfRgOKBKRfiglhvibGbpeeU/pQn1bJxQqm32OD/AIeEuHxU9roxXxg34Byp/Ww==", + "version": "5.6.0", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.6.0.tgz", + "integrity": "sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw==", "requires": { "base64-js": "^1.0.2", "ieee754": "^1.1.4" @@ -632,28 +622,27 @@ } }, "circom": { - "version": "0.5.3", - "resolved": "https://registry.npmjs.org/circom/-/circom-0.5.3.tgz", - "integrity": "sha512-uKmcWE+qvy7+wJ4aXxv2cfNJxhdAV7NiXwCWsiBCzwhzFpA2P+JQQGgUj7C1GIMiJLNmmhx62jWnXW+fQ2PvBw==", + "version": "0.5.8", + "resolved": "https://registry.npmjs.org/circom/-/circom-0.5.8.tgz", + "integrity": "sha512-NTp5R3qiNdtuF0SzZlnuKabrGRySR+uNK1+5ZIIpHUPLzIPXwle7sha/Jz1RDyHtV/z8yZNNVm6PXVoYuvnR3A==", "requires": { - "big-integer": "^1.6.32", "chai": "^4.2.0", - "circom_runtime": "0.0.3", + "circom_runtime": "0.0.5", "ffiasm": "0.0.2", - "ffjavascript": "0.0.3", - "ffwasm": "0.0.6", + "ffjavascript": "0.1.0", + "ffwasm": "0.0.7", "fnv-plus": "^1.3.1", - "r1csfile": "0.0.3", + "r1csfile": "0.0.4", "tmp-promise": "^2.0.2", "wasmbuilder": "0.0.10" } }, "circom_runtime": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.0.3.tgz", - "integrity": "sha512-z4ypbs9cTQn7+2FHZNTnccMj6kQCcKT2agYqCrm2kdLBJh9LDoxU1JVu5mSnVuOtgc7BclQ7r0xclG0zP2rxhw==", + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.0.5.tgz", + "integrity": "sha512-WEZHnRO1AJIDI2w1yX6rq4G1/MMKnExT3qirjBQR7x43i9Ww3E/wusBHTbXWLb8aYyxsBU07LGy0YXPywPGYvA==", "requires": { - "big-integer": "^1.6.48", + "ffjavascript": "0.1.0", "fnv-plus": "^1.3.1" } }, @@ -681,11 +670,6 @@ "mimic-response": "^1.0.0" } }, - "code-point-at": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", - "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=" - }, "color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", @@ -807,6 +791,7 @@ "version": "6.0.5", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", + "dev": true, "requires": { "nice-try": "^1.0.4", "path-key": "^2.0.1", @@ -870,9 +855,9 @@ "integrity": "sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=" }, "decompress": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/decompress/-/decompress-4.2.0.tgz", - "integrity": "sha1-eu3YVCflqS2s/lVnSnxQXpbQH50=", + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/decompress/-/decompress-4.2.1.tgz", + "integrity": "sha512-e48kc2IjU+2Zw8cTb6VZcJQ3lgVbS4uuB1TfCHbiZIP/haNXm+SVyhu+87jts5/3ROpd82GSVCoNs/z8l4ZOaQ==", "requires": { "decompress-tar": "^4.0.0", "decompress-tarbz2": "^4.0.0", @@ -1036,9 +1021,9 @@ } }, "dom-walk": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.1.tgz", - "integrity": "sha1-ZyIm3HTI95mtNTB9+TaroRrNYBg=" + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.2.tgz", + "integrity": "sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w==" }, "drbg.js": { "version": "1.0.1", @@ -1070,9 +1055,9 @@ "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" }, "ejs": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.0.1.tgz", - "integrity": "sha512-cuIMtJwxvzumSAkqaaoGY/L6Fc/t6YvoP9/VIaK0V/CyqKLEQ8sqODmYfy/cjXEdZ9+OOL8TecbJu+1RsofGDw==" + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.0.2.tgz", + "integrity": "sha512-IncmUpn1yN84hy2shb0POJ80FWrfGNY0cxO9f4v+/sG7qcBvAtVWUA1IdzY/8EYUmOVhoKJVdJjNd3AZcnxOjA==" }, "elliptic": { "version": "6.5.2", @@ -1173,7 +1158,8 @@ "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true }, "eslint": { "version": "6.8.0", @@ -1504,9 +1490,9 @@ } }, "ethereum-bloom-filters": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/ethereum-bloom-filters/-/ethereum-bloom-filters-1.0.6.tgz", - "integrity": "sha512-dE9CGNzgOOsdh7msZirvv8qjHtnHpvBlKe2647kM8v+yeF71IRso55jpojemvHV+jMjr48irPWxMRaHuOWzAFA==", + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/ethereum-bloom-filters/-/ethereum-bloom-filters-1.0.7.tgz", + "integrity": "sha512-cDcJJSJ9GMAcURiAWO3DxIEhTL/uWqlQnvgKpuYQzYPrt/izuGU+1ntQmHt0IRq6ADoSYHFnB+aCEFIldjhkMQ==", "requires": { "js-sha3": "^0.8.0" } @@ -1557,9 +1543,9 @@ }, "dependencies": { "@types/node": { - "version": "10.17.17", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.17.tgz", - "integrity": "sha512-gpNnRnZP3VWzzj5k3qrpRC6Rk3H/uclhAVo1aIvwzK5p5cOrs9yEyQ8H/HBsBY0u5rrWxXEiVPQ0dEB6pkjE8Q==" + "version": "10.17.20", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.20.tgz", + "integrity": "sha512-XgDgo6W10SeGEAM0k7FosJpvLCynOTYns4Xk3J5HGrA+UI/bKZ30PGMzOP5Lh2zs4259I71FSYLAtjnx3qhObw==" }, "elliptic": { "version": "6.3.3", @@ -1637,20 +1623,6 @@ "safe-buffer": "^5.1.1" } }, - "execa": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/execa/-/execa-1.0.0.tgz", - "integrity": "sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==", - "requires": { - "cross-spawn": "^6.0.0", - "get-stream": "^4.0.0", - "is-stream": "^1.1.0", - "npm-run-path": "^2.0.0", - "p-finally": "^1.0.0", - "signal-exit": "^3.0.0", - "strip-eof": "^1.0.0" - } - }, "express": { "version": "4.17.1", "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", @@ -1790,17 +1762,17 @@ } }, "ffjavascript": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.0.3.tgz", - "integrity": "sha512-uXbiC7cNbFzNJCdkGlbQf2d7GciY1ICMcBeAA7+D8RHPr9Y5zYiDRWtU5etjAV8TplE7eZQ9Iqd9ieFi0ARJLA==", + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.1.0.tgz", + "integrity": "sha512-dmKlUasSfvUcxBm8nCSKl2x7EFJsXA7OVP8XLFA03T2+6mAc3IiVLC2ambEVOcMOhyhl0vJfVZjM9f9d38D1rw==", "requires": { "big-integer": "^1.6.48" } }, "ffwasm": { - "version": "0.0.6", - "resolved": "https://registry.npmjs.org/ffwasm/-/ffwasm-0.0.6.tgz", - "integrity": "sha512-bEBKYANozdyZBCGE6XLg4s/CaJRZdFGQgbthy7EZ4OhNCIpycgklS5mlf88Bw4fXSddlU1V9iYXI4JwfGO3BhQ==", + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/ffwasm/-/ffwasm-0.0.7.tgz", + "integrity": "sha512-17cTLzv7HHAKqZbX8MvHxjSrR0yDdn1sh4TVsTbAvO9e6klhFicnyoVXc/sCuViV/M8g65sCmVrAmoPCZp1YkQ==", "requires": { "big-integer": "^1.6.48", "wasmbuilder": "0.0.10" @@ -2935,11 +2907,6 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, - "invert-kv": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-2.0.0.tgz", - "integrity": "sha512-wPVv/y/QQ/Uiirj/vh3oP+1Ww+AWehmi1g5fFWGPF6IpCBCDVrhgHRMvrLfdYcwDh3QJbGXDW4JAuzxElLSqKA==" - }, "ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -3070,7 +3037,8 @@ "isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", + "dev": true }, "isstream": { "version": "0.1.2", @@ -3176,14 +3144,6 @@ "json-buffer": "3.0.0" } }, - "lcid": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", - "integrity": "sha512-avPEb8P8EGnwXKClwsNUgryVjllcRqtMYa49NTsbQagYuT1DcXnl1915oxWjoyGrXR6zH/Y0Zc96xWsPcoDKeA==", - "requires": { - "invert-kv": "^2.0.0" - } - }, "levn": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/levn/-/levn-0.3.0.tgz", @@ -3237,14 +3197,6 @@ } } }, - "map-age-cleaner": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz", - "integrity": "sha512-bJzx6nMoP6PDLPBFmg7+xRKeFZvFboMrGlxmNj9ClvX53KrmvM5bXFXEWjbz4cz1AFn+jWJ9z/DJSz7hrs0w3w==", - "requires": { - "p-defer": "^1.0.0" - } - }, "md5.js": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz", @@ -3260,16 +3212,6 @@ "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, - "mem": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/mem/-/mem-4.3.0.tgz", - "integrity": "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w==", - "requires": { - "map-age-cleaner": "^0.1.1", - "mimic-fn": "^2.0.0", - "p-is-promise": "^2.0.0" - } - }, "merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -3307,11 +3249,6 @@ "mime-db": "1.43.0" } }, - "mimic-fn": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", - "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==" - }, "mimic-response": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-1.0.1.tgz", @@ -3653,7 +3590,8 @@ "nice-try": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", - "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==" + "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", + "dev": true }, "node-environment-flags": { "version": "1.0.6", @@ -3676,19 +3614,6 @@ "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.0.tgz", "integrity": "sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ==" }, - "npm-run-path": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz", - "integrity": "sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=", - "requires": { - "path-key": "^2.0.0" - } - }, - "number-is-nan": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", - "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" - }, "number-to-bn": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/number-to-bn/-/number-to-bn-1.7.0.tgz", @@ -3787,16 +3712,6 @@ "word-wrap": "~1.2.3" } }, - "os-locale": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-3.1.0.tgz", - "integrity": "sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==", - "requires": { - "execa": "^1.0.0", - "lcid": "^2.0.0", - "mem": "^4.0.0" - } - }, "os-tmpdir": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", @@ -3808,21 +3723,11 @@ "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-1.1.0.tgz", "integrity": "sha512-s73XxOZ4zpt1edZYZzvhqFa6uvQc1vwUa0K0BdtIZgQMAJj9IbebH+JkgKZc9h+B05PKHLOTl4ajG1BmNrVZlw==" }, - "p-defer": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/p-defer/-/p-defer-1.0.0.tgz", - "integrity": "sha1-n26xgvbJqozXQwBKfU+WsZaw+ww=" - }, "p-finally": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", "integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=" }, - "p-is-promise": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-2.1.0.tgz", - "integrity": "sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==" - }, "p-limit": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.2.tgz", @@ -3897,7 +3802,8 @@ "path-key": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", - "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=" + "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=", + "dev": true }, "path-to-regexp": { "version": "0.1.7", @@ -4039,11 +3945,11 @@ } }, "r1csfile": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.3.tgz", - "integrity": "sha512-TNrodnbHw5yAMv2gj0Ezf22XS3q8zGEjdPHZLBmJauIPFxm6QmyzxlB92yZ5WNkjEtJiS7p1hvkO9/RsJXRDjw==", + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.4.tgz", + "integrity": "sha512-1Y/zzzEjQVTR/gPlduRaKi2K+yU+UxqtsS+obDLEEb4WAzwCkKGybRfp037CUW5OApeleS1WdGmtKv9K9FPhsA==", "requires": { - "big-integer": "^1.6.48" + "ffjavascript": "0.1.0" } }, "randombytes": { @@ -4262,7 +4168,8 @@ "semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", - "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", + "dev": true }, "send": { "version": "0.17.1", @@ -4357,6 +4264,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", + "dev": true, "requires": { "shebang-regex": "^1.0.0" } @@ -4364,12 +4272,14 @@ "shebang-regex": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", - "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=" + "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=", + "dev": true }, "signal-exit": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", - "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=" + "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=", + "dev": true }, "simple-concat": { "version": "1.0.0", @@ -4429,174 +4339,6 @@ } } }, - "snarkjs": { - "version": "0.1.22", - "resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.1.22.tgz", - "integrity": "sha512-Z1ifCeSluzxbG6TLpI1eL7cCnG/7XVdmPw0ClvPc4Uu28Pp1nAikm9gbp1VnqmFYyJyop0sloS7uOv7UK/CXhQ==", - "requires": { - "big-integer": "^1.6.43", - "chai": "^4.2.0", - "circom_runtime": "0.0.3", - "escape-string-regexp": "^1.0.5", - "keccak": "^2.0.0", - "r1csfile": "0.0.1", - "yargs": "^12.0.5" - }, - "dependencies": { - "ansi-regex": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", - "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=" - }, - "cliui": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/cliui/-/cliui-4.1.0.tgz", - "integrity": "sha512-4FG+RSG9DL7uEwRUZXZn3SS34DiDPfzP0VOiEwtUWlE+AR2EIg+hSyvrIgUUfhdgR/UkAeW2QHgeP+hWrXs7jQ==", - "requires": { - "string-width": "^2.1.1", - "strip-ansi": "^4.0.0", - "wrap-ansi": "^2.0.0" - } - }, - "find-up": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz", - "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==", - "requires": { - "locate-path": "^3.0.0" - } - }, - "get-caller-file": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-1.0.3.tgz", - "integrity": "sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==" - }, - "is-fullwidth-code-point": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", - "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=" - }, - "locate-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", - "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==", - "requires": { - "p-locate": "^3.0.0", - "path-exists": "^3.0.0" - } - }, - "p-locate": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz", - "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==", - "requires": { - "p-limit": "^2.0.0" - } - }, - "path-exists": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" - }, - "r1csfile": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.1.tgz", - "integrity": "sha512-1mUgD7XXpU/EAf4xWiIt1jaQbQuMBDKAiEJ2eZYsN9rHOJtBWZqLYDkAmC4WJhCwK3O3NZKhvRMaNBM5dBpp1Q==" - }, - "require-main-filename": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-1.0.1.tgz", - "integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE=" - }, - "string-width": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", - "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", - "requires": { - "is-fullwidth-code-point": "^2.0.0", - "strip-ansi": "^4.0.0" - } - }, - "strip-ansi": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", - "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", - "requires": { - "ansi-regex": "^3.0.0" - } - }, - "wrap-ansi": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", - "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", - "requires": { - "string-width": "^1.0.1", - "strip-ansi": "^3.0.1" - }, - "dependencies": { - "ansi-regex": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", - "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" - }, - "is-fullwidth-code-point": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", - "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", - "requires": { - "number-is-nan": "^1.0.0" - } - }, - "string-width": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", - "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", - "requires": { - "code-point-at": "^1.0.0", - "is-fullwidth-code-point": "^1.0.0", - "strip-ansi": "^3.0.0" - } - }, - "strip-ansi": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", - "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", - "requires": { - "ansi-regex": "^2.0.0" - } - } - } - }, - "yargs": { - "version": "12.0.5", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-12.0.5.tgz", - "integrity": "sha512-Lhz8TLaYnxq/2ObqHDql8dX8CJi97oHxrjUcYtzKbbykPtVW9WB+poxI+NM2UIzsMgNCZTIf0AQwsjK5yMAqZw==", - "requires": { - "cliui": "^4.0.0", - "decamelize": "^1.2.0", - "find-up": "^3.0.0", - "get-caller-file": "^1.0.1", - "os-locale": "^3.0.0", - "require-directory": "^2.1.1", - "require-main-filename": "^1.0.1", - "set-blocking": "^2.0.0", - "string-width": "^2.0.0", - "which-module": "^2.0.0", - "y18n": "^3.2.1 || ^4.0.0", - "yargs-parser": "^11.1.1" - } - }, - "yargs-parser": { - "version": "11.1.1", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-11.1.1.tgz", - "integrity": "sha512-C6kB/WJDiaxONLJQnF8ccx9SEeoTTLek8RVbaOIsrAUS8VrBEXfmeSnCZxygc+XC2sNMBIwOOnfcxiynjHsVSQ==", - "requires": { - "camelcase": "^5.0.0", - "decamelize": "^1.2.0" - } - } - } - }, "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -4690,11 +4432,6 @@ "is-natural-number": "^4.0.1" } }, - "strip-eof": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", - "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=" - }, "strip-hex-prefix": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/strip-hex-prefix/-/strip-hex-prefix-1.0.0.tgz", @@ -4995,9 +4732,9 @@ "integrity": "sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og==" }, "unbzip2-stream": { - "version": "1.3.3", - "resolved": "https://registry.npmjs.org/unbzip2-stream/-/unbzip2-stream-1.3.3.tgz", - "integrity": "sha512-fUlAF7U9Ah1Q6EieQ4x4zLNejrRvDWUYmxXUpN3uziFYCHapjWFaCAnreY9bGgxzaMCFAPPpYNng57CypwJVhg==", + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/unbzip2-stream/-/unbzip2-stream-1.4.1.tgz", + "integrity": "sha512-sgDYfSDPMsA4Hr2/w7vOlrJBlwzmyakk1+hW8ObLvxSp0LA36LcL2XItGvOT3OSblohSdevMuT8FQjLsqyy4sA==", "requires": { "buffer": "^5.2.1", "through": "^2.3.8" @@ -5120,9 +4857,9 @@ }, "dependencies": { "@types/node": { - "version": "10.17.17", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.17.tgz", - "integrity": "sha512-gpNnRnZP3VWzzj5k3qrpRC6Rk3H/uclhAVo1aIvwzK5p5cOrs9yEyQ8H/HBsBY0u5rrWxXEiVPQ0dEB6pkjE8Q==" + "version": "10.17.20", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.17.20.tgz", + "integrity": "sha512-XgDgo6W10SeGEAM0k7FosJpvLCynOTYns4Xk3J5HGrA+UI/bKZ30PGMzOP5Lh2zs4259I71FSYLAtjnx3qhObw==" } } }, @@ -5392,6 +5129,7 @@ "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", + "dev": true, "requires": { "isexe": "^2.0.0" } @@ -5516,11 +5254,11 @@ } }, "xhr-request-promise": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/xhr-request-promise/-/xhr-request-promise-0.1.2.tgz", - "integrity": "sha1-NDxE0e53JrhkgGloLQ+EDIO0Jh0=", + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/xhr-request-promise/-/xhr-request-promise-0.1.3.tgz", + "integrity": "sha512-YUBytBsuwgitWtdRzXDDkWAXzhdGB8bYm0sSzMPZT7Z2MBjMSTHFsyCT1yCRATY+XC69DUrQraRAEgcoCRaIPg==", "requires": { - "xhr-request": "^1.0.1" + "xhr-request": "^1.1.0" } }, "xhr2-cookies": { @@ -5575,9 +5313,9 @@ } }, "yargs-parser": { - "version": "18.1.2", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.2.tgz", - "integrity": "sha512-hlIPNR3IzC1YuL1c2UwwDKpXlNFBqD1Fswwh1khz5+d8Cq/8yc/Mn0i+rQXduu8hcrFKvO7Eryk+09NecTQAAQ==", + "version": "18.1.3", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz", + "integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==", "requires": { "camelcase": "^5.0.0", "decamelize": "^1.2.0" diff --git a/package.json b/package.json index 78195ba..d80b1ba 100644 --- a/package.json +++ b/package.json @@ -24,12 +24,9 @@ "author": "0Kims", "license": "GPL-3.0", "dependencies": { - "blake-hash": "^1.1.0", "blake2b": "^2.1.3", - "circom": "0.5.3", - "ffjavascript": "0.0.3", - "snarkjs": "^0.1.22", - "typedarray-to-buffer": "^3.1.5", + "circom": "0.5.8", + "ffjavascript": "0.1.0", "web3": "^1.2.6" }, "devDependencies": { diff --git a/src/babyjub.js b/src/babyjub.js index 38c60d9..6651571 100644 --- a/src/babyjub.js +++ b/src/babyjub.js @@ -1,6 +1,6 @@ -const bigInt = require("big-integer"); -const ZqField = require("ffjavascript").ZqField; -const utils = require("./utils.js"); +const F1Field = require("ffjavascript").F1Field; +const Scalar = require("ffjavascript").Scalar; +const utils = require("ffjavascript").utils; exports.addPoint = addPoint; exports.mulPointEscalar = mulPointEscalar; @@ -8,23 +8,27 @@ exports.inCurve = inCurve; exports.inSubgroup = inSubgroup; exports.packPoint = packPoint; exports.unpackPoint = unpackPoint; + + +exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); +const F = new F1Field(exports.p); +exports.F = F; + exports.Generator = [ - bigInt("995203441582195749578291179787384436505546430278305826713579947235728471134"), - bigInt("5472060717959818805561601436314318772137091100104008585924551046643952123905") + F.e("995203441582195749578291179787384436505546430278305826713579947235728471134"), + F.e("5472060717959818805561601436314318772137091100104008585924551046643952123905") ]; exports.Base8 = [ - bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), - bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") + F.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), + F.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") ]; -exports.order = bigInt("21888242871839275222246405745257275088614511777268538073601725287587578984328"); -exports.subOrder = exports.order.shiftRight(3); -exports.p = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"); -exports.A = bigInt("168700"); -exports.D = bigInt("168696"); +exports.order = Scalar.fromString("21888242871839275222246405745257275088614511777268538073601725287587578984328"); +exports.subOrder = Scalar.shiftRight(exports.order, 3); +exports.A = F.e("168700"); +exports.D = F.e("168696"); function addPoint(a,b) { - const F = new ZqField(exports.p); const res = []; @@ -44,28 +48,28 @@ function addPoint(a,b) { res[0] = F.div( F.add(beta, gamma), - F.add(bigInt.one, dtau) + F.add(F.one, dtau) ); res[1] = F.div( F.add(delta, F.sub(F.mul(exports.A,beta), gamma)), - F.sub(bigInt.one, dtau) + F.sub(F.one, dtau) ); return res; } function mulPointEscalar(base, e) { - let res = [bigInt("0"),bigInt("1")]; - let rem = bigInt(e); + let res = [F.e("0"),F.e("1")]; + let rem = e; let exp = base; - while (! rem.isZero()) { - if (rem.isOdd()) { + while (! Scalar.isZero(rem)) { + if (Scalar.isOdd(rem)) { res = addPoint(res, exp); } exp = addPoint(exp, exp); - rem = rem.shiftRight(1); + rem = Scalar.shiftRight(rem, 1); } return res; @@ -74,11 +78,10 @@ function mulPointEscalar(base, e) { function inSubgroup(P) { if (!inCurve(P)) return false; const res= mulPointEscalar(P, exports.subOrder); - return (res[0].equals(bigInt(0))) && (res[1].equals(bigInt(1))); + return (F.isZero(res[0]) && F.eq(res[1], F.one)); } function inCurve(P) { - const F = new ZqField(exports.p); const x2 = F.square(P[0]); const y2 = F.square(P[1]); @@ -92,15 +95,13 @@ function inCurve(P) { function packPoint(P) { const buff = utils.leInt2Buff(P[1], 32); - if (P[0].greater(exports.p.shiftRight(1))) { + if (F.lt(P[0], F.zero)) { buff[31] = buff[31] | 0x80; } return buff; } function unpackPoint(_buff) { - const F = new ZqField(exports.p); - const buff = Buffer.from(_buff); let sign = false; const P = new Array(2); @@ -109,7 +110,7 @@ function unpackPoint(_buff) { buff[31] = buff[31] & 0x7F; } P[1] = utils.leBuff2int(buff); - if (P[1].greaterOrEquals(exports.p)) return null; + if (Scalar.gt(P[1], exports.p)) return null; const y2 = F.square(P[1]); diff --git a/src/eddsa.js b/src/eddsa.js index adc2d6c..34703b6 100644 --- a/src/eddsa.js +++ b/src/eddsa.js @@ -1,7 +1,8 @@ -const createBlakeHash = require("blake-hash"); -const bigInt = require("big-integer"); +const blake2b = require("blake2b"); +const Scalar = require("ffjavascript").Scalar; +const F1Field = require("ffjavascript").F1Field; const babyJub = require("./babyjub"); -const utils = require("./utils"); +const utils = require("ffjavascript").utils; const pedersenHash = require("./pedersenHash").hash; const mimc7 = require("./mimc7"); const poseidon = require("./poseidon.js"); @@ -31,27 +32,28 @@ function pruneBuffer(_buff) { } function prv2pub(prv) { - const sBuff = pruneBuffer(createBlakeHash("blake512").update(prv).digest().slice(0,32)); + const sBuff = pruneBuffer(blake2b(64).update(prv).digest().slice(0,32)); let s = utils.leBuff2int(sBuff); - const A = babyJub.mulPointEscalar(babyJub.Base8, s.shiftRight(3)); + const A = babyJub.mulPointEscalar(babyJub.Base8, Scalar.shr(s,3)); return A; } function sign(prv, msg) { - const h1 = createBlakeHash("blake512").update(prv).digest(); + const h1 = Buffer.from(blake2b(64).update(prv).digest()); const sBuff = pruneBuffer(h1.slice(0,32)); const s = utils.leBuff2int(sBuff); - const A = babyJub.mulPointEscalar(babyJub.Base8, s.shiftRight(3)); + const A = babyJub.mulPointEscalar(babyJub.Base8, Scalar.shr(s, 3)); - const rBuff = createBlakeHash("blake512").update(Buffer.concat([h1.slice(32,64), msg])).digest(); + const rBuff = Buffer.from(blake2b(64).update(Buffer.concat([h1.slice(32,64), msg])).digest()); let r = utils.leBuff2int(rBuff); - r = r.mod(babyJub.subOrder); + const Fr = new F1Field(babyJub.subOrder); + r = Fr.e(r); const R8 = babyJub.mulPointEscalar(babyJub.Base8, r); const R8p = babyJub.packPoint(R8); const Ap = babyJub.packPoint(A); const hmBuff = pedersenHash(Buffer.concat([R8p, Ap, msg])); const hm = utils.leBuff2int(hmBuff); - const S = r.add(hm.times(s)).mod(babyJub.subOrder); + const S = Fr.add(r , Fr.mul(hm, s)); return { R8: R8, S: S @@ -59,18 +61,19 @@ function sign(prv, msg) { } function signMiMC(prv, msg) { - const h1 = createBlakeHash("blake512").update(prv).digest(); + const h1 = Buffer.from(blake2b(64).update(prv).digest()); const sBuff = pruneBuffer(h1.slice(0,32)); const s = utils.leBuff2int(sBuff); - const A = babyJub.mulPointEscalar(babyJub.Base8, s.shiftRight(3)); + const A = babyJub.mulPointEscalar(babyJub.Base8, Scalar.shr(s, 3)); const msgBuff = utils.leInt2Buff(msg, 32); - const rBuff = createBlakeHash("blake512").update(Buffer.concat([h1.slice(32,64), msgBuff])).digest(); + const rBuff = Buffer.from(blake2b(64).update(Buffer.concat([h1.slice(32,64), msgBuff])).digest()); let r = utils.leBuff2int(rBuff); - r = r.mod(babyJub.subOrder); + const Fr = new F1Field(babyJub.subOrder); + r = Fr.e(r); const R8 = babyJub.mulPointEscalar(babyJub.Base8, r); const hm = mimc7.multiHash([R8[0], R8[1], A[0], A[1], msg]); - const S = r.add(hm.times(s)).mod(babyJub.subOrder); + const S = Fr.add(r , Fr.mul(hm, s)); return { R8: R8, S: S @@ -78,18 +81,19 @@ function signMiMC(prv, msg) { } function signMiMCSponge(prv, msg) { - const h1 = createBlakeHash("blake512").update(prv).digest(); + const h1 = Buffer.from(blake2b(64).update(prv).digest()); const sBuff = pruneBuffer(h1.slice(0,32)); const s = utils.leBuff2int(sBuff); - const A = babyJub.mulPointEscalar(babyJub.Base8, s.shiftRight(3)); + const A = babyJub.mulPointEscalar(babyJub.Base8, Scalar.shr(s, 3)); const msgBuff = utils.leInt2Buff(msg, 32); - const rBuff = createBlakeHash("blake512").update(Buffer.concat([h1.slice(32,64), msgBuff])).digest(); + const rBuff = Buffer.from(blake2b(64).update(Buffer.concat([h1.slice(32,64), msgBuff])).digest()); let r = utils.leBuff2int(rBuff); - r = r.mod(babyJub.subOrder); + const Fr = new F1Field(babyJub.subOrder); + r = Fr.e(r); const R8 = babyJub.mulPointEscalar(babyJub.Base8, r); const hm = mimcsponge.multiHash([R8[0], R8[1], A[0], A[1], msg]); - const S = r.add(hm.times(s)).mod(babyJub.subOrder); + const S = Fr.add(r , Fr.mul(hm, s)); return { R8: R8, S: S @@ -97,19 +101,20 @@ function signMiMCSponge(prv, msg) { } function signPoseidon(prv, msg) { - const h1 = createBlakeHash("blake512").update(prv).digest(); + const h1 = Buffer.from(blake2b(64).update(prv).digest()); const sBuff = pruneBuffer(h1.slice(0,32)); const s = utils.leBuff2int(sBuff); - const A = babyJub.mulPointEscalar(babyJub.Base8, s.shiftRight(3)); + const A = babyJub.mulPointEscalar(babyJub.Base8, Scalar.shr(s, 3)); const msgBuff = utils.leInt2Buff(msg, 32); - const rBuff = createBlakeHash("blake512").update(Buffer.concat([h1.slice(32,64), msgBuff])).digest(); + const rBuff = Buffer.from(blake2b(64).update(Buffer.concat([h1.slice(32,64), msgBuff])).digest()); let r = utils.leBuff2int(rBuff); - r = r.mod(babyJub.subOrder); + const Fr = new F1Field(babyJub.subOrder); + r = Fr.e(r); const R8 = babyJub.mulPointEscalar(babyJub.Base8, r); const hash = poseidon.createHash(6, 8, 57); const hm = hash([R8[0], R8[1], A[0], A[1], msg]); - const S = r.add(hm.times(s)).mod(babyJub.subOrder); + const S = Fr.add(r , Fr.mul(hm, s)); return { R8: R8, S: S @@ -133,11 +138,11 @@ function verify(msg, sig, A) { const hm = utils.leBuff2int(hmBuff); const Pleft = babyJub.mulPointEscalar(babyJub.Base8, sig.S); - let Pright = babyJub.mulPointEscalar(A, hm.times(bigInt("8"))); + let Pright = babyJub.mulPointEscalar(A, Scalar.mul(hm,8)); Pright = babyJub.addPoint(sig.R8, Pright); - if (!Pleft[0].equals(Pright[0])) return false; - if (!Pleft[1].equals(Pright[1])) return false; + if (!babyJub.F.eq(Pleft[0],Pright[0])) return false; + if (!babyJub.F.eq(Pleft[1],Pright[1])) return false; return true; } @@ -155,11 +160,11 @@ function verifyMiMC(msg, sig, A) { const hm = mimc7.multiHash([sig.R8[0], sig.R8[1], A[0], A[1], msg]); const Pleft = babyJub.mulPointEscalar(babyJub.Base8, sig.S); - let Pright = babyJub.mulPointEscalar(A, hm.times(bigInt("8"))); + let Pright = babyJub.mulPointEscalar(A, Scalar.mul(hm, 8)); Pright = babyJub.addPoint(sig.R8, Pright); - if (!Pleft[0].equals(Pright[0])) return false; - if (!Pleft[1].equals(Pright[1])) return false; + if (!babyJub.F.eq(Pleft[0],Pright[0])) return false; + if (!babyJub.F.eq(Pleft[1],Pright[1])) return false; return true; } @@ -179,11 +184,11 @@ function verifyPoseidon(msg, sig, A) { const hm = hash([sig.R8[0], sig.R8[1], A[0], A[1], msg]); const Pleft = babyJub.mulPointEscalar(babyJub.Base8, sig.S); - let Pright = babyJub.mulPointEscalar(A, hm.times(bigInt("8"))); + let Pright = babyJub.mulPointEscalar(A, Scalar.mul(hm, 8)); Pright = babyJub.addPoint(sig.R8, Pright); - if (!Pleft[0].equals(Pright[0])) return false; - if (!Pleft[1].equals(Pright[1])) return false; + if (!babyJub.F.eq(Pleft[0],Pright[0])) return false; + if (!babyJub.F.eq(Pleft[1],Pright[1])) return false; return true; } @@ -204,8 +209,8 @@ function verifyMiMCSponge(msg, sig, A) { let Pright = babyJub.mulPointEscalar(A, hm.times(bigInt("8"))); Pright = babyJub.addPoint(sig.R8, Pright); - if (!Pleft[0].equals(Pright[0])) return false; - if (!Pleft[1].equals(Pright[1])) return false; + if (!babyJub.F.eq(Pleft[0],Pright[0])) return false; + if (!babyJub.F.eq(Pleft[1],Pright[1])) return false; return true; } diff --git a/src/g2_gencontract.js b/src/g2_gencontract.js index 9a9d995..9ed1698 100644 --- a/src/g2_gencontract.js +++ b/src/g2_gencontract.js @@ -4,7 +4,6 @@ const Contract = require("./evmasm"); const G2 = require("snarkjs").bn128.G2; -const bigInt = require("snarkjs").bigInt; function toHex256(a) { @@ -539,7 +538,7 @@ function createCode(P, w) { function storeVals() { C.push(VAR_POINTS); // p for (let i=0; i { if (typeof seed === "undefined") seed = SEED; const c = Web3Utils.keccak256(seed+"_iv"); - const cn = bigInt(Web3Utils.toBN(c).toString()); + const cn = Scalar.FromString(Web3Utils.toBN(c).toString()); const iv = cn.mod(F.p); return iv; }; @@ -25,17 +26,17 @@ exports.getConstants = (seed, nRounds) => { const n1 = Web3Utils.toBN(c).mod(Web3Utils.toBN(F.p.toString())); const c2 = Web3Utils.padLeft(Web3Utils.toHex(n1), 64); - cts[i] = bigInt(Web3Utils.toBN(c2).toString()); + cts[i] = Scalar.fromString(Web3Utils.toBN(c2).toString()); } - cts[0] = bigInt(0); + cts[0] = F.e(0); return cts; }; const cts = exports.getConstants(SEED, 91); exports.hash = (_x_in, _k) =>{ - const x_in = bigInt(_x_in); - const k = bigInt(_k); + const x_in = F.e(_x_in); + const k = F.e(_k); let r; for (let i=0; i { r, arr[i] ), - exports.hash(bigInt(arr[i]), r) + exports.hash(F.e(arr[i]), r) ); } return r; diff --git a/src/mimcsponge.js b/src/mimcsponge.js index 6b5e4db..bed4f54 100644 --- a/src/mimcsponge.js +++ b/src/mimcsponge.js @@ -1,7 +1,7 @@ -const bigInt = require("big-integer"); +const Scalar = require("ffjavascript").Scalar const Web3Utils = require("web3-utils"); const ZqField = require("ffjavascript").ZqField; -const F = new ZqField(bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617")); +const F = new ZqField(Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617")); const SEED = "mimcsponge"; const NROUNDS = 220; @@ -9,7 +9,7 @@ const NROUNDS = 220; exports.getIV = (seed) => { if (typeof seed === "undefined") seed = SEED; const c = Web3Utils.keccak256(seed+"_iv"); - const cn = bigInt(Web3Utils.toBN(c).toString()); + const cn = Scalar.fromString(Web3Utils.toBN(c).toString()); const iv = cn.mod(F.p); return iv; }; @@ -24,23 +24,23 @@ exports.getConstants = (seed, nRounds) => { const n1 = Web3Utils.toBN(c).mod(Web3Utils.toBN(F.p.toString())); const c2 = Web3Utils.padLeft(Web3Utils.toHex(n1), 64); - cts[i] = bigInt(Web3Utils.toBN(c2).toString()); + cts[i] = F.e(Web3Utils.toBN(c2).toString()); } - cts[0] = bigInt(0); - cts[cts.length - 1] = bigInt(0); + cts[0] = F.e(0); + cts[cts.length - 1] = F.e(0); return cts; }; const cts = exports.getConstants(SEED, NROUNDS); exports.hash = (_xL_in, _xR_in, _k) =>{ - let xL = bigInt(_xL_in); - let xR = bigInt(_xR_in); - const k = bigInt(_k); + let xL = F.e(_xL_in); + let xR = F.e(_xR_in); + const k = F.e(_k); for (let i=0; i { let C = F.zero; for (let i=0; i { let state = []; assert(inputs.length <= t); assert(inputs.length > 0); - for (let i=0; i 0) { const record = await this.db.get(resFind.siblings[resFind.siblings.length - 1]); - if ((record.length == 3)&&(record[0].equals(bigInt.one))) { + if ((record.length == 3)&&(F.eq(record[0], F.one))) { mixed = false; res.oldKey = record[1]; res.oldValue = record[2]; @@ -108,16 +96,16 @@ class SMT { } else if (record.length == 2) { mixed = true; res.oldKey = key; - res.oldValue = bigInt(0); + res.oldValue = F.zero; res.isOld0 = true; - rtNew = bigInt.zero; + rtNew = F.zero; } else { throw new Error("Invalid node. Database corrupted"); } } else { - rtNew = bigInt.zero; + rtNew = F.zero; res.oldKey = key; - res.oldValue = bigInt(0); + res.oldValue = F.zero; res.isOld0 = true; } @@ -126,7 +114,7 @@ class SMT { for (let level = resFind.siblings.length-1; level >=0; level--) { let newSibling = resFind.siblings[level]; if ((level == resFind.siblings.length-1)&&(!res.isOld0)) { - newSibling = bigInt.zero; + newSibling = F.zero; } const oldSibling = resFind.siblings[level]; if (keyBits[level]) { @@ -135,7 +123,7 @@ class SMT { rtOld = hash0(rtOld, oldSibling); } dels.push(rtOld); - if (!newSibling.isZero()) { + if (!F.isZero(newSibling)) { mixed = true; } @@ -164,8 +152,8 @@ class SMT { } async insert(_key, _value) { - const key = bigInt(_key); - const value = bigInt(_value); + const key = Scalar.e(_key); + const value = F.e(_value); let addedOne = false; const res = {}; res.oldRoot = this.root; @@ -183,7 +171,7 @@ class SMT { if (!resFind.isOld0) { const oldKeyits = this._splitBits(resFind.notFoundKey); for (let i= res.siblings.length; oldKeyits[i] == newKeyBits[i]; i++) { - res.siblings.push(bigInt.zero); + res.siblings.push(F.zero); } rtOld = hash1(resFind.notFoundKey, resFind.notFoundValue); res.siblings.push(rtOld); @@ -191,7 +179,7 @@ class SMT { mixed = false; } else if (res.siblings.length >0) { mixed = true; - rtOld = bigInt.zero; + rtOld = F.zero; } const inserts = []; @@ -201,7 +189,7 @@ class SMT { inserts.push([rt,[1, key, value]] ); for (let i=res.siblings.length-1; i>=0; i--) { - if ((i0) && (res.siblings[res.siblings.length-1].isZero())) { + while ((res.siblings.length>0) && (F.isZero(res.siblings[res.siblings.length-1]))) { res.siblings.pop(); } res.oldKey = resFind.notFoundKey; @@ -253,12 +241,12 @@ class SMT { if (typeof root === "undefined") root = this.root; let res; - if (root.isZero()) { + if (F.isZero(root)) { res = { found: false, siblings: [], notFoundKey: key, - notFoundValue: bigInt.zero, + notFoundValue: F.zero, isOld0: true }; return res; @@ -266,8 +254,8 @@ class SMT { const record = await this.db.get(root); - if ((record.length==3)&&(record[0].equals(bigInt.one))) { - if (record[1].equals(key)) { + if ((record.length==3)&&(F.eq(record[0],F.one))) { + if (F.eq(record[1],key)) { res = { found: true, siblings: [], diff --git a/src/smt_hashes_mimc.js b/src/smt_hashes_mimc.js index 0798338..1cc2921 100644 --- a/src/smt_hashes_mimc.js +++ b/src/smt_hashes_mimc.js @@ -8,3 +8,5 @@ exports.hash0 = function (left, right) { exports.hash1 = function(key, value) { return mimc7.multiHash([key, value], bigInt.one); }; + +exports.F = mimc7.F; diff --git a/src/smt_hashes_poseidon.js b/src/smt_hashes_poseidon.js index f4da325..24d2158 100644 --- a/src/smt_hashes_poseidon.js +++ b/src/smt_hashes_poseidon.js @@ -1,5 +1,4 @@ const Poseidon = require("./poseidon"); -const bigInt = require("big-integer"); const hash = Poseidon.createHash(6, 8, 57); @@ -8,5 +7,7 @@ exports.hash0 = function (left, right) { }; exports.hash1 = function(key, value) { - return hash([key, value, bigInt.one]); + return hash([key, value, Poseidon.F.one]); }; + +exports.F = Poseidon.F; diff --git a/src/smt_memdb.js b/src/smt_memdb.js index e4d9ce5..d752fa3 100644 --- a/src/smt_memdb.js +++ b/src/smt_memdb.js @@ -1,9 +1,11 @@ -const bigInt = require("big-integer"); + +const F = require("./poseidon.js").F; +const Scalar = require("ffjavascript").Scalar; class SMTMemDb { constructor() { this.nodes = {}; - this.root = bigInt(0); + this.root = F.zero; } async getRoot() { @@ -12,13 +14,13 @@ class SMTMemDb { _key2str(k) { // const keyS = bigInt(key).leInt2Buff(32).toString("hex"); - const keyS = bigInt(k).toString(); + const keyS = Scalar.e(k); return keyS; } _normalize(n) { for (let i=0; i=0)) { - let c = Number(r.and(bigInt(255))); - buff[o] = c; - o--; - r = r.shiftRight(8); - } - if (r.gt(bigInt.zero)) throw new Error("Number does not feed in buffer"); - return buff; -} - - -function stringifyBigInts(o) { - if ((typeof(o) == "bigint") || o.isZero !== undefined) { - return o.toString(10); - } else if (Array.isArray(o)) { - return o.map(stringifyBigInts); - } else if (typeof o == "object") { - const res = {}; - for (let k in o) { - res[k] = stringifyBigInts(o[k]); - } - return res; - } else { - return o; - } -} - -function unstringifyBigInts(o) { - if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) { - return bigInt(o); - } else if (Array.isArray(o)) { - return o.map(unstringifyBigInts); - } else if (typeof o == "object") { - const res = {}; - for (let k in o) { - res[k] = unstringifyBigInts(o[k]); - } - return res; - } else { - return o; - } -} diff --git a/test/aliascheck.js b/test/aliascheck.js index 2ec8700..5113d6d 100644 --- a/test/aliascheck.js +++ b/test/aliascheck.js @@ -3,7 +3,11 @@ const path = require("path"); const assert = chai.assert; -const bigInt = require("big-integer"); +const Scalar = require("ffjavascript").Scalar; +const F1Field = require("ffjavascript").F1Field; +const utils = require("ffjavascript").utils; +const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); +const F = new F1Field(q); const tester = require("circom").tester; @@ -14,16 +18,15 @@ function print(circuit, w, s) { function getBits(v, n) { const res = []; for (let i=0; i { - const inp = getBits(bigInt.zero, 254); + const inp = getBits(0, 254); await cir.calculateWitness({in: inp}, true); }); it("Satisfy the aliastest 3", async () => { - const inp = getBits(bigInt(3), 254); + const inp = getBits(3, 254); await cir.calculateWitness({in: inp}, true); }); it("Satisfy the aliastest q-1", async () => { - const inp = getBits(q.minus(bigInt.one), 254); + const inp = getBits(F.minusone, 254); + // console.log(JSON.stringify(utils.stringifyBigInts(inp))); await cir.calculateWitness({in: inp}, true); }); @@ -61,7 +65,7 @@ describe("Aliascheck test", function () { it("Should not satisfy all ones", async () => { - const inp = getBits(bigInt(1).shiftLeft(254).minus(bigInt.one), 254); + const inp = getBits(Scalar.sub(Scalar.shl(1, 254) , 1) , 254); try { await cir.calculateWitness({in: inp}, true); assert(false); diff --git a/test/babyjub.js b/test/babyjub.js index 4a89cc8..ddc2a54 100644 --- a/test/babyjub.js +++ b/test/babyjub.js @@ -1,14 +1,15 @@ const chai = require("chai"); const path = require("path"); -const createBlakeHash = require("blake-hash"); +const blake2b = require("blake2b"); const eddsa = require("../src/eddsa.js"); +const F = require("../src/babyjub.js").F; const assert = chai.assert; -const bigInt = require("big-integer"); const tester = require("circom").tester; -const utils = require("../src/utils.js"); +const utils = require("ffjavascript").utils; +const Scalar = require("ffjavascript").Scalar; describe("Baby Jub test", function () { let circuitAdd; @@ -28,31 +29,31 @@ describe("Baby Jub test", function () { it("Should add point (0,1) and (0,1)", async () => { const input={ - x1: bigInt(0), - y1: bigInt(1), - x2: bigInt(0), - y2: bigInt(1) + x1: F.e(0), + y1: F.e(1), + x2: F.e(0), + y2: F.e(1) }; const w = await circuitAdd.calculateWitness(input, true); - await circuitAdd.assertOut(w, {xout: bigInt(0), yout: bigInt(1)}); + await circuitAdd.assertOut(w, {xout: F.e(0), yout: F.e(1)}); }); it("Should add 2 same numbers", async () => { const input={ - x1: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - y1: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), - x2: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - y2: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475") + x1: F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + y1: F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + x2: F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + y2: F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475") }; const w = await circuitAdd.calculateWitness(input, true); await circuitAdd.assertOut(w, { - xout: bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"), - yout: bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889") + xout: F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"), + yout: F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889") }); }); @@ -60,17 +61,17 @@ describe("Baby Jub test", function () { it("Should add 2 different numbers", async () => { const input={ - x1: bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - y1: bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), - x2: bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"), - y2: bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311") + x1: F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + y1: F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + x2: F.e("16540640123574156134436876038791482806971768689494387082833631921987005038935"), + y2: F.e("20819045374670962167435360035096875258406992893633759881276124905556507972311") }; const w = await circuitAdd.calculateWitness(input, true); await circuitAdd.assertOut(w, { - xout: bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937"), - yout: bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499") + xout: F.e("7916061937171219682591368294088513039687205273691143098332585753343424131937"), + yout: F.e("14035240266687799601661095864649209771790948434046947201833777492504781204499") }); }); @@ -93,8 +94,8 @@ describe("Baby Jub test", function () { it("Should extract the public key from the private one", async () => { const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex"); - const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0,32)); - const S = utils.leBuff2int(pvk).shiftRight(3); + const pvk = eddsa.pruneBuffer(Buffer.from(blake2b(64).update(rawpvk).digest().slice(0,32))); + const S = Scalar.shr(utils.leBuff2int(pvk), 3); const A = eddsa.prv2pub(rawpvk); diff --git a/test/babyjub_js.js b/test/babyjub_js.js index b65d71c..cb27f12 100644 --- a/test/babyjub_js.js +++ b/test/babyjub_js.js @@ -1,6 +1,6 @@ const chai = require("chai"); -const bigInt = require("big-integer"); const babyjub = require("../src/babyjub.js"); +const Scalar = require("ffjavascript").Scalar; const assert = chai.assert; @@ -14,16 +14,16 @@ describe("Baby Jub js test", function () { it("Should add point (0,1) and (0,1)", () => { const p1 = [ - bigInt(0), - bigInt(1)]; + babyjub.F.e(0), + babyjub.F.e(1)]; const p2 = [ - bigInt(0), - bigInt(1) + babyjub.F.e(0), + babyjub.F.e(1) ]; const out = babyjub.addPoint(p1, p2); - assert(out[0].equals(0)); - assert(out[1].equals(1)); + assert(babyjub.F.eq(out[0], babyjub.F.zero)); + assert(babyjub.F.eq(out[1], babyjub.F.one)); }); it("Should base be 8*generator", () => { @@ -32,50 +32,50 @@ describe("Baby Jub js test", function () { res = babyjub.addPoint(res, res); res = babyjub.addPoint(res, res); - assert(res[0].equals(babyjub.Base8[0])); - assert(res[1].equals(babyjub.Base8[1])); + assert(babyjub.F.eq(res[0], babyjub.Base8[0])); + assert(babyjub.F.eq(res[1], babyjub.Base8[1])); }); it("Should add 2 same numbers", () => { const p1 = [ - bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), ]; const p2 = [ - bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), ]; const out = babyjub.addPoint(p1, p2); - assert(out[0].equals(bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"))); - assert(out[1].equals(bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"))); + assert(babyjub.F.eq(out[0], babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"))); + assert(babyjub.F.eq(out[1], babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"))); }); it("Should add 2 different numbers", () => { const p1 = [ - bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), ]; const p2 = [ - bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"), - bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"), + babyjub.F.e("16540640123574156134436876038791482806971768689494387082833631921987005038935"), + babyjub.F.e("20819045374670962167435360035096875258406992893633759881276124905556507972311"), ]; const out = babyjub.addPoint(p1, p2); + assert(babyjub.F.eq(out[0], babyjub.F.e("7916061937171219682591368294088513039687205273691143098332585753343424131937"))); + assert(babyjub.F.eq(out[1], babyjub.F.e("14035240266687799601661095864649209771790948434046947201833777492504781204499"))); - assert(out[0].equals(bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937"))); - assert(out[1].equals(bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499"))); }); it("should mulPointEscalar 0", () => { const p = [ - bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), ]; - const r = babyjub.mulPointEscalar(p, bigInt("3")); + const r = babyjub.mulPointEscalar(p, 3); let r2 = babyjub.addPoint(p, p); r2 = babyjub.addPoint(r2, p); assert.equal(r2[0].toString(), r[0].toString()); @@ -86,62 +86,62 @@ describe("Baby Jub js test", function () { it("should mulPointEscalar 1", () => { const p = [ - bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), ]; - const r = babyjub.mulPointEscalar(p, bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")); + const r = babyjub.mulPointEscalar(p, Scalar.fromString("14035240266687799601661095864649209771790948434046947201833777492504781204499")); assert.equal(r[0].toString(), "17070357974431721403481313912716834497662307308519659060910483826664480189605"); assert.equal(r[1].toString(), "4014745322800118607127020275658861516666525056516280575712425373174125159339"); }); it("should mulPointEscalar 2", () => { const p = [ - bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"), - bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"), + babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"), + babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"), ]; - const r = babyjub.mulPointEscalar(p, bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")); + const r = babyjub.mulPointEscalar(p, Scalar.fromString("20819045374670962167435360035096875258406992893633759881276124905556507972311")); assert.equal(r[0].toString(), "13563888653650925984868671744672725781658357821216877865297235725727006259983"); assert.equal(r[1].toString(), "8442587202676550862664528699803615547505326611544120184665036919364004251662"); }); it("should inCurve 1", () => { const p = [ - bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), ]; assert(babyjub.inCurve(p)); }); it("should inCurve 2", () => { const p = [ - bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"), - bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"), + babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"), + babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"), ]; assert(babyjub.inCurve(p)); }); it("should inSubgroup 1", () => { const p = [ - bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), ]; assert(babyjub.inSubgroup(p)); }); it("should inSubgroup 2", () => { const p = [ - bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"), - bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"), + babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"), + babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"), ]; assert(babyjub.inSubgroup(p)); }); it("should packPoint - unpackPoint 1", () => { const p = [ - bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"), - bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"), + babyjub.F.e("17777552123799933955779906779655732241715742912184938656739573121738514868268"), + babyjub.F.e("2626589144620713026669568689430873010625803728049924121243784502389097019475"), ]; const buf = babyjub.packPoint(p); assert.equal(buf.toString("hex"), "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85"); @@ -152,8 +152,8 @@ describe("Baby Jub js test", function () { it("should packPoint - unpackPoint 2", () => { const p = [ - bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365"), - bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"), + babyjub.F.e("6890855772600357754907169075114257697580319025794532037257385534741338397365"), + babyjub.F.e("4338620300185947561074059802482547481416142213883829469920100239455078257889"), ]; const buf = babyjub.packPoint(p); assert.equal(buf.toString("hex"), "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709"); diff --git a/test/binsub.js b/test/binsub.js index 972f1db..e5b5eba 100644 --- a/test/binsub.js +++ b/test/binsub.js @@ -1,6 +1,7 @@ const path = require("path"); -const bigInt = require("big-integer"); +const Fr = require("ffjavascript").bn128.Fr; +const Scalar = require("ffjavascript").Scalar; const tester = require("circom").tester; function print(circuit, w, s) { @@ -8,15 +9,16 @@ function print(circuit, w, s) { } async function checkSub(_a,_b, circuit) { - let a=bigInt(_a); - let b=bigInt(_b); - if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shiftLeft(16)); - if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shiftLeft(16)); + let a=Scalar.e(_a); + let b=Scalar.e(_b); + if (Scalar.lt(a, 0)) a = Scalar.add(a, Scalar.shl(1, 16)); + if (Scalar.lt(b, 0)) b = Scalar.add(b, Scalar.shl(1, 16)); const w = await circuit.calculateWitness({a: a, b: b}, true); - let res = a.minus(b); - if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shiftLeft(16)); - await circuit.assertOut(w, {out: bigInt(res)}); + let res = Scalar.sub(a, b); + if (Scalar.lt(res, 0)) res = Scalar.add(res, Scalar.shl(1, 16)); + + await circuit.assertOut(w, {out: res}); } describe("BinSub test", function () { diff --git a/test/binsum.js b/test/binsum.js index 54d7500..a3b5efc 100644 --- a/test/binsum.js +++ b/test/binsum.js @@ -3,7 +3,7 @@ const path = require("path"); const tester = require("circom").tester; -const bigInt = require("big-integer"); +const Fr = require("ffjavascript").bn128.Fr; const assert = chai.assert; @@ -18,10 +18,10 @@ describe("Binary sum test", function () { assert.equal(circuit.nVars, 2); assert.equal(circuit.constraints.length, 1); - const witness = await circuit.calculateWitness({ "in": bigInt("d807aa98", 16)}, true); + const witness = await circuit.calculateWitness({ "in": Fr.e("d807aa98", 16)}, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt("d807aa98", 16))); + assert(Fr.eq(witness[0],Fr.e(1))); + assert(Fr.eq(witness[1],Fr.e("d807aa98", 16))); }); it("Should create a sum circuit", async () => { const circuit = await tester(path.join(__dirname, "circuits", "sum_test.circom")); @@ -31,7 +31,7 @@ describe("Binary sum test", function () { const witness = await circuit.calculateWitness({ "a": "111", "b": "222" }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt("333"))); + assert(Fr.eq(witness[0],Fr.e(1))); + assert(Fr.eq(witness[1],Fr.e("333"))); }); }); diff --git a/test/circuits/in.json b/test/circuits/in.json deleted file mode 100644 index 2ebe0d5..0000000 --- a/test/circuits/in.json +++ /dev/null @@ -1,258 +0,0 @@ -{ - "in": [ - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1", - "1" - ] -} \ No newline at end of file diff --git a/test/comparators.js b/test/comparators.js index ea263e0..37590af 100644 --- a/test/comparators.js +++ b/test/comparators.js @@ -3,7 +3,7 @@ const path = require("path"); const tester = require("circom").tester; -const bigInt = require("big-integer"); +const Fr = require("ffjavascript").bn128.Fr; const assert = chai.assert; @@ -16,60 +16,61 @@ describe("Comparators test", function () { let witness; witness = await circuit.calculateWitness({ "in": 111}, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": 0 }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); }); it("Should create a isequal circuit", async() => { const circuit = await tester(path.join(__dirname, "circuits", "isequal.circom")); let witness; witness = await circuit.calculateWitness({ "in": [111,222] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); + witness = await circuit.calculateWitness({ "in": [444,444] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); }); it("Should create a comparison lessthan", async() => { const circuit = await tester(path.join(__dirname, "circuits", "lessthan.circom")); let witness; witness = await circuit.calculateWitness({ "in": [333,444] }), true; - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in":[1,1] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [661, 660] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [0, 1] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [0, 444] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [1, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [555, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [0, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); }); it("Should create a comparison lesseqthan", async() => { @@ -77,36 +78,36 @@ describe("Comparators test", function () { let witness; witness = await circuit.calculateWitness({ "in": [333,444] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in":[1,1] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [661, 660] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [0, 1] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [0, 444] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [1, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [555, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [0, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); }); it("Should create a comparison greaterthan", async() => { @@ -114,71 +115,71 @@ describe("Comparators test", function () { let witness; witness = await circuit.calculateWitness({ "in": [333,444] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in":[1,1] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [661, 660] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [0, 1] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [0, 444] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [1, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [555, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [0, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); }); it("Should create a comparison greatereqthan", async() => { const circuit = await tester(path.join(__dirname, "circuits", "greatereqthan.circom")); let witness; witness = await circuit.calculateWitness({ "in": [333,444] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in":[1,1] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [661, 660] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [0, 1] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [0, 444] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(0))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(0))); witness = await circuit.calculateWitness({ "in": [1, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [555, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); witness = await circuit.calculateWitness({ "in": [0, 0] }, true); - assert(witness[0].equals(bigInt(1))); - assert(witness[1].equals(bigInt(1))); + assert(Fr.eq(witness[0], Fr.e(1))); + assert(Fr.eq(witness[1], Fr.e(1))); }); }); diff --git a/test/eddsa.js b/test/eddsa.js index 7d2e02f..384f575 100644 --- a/test/eddsa.js +++ b/test/eddsa.js @@ -2,7 +2,7 @@ const chai = require("chai"); const path = require("path"); const tester = require("circom").tester; -const bigInt = require("big-integer"); +const Fr = require("ffjavascript").bn128.Fr; const eddsa = require("../src/eddsa.js"); const babyJub = require("../src/babyjub.js"); @@ -18,9 +18,9 @@ function buffer2bits(buff) { for (let i=0; i>j)&1) { - res.push(bigInt.one); + res.push(Fr.one); } else { - res.push(bigInt.zero); + res.push(Fr.zero); } } } diff --git a/test/eddsa_js.js b/test/eddsa_js.js index 1199626..65798e5 100644 --- a/test/eddsa_js.js +++ b/test/eddsa_js.js @@ -5,8 +5,7 @@ const babyJub = require("../src/babyjub.js"); const assert = chai.assert; -const bigInt = require("big-integer"); -const utils = require("../src/utils.js"); +const utils = require("ffjavascript").utils; describe("EdDSA js test", function () { @@ -23,24 +22,24 @@ describe("EdDSA js test", function () { const pubKey = eddsa.prv2pub(prvKey); assert.equal(pubKey[0].toString(), - "13277427435165878497778222415993513565335242147425444199013288855685581939618"); + "17579234973106307986399040784563986669343100608865726413246909559198451825625"); assert.equal(pubKey[1].toString(), - "13622229784656158136036771217484571176836296686641868549125388198837476602820"); + "21581828029826859845363968476425861244058376747493285816141526544272562145486"); const pPubKey = babyJub.packPoint(pubKey); const signature = eddsa.signMiMC(prvKey, msg); assert.equal(signature.R8[0].toString(), - "11384336176656855268977457483345535180380036354188103142384839473266348197733"); + "12672422877531089818651367820728973438446851190471722610781936061829103362897"); assert.equal(signature.R8[1].toString(), - "15383486972088797283337779941324724402501462225528836549661220478783371668959"); + "12052234579439634484237590306927118446073354173341433290934144373261241958718"); assert.equal(signature.S.toString(), - "2523202440825208709475937830811065542425109372212752003460238913256192595070"); + "1582013862333331285840015273849085014739146294568319205499642618291614907374"); const pSignature = eddsa.packSignature(signature); assert.equal(pSignature.toString("hex"), ""+ - "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+ - "7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405"); + "3e417cd811f9c9c545a680b962e45d22ccb62b2284b4fe4bbc9fdb50b252a59a" + + "eefbebe2b895393fa0e9b5b31b19e65a63fee5d7b6261d8d5b6b847c5b637f03"); const uSignature = eddsa.unpackSignature(pSignature); assert(eddsa.verifyMiMC(msg, uSignature, pubKey)); @@ -56,24 +55,24 @@ describe("EdDSA js test", function () { const pubKey = eddsa.prv2pub(prvKey); assert.equal(pubKey[0].toString(), - "13277427435165878497778222415993513565335242147425444199013288855685581939618"); + "17579234973106307986399040784563986669343100608865726413246909559198451825625"); assert.equal(pubKey[1].toString(), - "13622229784656158136036771217484571176836296686641868549125388198837476602820"); + "21581828029826859845363968476425861244058376747493285816141526544272562145486"); const pPubKey = babyJub.packPoint(pubKey); const signature = eddsa.signPoseidon(prvKey, msg); assert.equal(signature.R8[0].toString(), - "11384336176656855268977457483345535180380036354188103142384839473266348197733"); + "12672422877531089818651367820728973438446851190471722610781936061829103362897"); assert.equal(signature.R8[1].toString(), - "15383486972088797283337779941324724402501462225528836549661220478783371668959"); + "12052234579439634484237590306927118446073354173341433290934144373261241958718"); assert.equal(signature.S.toString(), - "248298168863866362217836334079793350221620631973732197668910946177382043688"); + "2318334603430781860679872910160434499077270843466490702990199622594868564504"); const pSignature = eddsa.packSignature(signature); assert.equal(pSignature.toString("hex"), ""+ - "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+ - "28506bce274aa1b3f7e7c2fd7e4fe09bff8f9aa37a42def7994e98f322888c00"); + "3e417cd811f9c9c545a680b962e45d22ccb62b2284b4fe4bbc9fdb50b252a59a" + + "1852c049fc6286138a0ddb57718049a09374fdf0390686c7ac5637b481212005"); const uSignature = eddsa.unpackSignature(pSignature); assert(eddsa.verifyPoseidon(msg, uSignature, pubKey)); diff --git a/test/eddsamimc.js b/test/eddsamimc.js index 6e14fd6..730de13 100644 --- a/test/eddsamimc.js +++ b/test/eddsamimc.js @@ -1,7 +1,8 @@ const chai = require("chai"); const path = require("path"); const tester = require("circom").tester; -const bigInt = require("big-integer"); + +const Fr = require("ffjavascript").bn128.Fr; const eddsa = require("../src/eddsa.js"); @@ -18,7 +19,7 @@ describe("EdDSA MiMC test", function () { }); it("Sign a single number", async () => { - const msg = bigInt(1234); + const msg = Fr.e(1234); const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex"); @@ -43,7 +44,7 @@ describe("EdDSA MiMC test", function () { }); it("Detect Invalid signature", async () => { - const msg = bigInt(1234); + const msg = Fr.e(1234); const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex"); @@ -58,7 +59,7 @@ describe("EdDSA MiMC test", function () { enabled: 1, Ax: pubKey[0], Ay: pubKey[1], - R8x: signature.R8[0].add(bigInt(1)), + R8x: Fr.add(signature.R8[0], Fr.e(1)), R8y: signature.R8[1], S: signature.S, M: msg}, true); @@ -70,7 +71,7 @@ describe("EdDSA MiMC test", function () { it("Test a dissabled circuit with a bad signature", async () => { - const msg = bigInt(1234); + const msg = Fr.e(1234); const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex"); @@ -85,7 +86,7 @@ describe("EdDSA MiMC test", function () { enabled: 0, Ax: pubKey[0], Ay: pubKey[1], - R8x: signature.R8[0].add(bigInt(1)), + R8x: Fr.add(signature.R8[0], Fr.e(1)), R8y: signature.R8[1], S: signature.S, M: msg}, true); diff --git a/test/eddsaposeidon.js b/test/eddsaposeidon.js index 31fad9c..a682f98 100644 --- a/test/eddsaposeidon.js +++ b/test/eddsaposeidon.js @@ -1,7 +1,7 @@ const chai = require("chai"); const path = require("path"); -const bigInt = require("big-integer"); const tester = require("circom").tester; +const Fr = require("ffjavascript").bn128.Fr; const eddsa = require("../src/eddsa.js"); @@ -19,7 +19,7 @@ describe("EdDSA Poseidon test", function () { }); it("Sign a single number", async () => { - const msg = bigInt(1234); + const msg = Fr.e(1234); const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex"); @@ -47,7 +47,7 @@ describe("EdDSA Poseidon test", function () { }); it("Detect Invalid signature", async () => { - const msg = bigInt(1234); + const msg = Fr.e(1234); const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex"); @@ -62,7 +62,7 @@ describe("EdDSA Poseidon test", function () { enabled: 1, Ax: pubKey[0], Ay: pubKey[1], - R8x: signature.R8[0].add(bigInt(1)), + R8x: Fr.add(signature.R8[0], Fr.e(1)), R8y: signature.R8[1], S: signature.S, M: msg}, true); @@ -74,7 +74,7 @@ describe("EdDSA Poseidon test", function () { it("Test a dissabled circuit with a bad signature", async () => { - const msg = bigInt(1234); + const msg = Fr.e(1234); const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex"); @@ -89,7 +89,7 @@ describe("EdDSA Poseidon test", function () { enabled: 0, Ax: pubKey[0], Ay: pubKey[1], - R8x: signature.R8[0].add(bigInt(1)), + R8x: Fr.add(signature.R8[0], Fr.e(1)), R8y: signature.R8[1], S: signature.S, M: msg}, true); diff --git a/test/escalarmul.js b/test/escalarmul.js index ec60597..5463e24 100644 --- a/test/escalarmul.js +++ b/test/escalarmul.js @@ -1,8 +1,9 @@ const chai = require("chai"); const path = require("path"); -const bigInt = require("big-integer"); const tester = require("circom").tester; const babyJub = require("../src/babyjub.js"); +const Fr = require("ffjavascript").bn128.Fr; + const assert = chai.assert; @@ -23,11 +24,11 @@ describe("Exponentioation test", function () { await circuit.checkConstraints(w); let g = [ - bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), - bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") + Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), + Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") ]; - let dbl= [bigInt("0"), bigInt("1")]; + let dbl= [Fr.e("0"), Fr.e("1")]; const expectedOut = []; @@ -50,15 +51,15 @@ describe("Exponentioation test", function () { await circuit.checkConstraints(w); let g = [ - bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), - bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") + Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), + Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") ]; for (let i=0; i<12;i++) { g = babyJub.addPoint(g,g); } - let dbl= [bigInt("0"), bigInt("1")]; + let dbl= [Fr.e("0"), Fr.e("1")]; const expectedOut = []; @@ -81,11 +82,11 @@ describe("Exponentioation test", function () { await circuit.checkConstraints(w); let g = [ - bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), - bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") + Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), + Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") ]; - let c = [bigInt(0), bigInt(1)]; + let c = [Fr.e(0), Fr.e(1)]; for (let i=0; i<31;i++) { c = babyJub.addPoint(c,g); @@ -93,7 +94,7 @@ describe("Exponentioation test", function () { await circuit.assertOut(w, {out: c}); - const w2 = await circuit.calculateWitness({"in": bigInt(1).shiftLeft(252).add(bigInt.one)}); + const w2 = await circuit.calculateWitness({"in": Fr.add(Fr.shl(Fr.e(1), Fr.e(252)),Fr.one)}); c = [g[0], g[1]]; for (let i=0; i<252;i++) { diff --git a/test/escalarmulany.js b/test/escalarmulany.js index 3a831d0..9ebaa93 100644 --- a/test/escalarmulany.js +++ b/test/escalarmulany.js @@ -1,7 +1,8 @@ const chai = require("chai"); const path = require("path"); -const bigInt = require("big-integer"); const tester = require("circom").tester; +const Fr = require("ffjavascript").bn128.Fr; + function print(circuit, w, s) { console.log(s + ": " + w[circuit.getSignalIdx(s)]); @@ -13,8 +14,8 @@ describe("Escalarmul test", function () { this.timeout(100000); let g = [ - bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), - bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") + Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), + Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") ]; before( async() => { @@ -33,7 +34,7 @@ describe("Escalarmul test", function () { it("If multiply by order should return 0", async () => { - const r = bigInt("2736030358979909402780800718157159386076813972158567259200215660948447373041"); + const r = Fr.e("2736030358979909402780800718157159386076813972158567259200215660948447373041"); const w = await circuitEMulAny.calculateWitness({"e": r, "p": g}); await circuitEMulAny.checkConstraints(w); diff --git a/test/escalarmulfix.js b/test/escalarmulfix.js index 2486695..772712a 100644 --- a/test/escalarmulfix.js +++ b/test/escalarmulfix.js @@ -1,8 +1,8 @@ const chai = require("chai"); const path = require("path"); -const bigInt = require("big-integer"); const tester = require("circom").tester; const babyjub = require("../src/babyjub"); +const Fr = require("ffjavascript").bn128.Fr; const assert = chai.assert; @@ -41,10 +41,10 @@ describe("Escalarmul test", function () { it("Should generate scalar mul of a specific constant", async () => { - const s = bigInt("2351960337287830298912035165133676222414898052661454064215017316447594616519"); + const s = Fr.e("2351960337287830298912035165133676222414898052661454064215017316447594616519"); const base8 = [ - bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), - bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") + Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), + Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") ]; const w = await circuit.calculateWitness({"e": s}, true); @@ -60,12 +60,12 @@ describe("Escalarmul test", function () { it("Should generate scalar mul of the firsts 50 elements", async () => { const base8 = [ - bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), - bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") + Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), + Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") ]; for (let i=0; i<50; i++) { - const s = bigInt(i); + const s = Fr.e(i); const w = await circuit.calculateWitness({"e": s}, true); diff --git a/test/montgomery.js b/test/montgomery.js index d53fa5a..a1602c8 100644 --- a/test/montgomery.js +++ b/test/montgomery.js @@ -1,8 +1,8 @@ const chai = require("chai"); const path = require("path"); -const bigInt = require("big-integer"); const tester = require("circom").tester; const babyJub = require("../src/babyjub.js"); +const Fr = require("ffjavascript").bn128.Fr; const assert = chai.assert; @@ -13,8 +13,8 @@ describe("Montgomery test", function () { let circuitMDouble; let g = [ - bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), - bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") + Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"), + Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203") ]; let mg, mg2, g2, g3, mg3; @@ -45,8 +45,8 @@ describe("Montgomery test", function () { xout = w[circuitM2E.symbols["main.out[0]"].varIdx]; yout = w[circuitM2E.symbols["main.out[1]"].varIdx]; - assert(xout.equals(g[0])); - assert(yout.equals(g[1])); + assert(Fr.eq(xout, g[0])); + assert(Fr.eq(yout, g[1])); }); it("Should double a point", async () => { let w, xout, yout; @@ -65,8 +65,9 @@ describe("Montgomery test", function () { xout = w[circuitM2E.symbols["main.out[0]"].varIdx]; yout = w[circuitM2E.symbols["main.out[1]"].varIdx]; - assert(xout.equals(g2[0])); - assert(yout.equals(g2[1])); + + assert(Fr.eq(xout, g2[0])); + assert(Fr.eq(yout, g2[1])); }); it("Should add a point", async () => { let w, xout, yout; @@ -85,7 +86,7 @@ describe("Montgomery test", function () { xout = w[circuitM2E.symbols["main.out[0]"].varIdx]; yout = w[circuitM2E.symbols["main.out[1]"].varIdx]; - assert(xout.equals(g3[0])); - assert(yout.equals(g3[1])); + assert(Fr.eq(xout, g3[0])); + assert(Fr.eq(yout, g3[1])); }); }); diff --git a/test/multiplexer.js b/test/multiplexer.js index 01a8338..40d0cb3 100644 --- a/test/multiplexer.js +++ b/test/multiplexer.js @@ -1,6 +1,6 @@ const path = require("path"); -const bigInt = require("big-integer"); const tester = require("circom").tester; +const Fr = require("ffjavascript").bn128.Fr; describe("Mux4 test", function() { this.timeout(100000); @@ -9,22 +9,22 @@ describe("Mux4 test", function() { const circuit = await tester(path.join(__dirname, "circuits", "mux4_1.circom")); const ct16 = [ - bigInt("123"), - bigInt("456"), - bigInt("789"), - bigInt("012"), - bigInt("111"), - bigInt("222"), - bigInt("333"), - bigInt("4546"), - bigInt("134523"), - bigInt("44356"), - bigInt("15623"), - bigInt("4566"), - bigInt("1223"), - bigInt("4546"), - bigInt("4256"), - bigInt("4456") + Fr.e("123"), + Fr.e("456"), + Fr.e("789"), + Fr.e("012"), + Fr.e("111"), + Fr.e("222"), + Fr.e("333"), + Fr.e("4546"), + Fr.e("134523"), + Fr.e("44356"), + Fr.e("15623"), + Fr.e("4566"), + Fr.e("1223"), + Fr.e("4546"), + Fr.e("4256"), + Fr.e("4456") ]; for (let i=0; i<16; i++) { @@ -41,14 +41,14 @@ describe("Mux4 test", function() { const circuit = await tester(path.join(__dirname, "circuits", "mux3_1.circom")); const ct8 = [ - bigInt("37"), - bigInt("47"), - bigInt("53"), - bigInt("71"), - bigInt("89"), - bigInt("107"), - bigInt("163"), - bigInt("191") + Fr.e("37"), + Fr.e("47"), + Fr.e("53"), + Fr.e("71"), + Fr.e("89"), + Fr.e("107"), + Fr.e("163"), + Fr.e("191") ]; for (let i=0; i<8; i++) { @@ -64,10 +64,10 @@ describe("Mux4 test", function() { const circuit = await tester(path.join(__dirname, "circuits", "mux2_1.circom")); const ct4 = [ - bigInt("37"), - bigInt("47"), - bigInt("53"), - bigInt("71"), + Fr.e("37"), + Fr.e("47"), + Fr.e("53"), + Fr.e("71"), ]; for (let i=0; i<4; i++) { @@ -83,8 +83,8 @@ describe("Mux4 test", function() { const circuit = await tester(path.join(__dirname, "circuits", "mux1_1.circom")); const ct2 = [ - bigInt("37"), - bigInt("47"), + Fr.e("37"), + Fr.e("47"), ]; for (let i=0; i<2; i++) { diff --git a/test/pedersen.js b/test/pedersen.js index 5de9276..f580f5c 100644 --- a/test/pedersen.js +++ b/test/pedersen.js @@ -1,18 +1,18 @@ const chai = require("chai"); const path = require("path"); -const bigInt = require("big-integer"); +const Fr = require("ffjavascript").bn128.Fr; const tester = require("circom").tester; const babyJub = require("../src/babyjub.js"); const PBASE = [ - [bigInt("10457101036533406547632367118273992217979173478358440826365724437999023779287"),bigInt("19824078218392094440610104313265183977899662750282163392862422243483260492317")], - [bigInt("2671756056509184035029146175565761955751135805354291559563293617232983272177"),bigInt("2663205510731142763556352975002641716101654201788071096152948830924149045094")], - [bigInt("5802099305472655231388284418920769829666717045250560929368476121199858275951"),bigInt("5980429700218124965372158798884772646841287887664001482443826541541529227896")], - [bigInt("7107336197374528537877327281242680114152313102022415488494307685842428166594"),bigInt("2857869773864086953506483169737724679646433914307247183624878062391496185654")], - [bigInt("20265828622013100949498132415626198973119240347465898028410217039057588424236"),bigInt("1160461593266035632937973507065134938065359936056410650153315956301179689506")] + [Fr.e("7688621503272331394947188562469131124099290577812125474996268020905176040083"),Fr.e("6637287939860384587467947982369268811366630904563077767287326262235485629411")], + [Fr.e("11549681895645637778324638856880330712650895608496649854094912415387988201330"),Fr.e("5771732722784528537721081267383956005090479808901717812009343940574217488577")], + [Fr.e("18790245153471844934157747708238883966079935875787657036767664036124524381945"),Fr.e("18300275459419441151064576487317481499516933849631632883767173501999997278432")], + [Fr.e("16301069151422548986850494139112207641738464387919729729324473657161689764196"),Fr.e("8215273507373494014441104012907835625670941526105528197815397741007626226499")], + [Fr.e("12597665704678284488008395353749282149622295037737374782196049599390683534185"),Fr.e("4072455241781501621593714139281767473040087753548015968773801065193764079468")] ]; describe("Double Pedersen test", function() { @@ -63,7 +63,7 @@ describe("Double Pedersen test", function() { it("Should pedersen all ones", async () => { let w; - const allOnes = bigInt("1").shiftLeft(250).minus(bigInt("1")); + const allOnes = Fr.sub(Fr.shl(Fr.e("1"), Fr.e(250)), Fr.e("1")); w = await circuit.calculateWitness({ in: [allOnes, allOnes]}, true); diff --git a/test/pedersen2.js b/test/pedersen2.js index 9a9712d..06417da 100644 --- a/test/pedersen2.js +++ b/test/pedersen2.js @@ -1,6 +1,6 @@ const path = require("path"); -const bigInt = require("big-integer"); +const Fr = require("ffjavascript").bn128.Fr; const tester = require("circom").tester; const babyJub = require("../src/babyjub.js"); @@ -32,7 +32,7 @@ describe("Pedersen test", function() { let w; - const n = bigInt.one.shiftLeft(253).minus(bigInt.one); + const n = Fr.sub(Fr.shl(Fr.one, Fr.e(253)), Fr.one); w = await circuit.calculateWitness({ in: n}, true); diff --git a/test/poseidoncircuit.js b/test/poseidoncircuit.js index d5e2a9f..1d8f409 100644 --- a/test/poseidoncircuit.js +++ b/test/poseidoncircuit.js @@ -1,7 +1,6 @@ const chai = require("chai"); const path = require("path"); var blake2b = require("blake2b"); -const bigInt = require("big-integer"); const tester = require("circom").tester; const poseidon = require("../src/poseidon.js"); diff --git a/test/poseidoncontract.js b/test/poseidoncontract.js index caad1ca..6a6b2fa 100644 --- a/test/poseidoncontract.js +++ b/test/poseidoncontract.js @@ -3,7 +3,6 @@ const Web3 = require("web3"); const chai = require("chai"); const poseidonGenContract = require("../src/poseidon_gencontract.js"); const Poseidon = require("../src/poseidon.js"); -const bigInt = require("snarkjs").bigInt; const assert = chai.assert; const log = (msg) => { if (process.env.MOCHA_VERBOSE) console.log(msg); }; diff --git a/test/sha256.js b/test/sha256.js index e734490..cb51e74 100644 --- a/test/sha256.js +++ b/test/sha256.js @@ -1,7 +1,7 @@ const chai = require("chai"); const path = require("path"); -const snarkjs = require("snarkjs"); const crypto = require("crypto"); +const Fr = require("ffjavascript").bn128.Fr; const assert = chai.assert; @@ -67,7 +67,7 @@ describe("SHA256 test", function () { assert.equal(hash, hash2); - assert(witness[1].equals(snarkjs.bigInt(r))); + assert(Fr.eq(witness[1], Fr.e(r))); }).timeout(1000000); it("Should calculate a hash of 2 compressor", async () => { diff --git a/test/sign.js b/test/sign.js index b3e9452..45c4f8a 100644 --- a/test/sign.js +++ b/test/sign.js @@ -1,5 +1,6 @@ const path = require("path"); -const bigInt = require("big-integer"); +const Fr = require("ffjavascript").bn128.Fr; +const Scalar = require("ffjavascript").Scalar; const tester = require("circom").tester; function print(circuit, w, s) { @@ -9,16 +10,16 @@ function print(circuit, w, s) { function getBits(v, n) { const res = []; for (let i=0; i { - const inp = getBits(bigInt.zero, 254); + const inp = getBits(Scalar.e(0), 254); const w = await circuit.calculateWitness({in: inp}, true); await circuit.assertOut(w, {sign: 0}); }); it("Sign of 3", async () => { - const inp = getBits(bigInt(3), 254); + const inp = getBits(Scalar.e(3), 254); const w = await circuit.calculateWitness({in: inp}, true); await circuit.assertOut(w, {sign: 0}); }); it("Sign of q/2", async () => { - const inp = getBits(q.shiftRight(bigInt.one), 254); + const inp = getBits(Scalar.shr(q, 1), 254); const w = await circuit.calculateWitness({in: inp}, true); await circuit.assertOut(w, {sign: 0}); }); it("Sign of q/2+1", async () => { - const inp = getBits(q.shiftRight(bigInt.one).add(bigInt.one), 254); + const inp = getBits(Scalar.add(Scalar.shr(q, 1), 1) , 254); const w = await circuit.calculateWitness({in: inp}, true); await circuit.assertOut(w, {sign: 1}); }); it("Sign of q-1", async () => { - const inp = getBits(q.minus(bigInt.one), 254); + const inp = getBits(Scalar.sub(q, 1), 254); const w = await circuit.calculateWitness({in: inp}, true); await circuit.assertOut(w, {sign: 1}); @@ -71,7 +72,7 @@ describe("Sign test", function() { }); it("Sign of all ones", async () => { - const inp = getBits(bigInt(1).shiftLeft(254).minus(bigInt(1)), 254); + const inp = getBits(Scalar.sub(Scalar.shl(1,254),1), 254); const w = await circuit.calculateWitness({in: inp}, true); await circuit.assertOut(w, {sign: 1}); diff --git a/test/smtjs.js b/test/smtjs.js index 732a398..e8643e2 100644 --- a/test/smtjs.js +++ b/test/smtjs.js @@ -1,28 +1,11 @@ const chai = require("chai"); -const bigInt = require("big-integer"); +const Fr = require("ffjavascript").bn128.Fr; const smt = require("../src/smt.js"); const assert = chai.assert; - -function stringifyBigInts(o) { - if ((typeof(o) == "bigint") || (o instanceof bigInt)) { - return o.toString(10); - } else if (Array.isArray(o)) { - return o.map(stringifyBigInts); - } else if (typeof o == "object") { - const res = {}; - for (let k in o) { - res[k] = stringifyBigInts(o[k]); - } - return res; - } else { - return o; - } -} - describe("SMT Javascript test", function () { this.timeout(100000); before( async () => { @@ -30,22 +13,22 @@ describe("SMT Javascript test", function () { it("Should insert 2 elements and empty them", async () => { const tree = await smt.newMemEmptyTrie(); - const key1 = bigInt(111); - const value1 = bigInt(222); - const key2 = bigInt(333); - const value2 = bigInt(444); + const key1 = Fr.e(111); + const value1 = Fr.e(222); + const key2 = Fr.e(333); + const value2 = Fr.e(444); await tree.insert(key1,value1); await tree.insert(key2,value2); await tree.delete(key2); await tree.delete(key1); - assert(tree.root.isZero()); + assert(Fr.isZero(tree.root)); }); it("Should insert 3 elements in dferent order and should be the same", async () => { - const keys = [bigInt(8), bigInt(9), bigInt(32)]; - const values = [bigInt(88), bigInt(99), bigInt(3232)]; + const keys = [Fr.e(8), Fr.e(9), Fr.e(32)]; + const values = [Fr.e(88), Fr.e(99), Fr.e(3232)]; const tree1 = await smt.newMemEmptyTrie(); const tree2 = await smt.newMemEmptyTrie(); const tree3 = await smt.newMemEmptyTrie(); @@ -77,11 +60,11 @@ describe("SMT Javascript test", function () { await tree6.insert(keys[1],values[1]); await tree6.insert(keys[0],values[0]); - assert(tree1.root.equals(tree2.root)); - assert(tree2.root.equals(tree3.root)); - assert(tree3.root.equals(tree4.root)); - assert(tree4.root.equals(tree5.root)); - assert(tree5.root.equals(tree6.root)); + assert(Fr.eq(tree1.root, tree2.root)); + assert(Fr.eq(tree2.root, tree3.root)); + assert(Fr.eq(tree3.root, tree4.root)); + assert(Fr.eq(tree4.root, tree5.root)); + assert(Fr.eq(tree5.root, tree6.root)); assert.equal(Object.keys(tree1.db.nodes).length, Object.keys(tree2.db.nodes).length); assert.equal(Object.keys(tree2.db.nodes).length, Object.keys(tree3.db.nodes).length); @@ -93,19 +76,19 @@ describe("SMT Javascript test", function () { await tree1.delete(keys[1]); await tree2.delete(keys[1]); await tree2.delete(keys[0]); - assert(tree1.root.equals(tree2.root)); + assert(Fr.eq(tree1.root, tree2.root)); await tree3.delete(keys[0]); await tree3.delete(keys[2]); await tree4.delete(keys[2]); await tree4.delete(keys[0]); - assert(tree3.root.equals(tree4.root)); + assert(Fr.eq(tree3.root, tree4.root)); await tree5.delete(keys[1]); await tree5.delete(keys[2]); await tree6.delete(keys[2]); await tree6.delete(keys[1]); - assert(tree5.root.equals(tree6.root)); + assert(Fr.eq(tree5.root, tree6.root)); await tree1.delete(keys[2]); await tree2.delete(keys[2]); @@ -114,12 +97,12 @@ describe("SMT Javascript test", function () { await tree5.delete(keys[0]); await tree6.delete(keys[0]); - assert(tree1.root.isZero()); - assert(tree2.root.isZero()); - assert(tree3.root.isZero()); - assert(tree4.root.isZero()); - assert(tree5.root.isZero()); - assert(tree6.root.isZero()); + assert(Fr.isZero(tree1.root)); + assert(Fr.isZero(tree2.root)); + assert(Fr.isZero(tree3.root)); + assert(Fr.isZero(tree4.root)); + assert(Fr.isZero(tree5.root)); + assert(Fr.isZero(tree6.root)); assert.equal(Object.keys(tree1.db.nodes).length, 0); assert.equal(Object.keys(tree2.db.nodes).length, 0); @@ -144,7 +127,7 @@ describe("SMT Javascript test", function () { const arr = []; const N = 100; for (let i=0; i { - const key = bigInt(111); - const value = bigInt(222); + const key = Fr.e(111); + const value = Fr.e(222); await testInsert(tree, key, value, circuit); }); it("It should add another element", async () => { - const key = bigInt(333); - const value = bigInt(444); + const key = Fr.e(333); + const value = Fr.e(444); await testInsert(tree, key, value, circuit); }); @@ -110,8 +110,8 @@ describe("SMT Processor test", function () { }); it("Should test convination of adding and removing 3 elements", async () => { - const keys = [bigInt(8), bigInt(9), bigInt(32)]; - const values = [bigInt(88), bigInt(99), bigInt(3232)]; + const keys = [Fr.e(8), Fr.e(9), Fr.e(32)]; + const values = [Fr.e(88), Fr.e(99), Fr.e(3232)]; const tree1 = await smt.newMemEmptyTrie(); const tree2 = await smt.newMemEmptyTrie(); const tree3 = await smt.newMemEmptyTrie(); @@ -170,7 +170,7 @@ describe("SMT Processor test", function () { it("Should match a NOp with random vals", async () => { let siblings = []; - while (siblings.length<10) siblings.push(bigInt(88)); + while (siblings.length<10) siblings.push(Fr.e(88)); const w = await circuit.calculateWitness({ fnc: [0,0], oldRoot: 11, @@ -187,7 +187,7 @@ describe("SMT Processor test", function () { await circuit.checkConstraints(w); - assert(root1.equals(root2)); + assert(Fr.eq(root1, root2)); }); it("Should update an element", async () => { const tree1 = await smt.newMemEmptyTrie(); diff --git a/test/smtverifier.js b/test/smtverifier.js index f5992ad..b8b2e56 100644 --- a/test/smtverifier.js +++ b/test/smtverifier.js @@ -1,6 +1,6 @@ const chai = require("chai"); const path = require("path"); -const bigInt = require("big-integer"); +const Fr = require("ffjavascript").bn128.Fr; const tester = require("circom").tester; const smt = require("../src/smt.js"); @@ -17,7 +17,7 @@ async function testInclusion(tree, key, circuit) { assert(res.found); let siblings = res.siblings; - while (siblings.length<10) siblings.push(bigInt(0)); + while (siblings.length<10) siblings.push(Fr.e(0)); const w = await circuit.calculateWitness({ enabled: 1, @@ -40,7 +40,7 @@ async function testExclusion(tree, key, circuit) { assert(!res.found); let siblings = res.siblings; - while (siblings.length<10) siblings.push(bigInt(0)); + while (siblings.length<10) siblings.push(Fr.e(0)); const w = await circuit.calculateWitness({ enabled: 1, @@ -110,14 +110,14 @@ describe("SMT Verifier test", function () { }); it("Check inclussion Adria case", async () => { - const e1_hi= bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839"); - const e1_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"); + const e1_hi= Fr.e("17124152697573569611556136390143205198134245887034837071647643529178599000839"); + const e1_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179"); - const e2ok_hi= bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262"); - const e2ok_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"); + const e2ok_hi= Fr.e("16498254692537945203721083102154618658340563351558973077349594629411025251262"); + const e2ok_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179"); - const e2fail_hi= bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579"); - const e2fail_hv= bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"); + const e2fail_hi= Fr.e("17195092312975762537892237130737365903429674363577646686847513978084990105579"); + const e2fail_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179"); const tree1 = await smt.newMemEmptyTrie(); await tree1.insert(e1_hi,e1_hv);