Compute Functions

Used to do arbitrary computations inside an AxiomREPL circuit

You can use these functions independently or to do computation on top of the results returned from data queries.

/**
 * Adds two circuit values.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @returns The sum of the two circuit values.
 */
add: (a: CircuitValue, b: CircuitValue) => CircuitValue;

/**
 * Subtracts the second circuit value from the first circuit value.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @returns The difference between the two circuit values.
 */
sub: (a: CircuitValue, b: CircuitValue) => CircuitValue;

/**
 * Negates a circuit value.
 *
 * @param a - The circuit value to negate.
 * @returns The negation of the circuit value.
 */
neg: (a: CircuitValue) => CircuitValue;

/**
 * Multiplies two circuit values.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @returns The product of the two circuit values.
 */
mul: (a: CircuitValue, b: CircuitValue) => CircuitValue;

/**
 * Multiplies two circuit values and adds a third circuit value.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @param c - The third circuit value.
 * @returns The result of multiplying the first two circuit values and adding the third circuit value.
 */
mulAdd: (a: CircuitValue, b: CircuitValue, c: CircuitValue) => CircuitValue;

/**
 * Multiplies a circuit value by the negation of another circuit value.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @returns The result of multiplying the first circuit value by the negation of the second circuit value.
 */
mulNot: (a: CircuitValue, b: CircuitValue) => CircuitValue;

/**
 * Asserts that a circuit value is a bit.
 *
 * @param a - The circuit value to assert.
 */
assertBit: (a: CircuitValue) => void;

/**
 * Asserts that a circuit value is a constant.
 *
 * @param a - The circuit value to assert.
 * @param b - The raw value to compare to
 */
assertIsConst: (a: CircuitValue, b: number | bigint | string) => void;

/**
 * Computes the inner product of two arrays of circuit values.
 *
 * @param a - The first array of circuit values.
 * @param b - The second array of circuit values.
 * @returns The inner product of the two arrays.
 */
innerProduct: (a: CircuitValue[], b: CircuitValue[]) => CircuitValue;

/**
 * Computes the sum of an array of circuit values.
 *
 * @param arr - The array of circuit values.
 * @returns The sum of the array of circuit values.
 */
sum: (arr: CircuitValue[]) => CircuitValue;

/**
 * Performs an AND operation on two circuit values.
 * Assumes the CircuitValues are boolean.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @returns The a & b
 */
and: (a: CircuitValue, b: CircuitValue) => CircuitValue;

/**
 * Performs an OR operation on two circuit values.
 * Assumes the CircuitValues are boolean.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @returns a || b
 */
or: (a: CircuitValue, b: CircuitValue) => CircuitValue;

/**
 * Performs a NOT operation on a circuit value.
 *
 * @param a - The boolean circuit value.
 * @returns !a
 */
not: (a: CircuitValue) => CircuitValue;

/**
 * Decrements a circuit value by 1.
 *
 * @param a - The circuit value.
 * @returns a-1.
 */
dec: (a: CircuitValue) => CircuitValue;

/**
 * Selects a circuit value based on a condition.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @param c - The condition circuit value.
 * @returns The selected circuit value, given by c * a + (1 - c) * b
 */
select: (a: CircuitValue, b: CircuitValue, c: CircuitValue) => CircuitValue;

/**
 * Performs a OR-AND operation on three boolean circuit values.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @param c - The third circuit value.
 * @returns a || (b && c)
 */
orAnd: (a: CircuitValue, b: CircuitValue, c: CircuitValue) => CircuitValue;

/**
 * Converts an array of circuit values which are assumed to be bits to an 
 * indicator array.  The i_th returned circuit value is 1 if and only if 
 * i = (number represented by `bits` in little endian binary), and otherwise
 * it is 0.
 *
 * @param bits - The array of circuit values.
 * @returns The indicator array of circuit values.
 */
bitsToIndicator: (bits: CircuitValue[]) => CircuitValue[];

/**
 * Converts an index circuit value to a length `len` indicator array. The i_th
 * returned circuit value is 1 if and only if i == idx, and otherwise it is 0.
 *
 * @param idx - The index circuit value.
 * @param len - The length of the indicator circuit value.
 * @returns The indicator circuit value.
 */
