Skip to content

Commit 70d6eb9

Browse files
authored
Merge pull request #13 from zkp-application/circom2.0
feat(upgrade circom2.0): Circom2.0
2 parents 9dffda9 + 8bfaa17 commit 70d6eb9

20 files changed

+2999
-7507
lines changed

.gitmodules

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[submodule "circomlib"]
22
path = circomlib
33
url = https://github.com/iden3/circomlib.git
4-
[submodule "circom-bigint"]
5-
path = circom-bigint
6-
url = https://github.com/jacksoom/circom-bigint.git
4+
5+
[submodule "circom-ecdsa"]
6+
path = circom-ecdsa
7+
url = https://github.com/agnxsh/circom-ecdsa.git
8+
branch = 0xagnish/circom_tester-fix

README.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
# circom-rsa-verify
2-
This repository contains an implementation of a Zero Knowledge Proof for RSA signature verify for the circom language.
3-
Currently supported pkcs1v15 + sha256 and exponent is 65537
2+
3+
This repository contains an implementation of a Zero Knowledge Proof for RSA signature verify for the [Circom](https://docs.circom.io) language.
4+
Currently supported pkcs1v15 + sha256 and exponent is 65537. The Montgomery Exponentiation algorithm and Montgomery CIOS product is used to calculate large numbers [Modular exponentiation](https://en.wikipedia.org/wiki/Modular_exponentiation)
5+
46
# Getting started
7+
58
Running circuits test cases
9+
610
```sh
7-
git submodule update --init --recursive; npm install; npm test
11+
git submodule update --init --recursive; npm i; npm test
812
```
913

1014
## Circuits Benchmark
15+
1116
RSA verify: pkcs1v15/sha256/2048 bits key
12-
* Env: Mac mini (M1, 2020). 8 cores. 8 threads
13-
* Memory consumption: 1.7G
14-
* Time consumption: 150s
15-
## The circom compiler
16-
17-
This repository uses a modified version of the circom compiler found at
18-
[alex-ozdemir/circom](https://github.com/alex-ozdemir/circom).
19-
It includes a few extra features not found in the original:
20-
21-
* Clearer error printouts
22-
* More comprehensive/informative treatment of `log` statements
23-
* A new type `int` which enables bigints to be handled during witness
24-
computations.
25-
* `compute` blocks
17+
18+
* Env: Mac mini (M1, 2020). 8 cores. 8 threads
19+
20+
Circuit infomation
21+
22+
* snarkJS: Curve: bn-128
23+
* snarkJS: # of Wires: 530676
24+
* snarkJS: # of Constraints: 536212
25+
* snarkJS: # of Private Inputs: 0
26+
* snarkJS: # of Public Inputs: 100
27+
* snarkJS: # of Labels: 583860
28+
* snarkJS: # of Outputs: 0
29+
30+
## Ref
31+
32+
2. [Arithmetic of Finite Fields](https://www.researchgate.net/publication/319538235_Arithmetic_of_Finite_Fields)

circom-bigint

Lines changed: 0 additions & 1 deletion
This file was deleted.

circom-ecdsa

Submodule circom-ecdsa added at a9b0e06

circomlib

Submodule circomlib updated 164 files

circuits/pow_mod.circom

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
include "../circom-bigint/circomlib/circuits/bitify.circom"
2-
include "../circom-bigint/circuits/mult.circom"
1+
pragma circom 2.0.0;
2+
3+
include "../circom-ecdsa/circuits/bigint.circom";
34
// w = 32
45
// base ** exp mod modulus
56
// nb is the length of the input number
67
// exp = 65537
7-
template PowerModv2(w, nb, e_bits) {
8+
template PowerMod(w, nb, e_bits) {
89
signal input base[nb];
910
signal input exp[nb];
1011
signal input modulus[nb];
@@ -14,10 +15,10 @@ template PowerModv2(w, nb, e_bits) {
1415

1516
component muls[e_bits + 2];
1617
for (var i = 0; i < e_bits + 2; i++) {
17-
muls[i] = MultiplierReducer(w, nb);
18+
muls[i] = BigMultModP(w, nb);
1819
// modulus params
1920
for (var j = 0; j < nb; j++) {
20-
muls[i].modulus[j] <== modulus[j];
21+
muls[i].p[j] <== modulus[j];
2122
}
2223
}
2324

@@ -38,8 +39,8 @@ template PowerModv2(w, nb, e_bits) {
3839
}
3940
} else {
4041
for(var j = 0; j < nb; j++) {
41-
muls[muls_index].a[j] <== muls[result_index].prod[j];
42-
muls[muls_index].b[j] <== muls[base_index].prod[j];
42+
muls[muls_index].a[j] <== muls[result_index].out[j];
43+
muls[muls_index].b[j] <== muls[base_index].out[j];
4344
}
4445
}
4546
result_index = muls_index;
@@ -53,17 +54,15 @@ template PowerModv2(w, nb, e_bits) {
5354
}
5455
} else {
5556
for (var j = 0; j < nb; j++) {
56-
muls[muls_index].a[j] <== muls[base_index].prod[j];
57-
muls[muls_index].b[j] <== muls[base_index].prod[j];
57+
muls[muls_index].a[j] <== muls[base_index].out[j];
58+
muls[muls_index].b[j] <== muls[base_index].out[j];
5859
}
5960
}
6061
base_index = muls_index;
6162
muls_index++;
6263
}
6364

6465
for (var i = 0; i < nb; i++) {
65-
out[i] <== muls[result_index].prod[i];
66+
out[i] <== muls[result_index].out[i];
6667
}
6768
}
68-
69-

circuits/rsa_verify.circom

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1+
pragma circom 2.0.0;
2+
13
include "./pow_mod.circom";
2-
include "../circom-bigint/circomlib/circuits/bitify.circom"
4+
5+
template NumToBits(n) {
6+
signal input in;
7+
signal output out[n];
8+
var lc1=0;
9+
10+
var e2=1;
11+
for (var i = 0; i<n; i++) {
12+
out[i] <-- (in >> i) & 1;
13+
out[i] * (out[i] -1 ) === 0;
14+
lc1 += out[i] * e2;
15+
e2 = e2+e2;
16+
}
17+
18+
lc1 === in;
19+
}
320

421
// Pkcs1v15 + Sha256
522
// exp 65537
@@ -11,7 +28,7 @@ template RsaVerifyPkcs1v15(w, nb, e_bits, hashLen) {
1128
signal input hashed[hashLen];
1229

1330
// sign ** exp mod modulus
14-
component pm = PowerModv2(w, nb, e_bits);
31+
component pm = PowerMod(w, nb, e_bits);
1532
for (var i = 0; i < nb; i++) {
1633
pm.base[i] <== sign[i];
1734
pm.exp[i] <== exp[i];
@@ -30,7 +47,7 @@ template RsaVerifyPkcs1v15(w, nb, e_bits, hashLen) {
3047
pm.out[4] === 217300885422736416;
3148
pm.out[5] === 938447882527703397;
3249
// // remain 24 bit
33-
component num2bits_6 = Num2Bits(w);
50+
component num2bits_6 = NumToBits(w);
3451
num2bits_6.in <== pm.out[6];
3552
var remainsBits[32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0];
3653
for (var i = 0; i < 32; i++) {
@@ -49,3 +66,4 @@ template RsaVerifyPkcs1v15(w, nb, e_bits, hashLen) {
4966
// 0b1111111111111111111111111111111111111111111111111
5067
pm.out[31] === 562949953421311;
5168
}
69+

0 commit comments

Comments
 (0)