Skip to content

Commit

Permalink
Add tlsp verifier js code
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSachinK committed Jun 15, 2024
1 parent 0a8f9b1 commit 8a25530
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ tlsn-verifier/target
tlsn-verifier/*.txt
tlsn_verify_outputs/*.txt

tlsp-verifier/node_modules

certs/*.json
1 change: 1 addition & 0 deletions tlsp-verifier/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
index.js
7 changes: 7 additions & 0 deletions tlsp-verifier/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## TLSP Verifier

```bash
tsc
// Usage: node index.js <proofJsonPath> <plaintext> <ciphertext>
node index.js ./proof.json 6f2c2073656375726520776f726c642122f214bbb5707a792158ca27f865ba3411e8cc5006498d3b0eb0de9a7f4d1e7cc3ab1b7cb396b1fca6a42551cffe56d1 077819785017d1cf8f56c111a631a1e4
```
19 changes: 19 additions & 0 deletions tlsp-verifier/fixture/proof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"pi_a":[
"18545182628801057987322180929860160194427518281467181052705937744550551744186",
"19813946850066265863623155441962579533949982611349566587513412386854710426700",
"1"
],
"pi_b":[
["1686061860494429426805613182976845039352849418982687778747463660363828673703","9290420138266975333901557822131144079131850509669186724218628267179553393357"],
["5759199333909836908049087436279210080126869174666078216915174560107012869292","3968458531947138909139570330355313366169116368946167774660423668984013328784"],
["1","0"]
],
"pi_c":[
"20004827253731281773736566563258359423845347003715728002244523839101154631967",
"19439621413090542545966536518110875107983445155158333769982822708623038179416",
"1"
],
"protocol":"groth16",
"curve":"bn128"
}
59 changes: 59 additions & 0 deletions tlsp-verifier/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { verifyProof, makeLocalSnarkJsZkOperator } from '@reclaimprotocol/circom-symmetric-crypto';
import * as fs from 'fs/promises';

function hexToBuffer(hexString: string): Buffer {
// Ensure the hex string has an even length (required for valid byte conversion)
if (hexString.length % 2 !== 0) {
throw new Error("Invalid hex string: length must be even");
}

// Create a Buffer from the hex string
const buffer = Buffer.from(hexString, 'hex');

return buffer;
}


async function verify() {
const argv = process.argv.slice(2);
if (argv.length < 3) {
console.error('Usage: node script.js <proofJsonPath> <plaintext> <ciphertext>');
process.exit(1);
}

const [proofJsonPath, plaintextHex, ciphertextHex] = argv;
console.log(proofJsonPath)

try {
const proofJson = await fs.readFile(proofJsonPath, 'utf8');
console.log(proofJson); // Log the raw string to check its format

const plaintext = hexToBuffer(plaintextHex)
const ciphertext = hexToBuffer(ciphertextHex)

const algorithm = 'chacha20';
const operator = await makeLocalSnarkJsZkOperator(algorithm);

// Will assert the proof is valid; otherwise, it will throw an error
await verifyProof({
proof: {
proofJson,
plaintext,
algorithm
},
// The public inputs to the circuit
publicInput: { ciphertext },
operator
});

console.log('proof verified');
} catch (error) {
console.error('Error during verification:', error);
process.exit(1);
}
}

verify().then(() => console.log('Verification complete')).catch(err => {
console.error('Verification failed:', err);
process.exit(1);
});
14 changes: 14 additions & 0 deletions tlsp-verifier/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "tlsp-verifier",
"version": "1.0.0",
"main": "index.js",
"author": "0xsachink",
"license": "MIT",
"dependencies": {
"@reclaimprotocol/circom-symmetric-crypto": "git+https://gitlab.reclaimprotocol.org/Reclaim/zk-symmetric-crypto",
"snarkjs": "^0.7.4"
},
"devDependencies": {
"@types/node": "^20.14.2"
}
}
Loading

0 comments on commit 8a25530

Please sign in to comment.