Skip to content

Commit b4a7a7b

Browse files
committed
Type system
1 parent f10d8a9 commit b4a7a7b

File tree

19 files changed

+439
-710
lines changed

19 files changed

+439
-710
lines changed

docs/definitions/constants.md

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

docs/definitions/functions.md

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

docs/definitions/let-bindings.md

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

docs/getting-started/hello-world.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The simplest example of a program in Rue is the classic hello world example.
99
Write the following program in a file named `hello.rue`:
1010

1111
```rue title="hello.rue"
12-
fun main() -> Bytes {
12+
fn main() -> Bytes {
1313
"Hello, world!"
1414
}
1515
```
@@ -21,7 +21,7 @@ You don't need to use `return` at the end of a function. Everything in Rue must
2121
Now, run the following command to run the file:
2222

2323
```bash
24-
rue build hello.rue --run
24+
rue build hello.rue
2525
```
2626

27-
You should see `"Hello, world!"` printed in the console.
27+
The output should be `(q . "Hello, world!)"`, which when run with CLVM outputs the string on its own.

docs/security.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
slug: /security
3+
---
4+
5+
# Security
6+
7+
## Untrusted Values
8+
9+
While Rue's type system aims to provide the means to comprehensively check the type of values at runtime, the types of values passed into the `main` function are currently assumed to be correct by default.
10+
11+
This means that a program like this is actually incorrect:
12+
13+
```rue
14+
fn main(
15+
parent_coin_id: Bytes32,
16+
puzzle_hash: Bytes32,
17+
amount: Int,
18+
) -> Bytes32 {
19+
sha256(parent_coin_id + puzzle_hash + amount as Bytes)
20+
}
21+
```
22+
23+
Because the length of the untrusted values `parent_coin_id` and `puzzle_hash` are unchecked, they may be manipulated in a way that increases the length of `amount` without affecting the concatenated value.
24+
25+
For example, if `parent_coin_id` was 28 bytes, `puzzle_hash` was shifted 4 bytes to the left, and the value of `amount` grew by 4 bytes, it would suddenly be much larger than it was before without changing the hash.
26+
27+
This is what led to the [CAT1 vulnerability](https://www.chia.net/2022/07/29/cat1-vulnerability-explained-cve-and-cwe/).
28+
29+
The solution is to use a more general type and check the invariants you care about at runtime:
30+
31+
```rue
32+
fn main(
33+
parent_coin_id: Bytes,
34+
puzzle_hash: Bytes,
35+
amount: Int,
36+
) -> Bytes32 {
37+
assert parent_coin_id is Bytes32;
38+
assert puzzle_hash is Bytes32;
39+
40+
sha256(parent_coin_id + puzzle_hash + amount as Bytes)
41+
}
42+
```
43+
44+
Now, if someone tried to play tricks with the lengths, the assertions would fail and the program would raise an error.

docs/type-system/aliases.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
slug: /aliases
3+
---
4+
5+
# Aliases
6+
7+
A type alias allows you to reuse complex types multiple times without having to repeat yourself.
8+
9+
For example:
10+
11+
```rue
12+
type MathOp = fn(a: Int, b: Int) -> Int;
13+
14+
fn adder(a: Int, y: Int) -> Int {
15+
a + b
16+
}
17+
18+
fn get_adder() -> MathOp {
19+
adder
20+
}
21+
```
22+
23+
## Recursion
24+
25+
You can use type aliases for recursive type definitions:
26+
27+
```rue
28+
type List<T> = nil | (T, List<T>);
29+
```
30+
31+
The type system should be equipped to handle recursive types like this, and they are quite useful for modeling complex CLVM values.
32+
33+
:::warning
34+
There is currently nothing preventing you from making a type that _always_ references itself (without any base case or nesting):
35+
36+
```rue
37+
type Value = Value;
38+
```
39+
40+
This type will allow any value to be assigned to it, and some things may unexpectedly fail. A future release will likely forbid such type declarations from being created.
41+
:::

0 commit comments

Comments
 (0)