Zkproof CLI Guide

Prerequisites

The snarkjs CLI tool is required. You can install it from this repository.

To install, use the following command:

npm install -g snarkjs@latest.

After installation, you can test it by typing snarkjs --help.

Preprocessing Text

To generate a proof two files are needed.

The original text and the subtext of the original text that you want to prove.

Both text files must have a length of 15872 symbols. If your original text is smaller, you must add zero byte symbols (\0x00) to fill it up to 15872. For the subtext, you must fill it with \0x00 up to 15872 like the original text, except you must change symbols that you want to hide with zero byte symbols (\0x00).

After that, you need to process the text to pass it to snarkjs. Convert the text and subtext into arrays containing BigInt (JavaScript) representation of text chunks (each chunk has a size of 31 bytes). After these transformations, create an input.json file with the following format:

{
    "text": [...],
    "subtext": [...]
}

Here is example code that generates processed text

function convertToByteArray(strToConvert) {
    let byte_arr = [];
    for (let i = 0; i < strToConvert.length; i++) {
        let code = strToConvert.charCodeAt(i);
        byte_arr.push(code);
    }
    return byte_arr
}

function byteArrayToBigInt(byteArray) {
    return BigInt('0x' + Array.from(byteArray, function (byte) {
        return ('0' + (byte & 0xFF).toString(16)).slice(-2);
    }).join(''));
}


function textToBigIntChunks(text) {
    text = text.substr(0, 15872);

    const textBytes = convertToByteArray(text)
    const paddedBytes = new Uint8Array(15872);
    paddedBytes.set(textBytes);

    const chunkSize = 31;
    const chunks = [];
    for (let i = 0; i < paddedBytes.length; i += chunkSize) {
        const chunk = paddedBytes.subarray(i, i + chunkSize);
        const chunkBigInt = byteArrayToBigInt(chunk);
        chunks.push(chunkBigInt);
    }

    return chunks;

}

Generating Proof

To generate proof generation you need to call the snarkjs groth16 fullprove CLI command with the following files that you can download from our repository:

  • input.json: your input file (preprocessed text and subtext in JSON file format)

  • circuit.wasm: file containing circuits compiled in WASM format (you can find this file in our repository)

  • circuit_final.zkey: file containing verification keys (you can find this file in our repository)

This command creates two files: proof.json and public.json.

  • proof.json contains the actual proof

  • public.json contains the values of the public inputs and output

Please save proof.json and public.json, because without these files, you cannot verify the proof!

Verifying Proof

For verification three files are needed - verification_key.json (located in our repository), public.json and proof.json that you get from Generating Proof.

To verify proof you must write the following command:

snarkjs groth16 verify verification_key.json public.json proof.json

If all is well, you should see that OK has been outputted to your console. This signifies the proof is valid.

Last updated