mirror of
https://github.com/logos-storage/logos-storage-proofs-circuits.git
synced 2026-01-02 13:33:07 +00:00
63 lines
1.5 KiB
Plaintext
63 lines
1.5 KiB
Plaintext
pragma circom 2.0.0;
|
|
|
|
//------------------------------------------------------------------------------
|
|
// compute (compile time) the log2 of a number
|
|
|
|
function FloorLog2(n) {
|
|
return (n==0) ? -1 : (1 + FloorLog2(n>>1));
|
|
}
|
|
|
|
function CeilLog2(n) {
|
|
return (n==0) ? 0 : (1 + FloorLog2(n-1));
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
// decompose an n-bit number into bits
|
|
|
|
template ToBits(n) {
|
|
signal input inp;
|
|
signal output out[n];
|
|
|
|
var sum = 0;
|
|
for(var i=0; i<n; i++) {
|
|
out[i] <-- (inp >> i) & 1;
|
|
out[i] * (1-out[i]) === 0;
|
|
sum += (1<<i) * out[i];
|
|
}
|
|
|
|
inp === sum;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
// check equality to zero; that is, compute `(inp==0) ? 1 : 0`
|
|
|
|
template IsZero() {
|
|
signal input inp;
|
|
signal output out;
|
|
|
|
// guess the inverse
|
|
signal inv;
|
|
inv <-- (inp != 0) ? (1/inp) : 0 ;
|
|
|
|
// if `inp==0`, then by definition `out==1`
|
|
// if `out==0`, then the inverse must must exist, so `inp!=0`
|
|
out <== 1 - inp * inv;
|
|
|
|
// enfore that either `inp` or `out` must be zero
|
|
inp*out === 0;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
// check equality of two field elements; that is, computes `(A==B) ? 1 : 0`
|
|
|
|
template IsEqual() {
|
|
signal input A,B;
|
|
signal output out;
|
|
|
|
component isz = IsZero();
|
|
isz.inp <== A - B;
|
|
isz.out ==> out;
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|