Skip to content

Commit

Permalink
remove 253-bits limitation from tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Mar 27, 2024
1 parent 1b80114 commit 9531b51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 42 deletions.
52 changes: 16 additions & 36 deletions src/lib/provable/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,17 +565,14 @@ class Field {
*
* @example
* ```ts
* Field(2).lessThan(3).assertEquals(Bool(true));
* let isTrue = Field(2).lessThan(3);
* ```
*
* **Warning**: Comparison methods only support Field elements of size <= 253 bits in provable code.
* The method will throw if one of the inputs exceeds 253 bits.
*
* **Warning**: As this method compares the bigint value of a {@link Field}, it can result in unexpected behavior when used with negative inputs or modular division.
*
* @example
* ```ts
* Field(1).div(Field(3)).lessThan(Field(1).div(Field(2))).assertEquals(Bool(true)); // This code will throw an error
* let isFalse = Field(1).div(3).lessThan(Field(1).div(2)); // in fact, 1/3 > 1/2
* ```
*
* @param value - the "field-like" value to compare with this {@link Field}.
Expand All @@ -595,17 +592,14 @@ class Field {
*
* @example
* ```ts
* Field(3).lessThanOrEqual(3).assertEquals(Bool(true));
* let isTrue = Field(3).lessThanOrEqual(3);
* ```
*
* **Warning**: Comparison methods only support Field elements of size <= 253 bits in provable code.
* The method will throw if one of the inputs exceeds 253 bits.
*
* **Warning**: As this method compares the bigint value of a {@link Field}, it can result in unexpected behaviour when used with negative inputs or modular division.
*
* @example
* ```ts
* Field(1).div(Field(3)).lessThanOrEqual(Field(1).div(Field(2))).assertEquals(Bool(true)); // This code will throw an error
* let isFalse = Field(1).div(3).lessThanOrEqual(Field(1).div(2)); // in fact, 1/3 > 1/2
* ```
*
* @param value - the "field-like" value to compare with this {@link Field}.
Expand All @@ -625,17 +619,14 @@ class Field {
*
* @example
* ```ts
* Field(5).greaterThan(3).assertEquals(Bool(true));
* let isTrue = Field(5).greaterThan(3);
* ```
*
* **Warning**: Comparison methods currently only support Field elements of size <= 253 bits in provable code.
* The method will throw if one of the inputs exceeds 253 bits.
*
* **Warning**: As this method compares the bigint value of a {@link Field}, it can result in unexpected behaviour when used with negative inputs or modular division.
*
* @example
* ```ts
* Field(1).div(Field(2)).greaterThan(Field(1).div(Field(3))).assertEquals(Bool(true)); // This code will throw an error
* let isFalse = Field(1).div(2).greaterThan(Field(1).div(3); // in fact, 1/3 > 1/2
* ```
*
* @param value - the "field-like" value to compare with this {@link Field}.
Expand All @@ -652,17 +643,14 @@ class Field {
*
* @example
* ```ts
* Field(3).greaterThanOrEqual(3).assertEquals(Bool(true));
* let isTrue = Field(3).greaterThanOrEqual(3);
* ```
*
* **Warning**: Comparison methods only support Field elements of size <= 253 bits in provable code.
* The method will throw if one of the inputs exceeds 253 bits.
*
* **Warning**: As this method compares the bigint value of a {@link Field}, it can result in unexpected behaviour when used with negative inputs or modular division.
*
* @example
* ```ts
* Field(1).div(Field(2)).greaterThanOrEqual(Field(1).div(Field(3))).assertEquals(Bool(true)); // This code will throw an error
* let isFalse = Field(1).div(2).greaterThanOrEqual(Field(1).div(3); // in fact, 1/3 > 1/2
* ```
*
* @param value - the "field-like" value to compare with this {@link Field}.
Expand All @@ -675,14 +663,12 @@ class Field {

/**
* Assert that this {@link Field} is less than another "field-like" value.
* Calling this function is equivalent to `Field(...).lessThan(...).assertEquals(Bool(true))`.
*
* Note: This uses fewer constraints than `x.lessThan(y).assertTrue()`.
* See {@link Field.lessThan} for more details.
*
* **Important**: If an assertion fails, the code throws an error.
*
* **Warning**: Comparison methods only support Field elements of size <= 253 bits in provable code.
* The method will throw if one of the inputs exceeds 253 bits.
*
* @param value - the "field-like" value to compare & assert with this {@link Field}.
* @param message? - a string error message to print if the assertion fails, optional.
*/
Expand All @@ -702,14 +688,12 @@ class Field {

/**
* Assert that this {@link Field} is less than or equal to another "field-like" value.
* Calling this function is equivalent to `Field(...).lessThanOrEqual(...).assertEquals(Bool(true))`.
*
* Note: This uses fewer constraints than `x.lessThanOrEqual(y).assertTrue()`.
* See {@link Field.lessThanOrEqual} for more details.
*
* **Important**: If an assertion fails, the code throws an error.
*
* **Warning**: Comparison methods only support Field elements of size <= 253 bits in provable code.
* The method will throw if one of the inputs exceeds 253 bits.
*
* @param value - the "field-like" value to compare & assert with this {@link Field}.
* @param message? - a string error message to print if the assertion fails, optional.
*/
Expand All @@ -729,14 +713,12 @@ class Field {

/**
* Assert that this {@link Field} is greater than another "field-like" value.
* Calling this function is equivalent to `Field(...).greaterThan(...).assertEquals(Bool(true))`.
*
* Note: This uses fewer constraints than `x.greaterThan(y).assertTrue()`.
* See {@link Field.greaterThan} for more details.
*
* **Important**: If an assertion fails, the code throws an error.
*
* **Warning**: Comparison methods only support Field elements of size <= 253 bits in provable code.
* The method will throw if one of the inputs exceeds 253 bits.
*
* @param value - the "field-like" value to compare & assert with this {@link Field}.
* @param message? - a string error message to print if the assertion fails, optional.
*/
Expand All @@ -746,14 +728,12 @@ class Field {

/**
* Assert that this {@link Field} is greater than or equal to another "field-like" value.
* Calling this function is equivalent to `Field(...).greaterThanOrEqual(...).assertEquals(Bool(true))`.
*
* Note: This uses fewer constraints than `x.greaterThanOrEqual(y).assertTrue()`.
* See {@link Field.greaterThanOrEqual} for more details.
*
* **Important**: If an assertion fails, the code throws an error.
*
* **Warning**: Comparison methods only support Field elements of size <= 253 bits in provable code.
* The method will throw if one of the inputs exceeds 253 bits.
*
* @param value - the "field-like" value to compare & assert with this {@link Field}.
* @param message? - a string error message to print if the assertion fails, optional.
*/
Expand Down
10 changes: 4 additions & 6 deletions src/lib/provable/test/field.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ let SmallField = Random.reject(
(x) => x.toString(2).length > Fp.sizeInBits - 2
);
let smallField: Spec<bigint, Field> = { ...field, rng: SmallField };
let smallBigint: Spec<bigint, bigint> = { ...bigintField, rng: SmallField };
let smallFieldOrBigint = oneOf(smallField, smallBigint);

// arithmetic, both in- and outside provable code
let equivalent1 = equivalent({ from: [field], to: field });
Expand Down Expand Up @@ -105,11 +103,11 @@ equivalent({ from: [field, fieldOrBigint], to: bool })(
(x, y) => x.equals(y)
);

equivalent({ from: [smallField, smallFieldOrBigint], to: bool })(
equivalent({ from: [field, fieldOrBigint], to: bool })(
(x, y) => x < y,
(x, y) => x.lessThan(y)
);
equivalent({ from: [smallField, smallFieldOrBigint], to: bool })(
equivalent({ from: [field, fieldOrBigint], to: bool })(
(x, y) => x <= y,
(x, y) => x.lessThanOrEqual(y)
);
Expand All @@ -121,11 +119,11 @@ equivalent({ from: [field, fieldOrBigint], to: unit })(
(x, y) => x !== y || throwError('equal'),
(x, y) => x.assertNotEquals(y)
);
equivalent({ from: [smallField, smallFieldOrBigint], to: unit })(
equivalent({ from: [field, fieldOrBigint], to: unit })(
(x, y) => x < y || throwError('not less than'),
(x, y) => x.assertLessThan(y)
);
equivalent({ from: [smallField, smallFieldOrBigint], to: unit })(
equivalent({ from: [field, fieldOrBigint], to: unit })(
(x, y) => x <= y || throwError('not less than or equal'),
(x, y) => x.assertLessThanOrEqual(y)
);
Expand Down

0 comments on commit 9531b51

Please sign in to comment.