This commit is contained in:
2023-07-07 20:25:41 -04:00
parent 98ab6fa73e
commit d8b05eb214
9 changed files with 900 additions and 78 deletions

880
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -12,6 +12,9 @@
"packages/*"
],
"dependencies": {
"@semaphore-protocol/group": "^3.10.1",
"@semaphore-protocol/identity": "^3.10.1",
"@semaphore-protocol/proof": "^3.10.1",
"typescript": "^5.1.3"
},
"devDependencies": {
@@ -20,4 +23,4 @@
"rlnjs": "^3.1.3",
"ts-node": "^10.9.1"
}
}
}

View File

@@ -33,6 +33,10 @@
},
"type": "module",
"dependencies": {
"@semaphore-protocol/group": "^3.10.1",
"@zk-kit/incremental-merkle-tree": "^1.1.0",
"circomlibjs": "^0.1.7",
"poseidon-lite": "^0.2.0",
"socket.io-client": "^4.7.1"
}
}

View File

@@ -1,6 +1,21 @@
import { writable } from 'svelte/store';
import { browser } from '$app/environment';
// This is just a list of endpoints of servers to connect to, no other information, this is mainly for bootstraping the app
export const serverListStore = storable(['http://localhost:3001/api/'], 'servers');
// This is what gets populated after querying the serverListStore, with the server's information, public rooms available, etc.
export const serverDataStore = storable([], 'serverData');
// JUST an index to the serverDataStore, so we can keep track of which server we're currently connected to
export const selectedServer = storable({}, 'selectedServer');
// Session store (removed after the session is cleared) of the last 500 messages or so of each room the user participates in; rooms they don't have selected will not be updated
export const messageStore = sessionable({}, 'messages');
// Stores the user's identity // TODO THIS NEEDS TO BE AN ENCRYPTED SEMAPHORE IDENTITY IN THE FUTURE
export const identityStore = storable([], 'identity');
export function storable(data: any, storagePath = 'storable') {
const store = writable(data);
const { subscribe, set, update } = store;
@@ -44,18 +59,3 @@ export function sessionable(data: any, storagePath = 'storable') {
}
};
}
// This is just a list of endpoints of servers to connect to, no other information, this is mainly for bootstraping the app
export const serverListStore = storable(['http://localhost:3001/api/'], 'servers');
// This is what gets populated after querying the serverListStore, with the server's information, public rooms available, etc.
export const serverDataStore = storable([], 'serverData');
// JUST an index to the serverDataStore, so we can keep track of which server we're currently connected to
export const selectedServer = storable({}, 'selectedServer');
// Session store (removed after the session is cleared) of the last 500 messages or so of each room the user participates in; rooms they don't have selected will not be updated
export const messageStore = sessionable({}, 'messages');
// Stores the user's identity // TODO THIS NEEDS TO BE AN ENCRYPTED SEMAPHORE IDENTITY IN THE FUTURE
export const identityStore = storable([], 'identity');

View File

@@ -1,3 +1,44 @@
import { RLNProver, type RLNFullProof, type MerkleProof } from 'rlnjs';
import { randomBigInt, genId } from '../../../protocol-interfaces/src/utils';
import { poseidon1 as poseidon } from 'poseidon-lite/poseidon1';
import { Group } from '@semaphore-protocol/group';
import type { Identity } from '@semaphore-protocol/identity';
import type { MessageI, RoomI } from './types';
export { randomBigInt, genId };
const prover: RLNProver = new RLNProver('/rln.wasm', '/rln_final.zkey');
interface proofInputsI {
rlnIdentifier: bigint;
identitySecret: bigint;
userMessageLimit: bigint;
messageId: bigint;
merkleProof: MerkleProof;
x: bigint;
epoch: bigint;
}
async function genProof(room: RoomI, message: string, identity: Identity): Promise<MessageI> {
const messageHash: bigint = poseidon([message]);
const group = new Group(room.id, 20, room.membership?.identityCommitments);
const merkleproof: MerkleProof = await group.getMerkleProof(identity.getCommitment());
const proofInputs: proofInputsI = {
rlnIdentifier: BigInt(room.id),
identitySecret: identity.getSecret(),
userMessageLimit: 1n,
messageId: 1n,
merkleProof: merkleproof,
x: messageHash,
epoch: BigInt(Date.now().toString())
};
const proof: RLNFullProof = await prover.generateProof(proofInputs);
const msg: MessageI = {
id: proof.snarkProof.publicSignals.nullifier.toString(),
message: message,
room: BigInt(proof.snarkProof.publicSignals.externalNullifier),
proof
};
return msg;
}
export { genProof, randomBigInt, genId };

Binary file not shown.

Binary file not shown.

View File

@@ -6,6 +6,7 @@
import AppFooter from './AppFooter.svelte';
import { identityStore, serverListStore, serverDataStore, selectedServer } from '$lib/stores';
import { randomBigInt } from '$lib/utils';
import { Identity } from '@semaphore-protocol/identity';
(BigInt.prototype as any).toJSON = function () {
return this.toString();
@@ -42,9 +43,7 @@
if ($identityStore.length != 2) {
console.log('MAKING UP SECRETS');
const nullifier = randomBigInt();
const trapdoor = randomBigInt();
$identityStore = [nullifier, trapdoor];
$identityStore = new Identity();
}
</script>

View File

@@ -1,9 +1,10 @@
<script lang="ts">
import { selectedServer, messageStore, serverDataStore } from '$lib/stores';
import { identityStore, selectedServer, messageStore, serverDataStore } from '$lib/stores';
import type { RoomI, MessageI } from '$lib/types';
import { io } from 'socket.io-client';
import { onDestroy } from 'svelte';
import { RLNProver } from 'rlnjs';
import { prover } from '$lib/utils';
export let room: RoomI;
if (!$messageStore[room.id.toString()]) {
@@ -64,11 +65,7 @@
});
function sendMessage(message: string) {
const msg = {
id: socket.id,
message,
room: room?.id
};
const msg = prover(room, message, $identityStore);
socket.emit('validateMessage', msg);
}
</script>