Browse Source

constants

feature/witness_bin
Jordi Baylina 5 years ago
parent
commit
6cdb006909
No known key found for this signature in database GPG Key ID: 7480C80C1BE43112
11 changed files with 166 additions and 908 deletions
  1. +3
    -0
      .gitignore
  2. +0
    -878
      Project.sublime-workspace
  3. +1
    -0
      c/calcwit.cpp
  4. +1
    -0
      c/main.cpp
  5. +87
    -13
      src/c_build.js
  6. +8
    -9
      src/c_gen.js
  7. +4
    -2
      src/compiler.js
  8. +8
    -6
      src/exec.js
  9. +5
    -0
      src/utils.js
  10. +10
    -0
      test/basiccases.js
  11. +39
    -0
      test/circuits/constants1.circom

+ 3
- 0
.gitignore

@ -64,3 +64,6 @@ tmp
.DS_Store .DS_Store
# Workspace files are user-specific
*.sublime-workspace

+ 0
- 878
Project.sublime-workspace

@ -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":
{
}
}

+ 1
- 0
c/calcwit.cpp

@ -5,6 +5,7 @@
#include <iomanip> #include <iomanip>
#include <stdlib.h> #include <stdlib.h>
#include <gmp.h> #include <gmp.h>
#include <assert.h>
#include "calcwit.h" #include "calcwit.h"
#include "utils.h" #include "utils.h"

+ 1
- 0
c/main.cpp

@ -2,6 +2,7 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <iomanip>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/mman.h> #include <sys/mman.h>

+ 87
- 13
src/c_build.js

