arbirps icon indicating copy to clipboard operation
arbirps copied to clipboard

On chain rock paper scissors game using zero knowledge proofs for hiding plays

Arbibot RPS

Implementation of an on chain rock paper scissors game using zero knowledge proofs for hiding the first throw until after the second throw has been played on chain. Oh and only Arbibots can play.

Wtf why?

So much fancy tech for a silly game? Yes.

Stages of play

  • Player 1 starts a round by submitting a zk proof that they have thrown a 0 (rock) 1 (paper) or 2 (scissors) without revealing which play it was
  • Player 2 responds by submitting a throw in the clear
  • Player 1 submits a second zk proof revealing what their original throw was and a winner (or tie) is calculated and stored on chain

Circuits Local build and test

Must have circom, foundry, and node installed and functioning

Install deps

yarn

Run the following two sections from ./circuits

Build circom circuits

./scripts/circomBuild.sh # compile the circuits

Prep snarks ceremony

Option 1: use all the files provided in the ./circuits/ceremony/ dir

./scripts/snarksCodeGen.sh

Option 2: Prep the ceremony yourself. Get an appropriate trusted powers of tau file, or generate your own. I used powersOfTau28_hez_final_11.ptau from here

./scripts/snarksCeremony.sh # finish the snarks ceremony for each circuit
./scripts/snarksCodeGen.sh

Fix solidity codegen

I have modified the solidity generated by snarksjs, the only thing required from the snarks codegen is the verifyingKey function from the ...Verifier2.sol output files. This must replace the same related functions in ./contracts/src/AttestValidMoveVerifier.sol and ./contracts/src/RevealMoveVerifier.sol after circuit or ceremony changes.

Test

Run through a snarksjs cli proof generation and verification test:

./scripts/testProof.sh

Output of the above test is used as valid proofs in solidity test files.

Contract building and testing

WARNING: All tests are written using the provided complete ceremony. If you modify the circuits or the ceremony tests will break.

from ./contracts

Build

forge build

Test

forge test

Licensing

SnarksJS generated verifier code licensed under the GPL-3.0 (🤮) license, see LICENSE-GPL3.

All other files including interface, circuits, and game functionality licensed under the MIT license, see LICENSE.