Skip to content

Commit

Permalink
Merge pull request #711 from semaphore-protocol/ref/add-tree-depth-co…
Browse files Browse the repository at this point in the history
…nstants

Add tree depth constants to JavaScript libraries and Solidity contracts
  • Loading branch information
cedoor authored Mar 15, 2024
2 parents 750c948 + c3b9b98 commit d256905
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/contracts/contracts/Semaphore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.23;
import {ISemaphore} from "./interfaces/ISemaphore.sol";
import {ISemaphoreVerifier} from "./interfaces/ISemaphoreVerifier.sol";
import {SemaphoreGroups} from "./base/SemaphoreGroups.sol";
import {MIN_DEPTH, MAX_DEPTH} from "./base/Constants.sol";

/// @title Semaphore
/// @dev This contract uses the Semaphore base contracts to provide a complete service
Expand Down Expand Up @@ -130,7 +131,7 @@ contract Semaphore is ISemaphore, SemaphoreGroups {
SemaphoreProof calldata proof
) public view override onlyExistingGroup(groupId) returns (bool) {
// The function will revert if the Merkle tree depth is not supported.
if (proof.merkleTreeDepth < 1 || proof.merkleTreeDepth > 12) {
if (proof.merkleTreeDepth < MIN_DEPTH || proof.merkleTreeDepth > MAX_DEPTH) {
revert Semaphore__MerkleTreeDepthIsNotSupported();
}

Expand Down
8 changes: 8 additions & 0 deletions packages/contracts/contracts/base/Constants.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

/// @dev Minimum supported tree depth.
uint8 constant MIN_DEPTH = 1;

/// @dev Maximum supported tree depth.
uint8 constant MAX_DEPTH = 12;
6 changes: 4 additions & 2 deletions packages/contracts/contracts/base/SemaphoreVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

pragma solidity 0.8.23;

import {MAX_DEPTH} from "./Constants.sol";

contract SemaphoreVerifier {
// Scalar field size
uint256 constant r = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
Expand All @@ -22,12 +24,12 @@ contract SemaphoreVerifier {
uint256 constant gammay2 = 8495653923123431417604973247489272438418190587263600148770280649306958101930;

// TODO: Add more variables when Semaphore supports tree depth > 12.
// Right now Semaphore supports tree depth 1-12.
// Right now Semaphore supports tree depth 1-12. It will support up to 32.

// Verification Key points.
// These values are taken from the verification key json file generated with snarkjs.
// It allows to use the same verifier to verify proofs for all the tree depths supported by Semaphore.
uint256[14][12] VK_POINTS = [
uint256[14][MAX_DEPTH] VK_POINTS = [
[
563562783592406106461234396505774794044312891062077216951605541624542949349,
16293410697967515504861065986355060225819302510590370360517024529684437085892,
Expand Down
5 changes: 3 additions & 2 deletions packages/proof/src/generate-proof.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Group, MerkleProof } from "@semaphore-protocol/group"
import type { Identity } from "@semaphore-protocol/identity"
import { requireDefined, requireNumber, requireObject, requireTypes } from "@semaphore-protocol/utils/errors"
import { MIN_DEPTH, MAX_DEPTH } from "@semaphore-protocol/utils/constants"
import { NumericString, groth16 } from "snarkjs"
import { packGroth16Proof } from "@zk-kit/utils/proof-packing"
import getSnarkArtifacts from "./get-snark-artifacts.node"
Expand Down Expand Up @@ -73,8 +74,8 @@ export default async function generateProof(
const merkleProofLength = merkleProof.siblings.length

if (merkleTreeDepth !== undefined) {
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
throw new TypeError("The tree depth must be a number between 1 and 12")
if (merkleTreeDepth < MIN_DEPTH || merkleTreeDepth > MAX_DEPTH) {
throw new TypeError(`The tree depth must be a number between ${MIN_DEPTH} and ${MAX_DEPTH}`)
}
} else {
merkleTreeDepth = merkleProofLength
Expand Down
6 changes: 3 additions & 3 deletions packages/proof/src/verify-proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
requireObject,
requireString
} from "@semaphore-protocol/utils/errors"
import { MIN_DEPTH, MAX_DEPTH } from "@semaphore-protocol/utils/constants"
import { groth16 } from "snarkjs"
import { unpackGroth16Proof } from "@zk-kit/utils/proof-packing"
import hash from "./hash"
Expand All @@ -30,9 +31,8 @@ export default async function verifyProof(proof: SemaphoreProof): Promise<boolea
requireString(scope, "proof.scope")
requireArray(points, "proof.points")

// TODO: support all tree depths after trusted-setup.
if (merkleTreeDepth < 1 || merkleTreeDepth > 12) {
throw new TypeError("The tree depth must be a number between 1 and 12")
if (merkleTreeDepth < MIN_DEPTH || merkleTreeDepth > MAX_DEPTH) {
throw new TypeError(`The tree depth must be a number between ${MIN_DEPTH} and ${MAX_DEPTH}`)
}

const verificationKey = {
Expand Down
5 changes: 5 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"types": "./dist/types/types.d.ts",
"require": "./dist/lib.commonjs/types.cjs",
"default": "./dist/lib.esm/types.js"
},
"./constants": {
"types": "./dist/types/constants.d.ts",
"require": "./dist/lib.commonjs/constants.cjs",
"default": "./dist/lib.esm/constants.js"
}
},
"files": [
Expand Down
5 changes: 5 additions & 0 deletions packages/utils/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// TODO: Support all tree depths after trusted-setup. MAX_DEPTH would be 32.

export const MIN_DEPTH = 1

export const MAX_DEPTH = 12
3 changes: 2 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as errors from "./errors"
import * as types from "./types"
import * as constants from "./constants"

export { errors, types }
export { errors, types, constants }

0 comments on commit d256905

Please sign in to comment.