Fix some stuff and sort of implement proving

This commit is contained in:
jude
2023-03-24 12:41:54 +00:00
parent 6de13d3b70
commit 07b1080b3d
7 changed files with 137 additions and 21 deletions

View File

@ -157,6 +157,17 @@ document.addEventListener("ACT", async (ev) => {
}
});
// todo has to filter by player
document.addEventListener("PROOF", async (ev) => {
const data = ev.detail;
if (data.stage === "CONJECTURE") {
// find the relevant entity
let region = Region.getRegion(data.region);
region.prove(data.plainText, data.noise());
}
});
document.addEventListener("endTurn", () => {
if (game.isPregame() && game.allReinforcementsPlaced()) {
game.incrementState();

View File

@ -1,3 +1,5 @@
import { Packet } from "./packet.js";
const REGIONS = {};
class Continent {
@ -22,6 +24,71 @@ class Strength {
this.assumedStrength = null;
}
prove(region) {
if (this.cipherText.readOnly) {
return;
}
const controller = new AbortController();
let proofSessionProver = this.cipherText.prove();
document.addEventListener(
"PROOF",
(ev) => {
const data = ev.detail;
if (data.region === region && data.stage === "CHALLENGE") {
let z = proofSessionProver.prove(data.challenge);
socket.emit("message", Packet.createProof(region, z));
controller.abort();
}
},
{ signal: controller.signal }
);
socket.emit(
"message",
Packet.createProofConjecture(
region,
this.cipherText.plainText,
proofSessionProver.a
)
);
}
verify(region, plainText, a) {
if (!this.cipherText.readOnly) {
return;
}
const controller = new AbortController();
let proofSessionVerifier = this.cipherText.prove(plainText, a);
document.addEventListener(
"PROOF",
(ev) => {
const data = ev.detail;
if (data.region === region && data.stage === "PROOF") {
if (proofSessionVerifier.verify(data.z)) {
console.log("verified");
this.assumedStrength = plainText;
controller.abort();
} else {
console.warn("Failed to verify ciphertext!");
}
}
},
{ signal: controller.signal }
);
socket.emit(
"message",
Packet.createProofChallenge(region, proofSessionVerifier.challenge)
);
}
}
export class Region {
@ -72,13 +139,19 @@ export class Region {
if (this.owner === null) {
return "";
} else if (!this.strength.cipherText.readOnly) {
return this.strength.cipherText.plainText;
return this.strength.cipherText.plainText.toString();
} else if (this.strength.assumedStrength !== null) {
return `${this.strength.assumedStrength}`;
return this.strength.assumedStrength.toString();
} else {
return "?";
}
}
prove() {}
verify(plainText, a) {
this.strength.verify(this.name, plainText, a);
}
}
const EAST = new Continent("East");

View File

@ -1,4 +1,4 @@
import { ID, game } from "./main.js";
import { ID } from "./main.js";
export class Packet {
static _createBase(name) {
@ -85,10 +85,10 @@ export class Packet {
});
}
static createReinforce(regionCyphertexts) {
static createReinforce(regionCipherTexts) {
return this._sign({
...this._createBase("ACT"),
regions: regionCyphertexts,
regions: regionCipherTexts,
});
}
@ -116,4 +116,32 @@ export class Packet {
action: "END",
});
}
static createProofConjecture(region, plainText, a) {
return this._sign({
...this._createBase("PROOF"),
stage: "CONJECTURE",
plainText: plainText,
a: a,
region: region,
});
}
static createProofChallenge(region, challenge) {
return this._sign({
...this._createBase("PROOF"),
stage: "CHALLENGE",
challenge: challenge,
region: region,
});
}
static createProof(region, z) {
return this._sign({
...this._createBase("PROOF"),
stage: "PROOF",
z: z,
region: region,
});
}
}

View File

@ -15,7 +15,7 @@ const PHASE_FORTIFY = 3;
let totalDice = 0;
export class Player {
constructor(id, local, rsa_key, paillier_key) {
constructor(id, local, rsaKey, paillierKey) {
// Game state
this.totalStrength = 0;
this.ready = false;
@ -23,8 +23,8 @@ export class Player {
// Protocol state
this.timeout = null;
this.id = id;
this.rsaPubKey = RsaPubKey.fromJSON(rsa_key);
this.paillierPubKey = PaillierPubKey.fromJSON(paillier_key);
this.rsaPubKey = RsaPubKey.fromJSON(rsaKey);
this.paillierPubKey = PaillierPubKey.fromJSON(paillierKey);
this.lastPacket = 0;
// Data which is reset every turn
@ -88,7 +88,7 @@ export class Player {
if (region.owner === null) {
region.claim(
this,
new ReadOnlyCyphertext(this.paillierPubKey, data.cipherText)
new ReadOnlyCyphertext(this.paillierPubKey, BigInt(data.cipherText))
);
this.totalStrength += 1;
@ -111,7 +111,12 @@ export class Player {
let region = Region.getRegion(regionName);
if (region.owner === this) {
region.reinforce(BigInt(data.regions[regionName]));
region.reinforce(
new ReadOnlyCyphertext(
this.paillierPubKey,
BigInt(data.regions[regionName])
)
);
}
}