-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from zkemail/Docs-update
Merge utils
- Loading branch information
Showing
9 changed files
with
268 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
pragma circom 2.1.5; | ||
|
||
include "circomlib/circuits/bitify.circom"; | ||
include "circomlib/circuits/comparators.circom"; | ||
include "circomlib/circuits/poseidon.circom"; | ||
include "./constants.circom"; | ||
|
||
function compute_ints_size(bytes_size) { | ||
var pack_bytes = pack_bytes_const(); | ||
var remain = bytes_size % pack_bytes; | ||
var num_chunk = (bytes_size - remain) / pack_bytes; | ||
if(remain>0) { | ||
num_chunk += 1; | ||
} | ||
return num_chunk; | ||
} | ||
|
||
template Bytes2Ints(bytes_size) { | ||
var num_chunk = compute_ints_size(bytes_size); | ||
signal input bytes[bytes_size]; | ||
signal output ints[num_chunk]; | ||
|
||
var pack_bytes = pack_bytes_const(); | ||
signal ints_sums[num_chunk][pack_bytes]; | ||
for(var i=0; i<num_chunk; i++) { | ||
for(var j=0; j<pack_bytes; j++) { | ||
var idx = pack_bytes*i+j; | ||
if(idx>=bytes_size) { | ||
ints_sums[i][j] <== ints_sums[i][j-1]; | ||
} else if (j==0){ | ||
ints_sums[i][j] <== bytes[idx]; | ||
} else { | ||
ints_sums[i][j] <== ints_sums[i][j-1] + (1<<(8*j)) * bytes[idx]; | ||
} | ||
} | ||
} | ||
for(var i=0; i<num_chunk; i++) { | ||
ints[i] <== ints_sums[i][pack_bytes-1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
pragma circom 2.1.5; | ||
|
||
function email_max_bytes_const() { | ||
return 256; | ||
} | ||
|
||
function domain_len_const() { | ||
return 255; | ||
} | ||
|
||
function invitation_code_len_const() { | ||
return 64; | ||
} | ||
|
||
function field_pack_bits_const() { | ||
return 248; | ||
} | ||
|
||
function pack_bytes_const() { | ||
return 31; | ||
} | ||
|
||
function timestamp_len_const() { | ||
return 10; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
pragma circom 2.1.5; | ||
|
||
include "circomlib/circuits/bitify.circom"; | ||
include "circomlib/circuits/comparators.circom"; | ||
include "circomlib/circuits/poseidon.circom"; | ||
include "./constants.circom"; | ||
|
||
|
||
// `in` is a big-endtian digit string of `out`. | ||
template Digit2Int(n) { | ||
signal input in[n]; | ||
signal output out; | ||
|
||
component digit2int[n]; | ||
signal sums[n+1]; | ||
sums[0] <== 0; | ||
for(var i = 0; i < n; i++) { | ||
digit2int[i] = Digit2Int1(); | ||
digit2int[i].in <== in[i]; | ||
sums[i+1] <== 10 * sums[i] + digit2int[i].out; | ||
} | ||
out <== sums[n]; | ||
} | ||
|
||
template Digit2Int1() { | ||
signal input in; | ||
signal output out; | ||
out <== in - 48; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
pragma circom 2.1.5; | ||
|
||
include "circomlib/circuits/poseidon.circom"; | ||
|
||
// email_addr_commit = hash(rand, emailAddr||0..0) | ||
template EmailAddrCommit(num_ints) { | ||
signal input rand; | ||
signal input email_addr_ints[num_ints]; | ||
signal output commit; | ||
|
||
component poseidon = Poseidon(1+num_ints); | ||
poseidon.inputs[0] <== rand; | ||
for(var i=0; i<num_ints; i++) { | ||
poseidon.inputs[1+i] <== email_addr_ints[i]; | ||
} | ||
commit <== poseidon.out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
pragma circom 2.1.5; | ||
|
||
include "circomlib/circuits/poseidon.circom"; | ||
|
||
// email_addr_pointer = hash(relayerRand, emailAddr||0..0) | ||
template EmailAddrPointer(num_ints) { | ||
signal input relayer_rand; | ||
signal input email_addr_ints[num_ints]; | ||
signal output pointer; | ||
|
||
component poseidon = Poseidon(1+num_ints); | ||
poseidon.inputs[0] <== relayer_rand; | ||
for(var i=0; i<num_ints; i++) { | ||
poseidon.inputs[1+i] <== email_addr_ints[i]; | ||
} | ||
pointer <== poseidon.out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
pragma circom 2.1.5; | ||
|
||
include "circomlib/circuits/poseidon.circom"; | ||
|
||
template EmailNullifier() { | ||
// signal input header_hash[256]; | ||
signal input sign_hash; | ||
|
||
signal output email_nullifier; | ||
|
||
// var field_pack_bits = field_pack_bits_const(); | ||
|
||
// signal header_hash_int[field_pack_bits+1]; | ||
// header_hash_int[0] <== 0; | ||
// for(var i = 0; i < field_pack_bits; i++) { | ||
// header_hash_int[i+1] <== 2 * header_hash_int[i] + header_hash[i]; | ||
// } | ||
// signal email_nullifier_input[1]; | ||
// email_nullifier_input[0] <== sign_hash; | ||
email_nullifier <== Poseidon(1)([sign_hash]); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
pragma circom 2.1.5; | ||
|
||
include "circomlib/circuits/poseidon.circom"; | ||
|
||
template HashSign(n,k) { | ||
// signal input pubkey[k]; | ||
signal input signature[k]; | ||
|
||
// signal output pubkey_hash; | ||
signal output sign_hash; | ||
|
||
var k2_chunked_size = k >> 1; | ||
if(k % 2 == 1) { | ||
k2_chunked_size += 1; | ||
} | ||
signal output sign_ints[k2_chunked_size]; | ||
|
||
// signal pubkey_hash_input[k2_chunked_size]; | ||
// for(var i = 0; i < k2_chunked_size; i++) { | ||
// if(i==k2_chunked_size-1 && k2_chunked_size % 2 == 1) { | ||
// pubkey_hash_input[i] <== pubkey[2*i]; | ||
// } else { | ||
// pubkey_hash_input[i] <== pubkey[2*i] + (1<<n) * pubkey[2*i+1]; | ||
// } | ||
// } | ||
// pubkey_hash <== Poseidon(k2_chunked_size)(pubkey_hash_input); | ||
for(var i = 0; i < k2_chunked_size; i++) { | ||
if(i==k2_chunked_size-1 && k2_chunked_size % 2 == 1) { | ||
sign_ints[i] <== signature[2*i]; | ||
} else { | ||
sign_ints[i] <== signature[2*i] + (1<<n) * signature[2*i+1]; | ||
} | ||
} | ||
sign_hash <== Poseidon(k2_chunked_size)(sign_ints); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
pragma circom 2.1.5; | ||
|
||
include "circomlib/circuits/bitify.circom"; | ||
include "circomlib/circuits/comparators.circom"; | ||
include "circomlib/circuits/poseidon.circom"; | ||
include "./constants.circom"; | ||
|
||
// `in` is a big-endtian hex string of `out`. | ||
template Hex2Field() { | ||
signal input in[64]; | ||
signal output out; | ||
signal bytes[32] <== Hex2Ints(64)(in); | ||
signal sums[33]; | ||
sums[0] <== 0; | ||
for(var i = 0; i < 32; i++) { | ||
sums[i+1] <== 256 * sums[i] + bytes[i]; | ||
} | ||
out <== sums[32]; | ||
} | ||
|
||
template Hex2Ints(n) { | ||
assert(n % 2 == 0); | ||
var bytes = n / 2; | ||
signal input in[n]; | ||
signal output out[bytes]; | ||
|
||
component hex2int[n]; | ||
for(var i = 0; i < bytes; i++) { | ||
for(var j = 0; j < 2; j++) { | ||
hex2int[2*i+j] = Hex2Int1(); | ||
hex2int[2*i+j].in <== in[2*i+j]; | ||
} | ||
out[i] <== 16 * hex2int[2*i].out + hex2int[2*i+1].out; | ||
} | ||
} | ||
|
||
template Hex2Int1() { | ||
signal input in; | ||
signal output out; | ||
|
||
// the given char is [0-9]. | ||
signal is_digit_min_in[2]; | ||
is_digit_min_in[0] <== in; | ||
is_digit_min_in[1] <== 48; | ||
signal is_digit_min <== GreaterEqThan(8)(is_digit_min_in); | ||
signal is_digit_max_in[2]; | ||
is_digit_max_in[0] <== in; | ||
is_digit_max_in[1] <== 57; | ||
signal is_digit_max <== LessEqThan(8)(is_digit_max_in); | ||
signal is_digit <== is_digit_min * is_digit_max; | ||
|
||
// the given char is [a-f]. | ||
signal is_alphabet_min_in[2]; | ||
is_alphabet_min_in[0] <== in; | ||
is_alphabet_min_in[1] <== 97; | ||
signal is_alphabet_min <== GreaterEqThan(8)(is_alphabet_min_in); | ||
signal is_alphabet_max_in[2]; | ||
is_alphabet_max_in[0] <== in; | ||
is_alphabet_max_in[1] <== 102; | ||
signal is_alphabet_max <== LessEqThan(8)(is_alphabet_max_in); | ||
signal is_alphabet <== is_alphabet_min * is_alphabet_max; | ||
|
||
is_digit + is_alphabet === 1; | ||
signal digit_int <== is_digit * (in - 48); | ||
// 87 = 97 - 10 | ||
signal alphabet_int <== is_alphabet * (in - 87); | ||
out <== digit_int + alphabet_int; | ||
} |