From d6a8fa87dd87816987201a1fb04decacdae16325 Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 9 May 2023 16:16:30 -0600 Subject: [PATCH] vdk bitstring testing --- libs/vdk/{src => scratch}/anth.ts | 0 libs/vdk/{scratch => scratch/scratch.txt} | 0 libs/vdk/{src => scratch}/sp.ts | 0 libs/vdk/src/bin.ts | 8 ++++---- libs/vdk/src/faert.ts | 9 +++++---- libs/vdk/test/cases/testgen/bits.erl | 23 +++++++++++++++++++++++ libs/vdk/test/index.html | 4 ---- libs/vdk/test/src/test.ts | 9 --------- 8 files changed, 32 insertions(+), 21 deletions(-) rename libs/vdk/{src => scratch}/anth.ts (100%) rename libs/vdk/{scratch => scratch/scratch.txt} (100%) rename libs/vdk/{src => scratch}/sp.ts (100%) create mode 100644 libs/vdk/test/cases/testgen/bits.erl diff --git a/libs/vdk/src/anth.ts b/libs/vdk/scratch/anth.ts similarity index 100% rename from libs/vdk/src/anth.ts rename to libs/vdk/scratch/anth.ts diff --git a/libs/vdk/scratch b/libs/vdk/scratch/scratch.txt similarity index 100% rename from libs/vdk/scratch rename to libs/vdk/scratch/scratch.txt diff --git a/libs/vdk/src/sp.ts b/libs/vdk/scratch/sp.ts similarity index 100% rename from libs/vdk/src/sp.ts rename to libs/vdk/scratch/sp.ts diff --git a/libs/vdk/src/bin.ts b/libs/vdk/src/bin.ts index f0a9c03..6433202 100644 --- a/libs/vdk/src/bin.ts +++ b/libs/vdk/src/bin.ts @@ -60,7 +60,7 @@ strong_rand_bytes : Uint8Array { let arr = new Uint8Array(how_many); - Crypto.getRandomValues(arr); + (new Crypto()).getRandomValues(arr); return arr; } @@ -243,8 +243,8 @@ bits_concat : bits { let result_bit_length : number = bits1.bit_length + bits2.bit_length; - let bytes1 : number = bits1.bytes; - let bytes2 : number = bits2.bytes; + let bytes1 : Uint8Array = bits1.bytes; + let bytes2 : Uint8Array = bits2.bytes; // using zeros here because of our xor trick in a minute let result_bits : bits = bits_zeros(result_bit_length); let result_bytes : Uint8Array = result_bits.bytes; @@ -261,7 +261,7 @@ bits_concat // next // we need to calculate the left-shift offset // this will be 8 - (bytes1.bit_length % 8) - let num_trailing_zeros_in_first_array : number = 8 - (bytes1.bit_length % 8); + let num_trailing_zeros_in_first_array : number = 8 - (bits1.bit_length % 8); // so // bytes1: ABCD_EF00 // bytes2: GH12_3000 diff --git a/libs/vdk/src/faert.ts b/libs/vdk/src/faert.ts index 96997b7..16c4c43 100644 --- a/libs/vdk/src/faert.ts +++ b/libs/vdk/src/faert.ts @@ -79,12 +79,13 @@ decode // at this point, we can assume we correctly decoded all words // compute the check byte - let computed_check_word : string = check_word(computed_bytes); + let computed_bytes_u8s : Uint8Array = new Uint8Array(computed_bytes); + let computed_check_word : string = check_word(computed_bytes_u8s); // check if it is correct // if so, return the computed bytes if (computed_check_word === input_check_word) - return safe.ok(new Uint8Array(computed_bytes)); + return safe.ok(computed_bytes_u8s); // otherwise, return an error else return safe.error('checksum failure! computed check word: ' + computed_check_word + '; input check word: ' + input_check_word); @@ -100,7 +101,7 @@ byte_of_word (word: string) : safe.Safe { - for (let i=0; i<=255, i++) + for (let i=0; i<=255; i++) { if (word === words[i]) return safe.ok(i); @@ -138,7 +139,7 @@ check_byte -words = [ +let words = [ "able", "abuse", "acquire", diff --git a/libs/vdk/test/cases/testgen/bits.erl b/libs/vdk/test/cases/testgen/bits.erl new file mode 100644 index 0000000..b7b4053 --- /dev/null +++ b/libs/vdk/test/cases/testgen/bits.erl @@ -0,0 +1,23 @@ +-module(bits). + +-compile([export_all, nowarn_export_all]). + + +empty() -> + <<>>. + +rand_bit() -> + case rand:uniform(2) of + 1 -> <<0:1>>; + 2 -> <<1:1>> + end. + +rand_bits(N) when N =< 8 -> + <> = rand:bytes(1), + <>. +rand_bits(N) when N > 8 -> + Num_Bytes = N div 8, + Num_Trailing_Bits = N rem 8, + <<( rand_bits(Num_Trailing_Bits) )/bits, + ( rand:bytes(Num_Bytes) )/binary>>. + diff --git a/libs/vdk/test/index.html b/libs/vdk/test/index.html index 19963a1..80b3b60 100644 --- a/libs/vdk/test/index.html +++ b/libs/vdk/test/index.html @@ -27,10 +27,6 @@
Failed case log:

 
-

Tx Decoding

- - - diff --git a/libs/vdk/test/src/test.ts b/libs/vdk/test/src/test.ts index 684612a..81f40c2 100644 --- a/libs/vdk/test/src/test.ts +++ b/libs/vdk/test/src/test.ts @@ -5,7 +5,6 @@ import * as cases from './jex_include/local-vanillae_test_cases-0.1.0/dist/cases import * as b64 from './jex_include/local-vanillae-0.1.0/dist/b64.js'; import * as b58 from './jex_include/local-vanillae-0.1.0/dist/b58.js'; import * as rlp from './jex_include/local-vanillae-0.1.0/dist/rlp.js'; -import * as ser from './jex_include/local-vanillae-0.1.0/dist/ser.js'; // TODO: move this to rlp library @@ -201,12 +200,6 @@ function rlp_tests(): void { } -function tx_tests(): void { - // @ts-ignore value exists - let tx_stuff = document.getElementById('tx-stuff')!.value; - console.log(ser.decode_tx(tx_stuff)); -} - function main(): void { document.getElementById('b64-total-cases')!.innerHTML = '' + cases.base64.length; @@ -217,8 +210,6 @@ function main(): void { document.getElementById('rlp-total-cases')!.innerHTML = '' + cases.rlp.length; document.getElementById('rlp-go')!.onclick = rlp_tests; - - document.getElementById('tx-go')!.onclick = tx_tests; } main();