this should work

This commit is contained in:
AtHeartEngineer
2023-07-07 23:31:35 -04:00
parent d8b05eb214
commit eb7643c6a3
13 changed files with 80 additions and 81 deletions

33
package-lock.json generated
View File

@@ -15,12 +15,12 @@
"@semaphore-protocol/group": "^3.10.1",
"@semaphore-protocol/identity": "^3.10.1",
"@semaphore-protocol/proof": "^3.10.1",
"rlnjs": "^3.1.3",
"typescript": "^5.1.3"
},
"devDependencies": {
"@types/node": "^20.3.1",
"lerna": "^7.0.2",
"rlnjs": "^3.1.3",
"ts-node": "^10.9.1"
}
},
@@ -38,7 +38,6 @@
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.9.2.tgz",
"integrity": "sha512-0h+FrQDqe2Wn+IIGFkTCd4aAwTJ+7834Ek1COohCyV26AXhwQ7WQaz+4F/nLOeVl/3BtWHOHLPsq46V8YB46Eg==",
"dev": true,
"license": "MIT"
},
"node_modules/@ampproject/remapping": {
@@ -1605,7 +1604,6 @@
"version": "0.0.11",
"resolved": "https://registry.npmjs.org/@iden3/binfileutils/-/binfileutils-0.0.11.tgz",
"integrity": "sha512-LylnJoZ0CTdgErnKY8OxohvW4K+p6UHD3sxt+3P9AmMyBQjYR4IpoqoYZZ+9aMj89cmCQ21UvdhndAx04er3NA==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"fastfile": "0.0.20",
@@ -1616,7 +1614,6 @@
"version": "0.2.59",
"resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.59.tgz",
"integrity": "sha512-QssOEUv+wilz9Sg7Zaj6KWAm7QceOAEsFuEBTltUsDo1cjn11rA/LGYvzFBPbzNfxRlZxwgJ7uxpCQcdDlrNfw==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"wasmbuilder": "0.0.16",
@@ -1628,7 +1625,6 @@
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.2.1.tgz",
"integrity": "sha512-9ciO7bUE5bgpbOcdK7IO3enrSVIKHwrQmPibok4GLJWaCA7Wyqc9PRYnu5HbiFv9NDFNqVKPtU5R6Is5KujBLg==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"wasmbuilder": "0.0.16"
@@ -2090,7 +2086,6 @@
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.1.2.tgz",
"integrity": "sha512-KYRCASVTv6aeUi1tsF8/vpyR7zpfs3FUzy2Jqm+MU+LmUKhQ0y2FpfwqkCcxSg2ua4GALJd8k2R76WxwZGbQpA==",
"dev": true,
"funding": [
{
"type": "individual",
@@ -2103,7 +2098,6 @@
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/@noble/secp256k1/-/secp256k1-1.7.1.tgz",
"integrity": "sha512-hOUk6AyBFmqVrv7k5WAw/LpszxVbj9gGN4JRkIX52fdFAj1UA61KXmZDvqVEm+pOyec3+fIeZB02LYa/pWOArw==",
"dev": true,
"funding": [
{
"type": "individual",
@@ -4981,7 +4975,6 @@
"version": "4.0.0-beta.5",
"resolved": "https://registry.npmjs.org/aes-js/-/aes-js-4.0.0-beta.5.tgz",
"integrity": "sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q==",
"dev": true,
"license": "MIT"
},
"node_modules/agent-base": {
@@ -5533,7 +5526,6 @@
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/bfj/-/bfj-7.0.2.tgz",
"integrity": "sha512-+e/UqUzwmzJamNF50tBV6tZPTORow7gQ96iFow+8b562OdMpEK0BcJEq2OSPEDmAbSMBQ7PKZ87ubFkgxpYWgw==",
"dev": true,
"license": "MIT",
"dependencies": {
"bluebird": "^3.5.5",
@@ -5638,7 +5630,6 @@
"version": "3.7.2",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz",
"integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==",
"dev": true,
"license": "MIT"
},
"node_modules/bn.js": {
@@ -6339,7 +6330,6 @@
"version": "11.2.2",
"resolved": "https://registry.npmjs.org/check-types/-/check-types-11.2.2.tgz",
"integrity": "sha512-HBiYvXvn9Z70Z88XKjz3AEKd4HJhBXsa3j7xFnITAzoS8+q6eIGi8qDB8FKPBAjtuxjI/zFpwuiCb8oDtKOYrA==",
"dev": true,
"license": "MIT"
},
"node_modules/chokidar": {
@@ -6398,7 +6388,6 @@
"version": "0.1.22",
"resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.22.tgz",
"integrity": "sha512-V/XYZWBhbZY8SotkaGH4FbiDYAZ8a1Md++MBiKPDOuWS/NIJB+Q+XIiTC8zKMgoDaa9cd2OiTvsC9J6te7twNg==",
"dev": true,
"license": "Apache-2.0",
"dependencies": {
"ffjavascript": "0.2.57"
@@ -8720,7 +8709,6 @@
"version": "6.6.2",
"resolved": "https://registry.npmjs.org/ethers/-/ethers-6.6.2.tgz",
"integrity": "sha512-vyWfVAj2g7xeZIivOqlbpt7PbS2MzvJkKgsncgn4A/1xZr8Q3BznBmEBRQyPXKCgHmX4PzRQLpnYG7jl/yutMg==",
"dev": true,
"funding": [
{
"type": "individual",
@@ -8749,21 +8737,18 @@
"version": "18.15.13",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.13.tgz",
"integrity": "sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q==",
"dev": true,
"license": "MIT"
},
"node_modules/ethers/node_modules/tslib": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.0.tgz",
"integrity": "sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==",
"dev": true,
"license": "0BSD"
},
"node_modules/ethers/node_modules/ws": {
"version": "8.5.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.5.0.tgz",
"integrity": "sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=10.0.0"
@@ -9117,9 +9102,7 @@
"node_modules/fastfile": {
"version": "0.0.20",
"resolved": "https://registry.npmjs.org/fastfile/-/fastfile-0.0.20.tgz",
"integrity": "sha512-r5ZDbgImvVWCP0lA/cGNgQcZqR+aYdFx3u+CtJqUE510pBUVGMn4ulL/iRTI4tACTYsNJ736uzFxEBXesPAktA==",
"dev": true,
"license": "GPL-3.0"
"integrity": "sha512-r5ZDbgImvVWCP0lA/cGNgQcZqR+aYdFx3u+CtJqUE510pBUVGMn4ulL/iRTI4tACTYsNJ736uzFxEBXesPAktA=="
},
"node_modules/fastq": {
"version": "1.15.0",
@@ -10116,7 +10099,6 @@
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/hoopy/-/hoopy-0.1.4.tgz",
"integrity": "sha512-HRcs+2mr52W0K+x8RzcLzuPPmVIKMSv97RGHy0Ea9y/mpcaK+xTrjICA04KAHi4GRzxliNqNJEFYWHghy3rSfQ==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">= 6.0.0"
@@ -14523,7 +14505,6 @@
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/poseidon-lite/-/poseidon-lite-0.0.2.tgz",
"integrity": "sha512-bGdDPTOQkJbBjbtSEWc3gY+YhqlGTxGlZ8041F8TGGg5QyGGp1Cfs4b8AEnFFjHbkPg6WdWXUgEjU1GKOKWAPw==",
"dev": true,
"license": "GPL-3.0"
},
"node_modules/postcss": {
@@ -15425,7 +15406,6 @@
"version": "0.0.45",
"resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.45.tgz",
"integrity": "sha512-YKIp4D441aZ6OoI9y+bfAyb2j4Cl+OFq/iiX6pPWDrL4ZO968h0dq0w07i65edvrTt7/G43mTnl0qEuLXyp/Yw==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"@iden3/bigarray": "0.0.2",
@@ -16070,7 +16050,6 @@
"version": "3.1.4",
"resolved": "https://registry.npmjs.org/rlnjs/-/rlnjs-3.1.4.tgz",
"integrity": "sha512-BKTBATi5pofLv3LxP+H2tV4riNkeMB6DSV6TQ9jaMqtv9MHCm165P87QjPa40X/fgomSmhsJU9VPFOuoBcN6XQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"@ethersproject/bytes": "^5.7.0",
@@ -16090,7 +16069,6 @@
"version": "0.2.55",
"resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.55.tgz",
"integrity": "sha512-8X0FCIPOWiK6DTWh3pnE3O6D6nIQsirStAXpWMzRDnoDX7SEnDX4I28aVhwjL7L35XS1vy2AU7zc0UCGYxdLjw==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"big-integer": "^1.6.48",
@@ -16103,7 +16081,6 @@
"version": "0.0.12",
"resolved": "https://registry.npmjs.org/wasmbuilder/-/wasmbuilder-0.0.12.tgz",
"integrity": "sha512-dTMpBgrnLOXrN58i2zakn2ScynsBhq9LfyQIsPz4CyxRF9k1GAORniuqn3xmE9NnI1l7g3iiVCkoB2Cl0/oG8w==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"big-integer": "^1.6.48"
@@ -16113,7 +16090,6 @@
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.1.0.tgz",
"integrity": "sha512-kIlcgbVUAv2uQ6lGsepGz/m5V40+Z6rvTBkqCYn3Y2+OcXst+UaP4filJYLh/xDxjJl62FFjZZeAnpeli1Y5/Q==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"big-integer": "^1.6.42",
@@ -16721,7 +16697,6 @@
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.7.0.tgz",
"integrity": "sha512-Vu5W+0Va6X1xvlCllpZ2r3/S7MafnL6IrAv09lk/F+VNDHuHEHx3xopR9Kr70p2KpbBBJ/HB9VCDZWism8WGlA==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"@iden3/binfileutils": "0.0.11",
@@ -16743,7 +16718,6 @@
"version": "0.2.59",
"resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.59.tgz",
"integrity": "sha512-QssOEUv+wilz9Sg7Zaj6KWAm7QceOAEsFuEBTltUsDo1cjn11rA/LGYvzFBPbzNfxRlZxwgJ7uxpCQcdDlrNfw==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"wasmbuilder": "0.0.16",
@@ -16755,7 +16729,6 @@
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.2.1.tgz",
"integrity": "sha512-9ciO7bUE5bgpbOcdK7IO3enrSVIKHwrQmPibok4GLJWaCA7Wyqc9PRYnu5HbiFv9NDFNqVKPtU5R6Is5KujBLg==",
"dev": true,
"license": "GPL-3.0",
"dependencies": {
"wasmbuilder": "0.0.16"
@@ -17770,7 +17743,6 @@
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/tryer/-/tryer-1.0.1.tgz",
"integrity": "sha512-c3zayb8/kWWpycWYg87P71E1S1ZL6b6IJxfb5fvsUgsf0S2MVGaDhDXXjDMpdCpfWXqptc+4mXwmiy1ypXqRAA==",
"dev": true,
"license": "MIT"
},
"node_modules/ts-node": {
@@ -19517,6 +19489,7 @@
"@semaphore-protocol/group": "^3.10.1",
"@zk-kit/incremental-merkle-tree": "^1.1.0",
"circomlibjs": "^0.1.7",
"fastfile": "^0.0.20",
"poseidon-lite": "^0.2.0",
"socket.io-client": "^4.7.1"
},

View File

@@ -15,12 +15,12 @@
"@semaphore-protocol/group": "^3.10.1",
"@semaphore-protocol/identity": "^3.10.1",
"@semaphore-protocol/proof": "^3.10.1",
"rlnjs": "^3.1.3",
"typescript": "^5.1.3"
},
"devDependencies": {
"@types/node": "^20.3.1",
"lerna": "^7.0.2",
"rlnjs": "^3.1.3",
"ts-node": "^10.9.1"
}
}
}

View File

@@ -36,6 +36,7 @@
"@semaphore-protocol/group": "^3.10.1",
"@zk-kit/incremental-merkle-tree": "^1.1.0",
"circomlibjs": "^0.1.7",
"fastfile": "^0.0.20",
"poseidon-lite": "^0.2.0",
"socket.io-client": "^4.7.1"
}

View File

@@ -0,0 +1,4 @@
// FIXME: This is a potential hack to get proofs to generate on the front end
export async function handle({ event, resolve }) {
return resolve(event, { ssr: false });
}

View File

@@ -1,11 +1,14 @@
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 { randomBigInt, genId, str2BigInt } from '../../../protocol-interfaces/src/utils';
import { poseidon1 } from 'poseidon-lite/poseidon1';
import { Group } from '@semaphore-protocol/group';
import type { Identity } from '@semaphore-protocol/identity';
import type { MessageI, RoomI } from './types';
import { poseidon2 } from 'poseidon-lite/poseidon2';
const prover: RLNProver = new RLNProver('/rln.wasm', '/rln_final.zkey');
const wasmPath = '/rln.wasm';
const zkeyPath = '/rln_final.zkey';
const prover: RLNProver = new RLNProver(wasmPath, zkeyPath);
interface proofInputsI {
rlnIdentifier: bigint;
@@ -18,27 +21,36 @@ interface proofInputsI {
}
async function genProof(room: RoomI, message: string, identity: Identity): Promise<MessageI> {
const messageHash: bigint = poseidon([message]);
const userMessageLimit = 1n;
const messageHash: bigint = poseidon1([str2BigInt(message)]);
const group = new Group(room.id, 20, room.membership?.identityCommitments);
const merkleproof: MerkleProof = await group.getMerkleProof(identity.getCommitment());
const idCommitment = BigInt(identity.getCommitment());
console.log(idCommitment);
const rateCommitment: bigint = poseidon2([idCommitment, userMessageLimit]);
group.addMember(rateCommitment);
const merkleproof: MerkleProof = await group.generateMerkleProof(group.indexOf(rateCommitment));
console.debug('MERKLEPROOF:', merkleproof);
const proofInputs: proofInputsI = {
rlnIdentifier: BigInt(room.id),
identitySecret: identity.getSecret(),
userMessageLimit: 1n,
messageId: 1n,
userMessageLimit: userMessageLimit,
messageId: 0n,
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;
console.debug('PROOFINPUTS:', proofInputs);
return prover.generateProof(proofInputs).then((proof: RLNFullProof) => {
console.debug('PROOF:', proof);
const msg: MessageI = {
id: proof.snarkProof.publicSignals.nullifier.toString(),
message: message,
room: BigInt(proof.snarkProof.publicSignals.externalNullifier),
proof
};
console.debug(msg);
return msg;
});
}
export { genProof, randomBigInt, genId };

View File

@@ -41,7 +41,7 @@
}
});
if ($identityStore.length != 2) {
if (!$identityStore['_nullifier']) {
console.log('MAKING UP SECRETS');
$identityStore = new Identity();
}

View File

@@ -4,7 +4,6 @@
import type { RoomGroupI, RoomI } from '$lib/types';
import { serverDataStore, selectedServer } from '$lib/stores';
import { onMount } from 'svelte';
import { roomGroups } from '$lib/rooms';
let room: RoomI;
@@ -43,7 +42,14 @@
<div class="container-fluid mt-2">
<div class="row">
<RoomList {selectRoom} />
{#if $serverDataStore[$selectedServer].roomGroups.length > 0}
<RoomList {selectRoom} />
{:else}
<div class="col-12">
<div class="alert alert-info" role="alert">Loading Rooms...</div>
</div>
{/if}
{#if room}
<ChatRoom {room} />
{:else}

View File

@@ -1,7 +1,4 @@
<script lang="ts">
import { selectedServer } from '$lib/stores';
</script>
<div class="col">
{$selectedServer.name}
</div>
<div class="col">Loading Server Rooms...</div>

View File

@@ -3,7 +3,8 @@
import type { RoomI, MessageI } from '$lib/types';
import { io } from 'socket.io-client';
import { onDestroy } from 'svelte';
import { prover } from '$lib/utils';
import { genProof } from '$lib/utils';
import { Identity } from '@semaphore-protocol/identity';
export let room: RoomI;
@@ -58,31 +59,32 @@
console.debug(err.message);
});
socket.on('messageBroadcast', (data) => {
socket.on('messageBroadcast', (data: MessageI) => {
messages = [data, ...messages];
messages = messages.slice(0, 500);
$messageStore[room.id.toString()] = messages;
});
function sendMessage(message: string) {
const msg = prover(room, message, $identityStore);
socket.emit('validateMessage', msg);
const identity = new Identity($identityStore.toString());
genProof(room, message, identity).then((msg) => {
console.log(msg);
socket.emit('validateMessage', msg);
});
}
</script>
<div class="col chat-room">
<div>
<h3>
{room?.name}
<span class="fs-6 fw-light">
{#if connected}
Connected!
{:else}
Disconnected!
{/if}
</span>
</h3>
</div>
<h3>
{room?.name}
<span class="fs-6 fw-light">
{#if connected}
Connected!
{:else}
Disconnected!
{/if}
</span>
</h3>
<div id="chat-messages" class="mb-3">
<section>
{#each messages as message}

View File

@@ -1,12 +1,16 @@
import { poseidon2 } from 'poseidon-lite/poseidon2';
export function str2BigInt(str: string) {
let num = '';
for (let i = 0; i < str.length; i++) {
num += str.charCodeAt(i).toString();
}
return BigInt(num);
}
export function genId(serverID: bigint, roomName: string | bigint | number) {
if (typeof roomName === 'string') {
let roomNum = '';
for (let i = 0; i < roomName.length; i++) {
roomNum += roomName.charCodeAt(i).toString();
}
return poseidon2([serverID, BigInt(roomNum)]);
return poseidon2([serverID, str2BigInt(roomName)]);
}
return poseidon2([serverID, BigInt(roomName)]);
}

View File

@@ -79,13 +79,13 @@ io.on('connection', (socket: Socket) => {
console.debug('a user connected');
socket.on('validateMessage', (msg: MessageI) => {
console.log('VALIDATING MESSAGE ' + msg);
console.log('VALIDATING MESSAGE ID:', msg.id, 'MSG:', msg.message);
const timestamp = Date.now().toString();
//const valid = verifyProof(msg);
// if (!valid) {
// console.log('INVALID MESSAGE');
// return;
// }
msg.id = Date.now().toString();
io.emit('messageBroadcast', msg);
});
@@ -120,7 +120,7 @@ app.get('/api/rooms', (req, res) => {
res.json(loadedRooms);
});
// TODO api endpoint that creates new rooms and generates invite codes for them
// TODO api endpoint that creates new rooms and generates invite codes for them
app.post('/join', (req, res) => {
const { code, idc } = req.body;
@@ -131,10 +131,10 @@ app.post('/join', (req, res) => {
if (result.status === 'CLAIMED') {
// join room
// update redis with new code status
redisClient.set('ccm', JSON.stringify(ccm.getClaimCodeSets()))
redisClient.set('ccm', JSON.stringify(ccm.getClaimCodeSets()));
console.log('Code claimed');
} else {
console.error('Code already claimed')
console.error('Code already claimed');
}
// TODO The `groupID` is the room ID like in https://github.com/AtHeartEngineering/Discreetly/blob/acc670fc4c43aa545dbbd03817879abfe5bc819e/packages/server/config/rooms.ts#L37
// TODO If the claim code is valid, then we would add the user to the room