diff --git a/openid-zkp-auth/circuits/helpers/strings.circom b/openid-zkp-auth/circuits/helpers/strings.circom index e064585169..b1d375e979 100644 --- a/openid-zkp-auth/circuits/helpers/strings.circom +++ b/openid-zkp-auth/circuits/helpers/strings.circom @@ -90,37 +90,6 @@ template Slice(inLen, outLen) { } } -// in[index: index + length*groupLen] + [0] * (outLen - length)*groupLen -// template SliceGrouped(inLen, outLen, groupLen) { -// signal input in[inLen]; -// signal input index; -// signal input length; - -// signal output out[outLen * groupLen]; - -// // eqs[i] = 1 if i = index, 0 otherwise -// signal eqs[inLen] <== OneBitVector(inLen)(index); -// // lt[i] = 1 if i < length, 0 otherwise -// signal lts[outLen] <== LTBitVector(outLen)(length); - -// signal tmp[outLen]; -// for(var i = 0; i < outLen; i++) { -// var arr[inLen]; -// for (var j = 0; j < inLen; j++) { -// if (j < i) { -// arr[j] = 0; -// } else { -// arr[j] = eqs[j - i]; -// } -// } -// tmp[i] <== EscalarProduct(inLen)(arr, in); - -// for (var j = 0; j < groupLen; j++) { -// out[i * groupLen + j] <== tmp[i] * lts[i]; -// } -// } -// } - /** Checks if an ASCII-encoded substring exists in a Base64-encoded string. @@ -221,4 +190,51 @@ template ASCIISubstrExistsInB64(b64StrLen, maxA) { } MyForceEqualIfEnabled()(enabled_2[i \ 8], [A_in_bits[i], B_in_bits[i + 4]]); } +} + +/** +JSON value extractor. + +Given a JSON member with string JSON values as input, + parses it according to JSON grammar and returns the JSON value. + +" s u b " x x x x : x x " 1 2 3 4 5 " x x x x x , 0 0 0 0 0 + | | | | + i j k l + +Check that + - in[0..3] = "sub" + - in[3..i] = ws + - in[i] = '"' + - in[i+1..j] = ws + - in[j] = '"' + - in[k] = '"' + - in[k+1..l] = ws + - in[l] = ',' or '}' + - in[l+1..endIndex] = 0 + +Where ws = " " | "\t" | "\n" | "\r" or 0x20 | 0x09 | 0x0A | 0x0D +**/ +template JSONValueExtractor(maxMemberLen, maxValueLen) { + signal input in[maxMemberLen]; + signal input colonIndex; + signal input startQuoteIndex; + signal input endQuoteIndex; + signal input endCharIndex; + + signal output out[maxValueLen]; + + in[0] === 0x22; // '"' + in[1] === 0x73; // 's' + in[2] === 0x75; // 'u' + in[3] === 0x62; // 'b' + in[4] === 0x22; // '"' + + signal startquote <== SingleMultiplexer(maxMemberLen)(in, startQuoteIndex); + startquote === 0x22; // '"' + signal endquote <== SingleMultiplexer(maxMemberLen)(in, endQuoteIndex); + endquote === 0x22; // '"' + + signal lastchar <== SingleMultiplexer(maxMemberLen)(in, endCharIndex); + (lastchar - 0x2C) * (lastchar - 0x7D) === 0; // ',' or '}' } \ No newline at end of file