Skip to content

Commit

Permalink
chore: removed un-used modulo function
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Sep 19, 2023
1 parent d13780b commit bd1a258
Showing 1 changed file with 0 additions and 55 deletions.
55 changes: 0 additions & 55 deletions packages/circuits/helpers/utils.circom
Original file line number Diff line number Diff line change
Expand Up @@ -66,61 +66,6 @@ template CalculateTotal(n) {
sum <== sums[n - 1];
}

// Modulo lifted from https://sourcegraph.com/github.com/darkforest-eth/circuits/-/blob/perlin/perlin.circom and https://sourcegraph.com/github.com/zk-ml/demo/-/blob/circuits/math/circuit.circom
// input: dividend and divisor field elements in [0, sqrt(p))
// output: remainder and quotient field elements in [0, p-1] and [0, sqrt(p)
// Haven't thought about negative divisor yet. Not needed.
// -8 % 5 = 2. [-8 -> 8. 8 % 5 -> 3. 5 - 3 -> 2.]
// (-8 - 2) // 5 = -2
// -8 + 2 * 5 = 2
// check: 2 - 2 * 5 = -8
template Modulo(divisor_bits) {
signal input dividend; // -8
signal input divisor; // 5
signal output remainder; // 2
signal output quotient; // -2

component is_neg = IsNegative();
is_neg.in <== dividend;

signal output is_dividend_negative;
is_dividend_negative <== is_neg.out;

signal output dividend_adjustment;
dividend_adjustment <== 1 + is_dividend_negative * -2; // 1 or -1

signal output abs_dividend;
abs_dividend <== dividend * dividend_adjustment; // 8

signal output raw_remainder;
raw_remainder <-- abs_dividend % divisor;

signal output neg_remainder;
neg_remainder <-- divisor - raw_remainder;

if (is_dividend_negative == 1 && raw_remainder != 0) {
remainder <-- neg_remainder;
} else {
remainder <-- raw_remainder;
}

quotient <-- (dividend - remainder) / divisor; // (-8 - 2) / 5 = -2.

dividend === divisor * quotient + remainder; // -8 = 5 * -2 + 2.

component rp = MultiRangeProof(3, 128);
rp.in[0] <== divisor;
rp.in[1] <== quotient;
rp.in[2] <== dividend;
//rp.max_abs_value <== SQRT_P;

// check that 0 <= remainder < divisor
component remainderUpper = LessThan(divisor_bits);
remainderUpper.in[0] <== remainder;
remainderUpper.in[1] <== divisor;
remainderUpper.out === 1;
}

// Written by us
// n bytes per signal, n = 31 usually
template Packed2Bytes(n){
Expand Down

0 comments on commit bd1a258

Please sign in to comment.