vdk bitstring testing

This commit is contained in:
2023-05-09 16:16:30 -06:00
parent e99981b73e
commit d6a8fa87dd
8 changed files with 32 additions and 21 deletions
+4 -4
View File
@@ -60,7 +60,7 @@ strong_rand_bytes
: Uint8Array : Uint8Array
{ {
let arr = new Uint8Array(how_many); let arr = new Uint8Array(how_many);
Crypto.getRandomValues(arr); (new Crypto()).getRandomValues(arr);
return arr; return arr;
} }
@@ -243,8 +243,8 @@ bits_concat
: bits : bits
{ {
let result_bit_length : number = bits1.bit_length + bits2.bit_length; let result_bit_length : number = bits1.bit_length + bits2.bit_length;
let bytes1 : number = bits1.bytes; let bytes1 : Uint8Array = bits1.bytes;
let bytes2 : number = bits2.bytes; let bytes2 : Uint8Array = bits2.bytes;
// using zeros here because of our xor trick in a minute // using zeros here because of our xor trick in a minute
let result_bits : bits = bits_zeros(result_bit_length); let result_bits : bits = bits_zeros(result_bit_length);
let result_bytes : Uint8Array = result_bits.bytes; let result_bytes : Uint8Array = result_bits.bytes;
@@ -261,7 +261,7 @@ bits_concat
// next // next
// we need to calculate the left-shift offset // we need to calculate the left-shift offset
// this will be 8 - (bytes1.bit_length % 8) // 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 // so
// bytes1: ABCD_EF00 // bytes1: ABCD_EF00
// bytes2: GH12_3000 // bytes2: GH12_3000
+5 -4
View File
@@ -79,12 +79,13 @@ decode
// at this point, we can assume we correctly decoded all words // at this point, we can assume we correctly decoded all words
// compute the check byte // 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 // check if it is correct
// if so, return the computed bytes // if so, return the computed bytes
if (computed_check_word === input_check_word) if (computed_check_word === input_check_word)
return safe.ok(new Uint8Array(computed_bytes)); return safe.ok(computed_bytes_u8s);
// otherwise, return an error // otherwise, return an error
else else
return safe.error('checksum failure! computed check word: ' + computed_check_word + '; input check word: ' + input_check_word); 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) (word: string)
: safe.Safe<number, string> : safe.Safe<number, string>
{ {
for (let i=0; i<=255, i++) for (let i=0; i<=255; i++)
{ {
if (word === words[i]) if (word === words[i])
return safe.ok(i); return safe.ok(i);
@@ -138,7 +139,7 @@ check_byte
words = [ let words = [
"able", "able",
"abuse", "abuse",
"acquire", "acquire",
+23
View File
@@ -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 ->
<<Foo:N, _/bits>> = rand:bytes(1),
<<Foo:N>>.
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>>.
-4
View File
@@ -27,10 +27,6 @@
<h5>Failed case log:</h5> <h5>Failed case log:</h5>
<pre id="rlp-assershins"></pre> <pre id="rlp-assershins"></pre>
<h1>Tx Decoding</h1>
<input style="width: 800px;" type="text" id="tx-stuff" value="tx_+FgMAaEByWN+RgDnqzvC5n/GQOgjdkRE9DBV2l1VeKSaN1r6GNyhAXtm5sMFBwg25Ol5IFI9w+pZy7/YbFi6BwPqi80KuKdsCoYPJvVhyAAACYdoYWluYW5hA7ZC1w=="></input>
<button id="tx-go">Go!</button>
<script type="module" src="dist/test.js"></script> <script type="module" src="dist/test.js"></script>
</body> </body>
-9
View File
@@ -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 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 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 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 // 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 { function main(): void {
document.getElementById('b64-total-cases')!.innerHTML = '' + cases.base64.length; 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-total-cases')!.innerHTML = '' + cases.rlp.length;
document.getElementById('rlp-go')!.onclick = rlp_tests; document.getElementById('rlp-go')!.onclick = rlp_tests;
document.getElementById('tx-go')!.onclick = tx_tests;
} }
main(); main();