Skip to content

Commit

Permalink
Merge pull request #18 from EYBlockchain/willkim/verbose-flag
Browse files Browse the repository at this point in the history
Add verbose flag
  • Loading branch information
Will Kim authored Nov 1, 2019
2 parents 17efe04 + 10ea92c commit 61b4541
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 13 deletions.
7 changes: 7 additions & 0 deletions lib/__tests__/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ it('should create the given output file', async () => {
await compile('./code/test.code', './code/', 'test-compiled');
expect(fs.existsSync('./code/test-compiled.code')).toBe(true);
});

it('should return a string given a verbose flag', async () => {
const output = await compile('./code/test.code', './code/', 'test-compiled', {
verbose: true,
});
expect(typeof output).toBe('string');
});
7 changes: 7 additions & 0 deletions lib/__tests__/compute-witness.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ it('should create a file called "witness" if no argument is given in outputName'
expect(fs.existsSync('./code/witness')).toBe(true);
deleteFile('./code/witness');
});

it('should return a string given a verbose flag', async () => {
const output = await computeWitness('./code/test-compiled', './code/', undefined, [5, 25], {
verbose: true,
});
expect(typeof output).toBe('string');
});
7 changes: 7 additions & 0 deletions lib/__tests__/export-verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ it('should create a file called "Verifier.sol" if no argument is given in output
expect(fs.existsSync('./code/Verifier.sol')).toBe(true);
deleteFile('./code/Verifier.sol');
});

it('should return a string given a verbose flag', async () => {
const output = await exportVerifier('./code/test-vk.key', './code/', undefined, 'gm17', {
verbose: true,
});
expect(typeof output).toBe('string');
});
7 changes: 7 additions & 0 deletions lib/__tests__/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ it('should allow G16, PGHR13, GM17 proving schemes', async () => {
expect(fs.existsSync(vkPath) && fs.existsSync(pkPath)).toBe(true);
}
});

it('should return a string given a verbose flag', async () => {
const output = await setup('./code/test-compiled', './code/', 'gm17', undefined, undefined, {
verbose: true,
});
expect(typeof output).toBe('string');
});
23 changes: 20 additions & 3 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const { spawn } = childProcess;
* @param {String} [outputPath=./] - Directory to output, defaults to current directory
* @param {String} [outputName=out] - name of `.code` and `out` files. Defaults to out.
*/
async function compile(codePath, outputPath = './', outputName = 'out') {
async function compile(codePath, outputPath = './', outputName = 'out', options = {}) {
const { maxReturn = 10000000, verbose = false } = options;
if (!fs.existsSync(codePath)) {
throw new Error('Compile input file(s) not found');
}
Expand All @@ -29,19 +30,35 @@ async function compile(codePath, outputPath = './', outputName = 'out') {
'/app/zokrates',
['compile', '-i', codePath, '-o', `${parsedOutputPath}${parsedOutputName}`],
{
stdio: ['ignore', 'ignore', 'pipe'],
stdio: ['ignore', 'pipe', 'pipe'],
env: {
ZOKRATES_HOME: '/app/stdlib',
},
},
);

let output = '';

zokrates.stdout.on('data', data => {
if (verbose) {
output += data.toString('utf8');
// If the entire output gets too large, just send ...[truncated].
if (output.length > maxReturn) output = '...[truncated]';
}
});

zokrates.stderr.on('data', err => {
reject(new Error(`Compile failed: ${err}`));
});

zokrates.on('close', () => {
resolve();
// ZoKrates sometimes outputs error through stdout instead of stderr,
// so we need to catch those errors manually.
if (output.includes('panicked')) {
reject(new Error(output.slice(output.indexOf('panicked'))));
}
if (verbose) resolve(output);
else resolve();
});
});
}
Expand Down
30 changes: 27 additions & 3 deletions lib/compute-witness.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ const { spawn } = childProcess;
* @param {String} [outputName=witness] - Name of file to output
* @param {Array} arguments to pass to compute witness (flag -a)
*/
async function computeWitness(codePath, outputDirectory = './', outputName = 'witness', args) {
async function computeWitness(
codePath,
outputDirectory = './',
outputName = 'witness',
args,
options = {},
) {
const { maxReturn = 10000000, verbose = false } = options;

if (!fs.existsSync(codePath)) {
throw new Error('compute-witness codePath input file(s) not found');
}
Expand All @@ -37,19 +45,35 @@ async function computeWitness(codePath, outputDirectory = './', outputName = 'wi
'/app/zokrates',
['compute-witness', '-i', codePath, '-o', `${parsedOutputPath}${outputName}`, '-a', ...args],
{
stdio: ['ignore', 'ignore', 'pipe'],
stdio: ['ignore', 'pipe', 'pipe'],
env: {
ZOKRATES_HOME: '/app/stdlib',
},
},
);

let output = '';

zokrates.stdout.on('data', data => {
if (verbose) {
output += data.toString('utf8');
// If the entire output gets too large, just send ...[truncated].
if (output.length > maxReturn) output = '...[truncated]';
}
});

zokrates.stderr.on('data', err => {
reject(new Error(`Compute witness failed: ${err}`));
});

zokrates.on('close', () => {
resolve();
// ZoKrates sometimes outputs error through stdout instead of stderr,
// so we need to catch those errors manually.
if (output.includes('panicked')) {
reject(new Error(output.slice(output.indexOf('panicked'))));
}
if (verbose) resolve(output);
else resolve();
});
});
}
Expand Down
23 changes: 21 additions & 2 deletions lib/export-verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ async function exportVerifier(
outputPath = './',
outputName = 'Verifier.sol',
provingScheme,
options = {},
) {
const { maxReturn = 10000000, verbose = false } = options;

if (!fs.existsSync(codePath)) {
throw new Error('export-verifier verifying key file not found');
}
Expand Down Expand Up @@ -49,19 +52,35 @@ async function exportVerifier(
provingScheme,
],
{
stdio: ['ignore', 'ignore', 'pipe'],
stdio: ['ignore', 'pipe', 'pipe'],
env: {
ZOKRATES_HOME: '/app/stdlib',
},
},
);

