Factor more potentially offline operations out of JS code
parent
86f5bae9b5
commit
69cfe72035
|
@ -1,6 +1,13 @@
|
||||||
import sha256 from 'crypto-js/sha256.js';
|
import sha256 from 'crypto-js/sha256.js';
|
||||||
import { initialize } from 'zokrates-js';
|
import { initialize } from 'zokrates-js';
|
||||||
|
|
||||||
|
window.addEventListener('load', () => {
|
||||||
|
document.getElementById('submitFlagBtn')
|
||||||
|
.addEventListener('click', () => {
|
||||||
|
submitFlag(document.getElementById('flagbox').value);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
const ZEROSTR = '0';
|
const ZEROSTR = '0';
|
||||||
|
|
||||||
/* Converts a string into a stringified hexadecimal number */
|
/* Converts a string into a stringified hexadecimal number */
|
||||||
|
@ -54,25 +61,15 @@ function expand_number(number) {
|
||||||
return [...Array(8 - parts.length).fill(ZEROSTR), ...parts];
|
return [...Array(8 - parts.length).fill(ZEROSTR), ...parts];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const proving_key = await (await fetch('/zok_proving.key')).text();
|
||||||
/* Hard coded zokrates program source code */
|
const program = await (await fetch('/zok_program')).arrayBuffer();
|
||||||
const zokSrc = `
|
const abi = await (await fetch('/zok_abi.json')).json();
|
||||||
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;
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
|
|
||||||
/* Get the proving key from the local server */
|
/* 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 proving_key = await (await fetch(PROVING_KEY_URI)).text();
|
||||||
|
const artefacts = { program: new Uint8Array(program), abi: abi };
|
||||||
|
console.log(artefacts);
|
||||||
|
|
||||||
function submitFlag(flag) {
|
function submitFlag(flag) {
|
||||||
initialize().then((defaultProvider) => {
|
initialize().then((defaultProvider) => {
|
||||||
|
@ -81,8 +78,6 @@ function submitFlag(flag) {
|
||||||
scheme: 'gm17',
|
scheme: 'gm17',
|
||||||
});
|
});
|
||||||
|
|
||||||
const artefacts = zokProvider.compile(zokSrc);
|
|
||||||
|
|
||||||
const flag_ascii = str2asciiarr(flag);
|
const flag_ascii = str2asciiarr(flag);
|
||||||
const flag_ascii_padded = flag_ascii.concat(new Array(64 - flag_ascii.length).fill(0));
|
const flag_ascii_padded = flag_ascii.concat(new Array(64 - flag_ascii.length).fill(0));
|
||||||
const flag_padded = asciiarr2str(flag_ascii_padded);
|
const flag_padded = asciiarr2str(flag_ascii_padded);
|
||||||
|
@ -96,15 +91,13 @@ function submitFlag(flag) {
|
||||||
const addr_split = map_0xprefix(addr.match(/.{1,8}/g));
|
const addr_split = map_0xprefix(addr.match(/.{1,8}/g));
|
||||||
|
|
||||||
// witness computation
|
// witness computation
|
||||||
|
console.log('witness');
|
||||||
const { witness, output } = zokProvider.computeWitness(artefacts, [hash_split, addr_split, flag_split_padded]);
|
const { witness, output } = zokProvider.computeWitness(artefacts, [hash_split, addr_split, flag_split_padded]);
|
||||||
|
|
||||||
// generate proof
|
// generate proof
|
||||||
|
console.log('proof');
|
||||||
const proof = zokProvider.generateProof(artefacts.program, witness, proving_key);
|
const proof = zokProvider.generateProof(artefacts.program, witness, proving_key);
|
||||||
|
|
||||||
console.log(proof);
|
console.log(proof);
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
document.getElementById('submitFlagBtn').addEventListener('click', () => {
|
|
||||||
submitFlag(document.getElementById('flagbox').value);
|
|
||||||
});
|
|
||||||
|
|
|
@ -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.
Loading…
Reference in New Issue