Skip to content

Commit 974bb38

Browse files
committed
Builtins
1 parent 69bb7a1 commit 974bb38

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

docs/builtins.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,62 @@ slug: /builtins
33
---
44

55
# Builtins
6+
7+
There are a number of built-in functions and types. These are intrinsic to the language and are used to implement the standard library, but can and should also be used in your code.
8+
9+
## Types
10+
11+
You can click on each of these types to go to their corresponding page and learn more:
12+
13+
1. [**Atom**](/docs/type-system/atoms.md#atom)
14+
2. [**Bytes**](/docs/type-system/atoms.md#bytes)
15+
3. [**Bytes32**](/docs/type-system/atoms.md#bytes32)
16+
4. [**PublicKey**](/docs/type-system/atoms.md#publickey)
17+
5. [**Int**](/docs/type-system/atoms.md#int)
18+
6. [**Bool**](/docs/type-system/atoms.md#bool)
19+
7. [**Any**](/docs/type-system/pairs.md#any)
20+
8. [**List**](/docs/type-system/pairs.md#lists)
21+
22+
## Functions
23+
24+
### `sha256`
25+
26+
Calculates the [SHA-256](https://en.wikipedia.org/wiki/SHA-2) hash of the bytes provided. This hash is calculated at runtime.
27+
28+
For example:
29+
30+
```rue
31+
let hash = sha256("hello");
32+
```
33+
34+
### `sha256_inline`
35+
36+
The same as `sha256`, except the value will be calculated at compile time and inserted into the program. In some rare scenario, this may be an important optimization to avoid hashing the same large constant value many times. However, in most cases, you should just use the regular `sha256` function.
37+
38+
For example:
39+
40+
```rue
41+
let hash = sha256_inline("hello");
42+
```
43+
44+
### `calculate_coin_id`
45+
46+
A builtin CLVM opcode for hashing the parent coin id, puzzle hash, and amount of a coin into its coin id. See the [Security](/docs/security.md#untrusted-values) section for an explanation of why this opcode exists. Essentially, it adds runtime checks to make sure that the length of the parent coin id and puzzle hash are 32.
47+
48+
For example:
49+
50+
```rue
51+
calculate_coin_id(parent_coin_id, puzzle_hash, amount)
52+
```
53+
54+
### `substr`
55+
56+
Returns a substring of a byte value, given a range of indices.
57+
58+
For example:
59+
60+
```rue
61+
substr("hello", 1, 4)
62+
```
63+
64+
This would return `"ell"`

docs/security.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ fn main(
4141
}
4242
```
4343

44+
Or you can use the builtin `calculate_coin_id` function:
45+
46+
```rue
47+
calculate_coin_id(parent_coin_id, puzzle_hash, amount)
48+
```
49+
4450
Now, if someone tried to play tricks with the lengths, the assertions would fail and the program would raise an error.

docs/standard-library.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

sidebars.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const sidebars: SidebarsConfig = {
2020
},
2121
"operators",
2222
"builtins",
23-
"standard-library",
2423
"security",
2524
],
2625
};

0 commit comments

Comments
 (0)