let output = '';

zokrates.stdout.on('data', data => {
if (verbose) {
output += data.toString('utf8');
// If the entire output gets too large, just send ...[truncated].
if (output.length > maxReturn) output = '...[truncated]';
}
});

zokrates.stderr.on('data', err => {
reject(new Error(`Export verifier failed: ${err}`));
});

zokrates.on('close', () => {
resolve();
// ZoKrates sometimes outputs error through stdout instead of stderr,
// so we need to catch those errors manually.
if (output.includes('panicked')) {
reject(new Error(output.slice(output.indexOf('panicked'))));
}
if (verbose) resolve(output);
else resolve();
});
});
}
Expand Down
5 changes: 2 additions & 3 deletions lib/generate-proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { spawn } = childProcess;
* @param {String} [options.fileName=proof.json] - Name of JSON proof file ()
* @returns {Object} JSON of the proof.
*/
async function generateProof(provingKeyPath, codePath, witnessPath, provingScheme, options) {
async function generateProof(provingKeyPath, codePath, witnessPath, provingScheme, options = {}) {
if (!fs.existsSync(codePath)) {
throw new Error('generate-proof codePath input file(s) not found');
}
Expand Down Expand Up @@ -73,8 +73,6 @@ async function generateProof(provingKeyPath, codePath, witnessPath, provingSchem
args.push(`${parsedOutputPath}${parsedFileName}`);
}

// TODO: I don't believe this actually returns anything,
// need to return to this when I can test off the Nightfall repo.
return new Promise((resolve, reject) => {
const zokrates = spawn('/app/zokrates', args, {
stdio: ['ignore', 'ignore', 'pipe'],
Expand All @@ -88,6 +86,7 @@ async function generateProof(provingKeyPath, codePath, witnessPath, provingSchem
});

zokrates.on('close', () => {
// Generate-proof doesn't seem to have any output, so we're not doing the same check as the other functions.
resolve();
});
});
Expand Down
23 changes: 21 additions & 2 deletions lib/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ async function setup(
provingScheme,
vkName = 'verification.key',
pkName = 'proving.key',
options = {},
) {
const { maxReturn = 10000000, verbose = false } = options;

if (!fs.existsSync(codePath)) {
throw new Error('Setup input file(s) not found');
}
Expand All @@ -50,19 +53,35 @@ async function setup(
'/app/zokrates',
['setup', '-i', codePath, '-s', provingScheme, '-v', vkPath, '-p', pkPath],
{
stdio: ['ignore', 'ignore', 'pipe'],
stdio: ['ignore', 'pipe', 'pipe'],
env: {
ZOKRATES_HOME: '/app/stdlib',
},
},
);

let output = '';

zokrates.stdout.on('data', data => {
if (verbose) {
output += data.toString('utf8');
// If the entire output gets too large, just send ...[truncated].
if (output.length > maxReturn) output = '...[truncated]';
}
});

zokrates.stderr.on('data', err => {
reject(new Error(`Setup failed: ${err}`));
});

zokrates.on('close', () => {
resolve();
// ZoKrates sometimes outputs error through stdout instead of stderr,
// so we need to catch those errors manually.
if (output.includes('panicked')) {
reject(new Error(output.slice(output.indexOf('panicked'))));
}
if (verbose) resolve(output);
else resolve();
});
});
}
Expand Down

0 comments on commit 61b4541

Please sign in to comment.