Skip to content

Commit

Permalink
Updated PostMessage circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrylR committed Jun 30, 2024
1 parent d7fa82f commit 03e13dc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 50 deletions.
16 changes: 12 additions & 4 deletions circuits/PostMessage.circom
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ template PostMessage(levels) {
signal input contractId;
signal input root;
signal input messageHash;
signal input deadline;
signal input expectedMessageTimestamp;

// Private
signal input nftId;
Expand All @@ -56,11 +56,10 @@ template PostMessage(levels) {

// ----------------------------------- Logic -----------------------------------

component credBuild = VerifiableCommitment();
component credBuild = BuildVerifiableCommitment();
credBuild.contractId <== contractId;
credBuild.nftId <== nftId;
credBuild.nftOwner <== nftOwner;
credBuild.deadline <== deadline;

credBuild.babyJubJubPK_Ax <== babyJubJubPK_Ax;
credBuild.babyJubJubPK_Ay <== babyJubJubPK_Ay;
Expand Down Expand Up @@ -98,8 +97,17 @@ template PostMessage(levels) {
sigVerifier.signatureR8X <== messageSignatureR8x;
sigVerifier.signatureR8Y <== messageSignatureR8y;
sigVerifier.data <== messageHash;

component greaterEqThanUpperTime = GreaterEqThan(64); // compare up to 2**64
greaterEqThanUpperTime.in[0] <== timestamp;
greaterEqThanUpperTime.in[1] <== expectedMessageTimestamp;

component timestampUpperBoundCheck = ForceEqualIfEnabled();
timestampUpperBoundCheck.in[0] <== greaterEqThanUpperTime.out;
timestampUpperBoundCheck.in[1] <== 1;
timestampUpperBoundCheck.enabled <== 1;
}

component main {
public [contractId, root, messageHash, deadline]
public [contractId, root, messageHash, expectedMessageTimestamp]
} = PostMessage(80);
9 changes: 0 additions & 9 deletions circuits/VerifiableCommitmentTemplate.circom
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,4 @@ template VerifiableCommitment() {
timestampLowerboundCheck.in[0] <== greaterEqThanLowerTime.out;
timestampLowerboundCheck.in[1] <== 1;
timestampLowerboundCheck.enabled <== 1;

component greaterEqThanUpperTime = GreaterEqThan(64); // compare up to 2**64
greaterEqThanUpperTime.in[0] <== timestamp + 500;
greaterEqThanUpperTime.in[1] <== deadline;

component timestampUpperBoundCheck = ForceEqualIfEnabled();
timestampUpperBoundCheck.in[0] <== greaterEqThanUpperTime.out;
timestampUpperBoundCheck.in[1] <== 1;
timestampUpperBoundCheck.enabled <== 1;
}
8 changes: 4 additions & 4 deletions contracts/Chat.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ contract Chat is Initializable, OwnableUpgradeable, UUPSUpgradeable {
IERC721 nft_,
string memory message_,
bytes32 root_,
uint256 deadline_,
uint256 expectedMessageTime_,
VerifierHelper.ProofPoints calldata zkPoints_
) external {
if (!credentialStorage.isRootValid(root_)) {
revert CredentialRootInvalid();
}

if (deadline_ < block.timestamp) {
revert DeadlineNotMet(deadline_, block.timestamp);
if (expectedMessageTime_ < block.timestamp) {
revert DeadlineNotMet(expectedMessageTime_, block.timestamp);
}

if (!_verifyZKProof(nft_, message_, root_, deadline_, zkPoints_)) {
if (!_verifyZKProof(nft_, message_, root_, expectedMessageTime_, zkPoints_)) {
revert InvalidZKProof();
}

Expand Down
28 changes: 14 additions & 14 deletions contracts/verifiers/PostMessageVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,34 @@ contract PostMessageVerifier {
uint256 public constant GAMMA_Y2 =
8495653923123431417604973247489272438418190587263600148770280649306958101930;
uint256 public constant DELTA_X1 =
3754928024521649700882316940530671078316762828908626006321275457145827634061;
7496425807565878802206166429244209866648025166603734761480880019245384171597;
uint256 public constant DELTA_X2 =
5257956645895755909920509974988641071718665744370476959216572329156772892428;
2426126935879591086796668463960468380279891110658612579374504649248882795785;
uint256 public constant DELTA_Y1 =
7342072234564244182600535504535204743669769258359741372506191834784394590967;
13518217156131645329083477705942680490863104970384987002343878909371733798135;
uint256 public constant DELTA_Y2 =
2178183139742335031055342472471728379220201484917889162243783637534058730535;
7355057493276029069828896377730862548361502069738392806485951795096962770633;

uint256 public constant IC0_X =
7628691115587631138761630990610405212665654629425354466106077653030434131362;
21884915298480216055842814220792519758308836646693691235722628031975512772488;
uint256 public constant IC0_Y =
8722150436132433583241101124035804383299657990209171335117341116150182177145;
20947377535303673212450158866778758285658012466375248480665701956414942966180;
uint256 public constant IC1_X =
19521029869919586818472728217372659126073693360327821516131240888878667597197;
4620117250677618852195482986749865474098202994020367976929616159724911725949;
uint256 public constant IC1_Y =
11707488513107923328183202989657825850743966974073978837269939103035371732510;
16236274241921183090661700167768790311627494025937201088679652017077122888813;
uint256 public constant IC2_X =
19593587138030766935700384279864410648354301740355748414141415933616881597695;
20950833580237707543463032534417735484079474207391313868462672549015555899085;
uint256 public constant IC2_Y =
4069039129759798479687591038446489018344099772545521547895985665032159921817;
15859113946602647017928374666722316968683814412557347512901027924965576026764;
uint256 public constant IC3_X =
7523832949938149133898758209740937162270527547835864754263755919401649495583;
9893885592438849431108792236848797713006397125141890466337356048670716338489;
uint256 public constant IC3_Y =
20225492314137488014267863984639083908197544959205439385361603137397818771725;
8390060764560142780906145306040026933424883203971631032639690966052782558129;
uint256 public constant IC4_X =
18104473174957195620836570104125057384931105055340448718709049735848137131413;
16314217675320516493996903710242497643972085088060060490350188222439629625099;
uint256 public constant IC4_Y =
8847610560809168773223992845848329025342693271017518270121203702893135677445;
18382580982449273655005301182444402398505021116876446569678394173933595760168;

/// @dev Memory data
uint16 public constant P_VK = 0;
Expand Down
32 changes: 16 additions & 16 deletions contracts/verifiers/VerifiableCommitmentVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,38 @@ contract VerifiableCommitmentVerifier {
uint256 public constant GAMMA_Y2 =
8495653923123431417604973247489272438418190587263600148770280649306958101930;
uint256 public constant DELTA_X1 =
67807239514022646664263889646347920599320242293977845135829851887625492488;
2144823353032305216023318453725473995266891058631818551924893131793530303857;
uint256 public constant DELTA_X2 =
18053402515244337525815813489875174343227724336602714685804813306905378748544;
341241003283463040271068777948286369666305419612288236239671477180713236800;
uint256 public constant DELTA_Y1 =
13457299472995272576618881102527994393812261588906330926106353593277816429446;
13792950901642111542812114957332501751554166104845285305279942335374153979798;
uint256 public constant DELTA_Y2 =
17834380212207192357506250631291213347300134565046231912013679558990206449340;
5210404528919218893639085932703385639956713570167900681878676703315093231716;

uint256 public constant IC0_X =
12769271964734440080911490228658786439696470329978405779125317584770143807291;
14976262950283006854394695209093636820000494915430524692358584483005742037072;
uint256 public constant IC0_Y =
16795383746133883461992527395353947551231683099878963801526102724770440785634;
17831647645008761545460704726150287063878694268295914678623518304308001801453;
uint256 public constant IC1_X =
1152827689017899334244401426892605355174739233595849242580252642133135077465;
13514366568713318741290276873284325600664262280565188275827166544091299542914;
uint256 public constant IC1_Y =
8145317932063950196332392479776498636228625744234274759207086347207086851826;
20855190042314782649952383156304434542535053077673155528824883250538558730081;
uint256 public constant IC2_X =
7641094827627831961621575434143077439067578943496905740100797097577327225811;
7042615263836124772465890098996196777861857842843925982728430391613042021675;
uint256 public constant IC2_Y =
20968546923864337314691884533913302163782178745891003631073605064891067474647;
1631680859252919024327382325270179288895224943315398074201320030319499003411;
uint256 public constant IC3_X =
1812910781831339153687002246942546662434796603441345756279030708115297734420;
17385730823640840692914818859392639926193764525974616083146382042231407361768;
uint256 public constant IC3_Y =
3239270325985353700652726192381370118518224503975526939481622278477783503331;
14485530642134726867327815332982092299270303463165879232912395122870909411907;
uint256 public constant IC4_X =
15231117918167224066107338091254028073581588025875054335319045851121655469478;
9803113550144060703111765572301995417280450576266012540977952501772133495567;
uint256 public constant IC4_Y =
11951390207507161939054316312363211627164245129291642967320983645632591244352;
4876047943452898267311923662526045085093483638465925983046551738839040745545;
uint256 public constant IC5_X =
7022461276653062283500796269902990957291700076490229687247435118999135763910;
2603292706814124836048867317062354755024508940313957952195693121270123993249;
uint256 public constant IC5_Y =
20481609515804386641066767044572132841673396058153867168517416677400507277668;
19802979835248112179022169403229611153444704810583024671924382580745791000781;

/// @dev Memory data
uint16 public constant P_VK = 0;
Expand Down
6 changes: 3 additions & 3 deletions test/Chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe("Chat", () => {
it("should post valid message successfully", async () => {
const circuit = await zkit.getCircuit("PostMessage");

const deadline = (await time.latest()) + 5300;
const expectedMessageArrival = (await time.latest()) + 1000;
const credentialId = buildCredentialId(
await erc721.getAddress(),
0,
Expand All @@ -145,7 +145,7 @@ describe("Chat", () => {
contractId: await erc721.getAddress(),
root: proof.root,
messageHash,
deadline: deadline,
expectedMessageTimestamp: expectedMessageArrival,
nftId: 0,
nftOwner: SECOND.address,
babyJubJubPK_Ax: secondIdentity.PK.p[0],
Expand Down Expand Up @@ -187,7 +187,7 @@ describe("Chat", () => {
]),
).to.be.true;

await expect(chat.postMessage(erc721.getAddress(), message, proof.root, deadline, formattedProof))
await expect(chat.postMessage(erc721.getAddress(), message, proof.root, expectedMessageArrival, formattedProof))
.to.emit(chat, "MessagePosted")
.withArgs(await erc721.getAddress(), message);

Expand Down

0 comments on commit 03e13dc

Please sign in to comment.