Skip to content

Commit

Permalink
Optimized BigAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
1KitCat1 committed Oct 24, 2024
1 parent 5449f41 commit 6ad43aa
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions circuits/bigInt/bigInt.circom
Original file line number Diff line number Diff line change
Expand Up @@ -181,32 +181,42 @@ template SplitThree(CHUNK_SIZE, M, CHUNK_NUMBER) {
in === small + medium * (1 << CHUNK_SIZE) + big * (1 << CHUNK_SIZE + M);
}

// a[i], b[i] in 0... 2**n-1
// represent a = a[0] + a[1] * 2**n + .. + a[k - 1] * 2**(n * k)
template BigAddNoCarry(n, k) {
assert(n <= 252);

signal input a[k];
signal input b[k];
signal output out[k];

for (var i = 0; i < k; i++) {
out[i] <== a[i] + b[i];
}
}

// a[i], b[i] in 0... 2**CHUNK_SIZE-1
// represent a = a[0] + a[1] * 2**CHUNK_SIZE + .. + a[CHUNK_NUMBER - 1] * 2**(CHUNK_SIZE * CHUNK_NUMBER)
template BigAdd(CHUNK_SIZE, CHUNK_NUMBER) {
assert(CHUNK_SIZE <= 252);
signal input a[CHUNK_NUMBER];
signal input b[CHUNK_NUMBER];
signal output out[CHUNK_NUMBER + 1];
template BigAdd(n, k) {
signal input a[k];
signal input b[k];
signal output out[k + 1];

component unit0 = ModSum(CHUNK_SIZE);
unit0.a <== a[0];
unit0.b <== b[0];
out[0] <== unit0.sum;
component add = BigAddNoCarry(n, k);
for (var i = 0; i < k; i++) {
add.a[i] <== a[i];
add.b[i] <== b[i];
}

component unit[CHUNK_NUMBER - 1];
for (var i = 1; i < CHUNK_NUMBER; i++) {
unit[i - 1] = ModSumThree(CHUNK_SIZE);
unit[i - 1].a <== a[i];
unit[i - 1].b <== b[i];
if (i == 1) {
unit[i - 1].c <== unit0.carry;
} else {
unit[i - 1].c <== unit[i - 2].carry;
}
out[i] <== unit[i - 1].sum;
var carry = 0;
var mod = ((1 << n) - 1);

for (var i = 0; i < k; i++) {
out[i] <-- (add.out[i] + carry) & mod;
carry = (add.out[i] + carry) >> n;
}
out[CHUNK_NUMBER] <== unit[CHUNK_NUMBER - 2].carry;

out[k] <-- carry;
}

/*
Expand Down

0 comments on commit 6ad43aa

Please sign in to comment.