Skip to content

Commit

Permalink
refactor: hash message and scope
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Jan 12, 2024
1 parent 4d4a31c commit b7d58bd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
11 changes: 9 additions & 2 deletions packages/contracts/contracts/Semaphore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity 0.8.4;

import "./interfaces/ISemaphore.sol";
import "./interfaces/ISemaphoreVerifier.sol";
import "./base/SemaphoreGroups.sol";
import {SemaphoreGroups} from "./base/SemaphoreGroups.sol";

/// @title Semaphore
/// @dev This contract uses the Semaphore base contracts to provide a complete service
Expand Down Expand Up @@ -189,7 +189,7 @@ contract Semaphore is ISemaphore, SemaphoreGroups {
[proof[0], proof[1]],
[[proof[2], proof[3]], [proof[4], proof[5]]],
[proof[6], proof[7]],
[merkleTreeRoot, nullifier, message, scope]
[merkleTreeRoot, nullifier, _hash(message), _hash(scope)]
)
) {
revert Semaphore__InvalidProof();
Expand All @@ -199,4 +199,11 @@ contract Semaphore is ISemaphore, SemaphoreGroups {

emit ProofVerified(groupId, merkleTreeRoot, nullifier, message, scope, proof);
}

/// @dev Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
/// @param message: Message to be hashed.
/// @return Message digest.
function _hash(uint256 message) private pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(message))) >> 8;
}
}
4 changes: 4 additions & 0 deletions packages/proof/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"access": "public"
},
"devDependencies": {
"@ethersproject/strings": "^5.7.0",
"@rollup/plugin-commonjs": "^24.1.0",
"@rollup/plugin-json": "^5.0.1",
"@rollup/plugin-node-resolve": "^15.0.2",
Expand All @@ -46,6 +47,9 @@
"@semaphore-protocol/identity": "3.15.2"
},
"dependencies": {
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/keccak256": "^5.7.0",
"@zk-kit/groth16": "0.5.0",
"download": "^8.0.0",
"tmp": "^0.2.1"
Expand Down
19 changes: 11 additions & 8 deletions packages/proof/src/generate-proof.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { BigNumber } from "@ethersproject/bignumber"
import { BytesLike, Hexable } from "@ethersproject/bytes"
import { Group } from "@semaphore-protocol/group"
import type { Identity } from "@semaphore-protocol/identity"
import { prove } from "@zk-kit/groth16"
import { NumericString, prove } from "@zk-kit/groth16"
import getSnarkArtifacts from "./get-snark-artifacts.node"
import hash from "./hash"
import packProof from "./pack-proof"
import { BigNumberish, SemaphoreProof, SnarkArtifacts } from "./types"
import { SemaphoreProof, SnarkArtifacts } from "./types"

/**
* Generates a Semaphore proof.
Expand All @@ -18,8 +21,8 @@ import { BigNumberish, SemaphoreProof, SnarkArtifacts } from "./types"
export default async function generateProof(
identity: Identity,
group: Group,
message: BigNumberish,
scope: BigNumberish,
message: BytesLike | Hexable | number | bigint,
scope: BytesLike | Hexable | number | bigint,
treeDepth?: number,
snarkArtifacts?: SnarkArtifacts
): Promise<SemaphoreProof> {
Expand Down Expand Up @@ -54,8 +57,8 @@ export default async function generateProof(
treeDepth: merkleProofLength,
treeIndices,
treeSiblings,
scope,
message
scope: hash(scope),
message: hash(message)
},
snarkArtifacts.wasmFilePath,
snarkArtifacts.zkeyFilePath
Expand All @@ -64,8 +67,8 @@ export default async function generateProof(
return {
treeRoot: publicSignals[0],
nullifier: publicSignals[1],
message: publicSignals[2],
scope: publicSignals[3],
message: BigNumber.from(message).toString() as NumericString,
scope: BigNumber.from(scope).toString() as NumericString,
proof: packProof(proof)
}
}
16 changes: 16 additions & 0 deletions packages/proof/src/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BigNumber } from "@ethersproject/bignumber"
import { BytesLike, Hexable, zeroPad } from "@ethersproject/bytes"
import { keccak256 } from "@ethersproject/keccak256"
import { NumericString } from "@zk-kit/groth16"

/**
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus.
* @param message The message to be hashed.
* @returns The message digest.
*/
export default function hash(message: BytesLike | Hexable | number | bigint): NumericString {
message = BigNumber.from(message).toTwos(256).toHexString()
message = zeroPad(message, 32)

return (BigInt(keccak256(message)) >> BigInt(8)).toString() as NumericString
}
5 changes: 3 additions & 2 deletions packages/proof/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { formatBytes32String } from "@ethersproject/strings"
import { Group } from "@semaphore-protocol/group"
import { Identity } from "@semaphore-protocol/identity"
import { getCurveFromName } from "ffjavascript"
Expand All @@ -10,8 +11,8 @@ import verifyProof from "./verify-proof"
describe("Proof", () => {
const treeDepth = 10

const message = 1
const scope = 2
const message = formatBytes32String("Hello world")
const scope = formatBytes32String("Scope")

const identity = new Identity(42)

Expand Down
3 changes: 2 additions & 1 deletion packages/proof/src/verify-proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { verify } from "@zk-kit/groth16"
import { SemaphoreProof } from "./types"
import unpackProof from "./unpack-proof"
import verificationKeys from "./verification-keys.json"
import hash from "./hash"

/**
* Verifies a Semaphore proof.
Expand All @@ -21,7 +22,7 @@ export default function verifyProof({ treeRoot, nullifier, message, scope, proof
}

return verify(verificationKey, {
publicSignals: [treeRoot, nullifier, message, scope],
publicSignals: [treeRoot, nullifier, hash(message), hash(scope)],
proof: unpackProof(proof)
})
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8499,6 +8499,10 @@ __metadata:
version: 0.0.0-use.local
resolution: "@semaphore-protocol/proof@workspace:packages/proof"
dependencies:
"@ethersproject/bignumber": ^5.7.0
"@ethersproject/bytes": ^5.7.0
"@ethersproject/keccak256": ^5.7.0
"@ethersproject/strings": ^5.7.0
"@rollup/plugin-commonjs": ^24.1.0
"@rollup/plugin-json": ^5.0.1
"@rollup/plugin-node-resolve": ^15.0.2
Expand Down

0 comments on commit b7d58bd

Please sign in to comment.