-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpow_mod.circom
68 lines (61 loc) · 1.91 KB
/
pow_mod.circom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
pragma circom 2.0.0;
include "./circom-ecdsa/circuits/bigint.circom";
// w = 32
// base ** exp mod modulus
// nb is the length of the input number
// exp = 65537
template PowerMod(w, nb, e_bits) {
signal input base[nb];
signal input exp[nb];
signal input modulus[nb];
signal output out[nb];
component muls[e_bits + 2];
for (var i = 0; i < e_bits + 2; i++) {
muls[i] = BigMultModP(w, nb);
// modulus params
for (var j = 0; j < nb; j++) {
muls[i].p[j] <== modulus[j];
}
}
// result/base muls component index
var result_index=0;
var base_index=0;
var muls_index=0;
for (var i = 0; i< e_bits; i++) {
if (i == 0 || i == e_bits - 1) {
if (i == 0) {
for(var j = 0; j < nb; j ++) {
if (j == 0) {
muls[muls_index].a[j] <== 1;
} else {
muls[muls_index].a[j] <== 0;
}
muls[muls_index].b[j] <== base[j];
}
} else {
for(var j = 0; j < nb; j++) {
muls[muls_index].a[j] <== muls[result_index].out[j];
muls[muls_index].b[j] <== muls[base_index].out[j];
}
}
result_index = muls_index;
muls_index++;
}
if (base_index == 0) {
for (var j = 0; j < nb; j++) {
muls[muls_index].a[j] <== base[j];
muls[muls_index].b[j] <== base[j];
}
} else {
for (var j = 0; j < nb; j++) {
muls[muls_index].a[j] <== muls[base_index].out[j];
muls[muls_index].b[j] <== muls[base_index].out[j];
}
}
base_index = muls_index;
muls_index++;
}
for (var i = 0; i < nb; i++) {
out[i] <== muls[result_index].out[i];
}
}