A Halo2 config for verifiable computation of a greatest common denominator (GCD) and least common factor (LCM) between two integers through Euclid's algorithm. It constrains each column to be a valid Euclid algorithm transition and allows to publicly expose the result, or to constrain it to 1. This can be useful in cryptosystems requiring coprimes, such as the Paillier homomorphic cryptosystem.
To calculate the GCD, we use a modified version of Euclid's algorithm to check the GCD. The original algorithm is as follows:
col_a | col_b |
---|---|
a | b |
b | a%b |
... | ... |
gcd(a,b) | 0 |
However, because moduli and integer divisions cannot be represented as polynomial constraints over fields, we have to introduce
col_a | col_b | q_euclid_init | q_euclid_ss | q_gcd | q_coprime | q_lookup |
---|---|---|---|---|---|---|
a | b | 0 | 0 | 0 | 0 | 1 |
a//b | a%b | 1 | 0 | 0 | 0 | 1 |
b//(a%b) | b%(a%b) | 0 | 1 | 0 | 0 | 1 |
... | ... | 0 | 1 | 0 | 0 | 1 |
x | gcd(a, b) | 0 | 1 | 0 | 0 | 1 |
x | 0 | 0 | 1 | 1 | 0/1 | 1 |
Here, we initialize our columns with