Add actual real-test data, detect bitLength (#282)

* Add actual real-test data, detect bitLength

* Cleanup misplaced log

* Cleanup test, no length passed
This commit is contained in:
Jaco Greeff
2018-12-21 12:47:40 +01:00
committed by GitHub
parent ea8723b26a
commit f011334152
4 changed files with 31 additions and 17 deletions
+2 -5
View File
@@ -2,10 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BitLength } from './types';
import { u8aConcat } from '../u8a';
import { DEFAULT_BITLENGTH } from './defaults';
import compactToU8a from './toU8a';
/**
@@ -20,9 +17,9 @@ import compactToU8a from './toU8a';
* console.log(compactAddLength(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))); // Uint8Array([4 << 2, 0xde, 0xad, 0xbe, 0xef])
* ```
*/
export default function compactAddLength (input: Uint8Array, bitLength: BitLength = DEFAULT_BITLENGTH): Uint8Array {
export default function compactAddLength (input: Uint8Array): Uint8Array {
return u8aConcat(
compactToU8a(input.length, bitLength),
compactToU8a(input.length),
input
);
}
@@ -4,6 +4,7 @@
import BN from 'bn.js';
import hexToU8a from '../hex/toU8a';
import compactFromU8a from './fromU8a';
describe('compactFromU8a', () => {
@@ -36,4 +37,12 @@ describe('compactFromU8a', () => {
compactFromU8a(new Uint8Array([3 + ((4 - 4) << 2), 249, 255, 255, 255]), 64)
).toEqual([5, new BN(0xfffffff9)]);
});
it('decodes an actual value', () => {
expect(
compactFromU8a(
hexToU8a('0x0b00407a10f35a')
)
).toEqual([7, new BN('5af3107a4000', 16)]);
});
});
+15 -5
View File
@@ -9,7 +9,7 @@ import compactToU8a from './toU8a';
describe('encode', () => {
it('encodes short u8', () => {
expect(
compactToU8a(18, 8)
compactToU8a(18)
).toEqual(
new Uint8Array([18 << 2])
);
@@ -17,7 +17,7 @@ describe('encode', () => {
it('encodes max u8 values', () => {
expect(
compactToU8a(new BN(63), 16)
compactToU8a(new BN(63))
).toEqual(
new Uint8Array([0b11111100])
);
@@ -25,7 +25,7 @@ describe('encode', () => {
it('encodes basic u16 value', () => {
expect(
compactToU8a(511, 32)
compactToU8a(511)
).toEqual(
new Uint8Array([0b11111101, 0b00000111])
);
@@ -33,7 +33,7 @@ describe('encode', () => {
it('encodes basic u16 (not at edge)', () => {
expect(
compactToU8a(111, 32)
compactToU8a(111)
).toEqual(
new Uint8Array([0xbd, 0x01])
);
@@ -41,7 +41,7 @@ describe('encode', () => {
it('encodes basic u32 values (short)', () => {
expect(
compactToU8a(0xffff, 32)
compactToU8a(0xffff)
).toEqual(
new Uint8Array([254, 255, 3, 0])
);
@@ -54,4 +54,14 @@ describe('encode', () => {
new Uint8Array([3 + ((4 - 4) << 2), 249, 255, 255, 255])
);
});
it('encodes a large value', () => {
expect(
compactToU8a(
new BN('5af3107a4000', 16)
)
).toEqual(
new Uint8Array([3 + ((6 - 4) << 2), 0x00, 0x40, 0x7a, 0x10, 0xf3, 0x5a])
);
});
});
+5 -7
View File
@@ -2,13 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BitLength } from './types';
import BN from 'bn.js';
import { bnToBn, bnToU8a } from '../bn';
import { u8aConcat } from '../u8a';
import { DEFAULT_BITLENGTH } from './defaults';
const MAX_U8 = new BN(2).pow(new BN(8 - 2)).subn(1);
const MAX_U16 = new BN(2).pow(new BN(16 - 2)).subn(1);
@@ -26,7 +23,7 @@ const MAX_U32 = new BN(2).pow(new BN(32 - 2)).subn(1);
* console.log(compactToU8a(511, 32)); // Uint8Array([0b11111101, 0b00000111])
* ```
*/
export default function compactToU8a (_value: BN | number, bitLength: BitLength = DEFAULT_BITLENGTH): Uint8Array {
export default function compactToU8a (_value: BN | number): Uint8Array {
const value = bnToBn(_value);
if (value.lte(MAX_U8)) {
@@ -37,11 +34,12 @@ export default function compactToU8a (_value: BN | number, bitLength: BitLength
return bnToU8a(value.shln(2).addn(0b10), 32, true);
}
const bitLength = value.bitLength();
return u8aConcat(
new Uint8Array([
// FIXME For optimal encoding, detemine actual used bit (do not just use max)
new BN(bitLength / 8)
.subn(4) // at least 4
new BN(Math.ceil(bitLength / 8))
.subn(4) // 4 bytes as a base
.shln(2) // clear low
.addn(0b11) // add flag
.toNumber()