@btc-vision/btc-runtime 1.0.30 → 1.0.31
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/package.json +1 -1
- package/runtime/buffer/BytesReader.ts +200 -200
- package/runtime/buffer/BytesWriter.ts +346 -346
- package/runtime/contracts/OP_20.ts +341 -341
- package/runtime/contracts/OP_NET.ts +73 -73
- package/runtime/contracts/interfaces/IOP_20.ts +21 -21
- package/runtime/env/BTCEnvironment.ts +4 -4
- package/runtime/env/index.ts +3 -3
- package/runtime/events/NetEvent.ts +27 -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 +1 -0
- 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 +46 -46
- package/runtime/math/rnd.ts +51 -51
- 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 -0
- package/runtime/shared-libraries/TransferHelper.ts +64 -64
- package/runtime/storage/Serializable.ts +6 -2
- package/runtime/storage/StoredString.ts +145 -145
- package/runtime/storage/StoredU256.ts +246 -246
- package/runtime/types/Address.ts +5 -5
- package/runtime/types/Revert.ts +5 -5
- package/runtime/types/SafeMath.ts +197 -197
- package/runtime/types/index.ts +8 -8
- package/runtime/universal/ABIRegistry.ts +72 -72
package/runtime/generic/Map.ts
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
import { Revert } from '../types/Revert';
|
|
2
|
-
|
|
3
|
-
export class Map<K, V> {
|
|
4
|
-
protected _keys: K[] = [];
|
|
5
|
-
protected _values: V[] = [];
|
|
6
|
-
|
|
7
|
-
public get size(): i32 {
|
|
8
|
-
return this._keys.length;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
public keys(): K[] {
|
|
12
|
-
return this._keys;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public values(): V[] {
|
|
16
|
-
return this._values;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public set(key: K, value: V): void {
|
|
20
|
-
const index: i32 = this.indexOf(key);
|
|
21
|
-
if (index == -1) {
|
|
22
|
-
this._keys.push(key);
|
|
23
|
-
this._values.push(value);
|
|
24
|
-
} else {
|
|
25
|
-
this._values[index] = value;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public indexOf(key: K): i32 {
|
|
30
|
-
return this._keys.indexOf(key);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public get(key: K): V {
|
|
34
|
-
const index: i32 = this.indexOf(key);
|
|
35
|
-
if (index == -1) {
|
|
36
|
-
throw new Revert('Key not found in map');
|
|
37
|
-
}
|
|
38
|
-
return this._values[index];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public has(key: K): bool {
|
|
42
|
-
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
43
|
-
if (this._keys[i] == key) {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public delete(key: K): bool {
|
|
52
|
-
const index: i32 = this.indexOf(key);
|
|
53
|
-
if (index == -1) {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
this._keys.splice(index, 1);
|
|
57
|
-
this._values.splice(index, 1);
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public clear(): void {
|
|
62
|
-
this._keys = [];
|
|
63
|
-
this._values = [];
|
|
64
|
-
}
|
|
65
|
-
}
|
|
1
|
+
import { Revert } from '../types/Revert';
|
|
2
|
+
|
|
3
|
+
export class Map<K, V> {
|
|
4
|
+
protected _keys: K[] = [];
|
|
5
|
+
protected _values: V[] = [];
|
|
6
|
+
|
|
7
|
+
public get size(): i32 {
|
|
8
|
+
return this._keys.length;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public keys(): K[] {
|
|
12
|
+
return this._keys;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public values(): V[] {
|
|
16
|
+
return this._values;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public set(key: K, value: V): void {
|
|
20
|
+
const index: i32 = this.indexOf(key);
|
|
21
|
+
if (index == -1) {
|
|
22
|
+
this._keys.push(key);
|
|
23
|
+
this._values.push(value);
|
|
24
|
+
} else {
|
|
25
|
+
this._values[index] = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public indexOf(key: K): i32 {
|
|
30
|
+
return this._keys.indexOf(key);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public get(key: K): V {
|
|
34
|
+
const index: i32 = this.indexOf(key);
|
|
35
|
+
if (index == -1) {
|
|
36
|
+
throw new Revert('Key not found in map');
|
|
37
|
+
}
|
|
38
|
+
return this._values[index];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public has(key: K): bool {
|
|
42
|
+
for (let i: i32 = 0; i < this._keys.length; i++) {
|
|
43
|
+
if (this._keys[i] == key) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public delete(key: K): bool {
|
|
52
|
+
const index: i32 = this.indexOf(key);
|
|
53
|
+
if (index == -1) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
this._keys.splice(index, 1);
|
|
57
|
+
this._values.splice(index, 1);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public clear(): void {
|
|
62
|
+
this._keys = [];
|
|
63
|
+
this._values = [];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -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,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,46 +1,46 @@
|
|
|
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
|
-
let 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
|
-
let 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
|
-
return (2097152 * (h2 & 0xffffffffffffffff) + (h1 >> 11)) & 0xffffffffffffffff;
|
|
46
|
-
}
|
|
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
|
+
let 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
|
+
let 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
|
+
return (2097152 * (h2 & 0xffffffffffffffff) + (h1 >> 11)) & 0xffffffffffffffff;
|
|
46
|
+
}
|
package/runtime/math/rnd.ts
CHANGED
|
@@ -1,51 +1,51 @@
|
|
|
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
|
-
h *= 0xff51afd7ed558ccd;
|
|
11
|
-
h ^= h >> 33;
|
|
12
|
-
h *= 0xc4ceb9fe1a85ec53;
|
|
13
|
-
h ^= h >> 33;
|
|
14
|
-
return h;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function splitMix32(h: u32): u32 {
|
|
18
|
-
h += 0x6d2b79f5;
|
|
19
|
-
h = (h ^ (h >> 15)) * (h | 1);
|
|
20
|
-
h ^= h + (h ^ (h >> 7)) * (h | 61);
|
|
21
|
-
return h ^ (h >> 14);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function seedRandom(value: i64): void {
|
|
25
|
-
// Instead zero seed use golden ratio:
|
|
26
|
-
// phi = (1 + sqrt(5)) / 2
|
|
27
|
-
// trunc(2^64 / phi) = 0x9e3779b97f4a7c15
|
|
28
|
-
if (value == 0) value = 0x9e3779b97f4a7c15;
|
|
29
|
-
random_state0_64 = murmurHash3(value);
|
|
30
|
-
random_state1_64 = murmurHash3(~random_state0_64);
|
|
31
|
-
random_state0_32 = splitMix32(<u32>value);
|
|
32
|
-
random_state1_32 = splitMix32(random_state0_32);
|
|
33
|
-
random_seeded = true;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Safe deterministic random number generator
|
|
38
|
-
* @param {u64} seed - The seed to use
|
|
39
|
-
*/
|
|
40
|
-
export function randomU64(seed: i64): u64 {
|
|
41
|
-
if (!random_seeded) seedRandom(seed);
|
|
42
|
-
let s1 = random_state0_64;
|
|
43
|
-
let s0 = random_state1_64;
|
|
44
|
-
random_state0_64 = s0;
|
|
45
|
-
s1 ^= s1 << 23;
|
|
46
|
-
s1 ^= s1 >> 17;
|
|
47
|
-
s1 ^= s0;
|
|
48
|
-
s1 ^= s0 >> 26;
|
|
49
|
-
random_state1_64 = s1;
|
|
50
|
-
return s0;
|
|
51
|
-
}
|
|
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
|
+
h *= 0xff51afd7ed558ccd;
|
|
11
|
+
h ^= h >> 33;
|
|
12
|
+
h *= 0xc4ceb9fe1a85ec53;
|
|
13
|
+
h ^= h >> 33;
|
|
14
|
+
return h;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function splitMix32(h: u32): u32 {
|
|
18
|
+
h += 0x6d2b79f5;
|
|
19
|
+
h = (h ^ (h >> 15)) * (h | 1);
|
|
20
|
+
h ^= h + (h ^ (h >> 7)) * (h | 61);
|
|
21
|
+
return h ^ (h >> 14);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function seedRandom(value: i64): void {
|
|
25
|
+
// Instead zero seed use golden ratio:
|
|
26
|
+
// phi = (1 + sqrt(5)) / 2
|
|
27
|
+
// trunc(2^64 / phi) = 0x9e3779b97f4a7c15
|
|
28
|
+
if (value == 0) value = 0x9e3779b97f4a7c15;
|
|
29
|
+
random_state0_64 = murmurHash3(value);
|
|
30
|
+
random_state1_64 = murmurHash3(~random_state0_64);
|
|
31
|
+
random_state0_32 = splitMix32(<u32>value);
|
|
32
|
+
random_state1_32 = splitMix32(random_state0_32);
|
|
33
|
+
random_seeded = true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Safe deterministic random number generator
|
|
38
|
+
* @param {u64} seed - The seed to use
|
|
39
|
+
*/
|
|
40
|
+
export function randomU64(seed: i64): u64 {
|
|
41
|
+
if (!random_seeded) seedRandom(seed);
|
|
42
|
+
let s1 = random_state0_64;
|
|
43
|
+
let s0 = random_state1_64;
|
|
44
|
+
random_state0_64 = s0;
|
|
45
|
+
s1 ^= s1 << 23;
|
|
46
|
+
s1 ^= s1 >> 17;
|
|
47
|
+
s1 ^= s0;
|
|
48
|
+
s1 ^= s0 >> 26;
|
|
49
|
+
random_state1_64 = s1;
|
|
50
|
+
return s0;
|
|
51
|
+
}
|
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
|
+
}
|