Skip to content

Commit

Permalink
Merge pull request #9 from Portkey-Wallet/refactor/remove-old-circuits
Browse files Browse the repository at this point in the history
Refactor: remove old circuits
  • Loading branch information
stevenportkey authored Jul 11, 2024
2 parents 86543a6 + d632486 commit 3507e64
Show file tree
Hide file tree
Showing 66 changed files with 359 additions and 1,844 deletions.
Binary file removed .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
circuit_name: [zkLogin, zkLoginPoseidon, hashMapping] # Add your circuit names here
circuit_name: [zkLogin, zkLoginSha256, idHashMapping] # Add your circuit names here

container:
image: debian:bookworm
Expand Down
8 changes: 1 addition & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,4 @@ jobs:
run: npm install

- name: Test the main circuit
run: npm run test tests/zkLogin.test.ts

- name: Test circuit using Poseidon
run: npm run test tests/zkLoginPoseidon.test.ts

- name: Test hash mapping circuit
run: npm run test tests/hash-mapping.test.ts
run: npm run test
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ node_modules
out
*.ptau
*.zkey
tests/compiled-test-circuit
tests/**/compiled-test-circuit
*.r1cs
*.wtns
bls12381/
bn128/
bn128/
.DS_Store
Binary file removed circuits/.DS_Store
Binary file not shown.
84 changes: 0 additions & 84 deletions circuits/helpers/guardian-identifier-hash.circom

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma circom 2.0.0;
include "./hashtofield.circom";

