Skip to content

Commit

Permalink
Hasher smart contract deployment in Scroll Sepolia
Browse files Browse the repository at this point in the history
  • Loading branch information
manudev97 committed Jul 12, 2024
1 parent 753d533 commit 67ff164
Show file tree
Hide file tree
Showing 15 changed files with 3,792 additions and 110 deletions.
18 changes: 18 additions & 0 deletions packages/hardhat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Comandos (zk_ATM - Hasher y Verifier.sol)

# Scaffold-eth-2
```sh
yarn chain
yarn start #localhost
yarn generate #generar cuentas para las networks
yarn account #imprimir balances de cuentas
# Inicialmente no se tiene fondo para deploy. Enviamos fondos a Public address: 0x...
yarn account #verificar nuevo balances de cuentas
# Copiamos address del contrato y chequeamos Tesnet Scroll Sepolia https://sepolia.scrollscan.com/
yarn add circomlibjs-old@npm:[email protected] # necesario para hacer
yarn add big-integer
mkdir build
node scripts/compileHasher.js
npx hardhat run scripts/deploy.js --network scrollSepolia #Deploy de contrato Hasher en scrollSepolia
# Hasher address (Poseidon 1 args): 0xCc735e52E393f125cAFc4E0aEbD80AEd81eA4B41
yarn deploy --network scrollSepolia # Despliega los contratos en scrollSepolia
1 change: 1 addition & 0 deletions packages/hardhat/build/Hasher.json

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions packages/hardhat/circuits/merkleTree.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pragma circom 2.1.6;

include "../node_modules/circomlib/circuits/poseidon.circom";


// si s == 0 devuelve [in[0], in[1]]
// si s == 1 devuelve [in[1], in[0]]
template DualMux() {
signal input in[2];
signal input s;
signal output out[2];

s * (1 - s) === 0; // asegurar que s es 0 o 1
out[0] <== (in[1] - in[0])*s + in[0];
out[1] <== (in[0] - in[1])*s + in[1];
}

// Verifica que la prueba de Merkle sea correcta dada una raíz de merkle y una hoja
// pathIndices es un array de selectores 0/1, indican si pathElement está en el lado izquierdo (1) o derecho (0) del path merkle.
template MerkleTreeChecker(levels) {
signal input leaf;
signal input root;
signal input pathElements[levels];
signal input pathIndices[levels];

component selectors[levels];
component hashers[levels];

for (var i = 0; i < levels; i++) {
selectors[i] = DualMux();
selectors[i].in[0] <== i == 0 ? leaf : hashers[i - 1].out;
selectors[i].in[1] <== pathElements[i];
selectors[i].s <== pathIndices[i];

hashers[i] = Poseidon(2);
hashers[i].inputs[0] <== selectors[i].out[0];
hashers[i].inputs[1] <== selectors[i].out[1];
}

root === hashers[levels - 1].out;
}
37 changes: 37 additions & 0 deletions packages/hardhat/circuits/withdraw.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pragma circom 2.1.6;

include "../node_modules/circomlib/circuits/poseidon.circom";
include "merkleTree.circom";

template Withdraw(levels) {
signal input root;
signal input nullifierHash;
signal input nullifier;
signal input secret;
signal input pathElements[levels];
signal input pathIndices[levels];

component nullifierHasher = Poseidon(2);
nullifierHasher.inputs[0] <== nullifier;
nullifierHasher.inputs[1] <== nullifier;

log("nullifierHasher: ", nullifierHasher.out);
log("nullifierHash: " , nullifierHash);

nullifierHasher.out === nullifierHash;

component commitmentHasher = Poseidon(2);
commitmentHasher.inputs[0] <== nullifier;
commitmentHasher.inputs[1] <== secret;

component tree = MerkleTreeChecker(levels);
tree.leaf <== commitmentHasher.out;
tree.root <== root;
for (var i = 0; i < levels; i++) {
tree.pathElements[i] <== pathElements[i];
tree.pathIndices[i] <== pathIndices[i];
}

}

component main {public [root,nullifierHash]} = Withdraw(2);
2 changes: 1 addition & 1 deletion packages/hardhat/contracts/YourContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import "hardhat/console.sol";
contract YourContract {
// State Variables
address public immutable owner;
string public greeting = "Building Unstoppable Apps!!!";
string public greeting = "Starting to build my ZKATM project!!!";
bool public premium = false;
uint256 public totalCounter = 0;
mapping(address => uint) public userGreetingCounter;
Expand Down
1 change: 1 addition & 0 deletions packages/hardhat/deployments/scrollSepolia/.chainId
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
534351
256 changes: 256 additions & 0 deletions packages/hardhat/deployments/scrollSepolia/YourContract.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const etherscanApiKey = process.env.ETHERSCAN_API_KEY || "DNXJA8RX2Q3VZ4URQIWP7Z

const config: HardhatUserConfig = {
solidity: {
version: "0.8.17",
version: "0.8.20",
settings: {
optimizer: {
enabled: true,
Expand Down
8 changes: 6 additions & 2 deletions packages/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
"verify": "hardhat etherscan-verify"
},
"dependencies": {
"@chainlink/contracts": "^1.1.1",
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@openzeppelin/contracts": "~4.8.1",
"@typechain/ethers-v6": "~0.5.1",
"big-integer": "^1.6.52",
"circomlibjs-old": "npm:[email protected]",
"dotenv": "~16.0.3",
"envfile": "~6.18.0",
"qrcode": "~1.5.1"
Expand All @@ -27,7 +31,7 @@
"@ethersproject/abi": "~5.7.0",
"@ethersproject/providers": "~5.7.1",
"@nomicfoundation/hardhat-chai-matchers": "~2.0.3",
"@nomicfoundation/hardhat-ethers": "~3.0.5",
"@nomicfoundation/hardhat-ethers": "latest",
"@nomicfoundation/hardhat-network-helpers": "~1.0.6",
"@nomicfoundation/hardhat-verify": "~2.0.3",
"@typechain/ethers-v5": "~10.1.0",
Expand All @@ -42,7 +46,7 @@
"eslint": "~8.26.0",
"eslint-config-prettier": "~9.1.0",
"eslint-plugin-prettier": "~5.1.3",
"ethers": "~6.10.0",
"ethers": "latest",
"hardhat": "~2.19.4",
"hardhat-deploy": "~0.11.45",
"hardhat-deploy-ethers": "~0.4.1",
Expand Down
16 changes: 16 additions & 0 deletions packages/hardhat/scripts/compileHasher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('path')
const fs = require('fs')
const { poseidon_gencontract } = require('circomlibjs-old');

const outputPath = path.join(__dirname, '..', 'build', 'Hasher.json')

async function main() {
const contractData = {
contractName: 'Hasher',
abi: poseidon_gencontract.generateABI(2),
bytecode: poseidon_gencontract.createCode(2),
};

fs.writeFileSync(outputPath, JSON.stringify(contractData))
}
main()
16 changes: 16 additions & 0 deletions packages/hardhat/scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const hasherContract = require('../build/Hasher.json');
require('dotenv').config();
const { ethers } = require('hardhat');

async function main() {
Hasher = await ethers.getContractFactory(hasherContract.abi, hasherContract.bytecode)
hasher = await Hasher.deploy();
console.log(`Hasher address: ${hasher.target}`);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.log(error);
process.exit(1);
});
24 changes: 24 additions & 0 deletions packages/hardhat/scripts/quickSetup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Use existing public phase 1 setup
PHASE1=build/phase1_final.ptau
PHASE2=build/phase2_final.ptau
CIRCUIT_ZKEY=build/circuit_final.zkey

# Phase 1
if [ -f "$PHASE1" ]; then
echo "Phase 1 file exists, no action"
else
echo "Phase 1 file does not exist, downloading ..."
curl -o $PHASE1 https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_14.ptau
fi

# Untrusted phase 2
npx snarkjs powersoftau prepare phase2 $PHASE1 $PHASE2 -v

npx snarkjs zkey new build/withdraw.r1cs $PHASE2 $CIRCUIT_ZKEY

npx snarkjs zkey export verificationkey $CIRCUIT_ZKEY build/verification_key.json


npx snarkjs zkey export solidityverifier $CIRCUIT_ZKEY build/Verifier.sol
# Fix solidity version (and want the command to work on both linux and mac)
cd build/ && sed 's/0\.6\.11/0\.7\.3/g' Verifier.sol > tmp.txt && mv tmp.txt Verifier.sol
Loading

0 comments on commit 67ff164

Please sign in to comment.