diff --git a/.gitignore b/.gitignore index 0ad0eb3..9e7d574 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,6 @@ tmp .DS_Store +# Workspace files are user-specific +*.sublime-workspace + diff --git a/Project.sublime-workspace b/Project.sublime-workspace deleted file mode 100644 index b943a7c..0000000 --- a/Project.sublime-workspace +++ /dev/null @@ -1,878 +0,0 @@ -{ - "auto_complete": - { - "selected_items": - [ - [ - "ins", - "instantiateConstant" - ], - [ - "c", - "circuit" - ], - [ - "type", - "typeof" - ], - [ - "b", - "bName" - ], - [ - "e", - "eOut" - ], - [ - "sym", - "symbols" - ], - [ - "res", - "resStr" - ], - [ - "base", - "baseName" - ], - [ - "a", - "async" - ], - [ - "out", - "outFile" - ], - [ - "addS", - "addSymbolArray" - ], - [ - "of", - "offset" - ], - [ - "si", - "signal" - ], - [ - "s", - "signals" - ], - [ - "dir", - "dirName" - ], - [ - "main", - "mainComponent" - ], - [ - "ci", - "circomFile" - ], - [ - "pc", - "pcV1" - ], - [ - "re", - "require" - ], - [ - "buildC", - "buildCircuit" - ], - [ - "Ti", - "Ticker" - ], - [ - "Ma", - "MaxBandwith" - ], - [ - "Bl", - "BlackList" - ], - [ - "Max", - "MaxConnections" - ], - [ - "uint", - "uint64" - ], - [ - "he", - "helloLen" - ], - [ - "Pr", - "Printf" - ], - [ - "n", - "nil" - ], - [ - "ne", - "newBuff" - ], - [ - "new", - "newBuff" - ], - [ - "H", - "HiddenDomain" - ], - [ - "Hidden", - "hiddenDomain" - ], - [ - "ex", - "extensionLen" - ], - [ - "sess", - "sessionLen" - ], - [ - "ses", - "sessionId" - ], - [ - "exte", - "extensionLen" - ], - [ - "se", - "sessionLen" - ], - [ - "by", - "byte" - ], - [ - "clie", - "clientKS" - ], - [ - "de", - "decrypter" - ], - [ - "buf", - "buftype" - ], - [ - "set", - "setSignal" - ], - [ - "ha", - "handle_error" - ], - [ - "in", - "infilename" - ], - [ - "wri", - "writeOut" - ], - [ - "str", - "string" - ], - [ - "it", - "itFunc" - ], - [ - "iter", - "iterateArr" - ], - [ - "idx", - "idxInput" - ], - [ - "NS", - "NSignals" - ], - [ - "en", - "entryPos" - ], - [ - "h", - "hIdx" - ], - [ - "nS", - "NSignals" - ], - [ - "inc", - "include" - ], - [ - "bui", - "buildOutArray" - ], - [ - "N", - "NVars" - ], - [ - "pC", - "pCurrent" - ], - [ - "acc", - "account2" - ], - [ - "tim", - "timestamp" - ], - [ - "not", - "notAvTxs" - ], - [ - "con", - "continue" - ], - [ - "on", - "onChainTxs" - ], - [ - "ge", - "getOperatorFee" - ], - [ - "use", - "userFee" - ], - [ - "no", - "normalizedFee" - ], - [ - "fi", - "firstNonce" - ], - [ - "am", - "amountF" - ], - [ - "cal", - "calcSlots" - ], - [ - "tx", - "txPool" - ], - [ - "non", - "nonExecutableSlots" - ], - [ - "max", - "maxSlots" - ], - [ - "slo", - "slotKeys" - ], - [ - "Tx", - "TXPool" - ], - [ - "cla", - "_classifyTxs" - ], - [ - "remo", - "removed" - ], - [ - "u", - "updateSlotsPending" - ], - [ - "txs", - "txsByCoin" - ], - [ - "forged", - "forgedTxs" - ], - [ - "removed", - "removedCoins" - ], - [ - "bestN", - "bestNTx" - ], - [ - "av", - "avTxs" - ], - [ - "ad", - "adjustedFee" - ], - [ - "M", - "MaxCoins" - ], - [ - "norm", - "normalizedFee" - ], - [ - "us", - "userFeeF" - ], - [ - "from", - "idxFrom" - ], - [ - "eq", - "equal" - ], - [ - "fee", - "feePlanCoins" - ], - [ - "as", - "async" - ], - [ - "ke", - "key" - ], - [ - "pro", - "promises" - ], - [ - "DB", - "DB_Master" - ], - [ - "va", - "value" - ], - [ - "add", - "addSizes" - ], - [ - "labe", - "labelSize" - ], - [ - "las", - "last_column" - ], - [ - "stat", - "statements" - ], - [ - "firs", - "first_column" - ], - [ - "f", - "first_line" - ], - [ - "tmp", - "tmpNames" - ], - [ - "su", - "suggestedName" - ], - [ - "fn", - "fnvHash" - ], - [ - "def", - "definedHashTables" - ], - [ - "le", - "length" - ], - [ - "la", - "labelName" - ], - [ - "size", - "sizes" - ], - [ - "code", - "codeFooter" - ], - [ - "sc", - "scopes" - ], - [ - "rN", - "rName" - ], - [ - "genGet", - "genGetSignal" - ], - [ - "label", - "labelName" - ], - [ - "instan", - "instantiateRef" - ], - [ - "v", - "vOffset" - ], - [ - "BIGIN", - "BIGINT" - ], - [ - "st", - "stack" - ], - [ - "SI", - "SIZES" - ], - [ - "newS", - "newStackVar" - ], - [ - "sco", - "scopes" - ], - [ - "cons", - "constant" - ], - [ - "val", - "value" - ], - [ - "com", - "components" - ], - [ - "calc", - "calcAcc" - ], - [ - "ca", - "calcStr" - ], - [ - "to", - "toJSNumber" - ], - [ - "get", - "getTmpName" - ], - [ - "cS", - "cSourceDone" - ], - [ - "r1", - "r1csDone" - ], - [ - "B", - "Buffer" - ] - ] - }, - "buffers": - [ - ], - "build_system": "", - "build_system_choices": - [ - ], - "build_varint": "", - "command_palette": - { - "height": 0.0, - "last_filter": "", - "selected_items": - [ - [ - "in", - "Package Control: Install Package" - ], - [ - "ins", - "Package Control: Install Package" - ], - [ - "", - "Arithmetic" - ], - [ - "Package Control: ", - "Package Control: Disable Package" - ], - [ - "install", - "Package Control: Install Package" - ] - ], - "width": 0.0 - }, - "console": - { - "height": 210.0, - "history": - [ - ] - }, - "distraction_free": - { - "menu_visible": true, - "show_minimap": false, - "show_open_files": false, - "show_tabs": false, - "side_bar_visible": false, - "status_bar_visible": false - }, - "file_history": - [ - "/Users/jbaylina/git/iden3/circom/cli.js", - "/Users/jbaylina/git/iden3/circom/src/c_build.js", - "/Users/jbaylina/git/iden3/circom/src/c_gen.js", - "/Users/jbaylina/git/iden3/circom/test/circuits/add.circom", - "/Users/jbaylina/git/iden3/circom/src/c_tester.js", - "/Users/jbaylina/git/iden3/circom/test/basiccases.js", - "/Users/jbaylina/git/iden3/circom/test/inout.js", - "/Users/jbaylina/git/iden3/circom/c/main.cpp", - "/Users/jbaylina/git/iden3/circom/src/utils.js", - "/Users/jbaylina/git/iden3/circom/src/zqfield.js", - "/Users/jbaylina/git/iden3/circom/src/compiler.js", - "/Users/jbaylina/git/iden3/circom/c/calcwit.h", - "/Users/jbaylina/git/iden3/circom/c/zqfield.cpp", - "/Users/jbaylina/git/iden3/circom/c/circom.h", - "/Users/jbaylina/git/iden3/circom/test/circuits/add.cpp", - "/Users/jbaylina/git/iden3/circom/c/zqfield.h", - "/Users/jbaylina/git/iden3/circom/c/calcwit.cpp", - "/Users/jbaylina/git/iden3/circom/src/ctx.js", - "/Users/jbaylina/git/iden3/circom/index.js", - "/Users/jbaylina/git/iden3/circom/test/circuits/inout.cpp", - "/Users/jbaylina/git/iden3/circom/src/tester.js", - "/Users/jbaylina/git/iden3/rollup/doc/rollup_tx.txt", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/crypto/tls/conn.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/crypto/tls/handshake_client.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/crypto/tls/alert.go", - "/Users/jbaylina/git/personal/tls-tris/13.go", - "/Users/jbaylina/git/personal/tls-tris/tls_test.go", - "/Users/jbaylina/git/personal/tls-tris/conn.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/crypto/x509/verify.go", - "/Users/jbaylina/git/personal/tls-tris/handshake_client.go", - "/Users/jbaylina/git/personal/testimg/dosfilter.go", - "/Users/jbaylina/git/personal/testimg/main.go", - "/Users/jbaylina/git/personal/hiddenproxy/hiddenproxy/main.go", - "/Users/jbaylina/git/personal/tls-tris/cipher_suites.go", - "/Users/jbaylina/git/personal/hiddenproxy.old/proxy.go", - "/Users/jbaylina/git/personal/hiddenproxy/hiddenproxy/proxy.go", - "/Users/jbaylina/git/personal/hiddenproxy/client-test/client-test", - "/Users/jbaylina/git/personal/hiddenproxy/client-test/main.go", - "/Users/jbaylina/git/personal/tls-tris/key_agreement.go", - "/Users/jbaylina/git/personal/tls-tris/common.go", - "/Users/jbaylina/git/personal/testesni/testesni.go", - "/Users/jbaylina/git/personal/hiddenproxy/main.go", - "/Users/jbaylina/git/personal/testesni2/testesni2.go", - "/Users/jbaylina/git/personal/hiddenproxy/proxy.go", - "/Users/jbaylina/git/personal/hiddenproxy/testenc/main.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/crypto/blake2b/blake2b_amd64.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/crypto/blake2b/blake2b.go", - "/Users/jbaylina/git/personal/tls-tris/esni.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/tris-testclient/esni_query.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/tris-testclient/client.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/tris-localserver/server.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/crypto/tls/esni.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/crypto/tls/common.go", - "/Users/jbaylina/git/iden3/circom/src/exec.js", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/net/tcpsock_plan9.go", - "/Users/jbaylina/git/personal/tls-tris/tls.go", - "/Users/jbaylina/git/personal/tls-tris/hiddenproxy.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/net/tcpsock.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/net/tcpsock_posix.go", - "/Users/jbaylina/git/personal/tls-tris/_dev/GOROOT/darwin_amd64/src/net/ipsock_plan9.go", - "/Users/jbaylina/git/iden3/circom/src/gen_c.js", - "/Users/jbaylina/git/iden3/circom/test/circuits/inout.circom", - "/Users/jbaylina/git/iden3/circom/c/utils.cpp", - "/Users/jbaylina/git/iden3/circom/c/utils.h", - "/Users/jbaylina/git/iden3/circom/c/mainjson.cpp", - "/Users/jbaylina/git/iden3/circom/c/mainjson", - "/Users/jbaylina/git/iden3/circom/doc/r1cs_bin_format.md", - "/Users/jbaylina/git/iden3/circom/c/circom.cpp", - "/Users/jbaylina/git/iden3/circomlib/src/eddsa.js", - "/Users/jbaylina/git/iden3/rollup/js/utils.js", - "/Users/jbaylina/git/iden3/rollup/js/rollupaccount.js", - "/Users/jbaylina/git/iden3/rollup/js/txpool.js", - "/Users/jbaylina/git/iden3/rollup/test/txpool.js", - "/Users/jbaylina/git/iden3/rollup/js/tmpstate.js", - "/Users/jbaylina/git/iden3/rollup/js/rollupdb.js", - "/Users/jbaylina/git/iden3/rollup/circuits/balancesupdater.circom", - "/Users/jbaylina/git/iden3/rollup/test/feeselector.js", - "/Users/jbaylina/git/iden3/rollup/test/circuits/rollup_test.circom", - "/Users/jbaylina/git/iden3/rollup/js/batchbuilder.js", - "/Users/jbaylina/git/iden3/rollup/test/rollup_circuit.js", - "/Users/jbaylina/git/iden3/rollup/circuits/rollup.circom", - "/Users/jbaylina/git/iden3/rollup/circuits/rolluptx.circom", - "/Users/jbaylina/git/iden3/rollup/circuits/feeselector.circom", - "/Users/jbaylina/git/iden3/rollup/circuits/feeplandecoder.circom", - "/Users/jbaylina/git/iden3/rollup/test.txt", - "/Users/jbaylina/git/iden3/rollup/package.json", - "/Users/jbaylina/git/iden3/rollup/circuits/rolluptxstates.circom", - "/Users/jbaylina/git/iden3/rollup/js/constants.js", - "/Users/jbaylina/git/iden3/rollup/.gitignore", - "/Users/jbaylina/git/iden3/rollup/test/helpers/checkbatch.js", - "/Users/jbaylina/git/iden3/circomlib/package.json", - "/Users/jbaylina/git/iden3/snarkjs/src/bn128.js", - "/Users/jbaylina/git/iden3/circomlib/test/smtverifier.js", - "/Users/jbaylina/git/iden3/circomlib/test/smtverifier_adria.js", - "/Users/jbaylina/git/iden3/circomlib/src/smt_memdb.js", - "/Users/jbaylina/git/iden3/rollup/js/smttmpdb.js", - "/Users/jbaylina/git/iden3/rollup/js/blockbuilder.js", - "/Users/jbaylina/git/iden3/circom/src/buildc.js", - "/Users/jbaylina/git/iden3/circom/package.json", - "/Users/jbaylina/git/iden3/circom/test/cases.js", - "/Users/jbaylina/git/iden3/circom/src/lcalgebra.js", - "/Users/jbaylina/git/iden3/rollup/circuits/decodetx.circom", - "/Users/jbaylina/git/personal/semaphore/semaphorejs/snark/test.circom", - "/Users/jbaylina/git/iden3/circom/src/gencode.js", - "/Users/jbaylina/git/iden3/circom/src/buildwasm.js", - "/Users/jbaylina/git/iden3/rollup/circuits/statepacker.circom", - "/Users/jbaylina/git/personal/semaphore/semaphorejs/snark/circuit.json", - "/Users/jbaylina/git/iden3/circomlib/test/eddsaposeidon.js", - "/Users/jbaylina/git/iden3/rollup/.eslintrc.js", - "/Users/jbaylina/git/iden3/rollup/.eslintrc", - "/Users/jbaylina/git/iden3/circomlib/index.js", - "/Users/jbaylina/git/iden3/snarkjs/src/calculateWitness.js", - "/Users/jbaylina/git/iden3/circomlib/src/smt.js", - "/Users/jbaylina/git/iden3/snarkjs/src/bigint.js", - "/Users/jbaylina/git/iden3/rollup/circuits/requiredtxverifier.circom", - "/Users/jbaylina/git/codereview/rattack/deploy_withdraw.js", - "/Users/jbaylina/git/iden3/circomlib/src/poseidon.js", - "/Users/jbaylina/git/iden3/rollup/test/rollupaccount.js", - "/Users/jbaylina/git/iden3/rollup/test/circuits/input.json", - "/Users/jbaylina/git/iden3/rollup/js/tmpdb.js", - "/Users/jbaylina/git/iden3/circomlib/circuits/eddsaposeidon.circom", - "/Users/jbaylina/git/iden3/rollup/circuits/decodefloat.circom", - "/Users/jbaylina/git/iden3/circomlib/circuits/escalarmulany.circom", - "/Users/jbaylina/git/iden3/circomlib/circuits/escalarmulfix.circom", - "/Users/jbaylina/git/iden3/rollup/test/decodefloat.js", - "/Users/jbaylina/git/iden3/circomlib/test/sha256.js", - "/Users/jbaylina/git/iden3/circomlib/test/circuits/sha256_test448.circom", - "/Users/jbaylina/git/iden3/circomlib/test/circuits/sha256_test512.circom" - ], - "find": - { - "height": 40.0 - }, - "find_in_files": - { - "height": 313.0, - "where_history": - [ - ] - }, - "find_state": - { - "case_sensitive": true, - "find_history": - [ - "genSignalAssignConstrain", - "mpz_impo", - "instantiateConstant", - "genVariable", - "instantiateConstant", - "error", - "used", - "instantiateRef", - "newRef", - "used", - "main", - "components", - "execDeclareSignal", - ".e", - "execSignalAssign", - "execSignalAssignConstrain", - "buildWit2Sig", - "__P__", - "genConstraint", - "checkConstraint", - "genConstraint", - "genConstrain", - "setS", - "asse", - "genConstrain", - "codeHeader", - "ht_InOut", - "addSizes", - "codes_sizes", - "definedSizes", - "getTmpName", - "getTmpNames", - "getTmp", - "getTmpName", - "utils", - "buildWit2Sig", - "buildMapIsInput", - "globalNames", - "buildInit", - "ctx", - "buildInit", - "genHe" - ], - "highlight": true, - "in_selection": false, - "preserve_case": false, - "regex": false, - "replace_history": - [ - ], - "reverse": false, - "show_context": true, - "use_buffer2": true, - "whole_word": false, - "wrap": true - }, - "groups": - [ - { - "sheets": - [ - ] - } - ], - "incremental_find": - { - "height": 28.0 - }, - "input": - { - "height": 107.0 - }, - "layout": - { - "cells": - [ - [ - 0, - 0, - 1, - 1 - ] - ], - "cols": - [ - 0.0, - 1.0 - ], - "rows": - [ - 0.0, - 1.0 - ] - }, - "menu_visible": true, - "output.SublimeLinter": - { - "height": 0.0 - }, - "output.SublimeLinter Messages": - { - "height": 246.0 - }, - "output.find_results": - { - "height": 0.0 - }, - "pinned_build_system": "", - "project": "Project.sublime-project", - "replace": - { - "height": 52.0 - }, - "save_all_on_build": true, - "select_file": - { - "height": 0.0, - "last_filter": "", - "selected_items": - [ - [ - "", - "iden3/circom/src/exec.js" - ] - ], - "width": 0.0 - }, - "select_project": - { - "height": 0.0, - "last_filter": "", - "selected_items": - [ - ], - "width": 0.0 - }, - "select_symbol": - { - "height": 0.0, - "last_filter": "", - "selected_items": - [ - ], - "width": 0.0 - }, - "selected_group": 0, - "settings": - { - }, - "show_minimap": true, - "show_open_files": false, - "show_tabs": true, - "side_bar_visible": true, - "side_bar_width": 300.0, - "status_bar_visible": true, - "template_settings": - { - } -} diff --git a/c/calcwit.cpp b/c/calcwit.cpp index 3d5b35a..fe0b6a1 100644 --- a/c/calcwit.cpp +++ b/c/calcwit.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "calcwit.h" #include "utils.h" diff --git a/c/main.cpp b/c/main.cpp index 9abc482..1d9f700 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include diff --git a/src/c_build.js b/src/c_build.js index a25644a..728274f 100644 --- a/src/c_build.js +++ b/src/c_build.js @@ -140,7 +140,7 @@ function buildCode(ctx) { const fnComponents = []; for (let i=0; i=0) continue; // If has an alias, continue.. - assert(outIdx=NVars) continue; // Is a constant or a discarded variable if (typeof arr[ctx.signals[i].id] == "undefined") { arr[outIdx] = i; } @@ -312,11 +317,6 @@ function buildCircuitVar() { -function hashComponentCall(ctx, cIdx) { - // TODO: At the moment generate a diferent function for each instance of the component - return cIdx; -} - function getTmpName(_suggestedName) { let suggestedName; @@ -374,7 +374,7 @@ function addSizes(_sizes) { function buildFunction(name, paramValues) { const ctx = this; - const h = hashFunctionCall(ctx, name, paramValues); + const {h, instanceDef} = hashFunctionCall(ctx, name, paramValues); if (ctx.definedFunctions[h]) return ctx.definedFunctions[h]; @@ -430,7 +430,11 @@ function buildFunction(name, paramValues) { if (ctx.returnValue == null) { if (ctx.returnSizes == null) assert(false, `Funciont ${name} does not return any value`); res.type = "VARVAL_CONSTSIZE"; - let code = `void ${name}_${h}(Circom_CalcWit *ctx, PBigInt __retValue ${paramsStr}) {`; + let code = + "/*\n" + + instanceDef + + "\n*/\n" + + `void ${name}_${h}(Circom_CalcWit *ctx, PBigInt __retValue ${paramsStr}) {`; code += utils.ident(ctx.codeHeader); code += utils.ident(ctx.code); code += utils.ident("returnFunc:\n"); @@ -455,7 +459,77 @@ function buildFunction(name, paramValues) { return res; } + + +function hashComponentCall(ctx, cIdx) { + // TODO: At the moment generate a diferent function for each instance of the component + const constParams = []; + for (let p in ctx.components[cIdx].params) { + constParams.push(p + "=" + value2str(ctx.components[cIdx].params[p])); + } + + for (let n in ctx.components[cIdx].names.o) { + const entry = ctx.components[cIdx].names.o[n]; + if ((entry.type == "S")&&(ctx.signals[entry.offset].o & ctx.IN)) { + travelSizes(n, entry.offset, entry.sizes, (prefix, offset) => { + if (utils.isDefined(ctx.signals[offset].v)) { + constParams.push(prefix + "=" + bigInt(ctx.signals[offset].value)); + } + }); + } + } + + let instanceDef = ctx.components[cIdx].template; + if (constParams.length>0) { + instanceDef += "\n"; + constParams.sort(); + instanceDef += constParams.join("\n"); + } + const h = utils.fnvHash(instanceDef); + return {h, instanceDef}; + + function travelSizes(prefix, offset, sizes, fn) { + if (sizes.length == 0) { + fn(prefix, offset); + return 1; + } else { + let o = offset; + for (let i=0; i0) S+=","; + S+=value2str(v[i]); + } + return S; + } else { + return bigInt(v).toString(); + } } diff --git a/src/c_gen.js b/src/c_gen.js index c8be3e5..3b9a5b1 100644 --- a/src/c_gen.js +++ b/src/c_gen.js @@ -6,7 +6,6 @@ module.exports.gen = gen; module.exports.newRef = newRef; function newRef(ctx, type, _name, value, sizes) { - const isValue = ((typeof(value) != "undefined")&&(value != null)); let name; if (!_name) { name = ctx.getTmpName(); @@ -19,7 +18,7 @@ function newRef(ctx, type, _name, value, sizes) { } if (Array.isArray(sizes)) { sizes = utils.accSizes(sizes); - } else if (isValue) { + } else if (utils.isDefined(value)) { sizes = utils.accSizes(utils.extractSizes(value)); } else { sizes = [1, 0]; @@ -37,7 +36,7 @@ function newRef(ctx, type, _name, value, sizes) { label: label }; - if (isValue) { + if (utils.isDefined(value)) { scope[name].value = value; } @@ -59,7 +58,7 @@ function instantiateRef(ctx, name, initValue) { ctx.codeHeader += `Circom_Sizes ${v.label};\n`; } v.used = true; - if ((typeof initValue!= "undefined")&&(initValue != null)) { + if (utils.isDefined(initValue)) { const flatedValue = utils.flatArray(initValue); for (let i=0; ifield->isTrue(${condName});\n` + `while (${condVar}) {\n`; } else { - if ((typeof cond.value == "undefined")||(cond.value == null)) return error(ctx, ast, "condition value not assigned"); + if (!utils.isDefined(cond.value)) return error(ctx, ast, "condition value not assigned"); if (cond.value.isZero()) end=true; } @@ -834,8 +833,8 @@ function genReturn(ctx, ast) { if (v.used) { ctx.code += `ctx->field->copyn(__retValue, ${vName}, ${v.sizes[0]});\n`; } else { - if ((typeof v.value == "undefined") || v == null) return error(ctx, ast, "Returning an unknown value"); - if ((typeof ctx.returnValue == "undefined") || (ctx.returnValue == null)) { + if (!utils.isDefined(v.value)) return error(ctx, ast, "Returning an unknown value"); + if (!utils.isDefined(ctx.returnValue)) { ctx.returnValue = v.value; } } @@ -885,8 +884,8 @@ function genBinaryOp(ctx, ast, op) { if (ctx.error) return; const b = getScope(ctx, bName); - if ((!a.used)&&(typeof a.value == "undefined")) return error(ctx, ast, "Using a not assigned varialble: "+aName); - if ((!b.used)&&(typeof b.value == "undefined")) return error(ctx, ast, "Using a not assigned varialble: "+bName); + if ((!a.used)&&(!utils.isDefined(a.value))) return error(ctx, ast, "Using a not assigned varialble: "+aName); + if ((!b.used)&&(!utils.isDefined(b.value))) return error(ctx, ast, "Using a not assigned varialble: "+bName); let rName; if (a.used || b.used) { diff --git a/src/compiler.js b/src/compiler.js index 2b2c0a8..8887488 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -29,6 +29,7 @@ const exec = require("./exec"); const lc = require("./lcalgebra"); const Ctx = require("./ctx"); const ZqField = require("./zqfield"); +const utils = require("./utils"); module.exports = compile; @@ -130,6 +131,7 @@ function classifySignals(ctx) { return t1; } if ((t1 == ctx.stONE) || (t2 == ctx.stONE)) return ctx.stONE; + if ((t1 == ctx.stOUTPUT) || (t2 == ctx.stOUTPUT)) return ctx.stOUTPUT; if ((t1 == ctx.stCONSTANT) || (t2 == ctx.stCONSTANT)) return ctx.stCONSTANT; if ((t1 == ctx.stDISCARDED) || (t2 == ctx.stDISCARDED)) return ctx.stDISCARDED; if (t1!=t2) return ERROR; @@ -146,8 +148,6 @@ function classifySignals(ctx) { let t = lSignal.c || ctx.stINTERNAL; if (s == 0) { t = ctx.stONE; - } else if (lSignal.v) { - t = ctx.stCONSTANT; } else if (lSignal.o & ctx.MAIN) { if (lSignal.o & ctx.IN) { if (lSignal.o & ctx.PRV) { @@ -158,6 +158,8 @@ function classifySignals(ctx) { } else if (lSignal.o & ctx.OUT) { t = ctx.stOUTPUT; } + } else if (utils.isDefined(lSignal.v)) { + t = ctx.stCONSTANT; } tAll = priorize(t,tAll); if (lSignal.e>=0) { diff --git a/src/exec.js b/src/exec.js index 76f9d37..6578f6c 100644 --- a/src/exec.js +++ b/src/exec.js @@ -20,6 +20,8 @@ const path = require("path"); const fs = require("fs"); +const utils = require("./utils"); + const bigInt = require("big-integer"); const __P__ = new bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"); const __MASK__ = new bigInt(2).pow(253).minus(1); @@ -495,15 +497,15 @@ function execDeclareSignal(ctx, ast) { if (ast.name.type != "VARIABLE") return error(ctx, ast, "Invalid component name"); if (getScope(ctx, ast.name.name)) return error(ctx, ast, "Name already exists: "+ast.name.name); + let totalSize = 1; const sizes=[]; - let totalSize=1; for (let i=0; i< ast.name.selectors.length; i++) { const size = exec(ctx, ast.name.selectors[i]); if (ctx.error) return; if (size.type != "NUMBER") return error(ctx, ast.name.selectors[i], "expected a number"); const s = size.value.toJSNumber(); - totalSize *= s; + totalSize = totalSize * s; sizes.push( s ); } @@ -529,7 +531,7 @@ function execDeclareSignal(ctx, ast) { ctx.signals[i].o |= ctx.MAIN; } -// ctx.components[ctx.currentComponent].signals.push(i); + // ctx.components[ctx.currentComponent].signals.push(i); } scope[ast.name.name] = { type: "SIGNAL", @@ -584,10 +586,10 @@ function execVariable(ctx, ast) { if (!v) return error(ctx, ast, "Variable not defined"); // If the signal has an assigned value (constant) just return the constant - if ((v.type == "SIGNAL") && (ctx.signals[v.sIdx].value)) { + if ((v.type == "SIGNAL") && (utils.isDefined(ctx.signals[v.sIdx].v))) { return { type: "NUMBER", - value: ctx.signals[v.sIdx].value + value: ctx.signals[v.sIdx].v }; } let res; @@ -1121,7 +1123,7 @@ function execSignalAssign(ctx, ast) { const v = lc.evaluate(ctx, src); if (v.value) { - sDest.value = v.value; + sDest.v = v.value; } } diff --git a/src/utils.js b/src/utils.js index 5ac3844..ed27f80 100644 --- a/src/utils.js +++ b/src/utils.js @@ -12,6 +12,7 @@ module.exports.fnvHash = fnvHash; module.exports.stringifyBigInts = stringifyBigInts; module.exports.unstringifyBigInts = unstringifyBigInts; module.exports.sameSizes = sameSizes; +module.exports.isDefined = isDefined; function ident(text) { let lines = text.split("\n"); @@ -116,5 +117,9 @@ function sameSizes(s1, s2) { return true; } +function isDefined(v) { + return ((typeof(v) != "undefined")&&(v != null)); +} + diff --git a/test/basiccases.js b/test/basiccases.js index 82fa93f..c4f6c87 100644 --- a/test/basiccases.js +++ b/test/basiccases.js @@ -87,4 +87,14 @@ describe("basic cases", function () { ] ); }); + it("constants1", async () => { + await doTest( + "constants1.circom", + [ + [{in: 0}, {out: 42}], + [{in: 10}, {out: 52}], + [{in: __P__.minus(2)}, {out: 40}], + ] + ); + }); }); diff --git a/test/circuits/constants1.circom b/test/circuits/constants1.circom new file mode 100644 index 0000000..d1e7ae9 --- /dev/null +++ b/test/circuits/constants1.circom @@ -0,0 +1,39 @@ + + + +template Add(n) { + signal input in[n]; + signal output out; + + var lc = 0; + for (var i=0; i