Skip to content

Commit 75a6ba0

Browse files
committed
fix: fix bug in generating proof
1 parent 154e769 commit 75a6ba0

File tree

3 files changed

+338
-253
lines changed

3 files changed

+338
-253
lines changed

server/routers/zk_proof.js

Lines changed: 78 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ router.get("/gen/running", async (req, res) => {
2525
"./circom2contract/running/circuit_final.zkey"
2626
);
2727

28-
const proofHex = convertProofToHex(proof);
28+
const proofArrObj = reorderDict(proof);
29+
30+
const tempProof = convertProofToHex(proofArrObj)
31+
const formattedProof = convertObjectToHexPadded(tempProof);
2932
const pubSignalHex = convertToHexPadded(publicSignals);
3033

34+
console.log("length:", formattedProof.length);
35+
3136
res.send({
3237
proof: proof,
33-
proofHex: proofHex,
38+
proofHex: formattedProof,
3439
publicSignals: publicSignals,
3540
publicSignalsHex: pubSignalHex,
3641
});
@@ -44,11 +49,11 @@ router.get("/gen/sleeping", async (req, res) => {
4449
const input = {
4550
startTime: body.startTime,
4651
endTime: body.endTime,
47-
sleepHour: body.sleepHour,
52+
sleepTime: body.sleepTime,
4853
sleepLength: body.sleepLength,
4954
minStartTime: body.minStartTime,
5055
maxEndTime: body.maxEndTime,
51-
maxSleepHour: body.maxSleepHour,
56+
sleepBefore: body.sleepBefore,
5257
minSleepLength: body.minSleepLength,
5358
};
5459
try {
@@ -57,14 +62,17 @@ router.get("/gen/sleeping", async (req, res) => {
5762
"./circom2contract/sleeping/sleeping_js/sleeping.wasm",
5863
"./circom2contract/sleeping/circuit_final.zkey"
5964
);
65+
const proofArrObj = reorderDict(proof);
6066

61-
const proofObjHex = convertProofToObjectHex(proof);
62-
const proofArrHex = convertObjectToHexPadded(proofObjHex)
67+
const tempProof = convertProofToHex(proofArrObj)
68+
const formattedProof = convertObjectToHexPadded(tempProof);
6369
const pubSignalHex = convertToHexPadded(publicSignals);
6470

71+
console.log("length:", formattedProof.length);
72+
6573
res.send({
6674
proof: proof,
67-
proofHex: proofArrHex,
75+
proofHex: formattedProof,
6876
publicSignals: publicSignals,
6977
publicSignalsHex: pubSignalHex,
7078
});
@@ -73,7 +81,43 @@ router.get("/gen/sleeping", async (req, res) => {
7381
}
7482
});
7583

76-
function convertProofToObjectHex(proof) {
84+
function convertObjectToHexPadded(obj) {
85+
const hexArray = [];
86+
87+
// Function to convert a single BigInt number to a padded hex string
88+
const toPaddedHexString = (bigInt) => {
89+
return "0x" + bigInt.toString(16).padStart(64, "0");
90+
};
91+
92+
// Iterate over each key in the object
93+
for (const key of Object.keys(obj)) {
94+
const value = obj[key];
95+
96+
// Check if the value is an array (handle nested arrays too)
97+
if (Array.isArray(value)) {
98+
// Handle nested arrays for points like A, B, C, etc.
99+
if (Array.isArray(value[0])) {
100+
value.forEach((innerArray) => {
101+
innerArray.forEach((number) => {
102+
hexArray.push(toPaddedHexString(BigInt(number)));
103+
});
104+
});
105+
} else {
106+
// Handle flat arrays for points like eval_a, eval_b, etc.
107+
value.forEach((number) => {
108+
hexArray.push(toPaddedHexString(BigInt(number)));
109+
});
110+
}
111+
} else {
112+
// Handle single values that are not arrays
113+
hexArray.push(toPaddedHexString(BigInt(value)));
114+
}
115+
}
116+
117+
return hexArray;
118+
}
119+
120+
function convertProofToHex(proof) {
77121
// Helper function to convert to a 0x-prefixed hexadecimal string
78122
const toHex = (numStr) => {
79123
return "0x" + BigInt(numStr).toString(16);
@@ -96,6 +140,32 @@ function convertProofToObjectHex(proof) {
96140
return convertedProof;
97141
}
98142

143+
function reorderDict(dict) {
144+
dictReorder = [
145+
{ key: "A", value: dict["A"] },
146+
{ key: "B", value: dict["B"] },
147+
{ key: "C", value: dict["C"] },
148+
{ key: "Z", value: dict["Z"] },
149+
{ key: "T1", value: dict["T1"] },
150+
{ key: "T2", value: dict["T2"] },
151+
{ key: "T3", value: dict["T3"] },
152+
{ key: "Wxi", value: dict["Wxi"] },
153+
{ key: "Wxiw", value: dict["Wxiw"] },
154+
{ key: "eval_a", value: dict["eval_a"] },
155+
{ key: "eval_b", value: dict["eval_b"] },
156+
{ key: "eval_c", value: dict["eval_c"] },
157+
{ key: "eval_s1", value: dict["eval_s1"] },
158+
{ key: "eval_s2", value: dict["eval_s2"] },
159+
{ key: "eval_zw", value: dict["eval_zw"] },
160+
];
161+
result = {}
162+
for (let item of dictReorder) {
163+
result[item.key] = item.value;
164+
}
165+
166+
return result;
167+
}
168+
99169
function convertToHexPadded(pubSignals) {
100170
return pubSignals.map((signal) => {
101171
// Convert the decimal string to a BigInt
@@ -111,41 +181,4 @@ function convertToHexPadded(pubSignals) {
111181
return paddedHex;
112182
});
113183
}
114-
115-
function convertObjectToHexPadded(obj) {
116-
const hexArray = [];
117-
118-
// Function to convert a single BigInt number to a padded hex string
119-
const toPaddedHexString = (bigInt) => {
120-
return "0x" + bigInt.toString(16).padStart(64, "0");
121-
};
122-
123-
// Iterate over each key in the object
124-
for (const key of Object.keys(obj)) {
125-
const value = obj[key];
126-
127-
// Check if the value is an array (handle nested arrays too)
128-
if (Array.isArray(value)) {
129-
// Handle nested arrays for points like A, B, C, etc.
130-
if (Array.isArray(value[0])) {
131-
value.forEach((innerArray) => {
132-
innerArray.forEach((number) => {
133-
hexArray.push(toPaddedHexString(BigInt(number)));
134-
});
135-
});
136-
} else {
137-
// Handle flat arrays for points like eval_a, eval_b, etc.
138-
value.forEach((number) => {
139-
hexArray.push(toPaddedHexString(BigInt(number)));
140-
});
141-
}
142-
} else {
143-
// Handle single values that are not arrays
144-
hexArray.push(toPaddedHexString(BigInt(value)));
145-
}
146-
}
147-
148-
return hexArray;
149-
}
150-
151184
module.exports = router;

smart-contract/contracts/SleepTaskVerifier.sol

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ contract SleepPlonkVerifier {
4747

4848
uint256 constant Qmx = 19810065977015699893274382372956816176806666764352977929464766562334910080176;
4949
uint256 constant Qmy = 10602871389580507768060780547457094541616908927125853053206436589724791345246;
50-
uint256 constant Qlx = 17067688125818277921554704610197304409251942079303235485248472113745811207126;
51-
uint256 constant Qly = 15038381921273365721766267398161759070098245225293316185879148642263169173046;
52-
uint256 constant Qrx = 420235817875178638374730719833551976349370251235940273185603452207991884416;
53-
uint256 constant Qry = 15095431513240586905963815300457193138979720235200403206723105992466875421571;
50+
uint256 constant Qlx = 6556498781104859366505704255018457310478381558922058317977532732577086925537;
51+
uint256 constant Qly = 6759860220405217941353465435084711171030321015096534613504347077836210758435;
52+
uint256 constant Qrx = 15723217574806055333987190059024415425923542713404950821916190278953441543231;
53+
uint256 constant Qry = 19124279183797933268123378341446881707907796840316192809317914216288082096;
5454
uint256 constant Qox = 20978753413861463871269694763622612460077508989144277827766753704214079888680;
5555
uint256 constant Qoy = 12697738035947102355202409221195547802053531745034866321661347033268339520053;
5656
uint256 constant Qcx = 12083439680208740061524008204434001731104025470009211107592123874687837103264;
5757
uint256 constant Qcy = 74424240861454926122143614105341475184383261045662530025250704740020793845;
58-
uint256 constant S1x = 19063984262947622654871544888691619868556226892956005609600971664301404162551;
59-
uint256 constant S1y = 5856339444880172263849733359748529622247205123344595623780750218805592798771;
60-
uint256 constant S2x = 4830506908340608771126468339440569337508870349567510998579651397949201619792;
61-
uint256 constant S2y = 5011667671688626619904548982189140256772632091786904224808178983376549873311;
58+
uint256 constant S1x = 599284386181783115224587969308469101904182324593563741756085378497113901623;
59+
uint256 constant S1y = 2372934128039089451160652510085542332734738198456450913497845545316371041221;
60+
uint256 constant S2x = 7026715400890585860273230640890040274068814207363382902946030530798624791869;
61+
uint256 constant S2y = 12780072469842446313323100967433098350830617904942979904691677937618362080182;
6262
uint256 constant S3x = 27270399904118667807381925043139717540443504686809113412379705168054085113;
6363
uint256 constant S3y = 6268501916999133992321786426477069278290536029986009453590154815690622757270;
6464
uint256 constant k1 = 2;

0 commit comments

Comments
 (0)