Skip to content

Commit

Permalink
web assembly bleh
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian-Bright committed Mar 13, 2022
1 parent 8d8c064 commit 8d4994a
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 113 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"ethereum-blockies-base64": "^1.0.2",
"ethers": "^5.5.4",
"fake-tag": "^4.0.0",
"ffjavascript": "^0.2.50",
"graphql": "^16.3.0",
"https-browserify": "^1.0.0",
"os-browserify": "^0.3.0",
Expand All @@ -27,7 +26,6 @@
"react-jss": "^10.9.0",
"react-router-dom": "^6.2.1",
"react-scripts": "4.0.3",
"snarkjs": "^0.4.14",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"typescript": "^4.4.2",
Expand Down Expand Up @@ -80,5 +78,6 @@
"process": "^0.11.10",
"stream": "npm:stream-browserify",
"url": "npm:url"
}
},
"resolutions": {}
}
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<script src="/snarkjs.min.js"></script>
<title>Battlezips</title>
</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions public/snarkjs.min.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/hooks/useGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const useGame = (
const [refreshCount, setRefreshCount] = useState(0);

const fetchData = useCallback(async () => {
console.log('FLAG');
if (!chainId) return;
try {
setFecthing(true);
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useMiMCSponge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const useMiMCSponge = (): {
error: Error | null;
mimcSponge: any | null;
} => {
console.log('mim', buildMimcSponge)
const { chainId } = useWallet();
const [error, setError] = useState<Error | null>(null);
const [mimcSponge, setMimcSponge] = useState<any | null>(null);
Expand Down
1 change: 0 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ declare module 'fake-tag' {

declare module 'ffjavascript';
declare module 'circomlibjs';
declare module 'snarkjs';
declare module 'boardWasm';
declare module 'shotWasm';
declare module '@iden3/binfileutils';
9 changes: 9 additions & 0 deletions src/types/global/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export { };

declare global {
interface Window {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
snarkjs: any;

}
}
20 changes: 10 additions & 10 deletions src/views/BuildBoardView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { useNavigate, useParams } from 'react-router-dom';
import { useWallet } from 'contexts/WalletContext';
import { ActiveGameLocation } from 'Locations';
import { useMiMCSponge } from 'hooks/useMiMCSponge';
import { groth16 } from 'snarkjs';
import { buildProofArgs } from 'utils';
import { toast } from 'react-hot-toast';
import { BigNumber as BN } from 'ethers';
Expand Down Expand Up @@ -122,16 +121,17 @@ export default function BuildBoard(): JSX.Element {
const _shipHash = mimcSponge.F.toObject(
await mimcSponge.multiHash(board.flat())
);
const { proof, publicSignals } = await groth16.fullProve(
const { proof, publicSignals } = await window.snarkjs.groth16.fullProve(
{ ships: board, hash: _shipHash },
'https://ipfs.infura.io/ipfs/QmRt4Uzi5w57fUne4cgPoBdSDJzQhEgHNisnib4iKTQ7Xt',
'https://ipfs.infura.io/ipfs/Qmaope4n6y4zCSnLDNAHJYnPq1Kdf3yapbRPzGiFd11EUj'
);
await groth16.verify(
require('zk/board_verification_key.json'),
publicSignals,
proof
'https://ipfs.infura.io/ipfs/QmZ274ZUF3JAAdtnCaYNr2tEDFwu4ThqG6Hkbj7rvYbiDs',
'https://ipfs.infura.io/ipfs/QmQMfy99jyvzQ9wPSmHwYvxfXL929yjDFZ2dzyouvotBsk'
);
const vkey = await fetch(
'https://ipfs.infura.io/ipfs/QmWCeoJy8ZEmN33htuvzDUxk2YmoQqF9VymMJUzay4XdLo'
).then((res) => {
return res.json();
});
await window.snarkjs.groth16.verify(vkey, publicSignals, proof);
const proofArgs = buildProofArgs(proof);
return { hash: _shipHash, proof: proofArgs };
};
Expand Down Expand Up @@ -200,7 +200,7 @@ export default function BuildBoard(): JSX.Element {
navigate(ActiveGameLocation(`${+currentIndex + 1}`));
}
} catch (err) {
console.log('ERROR:', err);
console.log('ERROR: ', err);
toast.error(id ? 'Error joining game' : 'Error creating game', {
id: loadingToast,
duration: 5000
Expand Down
7 changes: 4 additions & 3 deletions src/views/Game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { firstTurn, turn } from 'web3/battleshipGame';
import { Shot } from './types';
import { toast } from 'react-hot-toast';
import { useMiMCSponge } from 'hooks/useMiMCSponge';
import { groth16 } from 'snarkjs';
import { buildProofArgs } from 'utils';
import GameOver from './components/GameOver';

Expand Down Expand Up @@ -74,12 +73,14 @@ export default function Game(): JSX.Element {
const _shipHash = mimcSponge.F.toObject(
await mimcSponge.multiHash(board.flat())
);
const { proof, publicSignals } = await groth16.fullProve(
console.log('SNARKJS: ', window.snarkjs);
const { proof, publicSignals } = await window.snarkjs.groth16.fullProve(
{ ships: board, hash: _shipHash, shot, hit },
'https://ipfs.infura.io/ipfs/QmW4GhGVofT9o1bGcGajuamWgY8QhMAp2vE8mKu4yfw3oW',
'https://ipfs.infura.io/ipfs/QmZFkHjGeCHfhE4xLYo3gAgRaqpTCm5YEmCWGtGFHfWTha'
);
await groth16.verify(
console.log('FLAG');
await window.snarkjs.groth16.verify(
require('zk/shot_verification_key.json'),
publicSignals,
proof
Expand Down
1 change: 0 additions & 1 deletion src/web3/battleshipGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export const playingGame = async (
ethersProvider: providers.Web3Provider,
player: string
) => {
console.log('FLAG: ', BATTLESHIP_GAME_CONTRACT[chainId])
if (!ethersProvider) return;
const abi = new utils.Interface([
'function playing(address player) public view returns(uint256)'
Expand Down
99 changes: 6 additions & 93 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2019,12 +2019,7 @@
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c"
integrity sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==

"@iden3/bigarray@0.0.2":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@iden3/bigarray/-/bigarray-0.0.2.tgz#6fc4ba5be18daf8a26ee393f2fb62b80d98c05e9"
integrity sha512-Xzdyxqm1bOFF6pdIsiHLLl3HkSLjbhqJHVyqaTxXt3RqXBEnmsUmEW47H7VOi/ak7TdkRpNkxjyK5Zbkm+y52g==

"@iden3/binfileutils@0.0.10", "@iden3/binfileutils@^0.0.10":
"@iden3/binfileutils@^0.0.10":
version "0.0.10"
resolved "https://registry.yarnpkg.com/@iden3/binfileutils/-/binfileutils-0.0.10.tgz#8791330780f6ea6bc063dda08c27b750d2233625"
integrity sha512-mDtBiKYcHs9K8vnznd8md0In6e5hL6i7ITzlHQ6Xxx6kvGAgB8UZeHJ0KswS6IJK4x9v2mwHsh5kIDl245cQZg==
Expand Down Expand Up @@ -3699,11 +3694,6 @@ async-mutex@^0.2.6:
dependencies:
tslib "^2.0.0"

async@0.9.x:
version "0.9.2"
resolved "https://registry.yarnpkg.com/async/-/async-0.9.2.tgz#aea74d5e61c1f899613bf64bda66d4c78f2fd17d"
integrity sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0=

async@^1.4.2:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
Expand Down Expand Up @@ -4780,13 +4770,6 @@ cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3:
inherits "^2.0.1"
safe-buffer "^5.0.1"

circom_runtime@0.1.17:
version "0.1.17"
resolved "https://registry.yarnpkg.com/circom_runtime/-/circom_runtime-0.1.17.tgz#9360017d6b5d9291128da4fe05830384ef293ec1"
integrity sha512-FCOCPz7ZbqL4TpzBlISRZ7/fcYHkdZz0DMfju1DYHiRU/+ZzJQfDS8JYENlnb9PO+HsLTr6/QtzphqvnEBp9AQ==
dependencies:
ffjavascript "0.2.48"

circomlibjs@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/circomlibjs/-/circomlibjs-0.1.1.tgz#00085017f6fa00072a13a2092f643cfd12b11f6b"
Expand Down Expand Up @@ -6086,13 +6069,6 @@ ejs@^2.6.1:
resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.7.4.tgz#48661287573dcc53e366c7a1ae52c3a120eec9ba"
integrity sha512-7vmuyh5+kuUyJKePhQfRQBhXV5Ce+RnaeeQArKu1EAMpL3WbgMt5WG6uQZpEVvYSSsxMXRKOewtDk9RaTKXRlA==

ejs@^3.1.6:
version "3.1.6"
resolved "https://registry.yarnpkg.com/ejs/-/ejs-3.1.6.tgz#5bfd0a0689743bb5268b3550cceeebbc1702822a"
integrity sha512-9lt9Zse4hPucPkoP7FHDF0LQAlGyF9JVpnClFLFH3aSSbxmyoqINRpp/9wePWJTUl4KOQwRL72Iw3InHPDkoGw==
dependencies:
jake "^10.6.1"

electron-to-chromium@^1.3.564, electron-to-chromium@^1.4.71:
version "1.4.71"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.71.tgz#17056914465da0890ce00351a3b946fd4cd51ff6"
Expand Down Expand Up @@ -7127,20 +7103,10 @@ fbjs@^3.0.0:
setimmediate "^1.0.5"
ua-parser-js "^0.7.30"

ffjavascript@0.2.48, ffjavascript@^0.2.45, ffjavascript@^0.2.48:
version "0.2.48"
resolved "https://registry.yarnpkg.com/ffjavascript/-/ffjavascript-0.2.48.tgz#0ca408471d7b18bfc096a9631aa3ef3549c8c82b"
integrity sha512-uNrWP+odLofNmmO+iCCPi/Xt/sJh1ku3pVKmKWVWCLFfdCP69hvRrogKUIGnsdiINcWn0lGxcEh5oEjStMFXQQ==
dependencies:
big-integer "^1.6.48"
wasmbuilder "^0.0.12"
wasmcurves "0.1.0"
web-worker "^1.2.0"

ffjavascript@^0.2.50:
version "0.2.50"
resolved "https://registry.yarnpkg.com/ffjavascript/-/ffjavascript-0.2.50.tgz#26a11624b0f6736f44299cc9f39d8692152f7199"
integrity sha512-fQyzkLj3XShhJx9KjIjgWuYS6e6quJQqKF+anPPDuIC/aA+isb+w+8Rf9ih6MSlQYYUu2T8Rh9y8jbuVscESbw==
ffjavascript@^0.2.45, ffjavascript@^0.2.48:
version "0.2.51"
resolved "https://registry.yarnpkg.com/ffjavascript/-/ffjavascript-0.2.51.tgz#5d714686e3a5bde96dc57be84babc61911283629"
integrity sha512-qshvFIjU6cWMQOtKwpU4JZOKfKHfN8TFJ0Tst1Bd/V8AfgzAUScqA+zRT6sLGdV9zlumLsMgrLzGVRK4kf6RQA==
dependencies:
big-integer "^1.6.48"
wasmbuilder "^0.0.12"
Expand Down Expand Up @@ -7194,13 +7160,6 @@ file-uri-to-path@1.0.0:
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==

filelist@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/filelist/-/filelist-1.0.2.tgz#80202f21462d4d1c2e214119b1807c1bc0380e5b"
integrity sha512-z7O0IS8Plc39rTCq6i6iHxk43duYOn8uFJiWSewIq0Bww1RNybVHSCjahmcC87ZqAm4OTvFzlzeGu3XAzG1ctQ==
dependencies:
minimatch "^3.0.4"

filesize@6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/filesize/-/filesize-6.1.0.tgz#e81bdaa780e2451d714d71c0d7a4f3238d37ad00"
Expand Down Expand Up @@ -8859,16 +8818,6 @@ iterall@^1.2.1:
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea"
integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==

jake@^10.6.1:
version "10.8.2"
resolved "https://registry.yarnpkg.com/jake/-/jake-10.8.2.tgz#ebc9de8558160a66d82d0eadc6a2e58fbc500a7b"
integrity sha512-eLpKyrfG3mzvGE2Du8VoPbeSkRry093+tyNjdYaBbJS9v17knImYGNXQCUV0gLxQtF82m3E8iRb/wdSQZLoq7A==
dependencies:
async "0.9.x"
chalk "^2.4.2"
filelist "^1.0.1"
minimatch "^3.0.4"

jest-changed-files@^26.6.2:
version "26.6.2"
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-26.6.2.tgz#f6198479e1cc66f22f9ae1e22acaa0b429c042d0"
Expand Down Expand Up @@ -9328,7 +9277,7 @@ jest@26.6.0:
import-local "^3.0.2"
jest-cli "^26.6.0"

js-sha3@0.8.0, js-sha3@^0.8.0:
js-sha3@0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"
integrity sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==
Expand Down Expand Up @@ -10092,11 +10041,6 @@ loglevel@^1.6.8:
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.8.0.tgz#e7ec73a57e1e7b419cb6c6ac06bf050b67356114"
integrity sha512-G6A/nJLRgWOuuwdNuA6koovfEV1YpqqAG4pRUlFaz3jj2QNZ8M4vBqnVA+HBTmU/AMNUtlOsMmSpF6NyOjztbA==

logplease@^1.2.15:
version "1.2.15"
resolved "https://registry.yarnpkg.com/logplease/-/logplease-1.2.15.tgz#3da442e93751a5992cc19010a826b08d0293c48a"
integrity sha512-jLlHnlsPSJjpwUfcNyUxXCl33AYg2cHhIf9QhGL2T4iPT0XPB+xP1LRKFPgIg1M/sg9kAJvy94w9CzBNrfnstA==

long@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/long/-/long-3.2.0.tgz#d821b7138ca1cb581c172990ef14db200b5c474b"
Expand Down Expand Up @@ -12352,16 +12296,6 @@ queue-microtask@^1.2.2:
resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243"
integrity sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==

r1csfile@0.0.35:
version "0.0.35"
resolved "https://registry.yarnpkg.com/r1csfile/-/r1csfile-0.0.35.tgz#07e956108d28cf9d388562e9fcc068880c517ed8"
integrity sha512-n6RTn7KxtfHxw5gjljYBaEuhVkXEQ2sZW1XVan7fwdwvQt9Kd65/A0cy+nNHL4GRGAHEaBMdYj0JOl/3kXln4Q==
dependencies:
"@iden3/bigarray" "0.0.2"
"@iden3/binfileutils" "0.0.10"
fastfile "0.0.19"
ffjavascript "0.2.48"

raf@^3.4.1:
version "3.4.1"
resolved "https://registry.yarnpkg.com/raf/-/raf-3.4.1.tgz#0742e99a4a6552f445d73e3ee0328af0ff1ede39"
Expand Down Expand Up @@ -12694,11 +12628,6 @@ readdirp@~3.6.0:
dependencies:
picomatch "^2.2.1"

readline@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/readline/-/readline-1.3.0.tgz#c580d77ef2cfc8752b132498060dc9793a7ac01c"
integrity sha1-xYDXfvLPyHUrEySYBg3JeTp6wBw=

recursive-readdir@2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f"
Expand Down Expand Up @@ -13597,22 +13526,6 @@ snapdragon@^0.8.1:
source-map-resolve "^0.5.0"
use "^3.1.0"

snarkjs@^0.4.14:
version "0.4.14"
resolved "https://registry.yarnpkg.com/snarkjs/-/snarkjs-0.4.14.tgz#b3fceb23c1674fc8c9d0cd6aff056c5100602937"
integrity sha512-DkYYlSG98ZOcXADDOMegxYC/Lp1GcUpllq2ASH25MpyW3CWx43Lvo8eCALuK10agEzSfDFYsKnXTr89emIPA8A==
dependencies:
"@iden3/binfileutils" "0.0.10"
blake2b-wasm "^2.4.0"
circom_runtime "0.1.17"
ejs "^3.1.6"
fastfile "0.0.19"
ffjavascript "0.2.48"
js-sha3 "^0.8.0"
logplease "^1.2.15"
r1csfile "0.0.35"
readline "^1.3.0"

sockjs-client@^1.5.0:
version "1.5.2"
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.5.2.tgz#4bc48c2da9ce4769f19dc723396b50f5c12330a3"
Expand Down

0 comments on commit 8d4994a

Please sign in to comment.