@ -140,7 +140,7 @@ function buildCode(ctx) {
const fnComponents = []; const fnComponents = [];
for (let i=0; i<ctx.components.length; i++) { for (let i=0; i<ctx.components.length; i++) {
const h = hashComponentCall(ctx, i);
const {h, instanceDef} = hashComponentCall(ctx, i);
const fName = ctx.components[i].template+"_"+h; const fName = ctx.components[i].template+"_"+h;
if (!fDefined[fName]) { if (!fDefined[fName]) {
@ -159,15 +159,20 @@ function buildCode(ctx) {
gen(ctx, ctx.templates[ctx.components[i].template].block); gen(ctx, ctx.templates[ctx.components[i].template].block);
const S = `void ${fName}(Circom_CalcWit *ctx) {\n` +
const S =
"/*\n" +
instanceDef +
"\n*/\n" +
`void ${fName}(Circom_CalcWit *ctx) {\n` +
utils.ident( utils.ident(
ctx.codeHeader + "\n" + ctx.codeHeader + "\n" +
ctx.code + "\n" + ctx.code + "\n" +
ctx.codeFooter ctx.codeFooter
) + ) +
"}\n";
"}\n";
fnComponents.push(S); fnComponents.push(S);
fDefined[fName] = true;
} }
ctx.components[i].fnName = fName; ctx.components[i].fnName = fName;
} }
@ -269,9 +274,9 @@ function buildWit2Sig(ctx) {
const arr = Array(NVars); const arr = Array(NVars);
for (let i=0; i<ctx.signals.length; i++) { for (let i=0; i<ctx.signals.length; i++) {
const outIdx = ctx.signals[i].id; const outIdx = ctx.signals[i].id;
if (typeof outIdx == "undefined") continue;
if (ctx.signals[i].e>=0) continue; // If has an alias, continue.. if (ctx.signals[i].e>=0) continue; // If has an alias, continue..
assert(outIdx<NVars);
assert(typeof outIdx != "undefined", `Signal ${i} does not have index`);
if (outIdx>=NVars) continue; // Is a constant or a discarded variable
if (typeof arr[ctx.signals[i].id] == "undefined") { if (typeof arr[ctx.signals[i].id] == "undefined") {
arr[outIdx] = i; 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) { function getTmpName(_suggestedName) {
let suggestedName; let suggestedName;
@ -374,7 +374,7 @@ function addSizes(_sizes) {
function buildFunction(name, paramValues) { function buildFunction(name, paramValues) {
const ctx = this; const ctx = this;
const h = hashFunctionCall(ctx, name, paramValues);
const {h, instanceDef} = hashFunctionCall(ctx, name, paramValues);
if (ctx.definedFunctions[h]) return ctx.definedFunctions[h]; if (ctx.definedFunctions[h]) return ctx.definedFunctions[h];
@ -430,7 +430,11 @@ function buildFunction(name, paramValues) {
if (ctx.returnValue == null) { if (ctx.returnValue == null) {
if (ctx.returnSizes == null) assert(false, `Funciont ${name} does not return any value`); if (ctx.returnSizes == null) assert(false, `Funciont ${name} does not return any value`);
res.type = "VARVAL_CONSTSIZE"; 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.codeHeader);
code += utils.ident(ctx.code); code += utils.ident(ctx.code);
code += utils.ident("returnFunc:\n"); code += utils.ident("returnFunc:\n");
@ -455,7 +459,77 @@ function buildFunction(name, paramValues) {
return res; 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; i<sizes[0]; i++) {
o += travelSizes(prefix + "[" + i + "]", o, sizes.slice(1), fn);
}
return o-offset;
}
}
}
function hashFunctionCall(ctx, name, paramValues) { function hashFunctionCall(ctx, name, paramValues) {
// TODO // TODO
return "1234";
const constParams = [];
for (let i=0; i<ctx.functions[name].params.length; i++) {
if (!paramValues[i].used) {
constParams.push(ctx.functions[name].params[i] + "=" + value2str(paramValues[i]));
}
}
let instanceDef = name;
if (constParams.length>0) {
instanceDef += "\n";
constParams.sort();
instanceDef += constParams.join("\n");
}
const h = utils.fnvHash(instanceDef);
return {h, instanceDef};
}
function value2str(v) {
if (Array.isArray(v)) {
let S="[";
for (let i=0; i<v.length; i++) {
if (i>0) S+=",";
S+=value2str(v[i]);
}
return S;
} else {
return bigInt(v).toString();
}
} }

+ 8
- 9
src/c_gen.js

@ -6,7 +6,6 @@ module.exports.gen = gen;
module.exports.newRef = newRef; module.exports.newRef = newRef;
function newRef(ctx, type, _name, value, sizes) { function newRef(ctx, type, _name, value, sizes) {
const isValue = ((typeof(value) != "undefined")&&(value != null));
let name; let name;
if (!_name) { if (!_name) {
name = ctx.getTmpName(); name = ctx.getTmpName();
@ -19,7 +18,7 @@ function newRef(ctx, type, _name, value, sizes) {
} }
if (Array.isArray(sizes)) { if (Array.isArray(sizes)) {
sizes = utils.accSizes(sizes); sizes = utils.accSizes(sizes);
} else if (isValue) {
} else if (utils.isDefined(value)) {
sizes = utils.accSizes(utils.extractSizes(value)); sizes = utils.accSizes(utils.extractSizes(value));
} else { } else {
sizes = [1, 0]; sizes = [1, 0];
@ -37,7 +36,7 @@ function newRef(ctx, type, _name, value, sizes) {
label: label label: label
}; };
if (isValue) {
if (utils.isDefined(value)) {
scope[name].value = value; scope[name].value = value;
} }
@ -59,7 +58,7 @@ function instantiateRef(ctx, name, initValue) {
ctx.codeHeader += `Circom_Sizes ${v.label};\n`; ctx.codeHeader += `Circom_Sizes ${v.label};\n`;
} }
v.used = true; v.used = true;
if ((typeof initValue!= "undefined")&&(initValue != null)) {
if (utils.isDefined(initValue)) {
const flatedValue = utils.flatArray(initValue); const flatedValue = utils.flatArray(initValue);
for (let i=0; i<flatedValue.length; i++) { for (let i=0; i<flatedValue.length; i++) {
const c = `mpz_set_str(${v.label}[${i}], "${flatedValue[i].toString(10)}", 10);\n`; const c = `mpz_set_str(${v.label}[${i}], "${flatedValue[i].toString(10)}", 10);\n`;
@ -738,7 +737,7 @@ function genFor(ctx, ast) {
`${condVar} = ctx->field->isTrue(${condName});\n` + `${condVar} = ctx->field->isTrue(${condName});\n` +
`while (${condVar}) {\n`; `while (${condVar}) {\n`;
} else { } 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; if (cond.value.isZero()) end=true;
} }
@ -834,8 +833,8 @@ function genReturn(ctx, ast) {
if (v.used) { if (v.used) {
ctx.code += `ctx->field->copyn(__retValue, ${vName}, ${v.sizes[0]});\n`; ctx.code += `ctx->field->copyn(__retValue, ${vName}, ${v.sizes[0]});\n`;
} else { } 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; ctx.returnValue = v.value;
} }
} }
@ -885,8 +884,8 @@ function genBinaryOp(ctx, ast, op) {
if (ctx.error) return; if (ctx.error) return;
const b = getScope(ctx, bName); 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; let rName;
if (a.used || b.used) { if (a.used || b.used) {

+ 4
- 2
src/compiler.js

@ -29,6 +29,7 @@ const exec = require("./exec");
const lc = require("./lcalgebra"); const lc = require("./lcalgebra");
const Ctx = require("./ctx"); const Ctx = require("./ctx");
const ZqField = require("./zqfield"); const ZqField = require("./zqfield");
const utils = require("./utils");
module.exports = compile; module.exports = compile;
@ -130,6 +131,7 @@ function classifySignals(ctx) {
return t1; return t1;
} }
if ((t1 == ctx.stONE) || (t2 == ctx.stONE)) return ctx.stONE; 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.stCONSTANT) || (t2 == ctx.stCONSTANT)) return ctx.stCONSTANT;
if ((t1 == ctx.stDISCARDED) || (t2 == ctx.stDISCARDED)) return ctx.stDISCARDED; if ((t1 == ctx.stDISCARDED) || (t2 == ctx.stDISCARDED)) return ctx.stDISCARDED;
if (t1!=t2) return ERROR; if (t1!=t2) return ERROR;
@ -146,8 +148,6 @@ function classifySignals(ctx) {
let t = lSignal.c || ctx.stINTERNAL; let t = lSignal.c || ctx.stINTERNAL;
if (s == 0) { if (s == 0) {
t = ctx.stONE; t = ctx.stONE;
} else if (lSignal.v) {
t = ctx.stCONSTANT;
} else if (lSignal.o & ctx.MAIN) { } else if (lSignal.o & ctx.MAIN) {
if (lSignal.o & ctx.IN) { if (lSignal.o & ctx.IN) {
if (lSignal.o & ctx.PRV) { if (lSignal.o & ctx.PRV) {
@ -158,6 +158,8 @@ function classifySignals(ctx) {
} else if (lSignal.o & ctx.OUT) { } else if (lSignal.o & ctx.OUT) {
t = ctx.stOUTPUT; t = ctx.stOUTPUT;
} }
} else if (utils.isDefined(lSignal.v)) {
t = ctx.stCONSTANT;
} }
tAll = priorize(t,tAll); tAll = priorize(t,tAll);
if (lSignal.e>=0) { if (lSignal.e>=0) {

+ 8
- 6
src/exec.js

@ -20,6 +20,8 @@
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const utils = require("./utils");
const bigInt = require("big-integer"); const bigInt = require("big-integer");
const __P__ = new bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"); const __P__ = new bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const __MASK__ = new bigInt(2).pow(253).minus(1); 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 (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); if (getScope(ctx, ast.name.name)) return error(ctx, ast, "Name already exists: "+ast.name.name);
let totalSize = 1;
const sizes=[]; const sizes=[];
let totalSize=1;
for (let i=0; i< ast.name.selectors.length; i++) { for (let i=0; i< ast.name.selectors.length; i++) {
const size = exec(ctx, ast.name.selectors[i]); const size = exec(ctx, ast.name.selectors[i]);
if (ctx.error) return; if (ctx.error) return;
if (size.type != "NUMBER") return error(ctx, ast.name.selectors[i], "expected a number"); if (size.type != "NUMBER") return error(ctx, ast.name.selectors[i], "expected a number");
const s = size.value.toJSNumber(); const s = size.value.toJSNumber();
totalSize *= s;
totalSize = totalSize * s;
sizes.push( s ); sizes.push( s );
} }
@ -529,7 +531,7 @@ function execDeclareSignal(ctx, ast) {
ctx.signals[i].o |= ctx.MAIN; ctx.signals[i].o |= ctx.MAIN;
} }
// ctx.components[ctx.currentComponent].signals.push(i);
// ctx.components[ctx.currentComponent].signals.push(i);
} }
scope[ast.name.name] = { scope[ast.name.name] = {
type: "SIGNAL", type: "SIGNAL",
@ -584,10 +586,10 @@ function execVariable(ctx, ast) {
if (!v) return error(ctx, ast, "Variable not defined"); if (!v) return error(ctx, ast, "Variable not defined");
// If the signal has an assigned value (constant) just return the constant // 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 { return {
type: "NUMBER", type: "NUMBER",
value: ctx.signals[v.sIdx].value
value: ctx.signals[v.sIdx].v
}; };
} }
let res; let res;
@ -1121,7 +1123,7 @@ function execSignalAssign(ctx, ast) {
const v = lc.evaluate(ctx, src); const v = lc.evaluate(ctx, src);
if (v.value) { if (v.value) {
sDest.value = v.value;
sDest.v = v.value;
} }
} }

+ 5
- 0
src/utils.js

@ -12,6 +12,7 @@ module.exports.fnvHash = fnvHash;
module.exports.stringifyBigInts = stringifyBigInts; module.exports.stringifyBigInts = stringifyBigInts;
module.exports.unstringifyBigInts = unstringifyBigInts; module.exports.unstringifyBigInts = unstringifyBigInts;
module.exports.sameSizes = sameSizes; module.exports.sameSizes = sameSizes;
module.exports.isDefined = isDefined;
function ident(text) { function ident(text) {
let lines = text.split("\n"); let lines = text.split("\n");
@ -116,5 +117,9 @@ function sameSizes(s1, s2) {
return true; return true;
} }
function isDefined(v) {
return ((typeof(v) != "undefined")&&(v != null));
}

+ 10
- 0
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}],
]
);
});
}); });

+ 39
- 0
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<n; i++) {
lc = lc + in[i];
}
out <== lc;
}
function FAdd(a,b) {
return a+b;
}
template Main() {
signal input in;
signal output out;
var o = FAdd(3,4);
o = o + FAdd(3,4);
o = o + FAdd(3,4); // o = 21
component A1 = Add(2);
A1.in[0] <== in;
A1.in[1] <== o;
component A2 = Add(2);
A2.in[0] <== A1.out;
A2.in[1] <== o;
out <== A2.out; // in + 42
}
component main = Main();

Loading…
Cancel
Save