o1js
Version:
TypeScript framework for zk-SNARKs and zkApps
115 lines (99 loc) • 3.33 kB
text/typescript
import {
Field,
SmartContract,
method,
Bool,
state,
State,
Poseidon,
Struct,
Provable,
} from 'o1js';
export { Sudoku, SudokuZkApp };
class Sudoku extends Struct({
value: Provable.Array(Provable.Array(Field, 9), 9),
}) {
static from(value: number[][]) {
return new Sudoku({ value: value.map((row) => row.map(Field)) });
}
hash() {
return Poseidon.hash(Sudoku.toFields(this));
}
}
class SudokuZkApp extends SmartContract {
(Field) sudokuHash = State<Field>();
(Bool) isSolved = State<Bool>();
/**
* by making this a `@method`, we ensure that a proof is created for the state initialization.
* alternatively (and, more efficiently), we could have used `super.init()` inside `update()` below,
* to ensure the entire state is overwritten.
* however, it's good to have an example which tests the CLI's ability to handle init() decorated with `@method`.
*/
async init() {
super.init();
}
async update(sudokuInstance: Sudoku) {
this.sudokuHash.set(sudokuInstance.hash());
this.isSolved.set(Bool(false));
}
async submitSolution(
sudokuInstance: Sudoku,
solutionInstance: Sudoku
) {
let sudoku = sudokuInstance.value;
let solution = solutionInstance.value;
// first, we check that the passed solution is a valid sudoku
// define helpers
let range9 = Array.from({ length: 9 }, (_, i) => i);
let oneTo9 = range9.map((i) => Field(i + 1));
function assertHas1To9(array: Field[]) {
oneTo9
.map((k) => range9.map((i) => array[i].equals(k)).reduce(Bool.or))
.reduce(Bool.and)
.assertTrue('array contains the numbers 1...9');
}
// check all rows
for (let i = 0; i < 9; i++) {
let row = solution[i];
assertHas1To9(row);
}
// check all columns
for (let j = 0; j < 9; j++) {
let column = solution.map((row) => row[j]);
assertHas1To9(column);
}
// check 3x3 squares
for (let k = 0; k < 9; k++) {
let [i0, j0] = divmod(k, 3);
let square = range9.map((m) => {
let [i1, j1] = divmod(m, 3);
return solution[3 * i0 + i1][3 * j0 + j1];
});
assertHas1To9(square);
}
// next, we check that the solution extends the initial sudoku
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
let cell = sudoku[i][j];
let solutionCell = solution[i][j];
// either the sudoku has nothing in it (indicated by a cell value of 0),
// or it is equal to the solution
Bool.or(cell.equals(0), cell.equals(solutionCell)).assertTrue(
`solution cell (${i + 1},${j + 1}) matches the original sudoku`
);
}
}
// finally, we check that the sudoku is the one that was originally deployed
let sudokuHash = this.sudokuHash.get(); // get the hash from the blockchain
this.sudokuHash.requireEquals(sudokuHash); // precondition that links this.sudokuHash.get() to the actual on-chain state
sudokuInstance
.hash()
.assertEquals(sudokuHash, 'sudoku matches the one committed on-chain');
// all checks passed => the sudoku is solved!
this.isSolved.set(Bool(true));
}
}
function divmod(k: number, n: number) {
let q = Math.floor(k / n);
return [q, k - q * n];
}