template IdentifierHashByPoseidon(sub_bytes, salt_bytes){
template IdHashPoseidon(sub_bytes, salt_bytes){
signal input sub[sub_bytes];
signal input salt[salt_bytes];

Expand Down
31 changes: 31 additions & 0 deletions circuits/helpers/idhash_sha256.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
pragma circom 2.0.0;
include "./sha256.circom";
include "./utils.circom";

template IdHashSha256(sub_bytes, salt_bytes){
// inputs
signal input sub[sub_bytes];
signal input sub_len;
signal input salt[salt_bytes];
signal input salt_len;
signal output out[32];

// Step 1: Hash the sub value
component subHasher = Sha256PadAndHash(sub_bytes);
subHasher.in <== sub;
subHasher.in_len <== sub_len;

// Step 2: Combine the hash with the salt
var hash2_bytes = salt_bytes + 32;

component concatenated = CombineBytes(32, salt_bytes);
concatenated.first <== subHasher.out;
concatenated.second <== salt;

// Step 3: Hash the concatenated value
component idHasher = Sha256PadAndHash(hash2_bytes);

idHasher.in <== concatenated.out;
idHasher.in_len <== hash2_bytes;
out <== idHasher.out;
}
45 changes: 0 additions & 45 deletions circuits/helpers/jwt-sub-extract.circom

This file was deleted.

38 changes: 34 additions & 4 deletions circuits/helpers/sha256.circom
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@ include "./string.circom";
include "./sha256general.circom";
include "./sha256partial.circom";

template Sha256PadAndHash(max_bytes){
signal input in[max_bytes];
signal input in_len;
signal output out[32];

var max_padded_len = (max_bytes + 9) + (64 - (max_bytes + 9) % 64);

var paddedBytes[max_padded_len];
for (var i = 0; i < in_len; i++) {
paddedBytes[i] = in[i];
}

for (var i = in_len; i < max_padded_len; i++) {
paddedBytes[i] = 0;
}

component sha256Pad = Sha256PadBytes(max_padded_len);
sha256Pad.in <-- paddedBytes;
sha256Pad.in_bytes <== in_len;

component sha256BB = Sha256BytesOutputBytes(max_padded_len);

sha256BB.in_padded <== sha256Pad.padded_text;
sha256BB.in_len_padded_bytes <== sha256Pad.padded_len;
out <== sha256BB.out;
}

template Sha256BytesOutputBytes(max_num_bytes) {
signal input in_padded[max_num_bytes];
signal input in_len_padded_bytes;
Expand Down Expand Up @@ -53,14 +80,16 @@ template Sha256PadBytes(max_bytes) {
// in_bytes + 1 bytes + 8 bytes length < max_bytes
assert(in_bytes + 9 < max_bytes);

padded_len <-- (in_bytes + 9) + (64 - (in_bytes + 9) % 64);
var padding_len = (in_bytes + 9) == 64 ? 0 : 64 - (in_bytes + 9) % 64;

padded_len <-- (in_bytes + 9) + padding_len;
assert(padded_len % 64 == 0);

component len2bytes = Packed2BytesBigEndian(8);
len2bytes.in <== in_bytes * 8;

for (var i = 0; i < max_bytes; i++) {
padded_text[i] <-- i < in_bytes ? in[i] : (i == in_bytes ? (1 << 7) : (i < padded_len ? (i % 64 < 56 ? 0 : (i % 64 > 56 ? len2bytes.out[(i % 64 - 56)]: 0)) : 0)); // Add the 1 on the end and text length
padded_text[i] <-- i < in_bytes ? in[i] : (i == in_bytes ? (1 << 7) : ((i < padded_len && i >= padded_len - 8) ? len2bytes.out[(i % 64 - 56)]: 0)); // Add the 1 on the end and text length
}
}

Expand All @@ -75,15 +104,16 @@ template Sha256Pad(max_bytes) {

// len.length + 1 bytes + 8 bytes length < max_bytes
assert(len.length + 9 < max_bytes);
var padding_len = (len.length + 9) == 64 ? 0 : 64 - (len.length + 9) % 64;

padded_len <-- (len.length + 9) + (64 - (len.length + 9) % 64);
padded_len <-- (len.length + 9) + padding_len;
assert(padded_len % 64 == 0);

component len2bytes = Packed2BytesBigEndian(8);
len2bytes.in <== len.length * 8;

for (var i = 0; i < max_bytes; i++) {
padded_text[i] <-- i < len.length ? text[i] : (i == len.length ? (1 << 7) : (i < padded_len ? (i % 64 < 56 ? 0 : (i % 64 > 56 ? len2bytes.out[(i % 64 - 56)]: 0)) : 0)); // Add the 1 on the end and text length
padded_text[i] <-- i < len.length ? text[i] : (i == len.length ? (1 << 7) : ((i < padded_len && i >= padded_len - 8) ? len2bytes.out[(i % 64 - 56)]: 0)); // Add the 1 on the end and text length
}
}

Expand Down
14 changes: 6 additions & 8 deletions circuits/hashMapping.circom → circuits/idHashMapping.circom
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
pragma circom 2.0.0;
include "./helpers/guardian-identifier-hash.circom";
include "./helpers/guardian-identifier-hash-poseidon.circom";
include "./helpers/base64.circom";
include "./helpers/jwt-sub-extract.circom";
include "./helpers/idhash_sha256.circom";
include "./helpers/idhash_poseidon.circom";
include "./helpers/jwtchecks.circom";

template Sha256ToPoseidonMapping(maxSubLen, maxSaltLen){
template IdHashMapping(maxSubLen, maxSaltLen){
signal input sub[maxSubLen];
signal input subLen;
signal input salt[maxSaltLen];
signal input saltLen;
component poseidonHasher = IdentifierHashByPoseidon(maxSubLen, maxSaltLen);
component poseidonHasher = IdHashPoseidon(maxSubLen, maxSaltLen);

poseidonHasher.sub <== sub;
poseidonHasher.salt <== salt;

component sha256Hasher = GuardianIdentifierHash(maxSubLen, 16);
component sha256Hasher = IdHashSha256(maxSubLen, 16);

sha256Hasher.sub <== sub;
sha256Hasher.sub_len <== subLen;
Expand All @@ -26,4 +24,4 @@ template Sha256ToPoseidonMapping(maxSubLen, maxSaltLen){
signal output sha256_hash[32] <== sha256Hasher.out;
}

component main = Sha256ToPoseidonMapping(255, 16);
component main = IdHashMapping(255, 16);
13 changes: 0 additions & 13 deletions circuits/testing.circom

This file was deleted.

Loading

0 comments on commit 3507e64

Please sign in to comment.