Skip to content

Commit

Permalink
Add comments to StringBodies circuit template (aptos-labs#14675)
Browse files Browse the repository at this point in the history
  • Loading branch information
rex1fernando authored Sep 18, 2024
1 parent 5daddb7 commit 7d41163
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions keyless/circuit/templates/helpers/misc.circom
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ template EmailVerifiedCheck(maxEVNameLen, maxEVValueLen, maxUIDNameLen) {
}
}


// Given an array of ascii characters representing a JSON object, output a binary array demarquing
// input = { asdfsdf "asdf" }
// output = 000000000001111000
template StringBodies(len) {
signal input in[len];
signal output out[len];
Expand Down Expand Up @@ -126,21 +128,24 @@ template StringBodies(len) {
for (var i = 1; i < len; i++) {
var is_quote = IsEqual()([in[i], 34]);
var prev_is_odd_backslash = adjacent_backslash_parity[i-1];
quotes[i] <== is_quote * (1 - prev_is_odd_backslash);
quote_parity_1[i] <== quotes[i] * (1 - quote_parity[i-1]);
quote_parity_2[i] <== (1 - quotes[i]) * quote_parity[i-1];
quote_parity[i] <== quote_parity_1[i] + quote_parity_2[i];
quotes[i] <== is_quote * (1 - prev_is_odd_backslash); // 1 iff there is a non-escaped quote at this position
quote_parity_1[i] <== quotes[i] * (1 - quote_parity[i-1]); // 1 iff quotes[i] == 1 and quote_parity[i-1] == 0, else 0
quote_parity_2[i] <== (1 - quotes[i]) * quote_parity[i-1]; // 1 iff quotes[i] == 0 and quote_party[i-1] == 1, else 0
quote_parity[i] <== quote_parity_1[i] + quote_parity_2[i]; // Or of previous two, i.e., XOR(quotes[i], quote_parity[i-1])
}

// input = { asdfsdf "asdf" }
// output = 000000000011111000
// i.e., still has offset-by-one error

out[0] <== 0;

for (var i = 1; i < len; i++) {
out[i] <== AND()(quote_parity[i-1], quote_parity[i]);
out[i] <== AND()(quote_parity[i-1], quote_parity[i]); // remove offset error
}
}



// Given a base64-encoded array `in`, max length `maxN`, and actual unpadded length `n`, returns
// the actual length of the decoded string
template Base64DecodedLength(maxN) {
Expand Down

0 comments on commit 7d41163

Please sign in to comment.