idxToIndicator: (idx: CircuitValue, len: number | bigint | string) => CircuitValue[];

/**
 * Selects circuit values from an array based on an indicator array of circuit values.
 *
 * @param arr - The array of circuit values.
 * @param indicator - The indicator circuit values.
 * @returns The selected circuit value.
 */
selectByIndicator: (arr: CircuitValue[], indicator: CircuitValue[]) => CircuitValue;

/**
 * Selects a circuit value from an array based on an index circuit value.
 *
 * @param arr - The array of circuit values.
 * @param idx - The index circuit value.
 * @returns The selected circuit value.
 */
selectFromIdx: (arr: CircuitValue[], idx: CircuitValue) => CircuitValue;

/**
 * Checks if a circuit value is zero.
 *
 * @param a - The circuit value to check.
 * @returns The indicator circuit value representing whether the input is zero.
 */
isZero: (a: CircuitValue) => CircuitValue;

/**
 * Checks if two circuit values are equal.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @returns The indicator circuit value representing whether the two inputs are equal.
 */
isEqual: (a: CircuitValue, b: CircuitValue) => CircuitValue;

/**
 * Converts a circuit value to an array of bits.
 *
 * @param a - The circuit value to convert.
 * @param len - The length of the resulting bit array.
 * @returns The array of bits representing the input circuit value.
 */
numToBits: (a: CircuitValue, len: number | bigint | string) => CircuitValue;

/**
 * Asserts that two circuit values are equal.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 */
checkEqual: (a: CircuitValue, b: CircuitValue) => void;

/**
 * Checks if a circuit value is within a specified range.
 *
 * @param a - The circuit value to check.
 * @param b - The range of the circuit value, in number of bits.
 */
rangeCheck: (a: CircuitValue, b: number | bigint | string) => void;

/**
 * Asserts that the first circuit value is less than the second circuit value.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @param c - The bit range of the circuit values. Defaults to 253.
 */
checkLessThan: (a: CircuitValue, b: CircuitValue, c?: string) => void;

/**
 * Checks if the first circuit value is less than the second circuit value.
 *
 * @param a - The first circuit value.
 * @param b - The second circuit value.
 * @param c - The bit range of the circuit values. Defaults to 253.
 * @returns 1 if a < b, else 0
 */
isLessThan: (a: CircuitValue, b: CircuitValue, c?: string) => CircuitValue;

/**
 * Divides two circuit values and returns the quotient.  
 *
 * @param a - The dividend circuit value.
 * @param b - The divisor circuit value.
 * @param c - The bit range of `a`. Defaults to 253 bits.
 * @param d - The bit range of `b`. Defaults to 253 bits.
 * @returns The quotient.
 *
*/
div: (a: CircuitValue, b: CircuitValue, c?: string, d?: string) => CircuitValue;

/**
 * Divides two circuit values and returns the remainder.
 *
 * @param a - The dividend circuit value.
 * @param b - The divisor circuit value.
 * @param c - The bit range of `a`. Defaults to 253 bits.
 * @param d - The bit range of `b`. Defaults to 253 bits.
 * @returns The remainder.
 *
*/
mod: (a: CircuitValue, b: CircuitValue, c?: string, d?: string) => CircuitValue;

/**
 * Raises a circuit value to the power of another circuit value.
 *
 * @param a - The base circuit value.
 * @param b - The exponent circuit value.
 * @param c - The bit range of b. Defaults to 253 bits.
 * @returns The result of the exponentiation.
 */
pow: (a: CircuitValue, b: CircuitValue, c?: string) => CircuitValue;

/**
 * Computes the Poseidon hash of multiple circuit values.
 *
 * @param args - The circuit values to hash.
 * @returns The hash value.
 */
poseidon: (...args: CircuitValue[]) => CircuitValue;

/**
 * Retrieves the value of a circuit value.
 *
 * @param a - The circuit value.
 * @returns The value of the circuit value.
 */
value: (a: CircuitValue) => bigint;

Last updated