Skip to content

Commit

Permalink
Calc roothash and digest (#16)
Browse files Browse the repository at this point in the history
* add hasher with Rescue Blake2 Keccak256 Keccak512 Sha256 Sha512

* blake3 hash function

* fill zero when call rescue hash

* update rescue hash

* calc rootHash

* change hashes to encoded + nonce

* calc digest
  • Loading branch information
zzcwoshizz committed Nov 11, 2022
1 parent c42ff4f commit 3da867d
Show file tree
Hide file tree
Showing 24 changed files with 1,034 additions and 264 deletions.
1 change: 1 addition & 0 deletions packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"version": "0.0.1-8-x.0",
"main": "index.js",
"dependencies": {
"@noble/hashes": "^1.1.3",
"@polkadot/util": "^10.1.11",
"@polkadot/util-crypto": "^10.1.11",
"@polkadot/wasm-crypto": "^6.3.1",
Expand Down
43 changes: 43 additions & 0 deletions packages/crypto/src/blake3/asU8a.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2021-2022 zcloak authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { initCrypto } from '../initCrypto';
import { blake3AsU8a } from './asU8a';

describe('blake3AsU8a', (): void => {
beforeEach(async (): Promise<void> => {
await initCrypto();
});

it('returns a 64-bit value by default', (): void => {
expect(
blake3AsU8a('abc', 64, undefined)
).toEqual(
new Uint8Array([100, 55, 179, 172, 56, 70, 81, 51])
);
});

it('returns a 128-bit value (as specified,)', (): void => {
expect(
blake3AsU8a('abc', 128, undefined)
).toEqual(
new Uint8Array([100, 55, 179, 172, 56, 70, 81, 51, 255, 182, 59, 117, 39, 58, 141, 181])
);
});

it('returns a 256-bit value (as specified)', (): void => {
expect(
blake3AsU8a('abc', 256, undefined)
).toEqual(
new Uint8Array([100, 55, 179, 172, 56, 70, 81, 51, 255, 182, 59, 117, 39, 58, 141, 181, 72, 197, 88, 70, 93, 121, 219, 3, 253, 53, 156, 108, 213, 189, 157, 133])
);
});

it('returns a 512-bit value (as specified)', (): void => {
expect(
blake3AsU8a('abc', 512, undefined)
).toEqual(
new Uint8Array([100, 55, 179, 172, 56, 70, 81, 51, 255, 182, 59, 117, 39, 58, 141, 181, 72, 197, 88, 70, 93, 121, 219, 3, 253, 53, 156, 108, 213, 189, 157, 133, 31, 178, 80, 174, 115, 147, 245, 208, 40, 19, 182, 93, 82, 26, 13, 73, 45, 155, 160, 156, 247, 206, 127, 76, 255, 217, 0, 242, 51, 116, 191, 11])
);
});
});
36 changes: 36 additions & 0 deletions packages/crypto/src/blake3/asU8a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021-2022 zcloak authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { HexString } from '../types';

import { blake3 } from '@noble/hashes/blake3';
import { u8aToHex, u8aToU8a } from '@polkadot/util';

/**
* @name blake3AsU8a
* @summary Creates a blake3 u8a from the input.
* @description
* From a `Uint8Array` input, create the blake3 and return the result as a u8a.
* @example
* <BR>
*
* ```javascript
* import { blake3AsU8a } from '@zcloak/crypto';
*
* blake3AsU8a('abcd1234'); // => [135,118,41,144,40,252,65,100,204,245,252,44,138,223,209,13,119,200,131,115,120,31,210,44,253,198,228,212,122,61,87,245]
* ```
*/
export function blake3AsU8a(data: HexString | Uint8Array | string, bitLength: 64 | 128 | 256 | 384 | 512 = 256, key?: Uint8Array | null): Uint8Array {
const byteLength = Math.ceil(bitLength / 8);

const u8a = u8aToU8a(data);

return blake3(u8a, { dkLen: byteLength, key: key || undefined });
}

/**
* @description Creates a blake3 hex from the input.
*/
export function blake3AsHex(data: HexString | Uint8Array | string): HexString {
return u8aToHex(blake3AsU8a(data));
}
4 changes: 4 additions & 0 deletions packages/crypto/src/blake3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright 2021-2022 zcloak authors & contributors
// SPDX-License-Identifier: Apache-2.0

export { blake3AsHex, blake3AsU8a } from './asU8a';
12 changes: 9 additions & 3 deletions packages/crypto/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2021-2022 zcloak authors & contributors
// SPDX-License-Identifier: Apache-2.0

export * from './blake3';
export * from './ed25519';
export * from './mnemonic';
export * from './multibase';
Expand All @@ -13,14 +14,13 @@ export * from './initCrypto';

export {
randomAsU8a,
randomAsHex,
ethereumEncode,
isEthereumAddress,
isEthereumChecksum,
hdEthereum,
keyExtractSuri,
keyFromPath,
sha256AsU8a,
sha512AsU8a,
shaAsU8a,
hmacSha256AsU8a,
hmacSha512AsU8a,
Expand All @@ -29,5 +29,11 @@ export {
scryptToU8a,
scryptFromU8a,
naclEncrypt,
naclDecrypt
naclDecrypt,
blake2AsHex,
blake2AsU8a,
sha256AsU8a,
sha512AsU8a,
keccak256AsU8a,
keccak512AsU8a
} from '@polkadot/util-crypto';
104 changes: 83 additions & 21 deletions packages/crypto/src/rescue/asU8a.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,95 @@
import { initCrypto } from '../initCrypto';
import { rescueAsU8a } from '.';

describe('blake2AsU8a', (): void => {
describe('rescueAsU8a', (): void => {
beforeEach(async (): Promise<void> => {
await initCrypto();
});

it('returns a 256 bit value', (): void => {
const bytes = new Uint8Array([
221, 232, 185, 77, 2, 251, 241, 95, 39, 243, 142, 48, 34, 12, 249, 159, 38, 154, 161, 127,
125, 135, 20, 204, 255, 187, 194, 127, 19, 246, 141, 171
]);

const hash = rescueAsU8a(bytes);

expect(hash).toHaveLength(32);
expect(hash).toEqual(
new Uint8Array([
132, 242, 4, 179, 208, 199, 40, 150, 43, 80, 99, 154, 232, 181, 187, 213, 93, 124, 201, 7,
75, 6, 104, 111, 252, 58, 108, 163, 213, 130, 31, 158
])
);
describe('not as u64', (): void => {
it('input 8 bytes length', (): void => {
const bytes = new Uint8Array([
221, 232, 185, 77, 2, 251, 43, 55
]);

const hash = rescueAsU8a(bytes);

expect(hash).toHaveLength(32);
expect(hash).toEqual(new Uint8Array([
13, 58, 150, 120, 89, 188, 67, 87,
187, 62, 231, 39, 60, 84, 79, 187,
222, 79, 86, 137, 87, 95, 214, 26,
6, 33, 140, 117, 119, 141, 119, 2
]));
});

it('input string', (): void => {
const hash = rescueAsU8a('abcd1234');

expect(hash).toHaveLength(32);
expect(hash).toEqual(
new Uint8Array([
212, 187, 16, 176, 223, 111, 125, 152,
232, 223, 53, 52, 239, 99, 173, 71,
181, 59, 174, 51, 80, 175, 78, 174,
66, 122, 204, 8, 39, 100, 158, 253
])
);
});

it('input no multiple of 8', (): void => {
const bytes = new Uint8Array([
221, 232, 73, 111, 67, 21, 2
]);

expect(() => rescueAsU8a(bytes)).toThrow('byte length of BigUint64Array should be a multiple of 8');
});
});

it('throw error when data length not right', (): void => {
const bytes = new Uint8Array([135, 194, 185, 125, 127]);
describe('as u64', (): void => {
it('input 8 bytes length', (): void => {
const bytes = new Uint8Array([
221, 232, 185, 77, 2, 251, 43, 55
]);

const hash = rescueAsU8a(bytes, true);

expect(hash).toHaveLength(32);
expect(hash).toEqual(new Uint8Array([
137, 85, 220, 139, 255, 19, 43, 192,
130, 88, 44, 228, 238, 183, 42, 144,
45, 12, 130, 174, 152, 200, 132, 167,
90, 183, 113, 188, 199, 52, 236, 70
]));
});

it('input string', (): void => {
const hash = rescueAsU8a('abcd1234', true);

expect(hash).toHaveLength(32);
expect(hash).toEqual(
new Uint8Array([
135, 118, 41, 144, 40, 252, 65,
100, 204, 245, 252, 44, 138, 223,
209, 13, 119, 200, 131, 115, 120,
31, 210, 44, 253, 198, 228, 212,
122, 61, 87, 245
])
);
});

it('input no multiple of 8', (): void => {
const bytes = new Uint8Array([
221, 232, 73, 111, 67, 21, 2
]);
const hash = rescueAsU8a(bytes, true);

expect(() => rescueAsU8a(bytes)).toThrow(
'data specifies the rescue input, it should contain 8 elements or more(over 8 but should be some multiple of 4)'
);
expect(hash).toEqual(new Uint8Array([
216, 218, 218, 150, 201, 105, 29, 238,
61, 188, 87, 77, 217, 169, 20, 240,
164, 50, 168, 9, 193, 174, 210, 163,
117, 26, 72, 214, 36, 169, 161, 77
]));
});
});
});
27 changes: 19 additions & 8 deletions packages/crypto/src/rescue/asU8a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { HexString } from '../types';

import { assert, u8aToHex, u8aToU8a } from '@polkadot/util';

import { rescueHash, u64ToU8 } from '@zcloak/wasm';
import { rescueHash } from '@zcloak/wasm';

/**
* @name rescueAsU8a
Expand All @@ -18,18 +18,29 @@ import { rescueHash, u64ToU8 } from '@zcloak/wasm';
* ```javascript
* import { rescueAsU8a } from '@zcloak/crypto';
*
* rescueAsU8a('abcd1234'); // => [135,118,41,144,40,252,65,100,204,245,252,44,138,223,209,13,119,200,131,115,120,31,210,44,253,198,228,212,122,61,87,245]
* rescueAsU8a('abcd1234'); // => [212, 187, 16, 176, 223, 111, 125, 152, 232, 223, 53, 52, 239, 99, 173, 71, 181, 59, 174, 51, 80, 175, 78, 174, 66, 122, 204, 8, 39, 100, 158, 253]
* ```
*/
export function rescueAsU8a(data: HexString | Uint8Array | string): Uint8Array {
export function rescueAsU8a(data: HexString | Uint8Array | string, asU64a = false): Uint8Array {
const u8a = u8aToU8a(data);

assert(
u8a.length >= 8 && u8a.length % 4 === 0,
'data specifies the rescue input, it should contain 8 elements or more(over 8 but should be some multiple of 4)'
);
let u64a: BigUint64Array;

return u64ToU8(rescueHash(u8a.toString()).toString());
if (asU64a) {
u64a = new BigUint64Array(u8a.length);
u8a.forEach((value, index) => {
u64a[index] = BigInt(value);
});
} else {
assert(u8a.length % 8 === 0, 'byte length of BigUint64Array should be a multiple of 8');
u64a = new BigUint64Array(u8a.buffer);
}

const result = rescueHash(u64a);

const resultU8a = new Uint8Array(result.buffer);

return resultU8a;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/vc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
"version": "0.0.1-8-x.0",
"main": "index.js",
"dependencies": {
"@ethereumjs/rlp": "^4.0.0",
"@polkadot/util": "^10.1.11",
"@zcloak/crypto": "0.0.1-8-x.0",
"@zcloak/did": "0.0.1-8-x.0",
"@zcloak/did-resolver": "0.0.1-8-x.0"
"@zcloak/did-resolver": "0.0.1-8-x.0",
"merkletreejs": "^0.3.1"
}
}
Loading

0 comments on commit 3da867d

Please sign in to comment.