Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
* Add the function insert_commitments in cyKlone tree.

* Add the new test vectors generator

* Add a new withdrawal test item generation

* Complete review of the tests

* Esthetic improvement of the readme

---------

Co-authored-by: CryptoPascal31 <[email protected]>
  • Loading branch information
CryptoPascal31 and CryptoPascal31 authored Mar 29, 2023
1 parent 410264c commit 2489486
Show file tree
Hide file tree
Showing 17 changed files with 1,567 additions and 100 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
client/node_modules/*
gen_test_vectors/node_modules/*
gen_test_vectors/test-vectors.pact
cyklone_js/node_modules/*
.pact-history
.pact_zk_state
Expand Down
6 changes: 5 additions & 1 deletion cyklone_js/cyklone_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class CyKloneTree
return this.kadena_local(`(at 'current-rank (${MODULE}.get-state))`);
}

insert_commitments(chunk)
{
this.tree.bulkInsert(chunk.map(b64_to_dec));
}

async update()
{
Expand All @@ -61,7 +65,7 @@ class CyKloneTree
{
let chunk = await this.get_deposit_chunk(tree_size, Math.min(tree_size + MAX_DOWNLOAD_LEAF, rank-1));
console.log(`Updating Progress ${tree_size} => ${tree_size + chunk.length}`)
this.tree.bulkInsert(chunk.map(b64_to_dec))
this.insert_commitments(chunk)
tree_size = this.tree.elements.length
}
console.log("Update complete")
Expand Down
15 changes: 15 additions & 0 deletions gen_test_vectors/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
all: install test-vectors.pact

install:
yarn install

copy: test-vectors.pact
cp test-vectors.pact ../pact/tests

test-vectors.pact: index.js
yarn run gen-tests

clean:
rm -rf node_modules
rm -f merkle_tree.json
rm -r test-vectors.pact
17 changes: 17 additions & 0 deletions gen_test_vectors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# cyKlone

## Test suite Generator

This directory contains a tool necessary to generate test vectors for the Pact REPL test files
located in `pact/tests/`


To install the dependencies:
```shell
make install
```

To generate and install the test vectors:
```shell
make copy
```
129 changes: 129 additions & 0 deletions gen_test_vectors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// SPDX-License-Identifier: MIT
import {CyKlone, CyKloneTree, CyKloneTransactionBuilder} from 'cyklone_js';
import { promises} from 'fs'
import {hash} from '@kadena/cryptography-utils'

const MNEMONIC = "obscure vivid ill elite sister evoke faculty accident slide alter kiwi captain"

const RELAY_MODULE = "free.cyKlone-relay-v0"

const MAIN_WITHDRAWER = "bob"

const compute_cap_guard_principal = (account) => "c:" + hash(`${RELAY_MODULE}.RELAY"${account}"`)



const PROOFS_TO_GENERATE = {
'0': [0, 1, 19],
'1': [1, 2, 3, 10, 19],
'2': [2, 3, 19],
'3': [3,],
'4': [4,40],
'10':[10, 18],
'19':[19,],
'36':[36,],
'40':[40,],
'41':[41,],
}

const nothing = () => undefined
const reject = async () => {throw new Error("")}
const local_read = (x) => promises.readFile("./"+x);

class CyKloneTreeTest extends CyKloneTree
{
constructor()
{
super(nothing, reject)
}

update()
{
return
}

}

const cyKlone = new CyKlone(nothing, local_read, false);
var deposit_data;



async function gen_proof(withdrawer, deposit_index, tree_size)
{
console.log(` ${deposit_index} => ${tree_size}`)
const testTree = new CyKloneTreeTest()
await testTree.load()
testTree.insert_commitments(deposit_data.slice(0, tree_size+1).map( (x) => x.commitment_str))
cyKlone.tree = testTree
return await cyKlone.compute_withdrawal_data(withdrawer, MNEMONIC, deposit_index.toString())
}



function gen_pact_proof(deposit_index, tree_size)
{
return gen_proof(MAIN_WITHDRAWER, deposit_index, tree_size)
.then( (x) =>[`(defconst WITHDRAW_${deposit_index}_${tree_size}_NULL:string "${x.nullifier_hash}")`,
`(defconst WITHDRAW_${deposit_index}_${tree_size}_ROOT:string "${x.root}")`,
`(defconst WITHDRAW_${deposit_index}_${tree_size}_PROOF:string "${x.proof}")`])
}

function gen_pact_proof_relay(deposit_index, tree_size)
{
const relayer = compute_cap_guard_principal(MAIN_WITHDRAWER)

console.log(relayer)
return gen_proof(relayer, deposit_index, tree_size)
.then( (x) =>[`(defconst WITHDRAW_RELAY_${deposit_index}_${tree_size}_NULL:string "${x.nullifier_hash}")`,
`(defconst WITHDRAW_RELAY_${deposit_index}_${tree_size}_ROOT:string "${x.root}")`,
`(defconst WITHDRAW_RELAY_${deposit_index}_${tree_size}_PROOF:string "${x.proof}")`])
}



async function main()
{

await cyKlone.init()

console.log("Generate deposits")
deposit_data = Array.from({length: 50}, (x, i) => i)
.map( (x)=> cyKlone.compute_deposit_data(MNEMONIC, x.toString()))

const pact_deposits_list = deposit_data.map( (x,i) => `(defconst DEPOSIT_${i}:string "${x.commitment_str}")`)

console.log("Generate stndard proofs")
const pact_withd_list = []
for (const k of Object.keys(PROOFS_TO_GENERATE))
{
for(const tree_idx of PROOFS_TO_GENERATE[k])
{
let x = await gen_pact_proof(parseInt(k), tree_idx);
pact_withd_list.push(...x)
}
}

console.log("Generate relay proofs")
const pact_withd_relay_list = [
...(await gen_pact_proof_relay(0,0)),
...(await gen_pact_proof_relay(1,1)),
]

const pact_code = `
; Generated test vectors
; Do not modifiy by hand
(module test-vectors G
(defcap G() true)
; --------------------------- DEPOSITS -----------------------------------------
${pact_deposits_list.join('\n ')}
; ------------------------- STANDARD WITHDRAWALS -------------------------------
${pact_withd_list.join('\n ')}
; -------------------------- RELAY WITHDRAWAL ----------------------------------
${pact_withd_relay_list.join('\n ')}
)
`
await promises.writeFile("test-vectors.pact", pact_code)
}

main()
13 changes: 13 additions & 0 deletions gen_test_vectors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "gen_test_vectors",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"type": "module",
"scripts": {
"gen-tests": "node index.js"
},
"dependencies": {
"cyklone_js": "file:../cyklone_js"
}
}
Loading

0 comments on commit 2489486

Please sign in to comment.