Replies: 1 comment
-
这里是我写的版本,供参考: include "node_modules/circomlib/circuits/comparators.circom";
template Compare() {
signal input in[2];
signal output out[2];
component g = GreaterEqThan(20); // bits length
g.in[0] <== in[0];
g.in[1] <== in[1];
g.out * (1 - g.out) === 0;
out[0] <== (in[0] - in[1]) * g.out + in[1];
out[1] <== (in[1] - in[0]) * g.out + in[0];
}
template BubbleSort(N) {
signal input in[N];
signal output out[N];
component c[N][N + 1];
for (var l = 0; l < N-1; l++){
for (var j = 0; j < N-l-1; j++) {
c[l][j] = Compare();
if (j == 0) {
if (l == 0) {
c[l][j].in[0] <== in[0];
c[l][j].in[1] <== in[1];
} else {
c[l][j].in[0] <== c[l-1][j].out[0];
c[l][j].in[1] <== c[l-1][j+1].out[0];
}
} else {
if (l == 0) {
c[l][j].in[0] <== c[0][j-1].out[1];
c[l][j].in[1] <== in[j+1];
} else {
c[l][j].in[0] <== c[l][j-1].out[1];
c[l][j].in[1] <== c[l-1][j+1].out[0];
}
}
}
}
for (var j = 1; j < N; j++){
out[j] <== c[N-1-j][j-1].out[1];
}
out[0] <== c[N-2][0].out[0];
}
component main = BubbleSort(6); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions