mina-attestations
Version:
Private Attestations on Mina
67 lines (66 loc) • 2.96 kB
TypeScript
import { Bool, Field, Int64, UInt32, UInt64, UInt8 } from 'o1js';
export { Numeric, numericMaximumType, type NumericMaximum };
type Numeric = Field | Int64 | UInt64 | UInt32 | UInt8;
type NumericMaximum<T> = Numeric extends T ? Numeric : Field extends T ? Field : Int64 extends T ? Int64 : UInt64 extends T ? UInt64 : UInt32 extends T ? UInt32 : UInt8 extends T ? UInt8 : never;
/**
* Arithmetic and comparison gadgets that work on all pairs of numeric types:
* Field, Int64, UInt64, UInt32, and UInt8.
*
* Example: check if a UInt64 is less than a Field, or compute the sum of a UInt32 and an Int64 (as an Int64).
*
* The general strategy is to convert both inputs to the "bigger" type, and then perform the operation there.
* We also add int64 comparisons which are missing from o1js.
*/
declare const Numeric: {
/**
* Add two numeric values.
* The returned type is the larger of the two input types, and follows the overflow rules of that type.
*
* Example:
* ```ts
* const sum: UInt32 = Numeric.add(UInt8.from(5), UInt32.from(10));
* ```
*/
add<T extends Numeric, S extends Numeric>(left: T, right: S): NumericMaximum<T | S>;
/**
* Subtract two numeric values.
* The returned type is the larger of the two input types, and follows the overflow rules of that type.
*
* Example:
* ```ts
* const difference: Field = Numeric.subtract(Int64.from(10), Field.from(15));
* ```
*/
subtract<T extends Numeric, S extends Numeric>(left: T, right: S): NumericMaximum<T | S>;
/**
* Multiply two numeric values.
* The returned type is the larger of the two input types, and follows the overflow rules of that type.
*/
multiply<T extends Numeric, S extends Numeric>(left: T, right: S): NumericMaximum<T | S>;
/**
* Divide two numeric values.
* The returned type is the larger of the two input types, and follows the overflow rules of that type.
*/
divide<T extends Numeric, S extends Numeric>(left: T, right: S): NumericMaximum<T | S>;
/**
* Compares two numeric values and returns true if `left < right`.
*
* **Warning**: when comparing an `Int64` and a `Field`, negative `Int64` are treated as large numbers close to the field size.
* This is to be consistent with how order of field elements is understood in general.
*/
lessThan(left: Numeric, right: Numeric): Bool;
/**
* See {@link Numeric.lessThan}.
*/
lessThanOrEqual(left: Numeric, right: Numeric): Bool;
/**
* See {@link Numeric.lessThan}.
*/
greaterThan(left: Numeric, right: Numeric): Bool;
/**
* See {@link Numeric.lessThanOrEqual}.
*/
greaterThanOrEqual(left: Numeric, right: Numeric): Bool;
};
type NumericType = typeof Field | typeof Int64 | typeof UInt64 | typeof UInt32 | typeof UInt8;
declare function numericMaximumType(left: unknown, right: unknown): NumericType;