forked from privacy-scaling-explorations/zk-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request privacy-scaling-explorations#164 from privacy-scal…
…ing-explorations/feat/comparators-circuits feat: add safe comparators templates
- Loading branch information
Showing
3 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
pragma circom 2.0.0; | ||
|
||
include "./bitify.circom"; | ||
|
||
// Template for safely comparing if one input is less than another, | ||
// ensuring inputs are within a specified bit-length. | ||
template SafeLessThan(n) { | ||
// Ensure the bit-length does not exceed 252 bits. | ||
assert(n <= 252); | ||
|
||
signal input in[2]; | ||
signal output out; | ||
|
||
// Convert both inputs to their bit representations to ensure | ||
// they fit within 'n' bits. | ||
var n2b1[n]; | ||
n2b1 = Num2Bits(n)(in[0]); | ||
|
||
var n2b2[n]; | ||
n2b2 = Num2Bits(n)(in[1]); | ||
|
||
// Additional conversion to handle arithmetic operation and capture the comparison result. | ||
var n2b[n+1]; | ||
n2b = Num2Bits(n + 1)(in[0] + (1<<n) - in[1]); | ||
|
||
// Determine if in[0] is less than in[1] based on the most significant bit. | ||
out <== 1 - n2b[n]; | ||
} | ||
|
||
// Template to check if one input is less than or equal to another. | ||
template SafeLessEqThan(n) { | ||
signal input in[2]; | ||
signal output out; | ||
|
||
// Use SafeLessThan to determine if in[0] is less than in[1] + 1. | ||
out <== SafeLessThan(n)([in[0], in[1] + 1]); | ||
} | ||
|
||
// Template for safely comparing if one input is greater than another. | ||
template SafeGreaterThan(n) { | ||
// Two inputs to compare. | ||
signal input in[2]; | ||
// Output signal indicating comparison result. | ||
signal output out; | ||
|
||
// Invert the inputs for SafeLessThan to check if in[1] is less than in[0]. | ||
out <== SafeLessThan(n)([in[1], in[0]]); | ||
} | ||
|
||
// Template to check if one input is greater than or equal to another. | ||
template SafeGreaterEqThan(n) { | ||
// Two inputs to compare. | ||
signal input in[2]; | ||
// Output signal indicating comparison result. | ||
signal output out; | ||
|
||
// Invert the inputs and adjust for equality in SafeLessThan to | ||
// check if in[1] is less than or equal to in[0]. | ||
out <== SafeLessThan(n)([in[1], in[0] + 1]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,284 @@ | ||
import { WitnessTester } from "circomkit" | ||
import { circomkit } from "./common" | ||
|
||
describe("safe-comparators", () => { | ||
describe("SafeLessThan", () => { | ||
let circuit: WitnessTester<["in"], ["out"]> | ||
|
||
it("Should correctly compare two numbers [x, x]", async () => { | ||
// Test values | ||
const inValues = [5, 5] // in[0] === in[1], expecting 'out' to be 0. | ||
const expectedOut = 0 // Since 5 is equal to 5. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeLessThan", { | ||
file: "safe-comparators", | ||
template: "SafeLessThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
|
||
it("Should correctly compare two numbers [x-1, x]", async () => { | ||
// Test values | ||
const inValues = [5, 6] // in[0] < in[1], expecting 'out' to be 1. | ||
const expectedOut = 1 // Since 5 is less than 6. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeLessThan", { | ||
file: "safe-comparators", | ||
template: "SafeLessThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
|
||
it("Should correctly compare two numbers [x, x-1]", async () => { | ||
// Test values | ||
const inValues = [6, 5] // in[0] > in[1], expecting 'out' to be 0. | ||
const expectedOut = 0 // Since 6 is greater than 5. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeLessThan", { | ||
file: "safe-comparators", | ||
template: "SafeLessThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
}) | ||
|
||
describe("SafeLessEqThan", () => { | ||
let circuit: WitnessTester<["in"], ["out"]> | ||
|
||
it("Should correctly compare two numbers [x, x]", async () => { | ||
// Test values | ||
const inValues = [5, 5] // in[0] === in[1], expecting 'out' to be 1. | ||
const expectedOut = 1 // Since 5 is equal to 5. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeLessEqThan", { | ||
file: "safe-comparators", | ||
template: "SafeLessEqThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
|
||
it("Should correctly compare two numbers [x-1, x]", async () => { | ||
// Test values | ||
const inValues = [5, 6] // in[0] < in[1], expecting 'out' to be 1. | ||
const expectedOut = 1 // Since 5 is less than 6. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeLessEqThan", { | ||
file: "safe-comparators", | ||
template: "SafeLessEqThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
|
||
it("Should correctly compare two numbers [x, x-1]", async () => { | ||
// Test values | ||
const inValues = [6, 5] // in[0] > in[1], expecting 'out' to be 0. | ||
const expectedOut = 0 // Since 6 is greater than 5. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeLessEqThan", { | ||
file: "safe-comparators", | ||
template: "SafeLessEqThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
}) | ||
|
||
describe("SafeGreaterThan", () => { | ||
let circuit: WitnessTester<["in"], ["out"]> | ||
|
||
it("Should correctly compare two numbers [x, x]", async () => { | ||
// Test values | ||
const inValues = [5, 5] // in[0] === in[1], expecting 'out' to be 0. | ||
const expectedOut = 0 // Since 5 is equal to 5. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeGreaterThan", { | ||
file: "safe-comparators", | ||
template: "SafeGreaterThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
|
||
it("Should correctly compare two numbers [x-1, x]", async () => { | ||
// Test values | ||
const inValues = [5, 6] // in[0] < in[1], expecting 'out' to be 0. | ||
const expectedOut = 0 // Since 5 is less than 6. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeGreaterThan", { | ||
file: "safe-comparators", | ||
template: "SafeGreaterThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
|
||
it("Should correctly compare two numbers [x, x-1]", async () => { | ||
// Test values | ||
const inValues = [6, 5] // in[0] > in[1], expecting 'out' to be 1. | ||
const expectedOut = 1 // Since 6 is greater than 5. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeGreaterThan", { | ||
file: "safe-comparators", | ||
template: "SafeGreaterThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
}) | ||
|
||
describe("SafeGreaterEqThan", () => { | ||
let circuit: WitnessTester<["in"], ["out"]> | ||
|
||
it("Should correctly compare two numbers [x, x]", async () => { | ||
// Test values | ||
const inValues = [5, 5] // in[0] === in[1], expecting 'out' to be 1. | ||
const expectedOut = 1 // Since 5 is equal to 5. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeGreaterEqThan", { | ||
file: "safe-comparators", | ||
template: "SafeGreaterEqThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
|
||
it("Should correctly compare two numbers [x-1, x]", async () => { | ||
// Test values | ||
const inValues = [5, 6] // in[0] < in[1], expecting 'out' to be 0. | ||
const expectedOut = 0 // Since 5 is less than 6. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeGreaterEqThan", { | ||
file: "safe-comparators", | ||
template: "SafeGreaterEqThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
|
||
it("Should correctly compare two numbers [x, x-1]", async () => { | ||
// Test values | ||
const inValues = [6, 5] // in[0] > in[1], expecting 'out' to be 1. | ||
const expectedOut = 1 // Since 6 is greater than 5. | ||
|
||
const INPUT = { | ||
in: inValues | ||
} | ||
|
||
const OUTPUT = { | ||
out: expectedOut | ||
} | ||
|
||
circuit = await circomkit.WitnessTester("SafeGreaterEqThan", { | ||
file: "safe-comparators", | ||
template: "SafeGreaterEqThan", | ||
params: [252] // Assuming we're working within 252-bit numbers. | ||
}) | ||
|
||
await circuit.expectPass(INPUT, OUTPUT) | ||
}) | ||
}) | ||
}) |