Skip to content

Commit

Permalink
Merge pull request #244 from ChainSafe/dapplion/sha256-ts
Browse files Browse the repository at this point in the history
Convert as-sha256 to typescript
  • Loading branch information
wemeetagain authored Mar 23, 2022
2 parents 33b3cd6 + f04efab commit 9e5bd76
Show file tree
Hide file tree
Showing 29 changed files with 489 additions and 438 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"mocha": "^8.3.2",
"nyc": "^15.0.0",
"prettier": "^2.2.1",
"ts-loader": "^9.2.8",
"ts-node": "^9.1.1",
"typescript": "~4.2.3",
"webpack": "^5.43.0",
Expand Down
1 change: 0 additions & 1 deletion packages/as-sha256/.babel-register

This file was deleted.

24 changes: 0 additions & 24 deletions packages/as-sha256/.babelrc

This file was deleted.

11 changes: 11 additions & 0 deletions packages/as-sha256/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
printWidth: 120,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: false,
quoteProps: "as-needed",
trailingComma: "es5",
bracketSpacing: false,
arrowParens: "always",
};
6 changes: 3 additions & 3 deletions packages/as-sha256/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AssemblyScript implementation of SHA256.
`yarn add @chainsafe/as-sha256`

```typescript
import SHA256 from "@chainsafe/as-sha256";
import {digest, digest64, SHA256} from "@chainsafe/as-sha256";

let hash: Uint8Array;

Expand All @@ -20,10 +20,10 @@ const sha256 = new SHA256();
hash = sha256.init().update(Buffer.from("Hello world")).final();

// or use a one-pass interface
hash = SHA256.digest(Buffer.from("Hello world"));
hash = digest(Buffer.from("Hello world"));

// or use a faster one-pass interface for hashing (only) 64 bytes
hash = SHA256.digest64(Buffer.from("abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh"));
hash = digest64(Buffer.from("abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh"));
```

### License
Expand Down
10 changes: 3 additions & 7 deletions packages/as-sha256/assembly/__tests__/as-pect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ declare function test(description: string, callback: () => void): void;
*/
declare function throws(description: string, callback: () => void, message?: string): void;


