@btc-vision/btc-runtime 1.1.7 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +186 -186
- package/package.json +2 -2
- package/runtime/buffer/BytesReader.ts +200 -200
- package/runtime/buffer/BytesWriter.ts +262 -350
- package/runtime/contracts/DeployableOP_20.ts +407 -402
- package/runtime/contracts/OP_20.ts +9 -9
- package/runtime/contracts/OP_NET.ts +78 -76
- package/runtime/contracts/interfaces/IOP_20.ts +21 -21
- package/runtime/contracts/interfaces/OP20InitParameters.ts +15 -15
- package/runtime/env/BTCEnvironment.ts +330 -330
- package/runtime/env/global.ts +32 -32
- package/runtime/env/index.ts +3 -3
- package/runtime/events/NetEvent.ts +23 -27
- package/runtime/events/predefined/ApproveEvent.ts +16 -16
- package/runtime/events/predefined/BurnEvent.ts +13 -13
- package/runtime/events/predefined/ClaimEvent.ts +13 -13
- package/runtime/events/predefined/MintEvent.ts +15 -15
- package/runtime/events/predefined/StakeEvent.ts +13 -13
- package/runtime/events/predefined/TransferEvent.ts +16 -16
- package/runtime/events/predefined/UnstakeEvent.ts +13 -13
- package/runtime/events/predefined/index.ts +7 -7
- package/runtime/exports/index.ts +37 -37
- package/runtime/generic/Map.ts +65 -65
- package/runtime/generic/MapU256.ts +57 -57
- package/runtime/index.ts +57 -57
- package/runtime/interfaces/DeployContractResponse.ts +12 -12
- package/runtime/interfaces/IBTC.ts +6 -6
- package/runtime/lang/Definitions.ts +1 -1
- package/runtime/math/abi.ts +37 -37
- package/runtime/math/bytes.ts +34 -34
- package/runtime/math/cyrb53.ts +48 -48
- package/runtime/math/rnd.ts +55 -55
- package/runtime/math/sha256.ts +12 -12
- package/runtime/memory/AddressMemoryMap.ts +44 -44
- package/runtime/memory/KeyMerger.ts +53 -53
- package/runtime/memory/MemorySlot.ts +1 -1
- package/runtime/memory/MemorySlotPointer.ts +3 -3
- package/runtime/memory/MultiAddressMemoryMap.ts +62 -62
- package/runtime/shared-libraries/OP20Utils.ts +21 -21
- package/runtime/shared-libraries/TransferHelper.ts +64 -64
- package/runtime/storage/Serializable.ts +79 -79
- package/runtime/storage/StoredBoolean.ts +48 -48
- package/runtime/storage/StoredString.ts +145 -145
- package/runtime/storage/StoredU256.ts +225 -250
- package/runtime/types/Address.ts +5 -5
- package/runtime/types/Revert.ts +5 -5
- package/runtime/types/SafeMath.ts +211 -197
- package/runtime/types/index.ts +8 -8
- package/runtime/universal/ABIRegistry.ts +72 -72
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import { Revert } from '../types/Revert';
|
|
2
|
-
import { u256 } from 'as-bignum/assembly';
|
|
3
|
-
import { Map } from './Map';
|
|
4
|
-
|
|
5
|
-
export class MapU256 extends Map<u256, u256> {
|
|
6
|
-
public set(key: u256, value: u256): void {
|
|
7
|
-
const index: i32 = this._keys.indexOf(key);
|
|
8
|
-
if (index == -1) {
|
|
9
|
-
this._keys.push(key);
|
|
10
|
-
this._values.push(value);
|
|
11
|
-
} else {
|
|
12
|
-
this._values[index] = value;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public indexOf(pointerHash: u256): i32 {
|
|
17
|
-
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
18
|
-
const key = this._keys[i];
|
|
19
|
-
|
|
20
|
-
if (u256.eq(key, pointerHash)) {
|
|
21
|
-
return i;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return -1;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public has(key: u256): bool {
|
|
29
|
-
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
30
|
-
if (u256.eq(this._keys[i], key)) {
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public get(key: u256): u256 {
|
|
39
|
-
const index: i32 = this.indexOf(key);
|
|
40
|
-
if (index == -1) {
|
|
41
|
-
throw new Revert('Key not found in map');
|
|
42
|
-
}
|
|
43
|
-
return this._values[index];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public delete(key: u256): bool {
|
|
47
|
-
const index: i32 = this.indexOf(key);
|
|
48
|
-
if (index == -1) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
this._keys.splice(index, 1);
|
|
53
|
-
this._values.splice(index, 1);
|
|
54
|
-
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
1
|
+
import { Revert } from '../types/Revert';
|
|
2
|
+
import { u256 } from 'as-bignum/assembly';
|
|
3
|
+
import { Map } from './Map';
|
|
4
|
+
|
|
5
|
+
export class MapU256 extends Map<u256, u256> {
|
|
6
|
+
public set(key: u256, value: u256): void {
|
|
7
|
+
const index: i32 = this._keys.indexOf(key);
|
|
8
|
+
if (index == -1) {
|
|
9
|
+
this._keys.push(key);
|
|
10
|
+
this._values.push(value);
|
|
11
|
+
} else {
|
|
12
|
+
this._values[index] = value;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public indexOf(pointerHash: u256): i32 {
|
|
17
|
+
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
18
|
+
const key = this._keys[i];
|
|
19
|
+
|
|
20
|
+
if (u256.eq(key, pointerHash)) {
|
|
21
|
+
return i;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return -1;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public has(key: u256): bool {
|
|
29
|
+
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
30
|
+
if (u256.eq(this._keys[i], key)) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public get(key: u256): u256 {
|
|
39
|
+
const index: i32 = this.indexOf(key);
|
|
40
|
+
if (index == -1) {
|
|
41
|
+
throw new Revert('Key not found in map');
|
|
42
|
+
}
|
|
43
|
+
return this._values[index];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public delete(key: u256): bool {
|
|
47
|
+
const index: i32 = this.indexOf(key);
|
|
48
|
+
if (index == -1) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this._keys.splice(index, 1);
|
|
53
|
+
this._values.splice(index, 1);
|
|
54
|
+
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}
|
package/runtime/index.ts
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
/** Environment */
|
|
2
|
-
export * from './env';
|
|
3
|
-
|
|
4
|
-
/** Contracts */
|
|
5
|
-
export * from './contracts/interfaces/IOP_20';
|
|
6
|
-
export * from './contracts/OP_20';
|
|
7
|
-
export * from './contracts/DeployableOP_20';
|
|
8
|
-
export * from './contracts/OP_NET';
|
|
9
|
-
export * from './contracts/interfaces/OP20InitParameters';
|
|
10
|
-
|
|
11
|
-
/** Buffer */
|
|
12
|
-
export * from './buffer/BytesReader';
|
|
13
|
-
export * from './buffer/BytesWriter';
|
|
14
|
-
|
|
15
|
-
/** Interfaces */
|
|
16
|
-
export * from './interfaces/DeployContractResponse';
|
|
17
|
-
|
|
18
|
-
/** Events */
|
|
19
|
-
export * from './events/NetEvent';
|
|
20
|
-
export * from './events/predefined';
|
|
21
|
-
|
|
22
|
-
/** Types */
|
|
23
|
-
export * from './generic/Map';
|
|
24
|
-
export * from './interfaces/IBTC';
|
|
25
|
-
|
|
26
|
-
/** Definitions */
|
|
27
|
-
export * from './lang/Definitions';
|
|
28
|
-
export * from './types/Address';
|
|
29
|
-
export * from './types/Revert';
|
|
30
|
-
export * from './types/SafeMath';
|
|
31
|
-
|
|
32
|
-
/** Math */
|
|
33
|
-
export * from './math/abi';
|
|
34
|
-
export * from './math/bytes';
|
|
35
|
-
export * from './math/cyrb53';
|
|
36
|
-
export * from './math/sha256';
|
|
37
|
-
export * from './math/rnd';
|
|
38
|
-
|
|
39
|
-
/** Memory */
|
|
40
|
-
export * from './memory/AddressMemoryMap';
|
|
41
|
-
export * from './memory/MemorySlot';
|
|
42
|
-
export * from './memory/MemorySlotPointer';
|
|
43
|
-
export * from './memory/KeyMerger';
|
|
44
|
-
export * from './memory/MultiAddressMemoryMap';
|
|
45
|
-
|
|
46
|
-
/** Storage */
|
|
47
|
-
export * from './storage/StoredU256';
|
|
48
|
-
export * from './storage/StoredString';
|
|
49
|
-
export * from './storage/StoredBoolean';
|
|
50
|
-
export * from './storage/Serializable';
|
|
51
|
-
|
|
52
|
-
/** Universal */
|
|
53
|
-
export * from './universal/ABIRegistry';
|
|
54
|
-
|
|
55
|
-
/** Shared libraries */
|
|
56
|
-
export * from './shared-libraries/TransferHelper';
|
|
57
|
-
export * from './shared-libraries/OP20Utils';
|
|
1
|
+
/** Environment */
|
|
2
|
+
export * from './env';
|
|
3
|
+
|
|
4
|
+
/** Contracts */
|
|
5
|
+
export * from './contracts/interfaces/IOP_20';
|
|
6
|
+
export * from './contracts/OP_20';
|
|
7
|
+
export * from './contracts/DeployableOP_20';
|
|
8
|
+
export * from './contracts/OP_NET';
|
|
9
|
+
export * from './contracts/interfaces/OP20InitParameters';
|
|
10
|
+
|
|
11
|
+
/** Buffer */
|
|
12
|
+
export * from './buffer/BytesReader';
|
|
13
|
+
export * from './buffer/BytesWriter';
|
|
14
|
+
|
|
15
|
+
/** Interfaces */
|
|
16
|
+
export * from './interfaces/DeployContractResponse';
|
|
17
|
+
|
|
18
|
+
/** Events */
|
|
19
|
+
export * from './events/NetEvent';
|
|
20
|
+
export * from './events/predefined';
|
|
21
|
+
|
|
22
|
+
/** Types */
|
|
23
|
+
export * from './generic/Map';
|
|
24
|
+
export * from './interfaces/IBTC';
|
|
25
|
+
|
|
26
|
+
/** Definitions */
|
|
27
|
+
export * from './lang/Definitions';
|
|
28
|
+
export * from './types/Address';
|
|
29
|
+
export * from './types/Revert';
|
|
30
|
+
export * from './types/SafeMath';
|
|
31
|
+
|
|
32
|
+
/** Math */
|
|
33
|
+
export * from './math/abi';
|
|
34
|
+
export * from './math/bytes';
|
|
35
|
+
export * from './math/cyrb53';
|
|
36
|
+
export * from './math/sha256';
|
|
37
|
+
export * from './math/rnd';
|
|
38
|
+
|
|
39
|
+
/** Memory */
|
|
40
|
+
export * from './memory/AddressMemoryMap';
|
|
41
|
+
export * from './memory/MemorySlot';
|
|
42
|
+
export * from './memory/MemorySlotPointer';
|
|
43
|
+
export * from './memory/KeyMerger';
|
|
44
|
+
export * from './memory/MultiAddressMemoryMap';
|
|
45
|
+
|
|
46
|
+
/** Storage */
|
|
47
|
+
export * from './storage/StoredU256';
|
|
48
|
+
export * from './storage/StoredString';
|
|
49
|
+
export * from './storage/StoredBoolean';
|
|
50
|
+
export * from './storage/Serializable';
|
|
51
|
+
|
|
52
|
+
/** Universal */
|
|
53
|
+
export * from './universal/ABIRegistry';
|
|
54
|
+
|
|
55
|
+
/** Shared libraries */
|
|
56
|
+
export * from './shared-libraries/TransferHelper';
|
|
57
|
+
export * from './shared-libraries/OP20Utils';
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { u256 } from 'as-bignum/assembly';
|
|
2
|
-
import { Address } from '../types/Address';
|
|
3
|
-
|
|
4
|
-
export class DeployContractResponse {
|
|
5
|
-
readonly virtualAddress: u256;
|
|
6
|
-
readonly contractAddress: Address;
|
|
7
|
-
|
|
8
|
-
constructor(virtualAddress: u256, contractAddress: Address) {
|
|
9
|
-
this.virtualAddress = virtualAddress;
|
|
10
|
-
this.contractAddress = contractAddress;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
1
|
+
import { u256 } from 'as-bignum/assembly';
|
|
2
|
+
import { Address } from '../types/Address';
|
|
3
|
+
|
|
4
|
+
export class DeployContractResponse {
|
|
5
|
+
readonly virtualAddress: u256;
|
|
6
|
+
readonly contractAddress: Address;
|
|
7
|
+
|
|
8
|
+
constructor(virtualAddress: u256, contractAddress: Address) {
|
|
9
|
+
this.virtualAddress = virtualAddress;
|
|
10
|
+
this.contractAddress = contractAddress;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Address } from '../types/Address';
|
|
2
|
-
|
|
3
|
-
export interface IBTC {
|
|
4
|
-
readonly owner: Address;
|
|
5
|
-
readonly address: Address;
|
|
6
|
-
}
|
|
1
|
+
import { Address } from '../types/Address';
|
|
2
|
+
|
|
3
|
+
export interface IBTC {
|
|
4
|
+
readonly owner: Address;
|
|
5
|
+
readonly address: Address;
|
|
6
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export type Potential<T> = T | null;
|
|
1
|
+
export type Potential<T> = T | null;
|
package/runtime/math/abi.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
// SO IN TYPESCRIPT, WE CAN NOT USE TWO METHOD WITH THE SAME NAME. SO NOT ADDING THE TYPE TO THE HASH IS A DESIGN CHOICE.
|
|
2
|
-
import { bytes32, bytes4 } from './bytes';
|
|
3
|
-
import { MemorySlotPointer } from '../memory/MemorySlotPointer';
|
|
4
|
-
import { u256 } from 'as-bignum/assembly';
|
|
5
|
-
import { Sha256 } from './sha256';
|
|
6
|
-
|
|
7
|
-
export type Selector = u32;
|
|
8
|
-
|
|
9
|
-
export function encodeSelector(name: string): Selector {
|
|
10
|
-
const typed = Uint8Array.wrap(String.UTF8.encode(name));
|
|
11
|
-
const hash = Sha256.hash(typed);
|
|
12
|
-
|
|
13
|
-
return bytes4(hash);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function encodePointer(str: string): MemorySlotPointer {
|
|
17
|
-
const typed = Uint8Array.wrap(String.UTF8.encode(str));
|
|
18
|
-
const hash = Sha256.hash(typed);
|
|
19
|
-
|
|
20
|
-
return bytes32(hash);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function encodePointerHash(pointer: u16, sub: u256): MemorySlotPointer {
|
|
24
|
-
const finalBuffer: Uint8Array = new Uint8Array(34);
|
|
25
|
-
const mergedKey: u8[] = [u8(pointer & u16(0xff)), u8((pointer >> u16(8)) & u16(0xff))];
|
|
26
|
-
|
|
27
|
-
for (let i: i32 = 0; i < mergedKey.length; i++) {
|
|
28
|
-
finalBuffer[i] = mergedKey[i];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const subKey = sub.toUint8Array();
|
|
32
|
-
for (let i: i32 = 0; i < subKey.length; i++) {
|
|
33
|
-
finalBuffer[mergedKey.length + i] = subKey[i];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return bytes32(Sha256.hash(finalBuffer));
|
|
37
|
-
}
|
|
1
|
+
// SO IN TYPESCRIPT, WE CAN NOT USE TWO METHOD WITH THE SAME NAME. SO NOT ADDING THE TYPE TO THE HASH IS A DESIGN CHOICE.
|
|
2
|
+
import { bytes32, bytes4 } from './bytes';
|
|
3
|
+
import { MemorySlotPointer } from '../memory/MemorySlotPointer';
|
|
4
|
+
import { u256 } from 'as-bignum/assembly';
|
|
5
|
+
import { Sha256 } from './sha256';
|
|
6
|
+
|
|
7
|
+
export type Selector = u32;
|
|
8
|
+
|
|
9
|
+
export function encodeSelector(name: string): Selector {
|
|
10
|
+
const typed = Uint8Array.wrap(String.UTF8.encode(name));
|
|
11
|
+
const hash = Sha256.hash(typed);
|
|
12
|
+
|
|
13
|
+
return bytes4(hash);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function encodePointer(str: string): MemorySlotPointer {
|
|
17
|
+
const typed = Uint8Array.wrap(String.UTF8.encode(str));
|
|
18
|
+
const hash = Sha256.hash(typed);
|
|
19
|
+
|
|
20
|
+
return bytes32(hash);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function encodePointerHash(pointer: u16, sub: u256): MemorySlotPointer {
|
|
24
|
+
const finalBuffer: Uint8Array = new Uint8Array(34);
|
|
25
|
+
const mergedKey: u8[] = [u8(pointer & u16(0xff)), u8((pointer >> u16(8)) & u16(0xff))];
|
|
26
|
+
|
|
27
|
+
for (let i: i32 = 0; i < mergedKey.length; i++) {
|
|
28
|
+
finalBuffer[i] = mergedKey[i];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const subKey = sub.toUint8Array();
|
|
32
|
+
for (let i: i32 = 0; i < subKey.length; i++) {
|
|
33
|
+
finalBuffer[mergedKey.length + i] = subKey[i];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return bytes32(Sha256.hash(finalBuffer));
|
|
37
|
+
}
|
package/runtime/math/bytes.ts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { u256 } from 'as-bignum/assembly';
|
|
2
|
-
|
|
3
|
-
export function bytes(number: u256[]): Uint8Array {
|
|
4
|
-
const result = new Uint8Array(32 * number.length);
|
|
5
|
-
for (let i: u8 = 0; i < 32; i++) {
|
|
6
|
-
const num: Uint8Array = number[31 - i].toUint8Array();
|
|
7
|
-
for (let j: u8 = 0; j < u8(number.length); j++) {
|
|
8
|
-
result[i + j * 32] = num[i];
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return result;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function bytes4(number: Uint8Array): u32 {
|
|
16
|
-
return (u32(number[0]) << 24) | (u32(number[1]) << 16) | (u32(number[2]) << 8) | u32(number[3]);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function bytes8(number: Uint8Array): u64 {
|
|
20
|
-
return (
|
|
21
|
-
(u64(number[0]) << u64(56)) |
|
|
22
|
-
(u64(number[1]) << u64(48)) |
|
|
23
|
-
(u64(number[2]) << u64(40)) |
|
|
24
|
-
(u64(number[3]) << u64(32)) |
|
|
25
|
-
(u64(number[4]) << 24) |
|
|
26
|
-
(u64(number[5]) << 16) |
|
|
27
|
-
(u64(number[6]) << 8) |
|
|
28
|
-
u64(number[7])
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function bytes32(number: Uint8Array): u256 {
|
|
33
|
-
return u256.fromBytes(number);
|
|
34
|
-
}
|
|
1
|
+
import { u256 } from 'as-bignum/assembly';
|
|
2
|
+
|
|
3
|
+
export function bytes(number: u256[]): Uint8Array {
|
|
4
|
+
const result = new Uint8Array(32 * number.length);
|
|
5
|
+
for (let i: u8 = 0; i < 32; i++) {
|
|
6
|
+
const num: Uint8Array = number[31 - i].toUint8Array();
|
|
7
|
+
for (let j: u8 = 0; j < u8(number.length); j++) {
|
|
8
|
+
result[i + j * 32] = num[i];
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function bytes4(number: Uint8Array): u32 {
|
|
16
|
+
return (u32(number[0]) << 24) | (u32(number[1]) << 16) | (u32(number[2]) << 8) | u32(number[3]);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function bytes8(number: Uint8Array): u64 {
|
|
20
|
+
return (
|
|
21
|
+
(u64(number[0]) << u64(56)) |
|
|
22
|
+
(u64(number[1]) << u64(48)) |
|
|
23
|
+
(u64(number[2]) << u64(40)) |
|
|
24
|
+
(u64(number[3]) << u64(32)) |
|
|
25
|
+
(u64(number[4]) << 24) |
|
|
26
|
+
(u64(number[5]) << 16) |
|
|
27
|
+
(u64(number[6]) << 8) |
|
|
28
|
+
u64(number[7])
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function bytes32(number: Uint8Array): u256 {
|
|
33
|
+
return u256.fromBytes(number);
|
|
34
|
+
}
|
package/runtime/math/cyrb53.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
export function cyrb53(str: string, seed: i32 = 0): i64 {
|
|
2
|
-
let h1: i32 = 0xdeadbeef ^ seed;
|
|
3
|
-
let h2: i32 = 0x41c6ce57 ^ seed;
|
|
4
|
-
for (let i: i32 = 0; i < str.length; i++) {
|
|
5
|
-
const ch: i32 = str.charCodeAt(i);
|
|
6
|
-
h1 = (h1 ^ ch) * 2654435761;
|
|
7
|
-
h2 = (h2 ^ ch) * 1597334677;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
h1 = ((h1 ^ (h1 >>> 16)) * 2246822507) ^ ((h2 ^ (h2 >>> 13)) * 3266489909);
|
|
11
|
-
h2 = ((h2 ^ (h2 >>> 16)) * 2246822507) ^ ((h1 ^ (h1 >>> 13)) * 3266489909);
|
|
12
|
-
|
|
13
|
-
return 4294967296 * i64((2097151 & h2) >>> 0) + i64(h1 >>> 0);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function imul64(a: u64, b: u64): u64 {
|
|
17
|
-
const aLow: u64 = a & 0xffffffff;
|
|
18
|
-
const aHigh: u64 = a >> 32;
|
|
19
|
-
const bLow: u64 = b & 0xffffffff;
|
|
20
|
-
const bHigh: u64 = b >> 32;
|
|
21
|
-
|
|
22
|
-
const low: u64 = aLow * bLow;
|
|
23
|
-
const middle1: u64 = (aHigh * bLow) << 32;
|
|
24
|
-
const middle2: u64 = (aLow * bHigh) << 32;
|
|
25
|
-
const high: u64 = (aHigh * bHigh) << 64;
|
|
26
|
-
|
|
27
|
-
return low + middle1 + middle2 + high;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function cyrb53a(str: u8[], seed: i32 = 0): u64 {
|
|
31
|
-
let h1: u64 = u64(0xdeadbeef ^ seed);
|
|
32
|
-
let h2: u64 = u64(0x41c6ce57 ^ seed);
|
|
33
|
-
|
|
34
|
-
for (let i: i32 = 0; i < str.length; i++) {
|
|
35
|
-
const ch: u64 = u64(str[i]);
|
|
36
|
-
h1 = imul64(h1 ^ ch, 0x85ebca77);
|
|
37
|
-
h2 = imul64(h2 ^ ch, 0xc2b2ae3d);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
h1 ^= imul64(h1 ^ (h2 >> 15), 0x735a2d97);
|
|
41
|
-
h2 ^= imul64(h2 ^ (h1 >> 15), 0xcaf649a9);
|
|
42
|
-
h1 ^= h2 >> 16;
|
|
43
|
-
h2 ^= h1 >> 16;
|
|
44
|
-
|
|
45
|
-
// We are sure that the result is within the range of u64.
|
|
46
|
-
// eslint-disable-next-line no-loss-of-precision
|
|
47
|
-
return (2097152 * (h2 & 0xffffffffffffffff) + (h1 >> 11)) & 0xffffffffffffffff;
|
|
48
|
-
}
|
|
1
|
+
export function cyrb53(str: string, seed: i32 = 0): i64 {
|
|
2
|
+
let h1: i32 = 0xdeadbeef ^ seed;
|
|
3
|
+
let h2: i32 = 0x41c6ce57 ^ seed;
|
|
4
|
+
for (let i: i32 = 0; i < str.length; i++) {
|
|
5
|
+
const ch: i32 = str.charCodeAt(i);
|
|
6
|
+
h1 = (h1 ^ ch) * 2654435761;
|
|
7
|
+
h2 = (h2 ^ ch) * 1597334677;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
h1 = ((h1 ^ (h1 >>> 16)) * 2246822507) ^ ((h2 ^ (h2 >>> 13)) * 3266489909);
|
|
11
|
+
h2 = ((h2 ^ (h2 >>> 16)) * 2246822507) ^ ((h1 ^ (h1 >>> 13)) * 3266489909);
|
|
12
|
+
|
|
13
|
+
return 4294967296 * i64((2097151 & h2) >>> 0) + i64(h1 >>> 0);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function imul64(a: u64, b: u64): u64 {
|
|
17
|
+
const aLow: u64 = a & 0xffffffff;
|
|
18
|
+
const aHigh: u64 = a >> 32;
|
|
19
|
+
const bLow: u64 = b & 0xffffffff;
|
|
20
|
+
const bHigh: u64 = b >> 32;
|
|
21
|
+
|
|
22
|
+
const low: u64 = aLow * bLow;
|
|
23
|
+
const middle1: u64 = (aHigh * bLow) << 32;
|
|
24
|
+
const middle2: u64 = (aLow * bHigh) << 32;
|
|
25
|
+
const high: u64 = (aHigh * bHigh) << 64;
|
|
26
|
+
|
|
27
|
+
return low + middle1 + middle2 + high;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function cyrb53a(str: u8[], seed: i32 = 0): u64 {
|
|
31
|
+
let h1: u64 = u64(0xdeadbeef ^ seed);
|
|
32
|
+
let h2: u64 = u64(0x41c6ce57 ^ seed);
|
|
33
|
+
|
|
34
|
+
for (let i: i32 = 0; i < str.length; i++) {
|
|
35
|
+
const ch: u64 = u64(str[i]);
|
|
36
|
+
h1 = imul64(h1 ^ ch, 0x85ebca77);
|
|
37
|
+
h2 = imul64(h2 ^ ch, 0xc2b2ae3d);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
h1 ^= imul64(h1 ^ (h2 >> 15), 0x735a2d97);
|
|
41
|
+
h2 ^= imul64(h2 ^ (h1 >> 15), 0xcaf649a9);
|
|
42
|
+
h1 ^= h2 >> 16;
|
|
43
|
+
h2 ^= h1 >> 16;
|
|
44
|
+
|
|
45
|
+
// We are sure that the result is within the range of u64.
|
|
46
|
+
// eslint-disable-next-line no-loss-of-precision
|
|
47
|
+
return (2097152 * (h2 & 0xffffffffffffffff) + (h1 >> 11)) & 0xffffffffffffffff;
|
|
48
|
+
}
|
package/runtime/math/rnd.ts
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
let random_state0_64: u64;
|
|
2
|
-
let random_state1_64: u64;
|
|
3
|
-
let random_state0_32: u32;
|
|
4
|
-
let random_state1_32: u32;
|
|
5
|
-
let random_seeded = false;
|
|
6
|
-
|
|
7
|
-
function murmurHash3(h: u64): u64 {
|
|
8
|
-
// Force all bits of a hash block to avalanche
|
|
9
|
-
h ^= h >> 33; // see: https://github.com/aappleby/smhasher
|
|
10
|
-
// eslint-disable-next-line no-loss-of-precision
|
|
11
|
-
h *= 0xff51afd7ed558ccd;
|
|
12
|
-
h ^= h >> 33;
|
|
13
|
-
// eslint-disable-next-line no-loss-of-precision
|
|
14
|
-
h *= 0xc4ceb9fe1a85ec53;
|
|
15
|
-
h ^= h >> 33;
|
|
16
|
-
return h;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function splitMix32(h: u32): u32 {
|
|
20
|
-
h += 0x6d2b79f5;
|
|
21
|
-
h = (h ^ (h >> 15)) * (h | 1);
|
|
22
|
-
h ^= h + (h ^ (h >> 7)) * (h | 61);
|
|
23
|
-
return h ^ (h >> 14);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function seedRandom(value: i64): void {
|
|
27
|
-
// Instead zero seed use golden ratio:
|
|
28
|
-
// phi = (1 + sqrt(5)) / 2
|
|
29
|
-
// trunc(2^64 / phi) = 0x9e3779b97f4a7c15
|
|
30
|
-
|
|
31
|
-
// eslint-disable-next-line no-loss-of-precision
|
|
32
|
-
if (value == 0) value = 0x9e3779b97f4a7c15;
|
|
33
|
-
random_state0_64 = murmurHash3(value);
|
|
34
|
-
random_state1_64 = murmurHash3(~random_state0_64);
|
|
35
|
-
random_state0_32 = splitMix32(<u32>value);
|
|
36
|
-
random_state1_32 = splitMix32(random_state0_32);
|
|
37
|
-
random_seeded = true;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Safe deterministic random number generator
|
|
42
|
-
* @param {u64} seed - The seed to use
|
|
43
|
-
*/
|
|
44
|
-
export function randomU64(seed: i64): u64 {
|
|
45
|
-
if (!random_seeded) seedRandom(seed);
|
|
46
|
-
let s1 = random_state0_64;
|
|
47
|
-
const s0 = random_state1_64;
|
|
48
|
-
random_state0_64 = s0;
|
|
49
|
-
s1 ^= s1 << 23;
|
|
50
|
-
s1 ^= s1 >> 17;
|
|
51
|
-
s1 ^= s0;
|
|
52
|
-
s1 ^= s0 >> 26;
|
|
53
|
-
random_state1_64 = s1;
|
|
54
|
-
return s0;
|
|
55
|
-
}
|
|
1
|
+
let random_state0_64: u64;
|
|
2
|
+
let random_state1_64: u64;
|
|
3
|
+
let random_state0_32: u32;
|
|
4
|
+
let random_state1_32: u32;
|
|
5
|
+
let random_seeded = false;
|
|
6
|
+
|
|
7
|
+
function murmurHash3(h: u64): u64 {
|
|
8
|
+
// Force all bits of a hash block to avalanche
|
|
9
|
+
h ^= h >> 33; // see: https://github.com/aappleby/smhasher
|
|
10
|
+
// eslint-disable-next-line no-loss-of-precision
|
|
11
|
+
h *= 0xff51afd7ed558ccd;
|
|
12
|
+
h ^= h >> 33;
|
|
13
|
+
// eslint-disable-next-line no-loss-of-precision
|
|
14
|
+
h *= 0xc4ceb9fe1a85ec53;
|
|
15
|
+
h ^= h >> 33;
|
|
16
|
+
return h;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function splitMix32(h: u32): u32 {
|
|
20
|
+
h += 0x6d2b79f5;
|
|
21
|
+
h = (h ^ (h >> 15)) * (h | 1);
|
|
22
|
+
h ^= h + (h ^ (h >> 7)) * (h | 61);
|
|
23
|
+
return h ^ (h >> 14);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function seedRandom(value: i64): void {
|
|
27
|
+
// Instead zero seed use golden ratio:
|
|
28
|
+
// phi = (1 + sqrt(5)) / 2
|
|
29
|
+
// trunc(2^64 / phi) = 0x9e3779b97f4a7c15
|
|
30
|
+
|
|
31
|
+
// eslint-disable-next-line no-loss-of-precision
|
|
32
|
+
if (value == 0) value = 0x9e3779b97f4a7c15;
|
|
33
|
+
random_state0_64 = murmurHash3(value);
|
|
34
|
+
random_state1_64 = murmurHash3(~random_state0_64);
|
|
35
|
+
random_state0_32 = splitMix32(<u32>value);
|
|
36
|
+
random_state1_32 = splitMix32(random_state0_32);
|
|
37
|
+
random_seeded = true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Safe deterministic random number generator
|
|
42
|
+
* @param {u64} seed - The seed to use
|
|
43
|
+
*/
|
|
44
|
+
export function randomU64(seed: i64): u64 {
|
|
45
|
+
if (!random_seeded) seedRandom(seed);
|
|
46
|
+
let s1 = random_state0_64;
|
|
47
|
+
const s0 = random_state1_64;
|
|
48
|
+
random_state0_64 = s0;
|
|
49
|
+
s1 ^= s1 << 23;
|
|
50
|
+
s1 ^= s1 >> 17;
|
|
51
|
+
s1 ^= s0;
|
|
52
|
+
s1 ^= s0 >> 26;
|
|
53
|
+
random_state1_64 = s1;
|
|
54
|
+
return s0;
|
|
55
|
+
}
|
package/runtime/math/sha256.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { sha256 } from '../env/global';
|
|
2
|
-
|
|
3
|
-
export class Sha256 {
|
|
4
|
-
static hash(buffer: Uint8Array): Uint8Array {
|
|
5
|
-
return sha256(buffer);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
static hash256(buffer: Uint8Array): Uint8Array {
|
|
9
|
-
const hash = sha256(buffer);
|
|
10
|
-
return sha256(hash);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
1
|
+
import { sha256 } from '../env/global';
|
|
2
|
+
|
|
3
|
+
export class Sha256 {
|
|
4
|
+
static hash(buffer: Uint8Array): Uint8Array {
|
|
5
|
+
return sha256(buffer);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
static hash256(buffer: Uint8Array): Uint8Array {
|
|
9
|
+
const hash = sha256(buffer);
|
|
10
|
+
return sha256(hash);
|
|
11
|
+
}
|
|
12
|
+
}
|