Created
January 5, 2023 11:12
-
-
Save enricobottazzi/ff6994fb9de67a8047cbcc3bed76901a to your computer and use it in GitHub Desktop.
Test for range-check.circom
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
const path = require("path"); | |
const {assert} = require("chai"); | |
const wasm_tester = require("circom_tester").wasm; | |
const Scalar = require("ffjavascript").Scalar; | |
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); | |
describe("Tree Testing", function async() { | |
beforeEach(async function () { | |
this.timeout(100000); | |
circuit = await wasm_tester(path.join(__dirname, "range-check.circom")); | |
}); | |
// Case 1 : 0 < a < 2**252 => NO ERROR | |
it("should not throw any error - the input that enters the circuit is below 2**252", async () => { | |
let input = { | |
"a" : BigInt(2 ** 252) - BigInt(1), | |
} | |
let witness = await circuit.calculateWitness(input); | |
await circuit.checkConstraints(witness); | |
}); | |
// Case 2.1 : 2**252 < a < p => ERROR | |
it("should throw an error - the input that enters the circuit is between 2**252 and p", async () => { | |
let input = { | |
"a" : exports.p - BigInt(1), | |
} | |
try { | |
let witness = await circuit.calculateWitness(input); | |
await circuit.checkConstraints(witness); | |
assert(false) | |
} catch (e) { | |
assert.equal(e.message.slice(0, 21), "Error: Assert Failed.") | |
} | |
}); | |
// Case 2.2 : 2**252 < a < p => ERROR | |
it("should throw an error - the input that enters the circuit is between 2**252 and p", async () => { | |
let input = { | |
"a" : BigInt(2 ** 252) + BigInt(1), | |
} | |
try { | |
let witness = await circuit.calculateWitness(input); | |
await circuit.checkConstraints(witness); | |
assert(false) | |
} catch (e) { | |
assert.equal(e.message.slice(0, 21), "Error: Assert Failed.") | |
} | |
}); | |
// Case 3 : a > p => NO ERROR Because it enters in the circuit as a mod p = 1 | |
it("should not throw an error - the input that enters the circuit has already overflowed", async () => { | |
let input = { | |
"a" : exports.p + BigInt(1), | |
} | |
let witness = await circuit.calculateWitness(input); | |
await circuit.checkConstraints(witness); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment