From 3ecc048fde5593a140c49e2640b0c7bf378fc8a2 Mon Sep 17 00:00:00 2001 From: "frederick.lyu" Date: Wed, 6 Mar 2024 17:30:33 -0800 Subject: [PATCH] fix decode base64 bug --- circuits/jwt.circom | 20 +++++--------------- scripts/generate_input.ts | 6 ++++-- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/circuits/jwt.circom b/circuits/jwt.circom index b3ae3f6..c043e75 100644 --- a/circuits/jwt.circom +++ b/circuits/jwt.circom @@ -10,7 +10,7 @@ include "./jwt_type_regex.circom"; include "./ascii.circom"; include "./timestamp.circom"; -template JWTVerify(max_msg_bytes, max_json_bytes, n, k) { +template JWTVerify(max_msg_bytes, max_body_bytes,max_json_bytes, n, k) { signal input message[max_msg_bytes]; signal input modulus[k]; // rsa pubkey signal input signature[k]; @@ -66,21 +66,11 @@ template JWTVerify(max_msg_bytes, max_json_bytes, n, k) { // decode to JSON format component message_b64 = Base64Decode(max_json_bytes); - component eqs[max_msg_bytes]; - signal int[max_msg_bytes]; + signal body[max_body_bytes] <== VarShiftLeft(max_msg_bytes, max_body_bytes)(message, period_idx + 1); - for (var i = 0; i < max_msg_bytes - 1; i++) { - eqs[i] = GreaterEqThan(15); - eqs[i].in[0] <== i; - eqs[i].in[1] <== period_idx; - - var i_plus_one = eqs[i].out; - var i_normal = 1 - eqs[i].out; - - int[i] <== (message[i] * i_normal); - message_b64.in[i] <== (message[i + 1] * (i_plus_one)) + int[i]; + for (var i = 0; i < max_body_bytes; i++) { + message_b64.in[i] <== body[i]; } - message_b64.in[max_msg_bytes - 1] <== 0; /************************** JWT REGEXES *****************************/ @@ -142,4 +132,4 @@ template JWTVerify(max_msg_bytes, max_json_bytes, n, k) { less_exp_time.out === 1; } -component main { public [ modulus, time ] } = JWTVerify(1024, 766, 121, 17); +component main { public [ modulus, time ] } = JWTVerify(1024, 900, 673, 121, 17); diff --git a/scripts/generate_input.ts b/scripts/generate_input.ts index 8a2e475..16a3ca8 100644 --- a/scripts/generate_input.ts +++ b/scripts/generate_input.ts @@ -106,7 +106,8 @@ function AsciiArrayToString(arr: Buffer) { function findDomain(msg: string) { let domain_idx; let domain; - var s = Buffer.from(msg, "base64"); + const allParts = msg.split("."); + var s = Buffer.from(allParts[1], "base64"); var json = AsciiArrayToString(s); const email_regex = /([-a-zA-Z._+]+)@([-a-zA-Z]+).([a-zA-Z]+)/; const match = json.match(email_regex); @@ -120,7 +121,8 @@ function findDomain(msg: string) { } function findTimestampInJSON(msg: string) { - var s = Buffer.from(msg, "base64"); + const allParts = msg.split("."); + var s = Buffer.from(allParts[1], "base64"); var json = AsciiArrayToString(s); let time_index = json.indexOf(`"exp":`) + 6; let timestamp = json.substring(time_index, time_index + 10);