Skip to content

Commit bed7666

Browse files
committed
My own test
1 parent 31f891b commit bed7666

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

packages/contracts/contracts/base/SemaphoreVerifier.sol

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pragma solidity 0.8.23;
55

66
import {MAX_DEPTH} from "./Constants.sol";
77
import {SemaphoreVerifierKeyPts} from "./SemaphoreVerifierKeyPts.sol";
8+
import "hardhat/console.sol";
89

910
contract SemaphoreVerifier {
1011
// Scalar field size
@@ -34,6 +35,87 @@ contract SemaphoreVerifier {
3435
SemaphoreVerifierKeyPts.checkInvariant(MAX_DEPTH);
3536
}
3637

38+
function test() external view returns(bytes32[3] memory) {
39+
// --- 0x06 ecAdd ---
40+
uint256[4] memory inputAdd = [
41+
uint256(5090583730862410755830979584235210054631184460526892606022709842010236308675),
42+
// invalid g1.x
43+
// uint256(0),
44+
uint256(4935746665478263580470501879719907639988899436008466660042851826337922233613),
45+
uint256(5090583730862410755830979584235210054631184460526892606022709842010236308675),
46+
uint256(4935746665478263580470501879719907639988899436008466660042851826337922233613)
47+
];
48+
assembly {
49+
// ref: https://www.evm.codes/precompiled on ecAdd
50+
let success := staticcall(150, 0x06, inputAdd, 0x80, inputAdd, 0x40)
51+
52+
if iszero(success) {
53+
mstore(0,0)
54+
return(0, 0x60)
55+
}
56+
}
57+
58+
bytes32 addOut = abi.decode(abi.encode(inputAdd), (bytes32));
59+
console.log("addOut: %i", uint256(addOut));
60+
61+
// --- 0x07 ecMul ---
62+
uint256[3] memory inputMul = [
63+
uint256(5090583730862410755830979584235210054631184460526892606022709842010236308675),
64+
// invalid g1.x
65+
// uint256(5090583730862410755830979584235210054631184460526892606022709842010236308674),
66+
uint256(4935746665478263580470501879719907639988899436008466660042851826337922233613),
67+
2
68+
// infinity scalar
69+
// 0
70+
];
71+
assembly {
72+
// ref: https://www.evm.codes/precompiled on ecMul
73+
let success := staticcall(6000, 0x07, inputMul, 0x60, inputMul, 0x40)
74+
75+
if iszero(success) {
76+
mstore(0,0)
77+
return(0, 0x60)
78+
}
79+
}
80+
81+
bytes32 mulOut = abi.decode(abi.encode(inputMul), (bytes32));
82+
console.log("mulOut: %i", uint256(mulOut));
83+
84+
// --- 0x08 ecPairing ---
85+
// The input must always be a multiple of 6 32-byte values.
86+
uint256[12] memory inputPairing = [
87+
uint256(3010198690406615200373504922352659861758983907867017329644089018310584441462),
88+
uint256(17861058253836152797273815394432013122766662423622084931972383889279925210507),
89+
uint256(7273165102799931111715871471550377909735733521218303035754523677688038059653),
90+
uint256(2725019753478801796453339367788033689375851816420509565303521482350756874229),
91+
uint256(957874124722006818841961785324909313781880061366718538693995380805373202866),
92+
uint256(2512659008974376214222774206987427162027254181373325676825515531566330959255),
93+
uint256(4503322228978077916651710446042370109107355802721800704639343137502100212473),
94+
uint256(6132642251294427119375180147349983541569387941788025780665104001559216576968),
95+
uint256(14583779054894525174450323658765874724019480979794335525732096752006891875705),
96+
uint256(18029695676650738226693292988307914797657423701064905010927197838374790804409),
97+
uint256(11474861747383700316476719153975578001603231366361248090558603872215261634898),
98+
uint256(2140229616977736810657479771656733941598412651537078903776637920509952744750)
99+
// switched y2, y1 to be invalid
100+
// uint256(2140229616977736810657479771656733941598412651537078903776637920509952744750),
101+
// uint256(11474861747383700316476719153975578001603231366361248090558603872215261634898)
102+
];
103+
assembly {
104+
// ref: https://www.evm.codes/precompiled on ecPairing
105+
// formula: 45000 + 34000 * (bytes/192)
106+
let success := staticcall(113000, 0x08, inputPairing, 0x180, inputPairing, 0x20)
107+
108+
// Specified 0 gas cost to make it returning false
109+
if iszero(success) {
110+
mstore(inputPairing, 0)
111+
}
112+
}
113+
bytes32 pairingOut = abi.decode(abi.encode(inputPairing), (bytes32));
114+
console.log("pairingOut: %i", uint256(pairingOut));
115+
116+
return [addOut, mulOut, pairingOut];
117+
}
118+
37119
function verifyProof(
38120
uint[2] calldata _pA,
39121
uint[2][2] calldata _pB,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* eslint-disable @typescript-eslint/no-shadow */
2+
/* eslint-disable jest/valid-expect */
3+
import { Group, Identity, SemaphoreProof, generateProof, proofHash } from "@semaphore-protocol/core"
4+
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"
5+
import { expect } from "chai"
6+
import { Signer, ZeroAddress } from "ethers"
7+
import { run } from "hardhat"
8+
// @ts-ignore
9+
import { Semaphore, SemaphoreVerifier } from "../typechain-types"
10+
11+
describe("SemaphoreVerifier", () => {
12+
async function deploySemaphoreFixture() {
13+
const { semaphore, verifierAddress } = await run("deploy", {
14+
logs: false
15+
})
16+
17+
const semaphoreContract: Semaphore = semaphore
18+
const semaphoreVerifierContract: SemaphoreVerifier = await ethers.getContractAt(
19+
"SemaphoreVerifier", verifierAddress
20+
),
21+
22+
const accounts = await run("accounts", { logs: false })
23+
const accountAddresses = await Promise.all(accounts.map((signer: Signer) => signer.getAddress()))
24+
25+
const groupId = 0
26+
27+
return {
28+
semaphoreContract,
29+
semaphoreVerifierContract,
30+
accounts,
31+
accountAddresses,
32+
groupId
33+
}
34+
}
35+
36+
async function deployVerifyProofFixture() {
37+
const {
38+
semaphoreContract,
39+
semaphoreVerifierContract,
40+
accountAddresses,
41+
groupId
42+
} = await loadFixture(deploySemaphoreFixture)
43+
44+
const members = Array.from({ length: 3 }, (_, i) => new Identity(i.toString())).map(
45+
({ commitment }) => commitment
46+
)
47+
48+
await semaphoreContract["createGroup(address)"](accountAddresses[0])
49+
await semaphoreContract.addMembers(groupId, members)
50+
51+
const identity = new Identity("0")
52+
const group = new Group()
53+
54+
group.addMembers(members)
55+
56+
const merkleTreeDepth = 12
57+
const message = 2
58+
const proof: SemaphoreProof = await generateProof(identity, group, message, group.root, merkleTreeDepth)
59+
60+
return {
61+
semaphoreContract,
62+
semaphoreVerifierContract,
63+
accountAddresses,
64+
groupId,
65+
members,
66+
proof
67+
}
68+
}
69+
70+
it("Should succeed", async () => {
71+
const { semaphoreContract, semaphoreVerifierContract, proof } = await loadFixture(deployVerifyProofFixture)
72+
// console.log("proof:", proof);
73+
let result = await semaphoreVerifierContract.verifyProof(
74+
[proof.points[0], proof.points[1]],
75+
[[proof.points[2], proof.points[3]], [proof.points[4], proof.points[5]]],
76+
[proof.points[6], proof.points[7]],
77+
[proof.merkleTreeRoot, proof.nullifier, proofHash(proof.message), proofHash(proof.scope)],
78+
proof.merkleTreeDepth
79+
)
80+
expect(result).to.be.true
81+
82+
result = await semaphoreVerifierContract.test();
83+
console.log("result:", result);
84+
})
85+
})

packages/proof/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { packGroth16Proof, unpackGroth16Proof } from "@zk-kit/utils/proof-packing"
2+
import hash from "./hash"
23
import generateProof from "./generate-proof"
34
import verifyProof from "./verify-proof"
45

56
export * from "./types"
6-
export { generateProof, packGroth16Proof, unpackGroth16Proof, verifyProof }
7+
export { generateProof, packGroth16Proof, unpackGroth16Proof, verifyProof, hash as proofHash }

0 commit comments

Comments
 (0)