fluidex / snarkit

A toolkit to compile and debug circom circuit.
23 stars 1 forks source link

feat: change behavior of 'false' option for '--force_recompile' #12

Closed lispc closed 3 years ago

lispc commented 3 years ago

Now, when force_recompile is false, we only check whether output of compiling exists. We should check whether the src files are same. https://github.com/Fluidex/snarkit/blob/cc4289e5354239ccfd8fdb2dee705dc09e00450b/src/witness_generator.ts#L129

When a circom src file and all the included src files don't change, we can skip recompiling.

maybe we can calculate hash for every src file, and then write to a file called circom.sum or circom.lock ? (inspired by go.sum / yarn.lock )

lispc commented 3 years ago
function checkSrcChanged(src) : bool {
  const circomDir = require.resolve('circom');
  const parser = require(path.join(circomDir, '..', 'parser/jaz.js')).parser;
  const srcContents = new Map<string, string>();
  traverse(src);
  const srcHashes = calculateSrcHashes(srcContents);
  const oldSrcHashes = loadOldSrcHashes();
  if (oldSrcHashes == null || !isEqual(srcHashes, oldSrcHashes)) {
    writeSrcHashes(srcContents);
    return true;
  } else {
    return false;
  }
  function traverse(src) {
    const content = fs.readFileSync(src, 'utf8');
    srcContents.set(src, content);
    const ast = parser.parse(content);
    for (const stat of ast.statements) {
      if (stat.type == 'INCLUDE') {
        const includedFile = path.normalize(path.join(src, '..', stat.file));
        if (!srcContents.has(includedFile)) {
          traverse(includedFile);
        }
      }
    }
  }
}

and change

if (!alwaysRecompile && fs.existsSync(binaryFilePath) && fs.statSync(binaryFilePath).size > 0) {

to

if (!alwaysRecompile && fs.existsSync(binaryFilePath) && fs.statSync(binaryFilePath).size > 0) && checkSrcChanged() == false )