zk Connect Four

zk Connect Four

Table of contents

Introduction

zk Connect Four is a zero-knowledge decentralised application that lets you play the game of Connect Four against a DQN agent-trained policy or against another user, and generate and verify a zk-SNARK proof to validate that a user knows who won a valid game without revealing any information.

Technical details

The bulk of this application is made up of (i) circuits authored in Circom and tested using TypeScript, (ii) smart contracts written in Solidity using Hardhat and (iii) a React application written in TypeScript using Next.js.

The policy that is used to play against a user consists of a neural network which is optimized by a DQN agent in a reinforcement learning task using PyTorch.

Overview

  • packages/*: npm packages that contain code and configuration files that are used by other packages or services.
  • apps/*: workspaces that make up the bulk of the application.
  • apps/circuits/*: circuits workspace, it includes circuits written in Circom and a circuit unit test.
  • apps/contracts/*: smart contracts workspace which gathers contracts written in Solidity language, deploy scripts and a deployment JSON.
  • apps/web/*: front-end workspace which includes React functional components, CSSModules files, PostCSS mixins and a global CSS file.

Running locally

Before running this application you will need to have pnpm and Circom installed in your computer and download the proper powers of Tau file and paste it to apps/circuits/ptau (more info in this readme file).

$ git clone https://github.com/albertobas/zk-connect-four.git
$ cd zk-connect-four
$ cp .env.example .env # fill in tha values for each variable in ./.env, NEXT_PUBLIC_NETWORK_NAME must be either sepolia or localhost.
$ pnpm install
$ pnpm dev

If you'd like to run this application locally and deploy the contracts on a local node, replace sepolia for localhost in the NEXT_PUBLIC_NETWORK_NAME environment variable (and import the corresponding contract ABI and address in apps/web/src/components/enabled-verify-button.tsx after deploying the contracts locally and exporting the data) and run:

$ pnpm node

Then, open another tab in terminal and:

$ pnpm compile && pnpm generate && pnpm deploy:contracts && pnpm export
$ pnpm dev

Testing the circuit

$ pnpm test

Deployment

This project has been deployed at this address and there is currently support for the Sepolia network.

I have written the following posts to explain in detail this project:

Notes

This project is released under GNU General Public License v3.0.


Related projects

Bank Microservices

Bank Microservices

Bank Microservices is my own approach to a bank application, with a microservices-based architecture and using the MERN stack.

Learn more
Olive Oil Trust

Olive Oil Trust

Olive Oil Trust is an olive oil traceability solution built on Ethereum that adapts the workflow of the olive oil long value chain to the blockchain through the use of smart contracts.

Learn more
albertobas.com

albertobas.com

This is my personal webpage, where I publish articles and side-projects.

Learn more

Ready to #buidl?

Are you interested in Web3 or the synergies between blockchain technology, artificial intelligence and zero knowledge?. Then, do not hesitate to contact me by e-mail or on my LinkedIn profile. You can also find me on GitHub.