Factor more potentially offline operations out of JS code

main
bzp99 2022-12-07 02:51:52 +01:00
parent 86f5bae9b5
commit 69cfe72035
Signed by: bp99
GPG Key ID: D9C8409092ED9C40
4 changed files with 53 additions and 23 deletions

View File

@ -1,6 +1,13 @@
import sha256 from 'crypto-js/sha256.js';
import { initialize } from 'zokrates-js';
window.addEventListener('load', () => {
document.getElementById('submitFlagBtn')
.addEventListener('click', () => {
submitFlag(document.getElementById('flagbox').value);
});
});
const ZEROSTR = '0';
/* Converts a string into a stringified hexadecimal number */
@ -54,25 +61,15 @@ function expand_number(number) {
return [...Array(8 - parts.length).fill(ZEROSTR), ...parts];
}
/* Hard coded zokrates program source code */
const zokSrc = `
import "hashes/sha256/sha256Padded.zok" as sha256;
from "utils/casts.zok" import cast;
def main(public u32[8] hash,public u32[5] address,private u8[64] flag){
u8[20] addr8 = cast(address);
u32[8] genHash = sha256(flag);
log("Hash: {} {} {} {} {} {} {} {}",genHash[0],genHash[1],genHash[2],genHash[3],genHash[4],genHash[5],genHash[6],genHash[7]);
assert(genHash == hash);
return;
}
`;
const proving_key = await (await fetch('/zok_proving.key')).text();
const program = await (await fetch('/zok_program')).arrayBuffer();
const abi = await (await fetch('/zok_abi.json')).json();
/* Get the proving key from the local server */
const PROVING_KEY_URI = '/proving.key';
const PROVING_KEY_URI = 'http://localhost:8080/proving.key';
const proving_key = await (await fetch(PROVING_KEY_URI)).text();
const artefacts = { program: new Uint8Array(program), abi: abi };
console.log(artefacts);
function submitFlag(flag) {
initialize().then((defaultProvider) => {
@ -81,8 +78,6 @@ function submitFlag(flag) {
scheme: 'gm17',
});
const artefacts = zokProvider.compile(zokSrc);
const flag_ascii = str2asciiarr(flag);
const flag_ascii_padded = flag_ascii.concat(new Array(64 - flag_ascii.length).fill(0));
const flag_padded = asciiarr2str(flag_ascii_padded);
@ -96,15 +91,13 @@ function submitFlag(flag) {
const addr_split = map_0xprefix(addr.match(/.{1,8}/g));
// witness computation
console.log('witness');
const { witness, output } = zokProvider.computeWitness(artefacts, [hash_split, addr_split, flag_split_padded]);
// generate proof
console.log('proof');
const proof = zokProvider.generateProof(artefacts.program, witness, proving_key);
console.log(proof);
});
}
document.getElementById('submitFlagBtn').addEventListener('click', () => {
submitFlag(document.getElementById('flagbox').value);
});
};

View File

@ -0,0 +1,37 @@
{
"inputs": [
{
"name": "hash",
"public": true,
"type": "array",
"components": {
"size": 8,
"type": "u32"
}
},
{
"name": "address",
"public": true,
"type": "array",
"components": {
"size": 5,
"type": "u32"
}
},
{
"name": "flag",
"public": false,
"type": "array",
"components": {
"size": 64,
"type": "u8"
}
}
],
"output": {
"type": "tuple",
"components": {
"elements": []
}
}
}

Binary file not shown.