2023-04-13 12:32:29 +00:00
|
|
|
/**
|
|
|
|
* CSPRNG Fisher-Yates shuffle.
|
|
|
|
*
|
|
|
|
* Only works on lists up to 255 elements.
|
|
|
|
*/
|
2023-04-10 18:05:10 +00:00
|
|
|
function cryptoShuffle(l) {
|
2023-04-13 15:33:32 +00:00
|
|
|
let out = [];
|
2023-04-10 21:23:06 +00:00
|
|
|
for (let i = l.length - 1; i > 0; i--) {
|
|
|
|
let value = new Uint8Array([0]);
|
|
|
|
crypto.getRandomValues(value);
|
|
|
|
while (value[0] > i) {
|
|
|
|
crypto.getRandomValues(value);
|
|
|
|
}
|
|
|
|
|
2023-04-13 15:33:32 +00:00
|
|
|
let v = l.splice(value[0], 1);
|
|
|
|
out.push(v[0]);
|
2023-04-10 21:23:06 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 15:33:32 +00:00
|
|
|
out.push(l[0]);
|
|
|
|
|
|
|
|
return out;
|
2023-04-10 18:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.cryptoShuffle = cryptoShuffle;
|
|
|
|
|
|
|
|
function proveRegions(regions) {
|
|
|
|
// Construct prover coins
|
2023-04-10 21:23:06 +00:00
|
|
|
let coins = [];
|
|
|
|
|
2023-04-13 15:33:32 +00:00
|
|
|
let regionNames = Object.keys(regions);
|
|
|
|
for (let x = 0; x < 40; x++) {
|
|
|
|
let psi = cryptoShuffle(structuredClone(regionNames)).join("");
|
|
|
|
let newRegions = structuredClone(regions);
|
|
|
|
// rearrange keys
|
|
|
|
for (let index = 0; index < regionNames.length; index++) {
|
|
|
|
let c = regions[psi[index]].clone();
|
|
|
|
// re-blind
|
|
|
|
c.update(c.pubKey.encrypt(0n));
|
|
|
|
newRegions[regionNames[index]] = c.toString();
|
|
|
|
}
|
|
|
|
coins.push(newRegions);
|
2023-04-10 21:23:06 +00:00
|
|
|
}
|
2023-04-10 18:05:10 +00:00
|
|
|
|
|
|
|
// Construct verifier coins
|
|
|
|
let hasher = new jsSHA("SHA3-256", "TEXT");
|
2023-04-13 15:33:32 +00:00
|
|
|
hasher.update(JSON.stringify(coins));
|
|
|
|
|
|
|
|
console.log(hasher.getHash("UINT8ARRAY"));
|
2023-04-10 18:05:10 +00:00
|
|
|
|
|
|
|
// Construct prover proofs
|
|
|
|
}
|
2023-04-13 15:33:32 +00:00
|
|
|
|
|
|
|
window.proveRegions = proveRegions;
|