# Axiom ZK Intensive Circuit Exercises
refer: https://hackmd.io/@axiom/HkAM8sp-T
## E1: Maximum
```typescript
// _ _ _ ___ _____ ______ _____ _
// | | | | | | |__ \| __ \| ____| __ \| |
// | |__| | __ _| | ___ ) | |__) | |__ | |__) | |
// | __ |/ _` | |/ _ \ / /| _ /| __| | ___/| |
// | | | | (_| | | (_) / /_| | \ \| |____| | | |____
// |_| |_|\__,_|_|\___/____|_| \_\______|_| |______|
//
/*
E1: Maximum
Write a circuit which constrains the following function:
public inputs:
an array `arr` of length 10, each entry of which is known to be 8-bit
public outputs:
the maximum of the array
*/
/*
input:
{
"arr": [10,6,5,7,2,16,6,9,2,11]
}
*/
const input_arr = arr.map(witness);
const uint8Max = constant(2n ** 8n);
// an array `arr` of length 10
assertEqual(witness(input_arr.length), constant(10))
let maximumArr = [];
for (let i = 0; i < 10; i++) {
const v = input_arr[i];
// each entry of which is known to be 8-bit
{
const _isLessThan = isLessThan(v, uint8Max);
assertEqual(_isLessThan, constant(1));
}
// the maximum of the array
{
if (i === 0) {
maximumArr.push(v)
} else {
const _isLessThan = isLessThan(maximumArr[i - 1], v);
const _isGreaterThan = sub(constant(1), _isLessThan);
maximumArr.push(
add(mul(_isLessThan, v), mul(_isGreaterThan, maximumArr[i - 1]))
);
}
}
}
makePublic(maximumArr[9]);
```
## E2: Integer division
```typescript=
// _ _ _ ___ _____ ______ _____ _
// | | | | | | |__ \| __ \| ____| __ \| |
// | |__| | __ _| | ___ ) | |__) | |__ | |__) | |
// | __ |/ _` | |/ _ \ / /| _ /| __| | ___/| |
// | | | | (_| | | (_) / /_| | \ \| |____| | | |____
// |_| |_|\__,_|_|\___/____|_| \_\______|_| |______|
//
/*
E2: Integer division
Write a circuit which constrains the following function:
public inputs:
* an non-negative integer x, which is known to be 16-bit
public outputs:
* The non-negative integer (x / 32), where "/" represents integer division.
*/
/*
input:
{
"x": 1111
}
*/
const input_x = witness(x);
// max to 16bit
const uint16Max = constant(2n ** 16n);
assertEqual(isLessThan(input_x, uint16Max), constant(1))
const output = div(input_x, constant(32));
makePublic(output);
```
## E3: Variable subarray shift
```typescript
// _ _ _ ___ _____ ______ _____ _
// | | | | | | |__ \| __ \| ____| __ \| |
// | |__| | __ _| | ___ ) | |__) | |__ | |__) | |
// | __ |/ _` | |/ _ \ / /| _ /| __| | ___/| |
// | | | | (_| | | (_) / /_| | \ \| |____| | | |____
// |_| |_|\__,_|_|\___/____|_| \_\______|_| |______|
//
/*
E3: Variable subarray shift
Write a circuit which constrains the following function:
public inputs:
* an array `arr` of length 20
* `start`, an index guaranteed to be in `[0, 20)`
* `end`, an index guaranteed to be in `[0, 20)`
* It is also known that `start <= end`
public outputs:
* an array `out` of length 20 such that
* the first `end - start` entries of `out` are the subarray `arr[start:end]`
* all other entries of `out` are 0.
*/
/*
input:
{
"arr": [3,4,5,1,3,5,6,7,4,3,4,5,7,8,4,5,2,3,4,7],
"start": 3,
"end": 8
}
*/
const input_arr = arr.map(witness);
const input_start = witness(start);
const input_end = witness(end);
// an array `arr` of length 20
assertEqual(witness(input_arr.length), constant(20))
// `end`, an index guaranteed to be in `[0, 20)`
assertEqual(isLessThan(input_end, constant(20)), constant(1));
// `start`, an index guaranteed to be in `[0, 20)`
// It is also known that `start <= end`
assertEqual(isLessThan(input_end, input_start), constant(0));
const v = sub(input_end, input_start).number();
const input_start_number = input_start.number();
let out = [];
// the first `end - start` entries of `out` are the subarray `arr[start:end]`
for (let i = 0; i < v; i++) {
out.push(input_arr[input_start_number + i]);
}
// all other entries of `out` are 0.
for (let i = v; i < 20; i++) {
out.push(constant(0));
}
// output
for (let i = 0; i < 20; i++) {
makePublic(out[i]);
}
```