Circom 对 if 语句的使用非常严格。必须遵守以下规则:
signal不能用于改变if语句的行为。- 不能在
if语句内部为signal赋值。
下面的示例电路展示了这两种违规行为:
template Foo() {
signal input in;
signal input cond;
signal output out;
// if-statements cannot depend on
// values not known at compile time
if (in == 3) {
// assigning a value inside an if-statement
// whose value is unknown at compile time
// is not allowed
out <== 4;
}
}
如果 if 语句不受任何 signal 的影响,并且不影响任何 signal,那么它们是可以被接受的。
实际上,它们不属于底层的 Rank 1 Constraint system (R1CS)。
例如,如果我们想要计算列表中的最大值(不生成约束),我们可以使用以下典型的解决方案。Circom 接受这种做法,因为它不涉及任何 signal:
var max;
for (var i = 0; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
这种计算不会创建任何约束,仅仅是为了方便。
Circom 中的分支
看起来 Circom 似乎无法进行条件分支,但事实并非如此。要在 Circom 中创建条件分支,必须执行语句的所有分支,将“不需要”的分支乘以 0,将“正确”的分支乘以 1。
包含分支的计算示例
假设我们正在对以下计算进行建模:
def foo(x):
if x == 5:
out = 14
elif x == 9:
out = 22
elif x == 10:
out = 23
else
out = 45
return out
由于 x 和 out 之间没有明确的数学联系,最好尽可能直接地对这个条件进行建模。下面是我们如何用数学方式描述该条件语句:
- 如果
x等于 5,则x_eq_5等于 1,否则为 0。这可以通过IsEqual()([x, 5])来实现 - 当
x等于 9 时,x_eq_9等于 1,否则为 0 - 当
x等于 10 时,x_eq_10等于 1,否则为 0 - 当上述所有项(
x_eq_5、x_eq_9、x_eq_10)均为 0 时,otherwise等于 1。
我们可以使用 Circomlib 中的 IsEqual() template 将值赋给 x_eq_5、x_eq_9、x_eq_10 和 otherwise 这些 signal——这同时也强制它们的值只能是 0 或 1。为了确保恰好只有一个 signal 为 1,其余全为 0,我们使用以下约束:
一般来说,我们会创建“二进制开关(binary switches)”,当特定分支处于活动状态时,其值为 1,否则为 0。然后,我们将所有分支的计算结果相加,每个分支都要乘以其对应的开关。
由于 中只有一个分支会处于活动状态,其余的计算结果都会乘以 0,因此不会产生影响。
这是完整的电路:
include "./node_modules/circomlib/circuits/comparators.circom";
template MultiBranchConditional() {
signal input x;
signal output out;
signal x_eq_5;
signal x_eq_9;
signal x_eq_10;
signal otherwise;
x_eq_5 <== IsEqual()([x, 5]);
x_eq_9 <== IsEqual()([x, 9]);
x_eq_10 <== IsEqual()([x, 10]);
otherwise <== IsZero()(x_eq_5 + x_eq_9 + x_eq_10);
signal branches_5_9;
signal branches_10_otherwise;
branches_5_9 <== x_eq_5 * 14 + x_eq_9 * 22;
branches_10_otherwise <== x_eq_10 * 23 + otherwise * 45;
out <== branches_5_9 + branches_10_otherwise;
}
component main = MultiBranchConditional();
为了使代码更整洁,最好将这个四路分支作为一个单独的 component——这样,我们就可以重用该分支 template。
include "./node_modules/circomlib/circuits/comparators.circom";
template Branch4(cond1, cond2, cond3, branch1, branch2, branch3, branch4) {
signal input x;
signal output out;
signal switch1;
signal switch2;
signal switch3;
signal otherwise;
switch1 <== IsEqual()([x, cond1]);
switch2 <== IsEqual()([x, cond2]);
switch3 <== IsEqual()([x, cond3]);
otherwise <== IsZero()(switch1 + switch2 + switch3);
signal branches_1_2 <== switch1 * branch1 + switch2 * branch2;
signal branches_3_4 <== switch3 * branch3 + otherwise * branch4;
out <== branches_1_2 + branches_3_4;
}
template MultiBranchConditional() {
signal input x;
signal output out;
component branch4 = Branch4(5,9,10,14,22,23,45);
branch4.x <== x;
branch4.out ==> out; // same as out <== branch4.out
}
component main = MultiBranchConditional();
涉及多个分支时的代码
在上面的代码中,我们必须显式地编写 switch1、switch2、…、otherwise,如果代码有很多分支,这将会非常繁琐。
相反,我们可以将计算过程视为开关和分支的内积(广义点积):
上述公式确保了有且仅有一个开关处于活动状态(等于 1),而所有其他开关均为 0,从而使相应的分支成为输出结果。
为了在 Circom 中高效地实现这一点,我们使用了 multiplexer.circom 中的 EscalarProduct template。该 template 接收两个长度为 n 的向量,将它们逐个元素相乘,并对结果求和。在下面的代码块中,我们使用 EscalarProduct 将每个开关与每个分支相乘。请注意,最后一个开关和分支的处理方式略有不同,因为最后一个条件是一个“包罗万象(catch-all)”的 else 语句。
include "./node_modules/circomlib/circuits/comparators.circom";
include "./node_modules/circomlib/circuits/multiplexer.circom";
template BranchN(n) {
assert(n > 1); // too small
signal input x;
// conds n - 1 is otherwise
signal input conds[n - 1];
// branch n - 1 is the otherwise branch
signal input branches[n];
signal output out;
signal switches[n];
component EqualityChecks[n - 1];
// only compute IsEqual up to the second-to-last switch
for (var i = 0; i < n - 1; i++) {
EqualityChecks[i] = IsEqual();
EqualityChecks[i].in[0] <== x;
EqualityChecks[i].in[1] <== conds[i];
switches[i] <== EqualityChecks[i].out;
}
// check the last condition
var total = 0;
for (var i = 0; i < n - 1; i++) {
total += switches[i];
}
// if none of the first n - 1 switches
// are active, then `otherwise` must be 1
switches[n - 1] <== IsZero()(total);
component InnerProduct = EscalarProduct(n);
for (var i = 0; i < n; i++) {
InnerProduct.in1[i] <== switches[i];
InnerProduct.in2[i] <== branches[i];
}
out <== InnerProduct.out;
}
template MultiBranchConditional() {
signal input x;
signal output out;
component branchn = BranchN(4);
var conds[3] = [5, 9, 10];
var branches[4] = [14, 22, 23, 45];
for (var i = 0; i < 4; i++) {
if (i < 3) {
branchn.conds[i] <== conds[i];
}
branchn.branches[i] <== branches[i];
}
branchn.x <== x;
branchn.out ==> out; // same as out <== branch4.out
}
component main = MultiBranchConditional();
什么时候可以使用 if 语句?
假设我们想创建一个 template,根据电路参数返回一个完全不同的电路。例如,如果我们正在创建一个 Max component,它接收一个数组 in[n] 并返回最大值,那么如果 n 等于 1,直接返回索引中的第 0 个元素会更高效。
下面,我们展示了一个与定义约束一起使用时,合理使用 if 语句的示例。在这里,if 语句在编译时执行,因此该 template 将生成一个定义明确的电路:
include "./node_modules/circomlib/circuits/comparators.circom";
template Max(n) {
signal input in[n];
signal output out;
assert(n > 0);
if (n == 1) {
out <== in[0];
}
// it is okay to declare signals inside
// the if-statement because the evaluation
// of the if-statement is known at compile time
else if (n == 2) {
signal zeroGtOne;
signal branch0;
signal branch1;
zeroGtOne <== GreaterThan(252)([in[0], in[1]]);
branch0 <== zeroGtOne * in[0];
branch1 <== (1 - zeroGtOne) * in[1];
out <== branch0 + branch1;
}
else {
// case for n > 2
}
}
component main = Max(2);
条件语句对 ZK 并不友好
一个关键的设计影响是,Circom 电路中的每个条件都会使其大小加倍,因为分支无法“短路(short-circuited)”。与传统编程不同,所有的分支都会被计算。
当使用 ZK 证明计算时,我们希望进行以下优化:
- 尽可能少的分支,因为每个分支都会增加 prover 的工作量。
- 最小化所有分支的总计算成本,而不仅仅是基于分支概率的预期计算。
- 尽可能避免使用条件语句。