iden3 / circom_old

Circuit compiler for zkSNARKs
GNU General Public License v3.0
471 stars 82 forks source link

Problems compiling sha256.circom from circomlib #44

Open benjaminstrasser opened 4 years ago

benjaminstrasser commented 4 years ago

I downloaded circom via npm i -g circom

I downloaded circomlib via npm i circomlib

After compiling this piece of code I encounter a problem.

include "../node_modules/circomlib/circuits/sha256/sha256.circom";

template Test(nBits) {
  component sha = Sha256(nBits);
}

component main = Test(512);

I encounter this problem

$ circom ./circom/circuit.circom -o ./circom/circuit.json

<--- Last few GCs --->

[27420:0000018859B33170]    41799 ms: Mark-sweep 1142.9 (1440.6) -> 1135.6 (1440.6) MB, 525.8 / 0.0 ms  (average mu = 0.179, current mu = 0.186) allocation failure scavenge might not succeed
[27420:0000018859B33170]    42734 ms: Mark-sweep 1147.4 (1442.6) -> 1137.9 (1443.1) MB, 880.6 / 0.0 ms  (average mu = 0.106, current mu = 0.058) allocation failure scavenge might not succeed

<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 00000231FA45C5C1]
    1: StubFrame [pc: 00000231FA43B79B]
Security context: 0x00be6201e6e1 <JSObject>
    2: substitute [0000036CC07C4571] [C:\Users\bstrasser\AppData\Roaming\npm\node_modules\circom\src\lcalgebra.js:~475] [pc=00000231FA4728C1](this=0x026fba89ad11 <JSGlobal Object>,where=0x03c41880b929 <Object map = 000000087A2733D9>,signal=0x0179ec812a39 <Strin
g[44]: main.sha.sha256compression[1].sume[5].out[0]>,equi...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF6FC210EFA v8::internal::GCIdleTimeHandler::GCIdleTimeHandler+4810
 2: 00007FF6FC1EA296 node::MakeCallback+4518
 3: 00007FF6FC1EAC80 node_module_register+2160
 4: 00007FF6FC4809BE v8::internal::FatalProcessOutOfMemory+846
 5: 00007FF6FC4808EF v8::internal::FatalProcessOutOfMemory+639
 6: 00007FF6FC9BE954 v8::internal::Heap::MaxHeapGrowingFactor+11476
 7: 00007FF6FC9B50B7 v8::internal::ScavengeJob::operator=+25543
 8: 00007FF6FC9B362C v8::internal::ScavengeJob::operator=+18748
 9: 00007FF6FC9BC5A7 v8::internal::Heap::MaxHeapGrowingFactor+2343
10: 00007FF6FC9BC626 v8::internal::Heap::MaxHeapGrowingFactor+2470
11: 00007FF6FC55F0CB v8::internal::Factory::AllocateRawWithImmortalMap+59
12: 00007FF6FC5670D0 v8::internal::Factory::NewBigInt+64
13: 00007FF6FC4E25A7 v8::internal::BigInt::InplaceMultiplyAdd+775
14: 00007FF6FC4DFB5C v8::internal::BigInt::Remainder+348
15: 00007FF6FC6D167D v8::internal::wasm::DecodeCustomSections+88685
16: 00000231FA45C5C1

When I try to compile the Sha256 with 1 bit It doesnt throw the error but creates an unfinised json

   ...
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.a[25]": 4255,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.a[26]": 4256,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.a[27]": 4257,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.a[28]": 4258,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.a[29]": 4259,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.a[30]": 4260,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.a[31]": 4261,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.b[0]": 4264,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.b[1]": 4265,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.b[2]": 4266,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.b[3]": 4267,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.b[4]": 4268,
  "main.sha256compression[0].sigmaPlus[4].sigma1.xor3.b[5]": 4269,
  "mai

Any suggestions what could cause this problem ?

Fattah-Saqib commented 4 years ago

Hi benjaminstrasser I am also using the sha256.circom file and compiled using circom circuit.circom --r1cs --wasm --sym my circuit file:

include "sha256.circom";

template x() {
    signal private input in[256];
    signal output out[256];

    component H2 = Sha256(256);

    for (var v = 0; v< 256; v++) {
            H2.in[v] <== in[v]; 
    }

    for (var k = 0; k< 256; k++) {
            out[k] <== H2.out[k];
    } 

}
component main = x();

But I am unable to generate the hash output & it doesn't work when I put letters as input. Any solution?

TheFrozenFire commented 2 years ago

Compiling the SHA256 circuit is very memory-intensive. In order to get it to compile, I had to bump up the max_old_space_size option to 8GB in node.

If invoking circom with npx, you'd do this: npx --node-arg=--max-old-space-size=8000 circom circuits/sha256.circom

If invoking the globally installed version, you'd do something like: node --max-old-space-size=8000 $(which circom) circuits/sha256.circom

For posterity, at some point the --node-arg parameter to npx will be deprecated in favour of --node-options.