Skip to content

Commit

Permalink
remove the misleading "range check helper" API
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Mar 14, 2024
1 parent 7ba854c commit bbc4a99
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 30 deletions.
29 changes: 0 additions & 29 deletions src/lib/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,35 +1007,6 @@ class Field {
.seal();
}

/**
* Create a new {@link Field} element from the first `length` bits of this {@link Field} element.
*
* The `length` has to be a multiple of 16, and has to be between 0 and 255, otherwise the method throws.
*
* As {@link Field} elements are represented using [little endian binary representation](https://en.wikipedia.org/wiki/Endianness),
* the resulting {@link Field} element will equal the original one if it fits in `length` bits.
*
* @param length - The number of bits to take from this {@link Field} element.
*
* @return A {@link Field} element that is equal to the `length` of this {@link Field} element.
*/
rangeCheckHelper(length: number) {
checkBitLength('Field.rangeCheckHelper()', length);
if (length % 16 !== 0)
throw Error(
'Field.rangeCheckHelper(): `length` has to be a multiple of 16.'
);
let lengthDiv16 = length / 16;
if (this.isConstant()) {
let bits = Fp.toBits(this.toBigInt())
.slice(0, length)
.concat(Array(Fp.sizeInBits - length).fill(false));
return new Field(Fp.fromBits(bits));
}
let x = Snarky.field.truncateToBits16(lengthDiv16, this.value);
return new Field(x);
}

/**
* **Warning**: This function is mainly for internal use. Normally it is not intended to be used by a zkApp developer.
*
Expand Down
7 changes: 6 additions & 1 deletion src/lib/gadgets/range-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export {
rangeCheck32,
multiRangeCheck,
compactMultiRangeCheck,
rangeCheckHelper,
rangeCheckN,
isInRangeN,
rangeCheck8,
Expand Down Expand Up @@ -239,6 +238,12 @@ function rangeCheck1Helper(inputs: {

/**
* Helper function that creates a new {@link Field} element from the first `length` bits of this {@link Field} element.
*
* This returns the `x` truncated to `length` bits. However, it does **not** prove this truncation or any
* other relation of the output with `x`.
*
* This only proves that the output value is in the range [0, 2^length), and so can be combined
* with the initial value to prove a range check.
*/
function rangeCheckHelper(length: number, x: Field) {
assert(
Expand Down

0 comments on commit bbc4a99

Please sign in to comment.