/**
* This function creates a test that is expected to fail. This is useful to verify if a given
* behavior is expected to throw.
Expand Down Expand Up @@ -257,7 +256,6 @@ declare function log<T>(value: T | null): void;
*/
// @ts-ignore
declare class Expectation<T> {

/**
* Create a new expectation.
*
Expand Down Expand Up @@ -534,7 +532,7 @@ declare class Expectation<T> {
* expect<i32[]>([1, 2, 3]).toContain(3);
* ```
*/
// @ts-ignore: expected value should be known at compile time
// @ts-ignore: expected value should be known at compile time
toContain(expected: valueof<T>, message?: string): void;

/**
Expand Down Expand Up @@ -668,9 +666,9 @@ declare class Performance {
public static reportVariance(value: bool): void;
}
/**
* Assemblyscript uses reference counting to perform garbage collection. This means when you
* Assemblyscript uses reference counting to perform garbage collection. This means when you
* allocate a managed object and return it, it's reference count is one. If another variable aliases
* it then the reference count goes up. This static class contains a few convenience methods for
* it then the reference count goes up. This static class contains a few convenience methods for
* developers to test the current number of blocks allocated on the heap to make sure you aren't leaking
* references, e.i. keeping references to objects you expect to be collected.
*/
Expand Down Expand Up @@ -809,7 +807,6 @@ declare class RTrace {
public static activeTestBlocks(): usize[];
}


/**
* This class is static and contains private global values that contain metadata about the Actual
* value.
Expand All @@ -835,7 +832,6 @@ declare class Actual {
public static clear(): void;
}


/**
* This class is static and contains private global values that contain metadata about the Expected
* value.
Expand Down
37 changes: 22 additions & 15 deletions packages/as-sha256/assembly/__tests__/example.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { init, update, final, digest64 } from '..';
import {init, update, final, digest64} from "..";

export function toHexString(bin: Uint8Array): string {
let bin_len = bin.length;
Expand All @@ -7,8 +7,7 @@ export function toHexString(bin: Uint8Array): string {
let bin_i = bin[i] as u32;
let c = bin_i & 0xf;
let b = bin_i >> 4;
let x: u32 = ((87 + c + (((c - 10) >> 8) & ~38)) << 8) |
(87 + b + (((b - 10) >> 8) & ~38));
let x: u32 = ((87 + c + (((c - 10) >> 8) & ~38)) << 8) | (87 + b + (((b - 10) >> 8) & ~38));
hex += String.fromCharCode(x as u8);
x >>= 8;
hex += String.fromCharCode(x as u8);
Expand All @@ -26,49 +25,57 @@ function hash(data: Uint8Array): Uint8Array {

function hash64(data: Uint8Array): Uint8Array {
const output = new Uint8Array(32);
digest64(changetype<usize>(data.buffer),changetype<usize>(output.buffer));
digest64(changetype<usize>(data.buffer), changetype<usize>(output.buffer));
return output;
}

describe("example", () => {
it("Hash: empty array", () => {
let preImgArrayBuffer = new Uint8Array(0);
expect<string>(toHexString(hash(preImgArrayBuffer))).toBe("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
expect<string>(toHexString(hash(preImgArrayBuffer))).toBe(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
);
});

it("Hash: abc", () => {
let preImgString = "abc";
let preImgArrayBuffer = Uint8Array.wrap(String.UTF8.encode(preImgString));
expect<string>(toHexString(hash(preImgArrayBuffer))).toBe("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
expect<string>(toHexString(hash(preImgArrayBuffer))).toBe(
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
});

it("Hash: lorem ipsum", () => {
let preImgString = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
let preImgString =
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
let preImgArrayBuffer = Uint8Array.wrap(String.UTF8.encode(preImgString));
expect<string>(toHexString(hash(preImgArrayBuffer))).toBe("7321348c8894678447b54c888fdbc4e4b825bf4d1eb0cfb27874286a23ea9fd2");
expect<string>(toHexString(hash(preImgArrayBuffer))).toBe(
"7321348c8894678447b54c888fdbc4e4b825bf4d1eb0cfb27874286a23ea9fd2"
);
});

it("Hash: abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", () => {
let preImgString = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
let preImgArrayBuffer = Uint8Array.wrap(String.UTF8.encode(preImgString));
expect<string>(toHexString(hash(preImgArrayBuffer))).toBe("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
expect<string>(toHexString(hash(preImgArrayBuffer))).toBe(
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
);
});

it("Hash64: gajindergajindergajindergajindergajindergajindergajindergajinder", () => {
let preImgString = "gajindergajindergajindergajindergajindergajindergajindergajinder";
let preImgArrayBuffer = Uint8Array.wrap(String.UTF8.encode(preImgString));
expect<string>(toHexString(hash64(preImgArrayBuffer))).toBe("be39380ff1d0261e6f37dafe4278b662ef611f1cb2f7c0a18348b2d7eb14cf6e");
expect<string>(toHexString(hash64(preImgArrayBuffer))).toBe(
"be39380ff1d0261e6f37dafe4278b662ef611f1cb2f7c0a18348b2d7eb14cf6e"
);
});

it("CompareHash64: harkamalharkamalharkamalharkamalharkamalharkamalharkamalharkamal", () => {
let preImgString = "harkamalharkamalharkamalharkamalharkamalharkamalharkamalharkamal";
let preImgArrayBuffer = Uint8Array.wrap(String.UTF8.encode(preImgString));

const normalHash=toHexString(hash(preImgArrayBuffer));
const fastHash64=toHexString(hash64(preImgArrayBuffer));
const normalHash = toHexString(hash(preImgArrayBuffer));
const fastHash64 = toHexString(hash64(preImgArrayBuffer));
expect<string>(fastHash64).toBe(normalHash);

});

});

6 changes: 2 additions & 4 deletions packages/as-sha256/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"extends": "../node_modules/assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}
"include": ["./**/*.ts"]
}
11 changes: 0 additions & 11 deletions packages/as-sha256/examples/browser-example.html

This file was deleted.

50 changes: 0 additions & 50 deletions packages/as-sha256/examples/run-wasm.js

This file was deleted.

8 changes: 4 additions & 4 deletions packages/as-sha256/karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ module.exports = function (config) {
config.set({
frameworks: ['mocha', 'chai'],
files: [
'test/index.spec.js'
'test/unit/index.test.ts'
],
preprocessors: {
'test/index.spec.js': ['webpack']
'test/unit/index.test.ts': ['webpack']
},
webpack: {
mode: "production",
module: {
rules: [
{
test: /\.js?$/,
test: /\.ts?$/,
exclude: /node_modules/,
loader: 'babel-loader',
loader: 'ts-loader',
},
]
},
Expand Down
Loading

0 comments on commit 9e5bd76

Please sign in to comment.