1
- # SATN Binary Format (BSATN)
1
+ # Binary SATN Format (BSATN)
2
2
3
3
The Spacetime Algebraic Type Notation binary (BSATN) format defines
4
4
how Spacetime ` AlgebraicValue ` s and friends are encoded as byte strings.
@@ -29,16 +29,26 @@ To do this, we use inductive definitions, and define the following notation:
29
29
| [ ` AlgebraicValue ` ] ( #algebraicvalue ) | A value of any type. |
30
30
| [ ` SumValue ` ] ( #sumvalue ) | A value of a sum type, i.e. an enum or tagged union. |
31
31
| [ ` ProductValue ` ] ( #productvalue ) | A value of a product type, i.e. a struct or tuple. |
32
- | [ ` BuiltinValue ` ] ( #builtinvalue ) | A value of a builtin type, including numbers, booleans and sequences. |
33
32
34
33
### ` AlgebraicValue `
35
34
36
35
The BSATN encoding of an ` AlgebraicValue ` defers to the encoding of each variant:
37
36
38
37
``` fsharp
39
- bsatn(AlgebraicValue) = bsatn(SumValue) | bsatn(ProductValue) | bsatn(BuiltinValue)
38
+ bsatn(AlgebraicValue)
39
+ = bsatn(SumValue)
40
+ | bsatn(ProductValue)
41
+ | bsatn(ArrayValue)
42
+ | bsatn(String)
43
+ | bsatn(Bool)
44
+ | bsatn(U8) | bsatn(U16) | bsatn(U32) | bsatn(U64) | bsatn(U128) | bsatn(U256)
45
+ | bsatn(I8) | bsatn(I16) | bsatn(I32) | bsatn(I64) | bsatn(I128) | bsatn(I256)
46
+ | bsatn(F32) | bsatn(F64)
40
47
```
41
48
49
+ Algebraic values include sums, products, arrays, strings, and primitives types.
50
+ The primitive types include booleans, unsigned and signed integers up to 256-bits, and floats, both single and double precision.
51
+
42
52
### ` SumValue `
43
53
44
54
An instance of a sum type, i.e. an enum or tagged union.
@@ -60,44 +70,58 @@ bsatn(elems) = bsatn(elem_0) ++ .. ++ bsatn(elem_n)
60
70
61
71
Field names are not encoded.
62
72
63
- ### ` BuiltinValue `
73
+ ### ` ArrayValue `
74
+
75
+ The encoding of an ` ArrayValue ` is:
76
+
77
+ ```
78
+ bsatn(ArrayValue(a))
79
+ = bsatn(len(a) as u32)
80
+ ++ bsatn(normalize(a)_0)
81
+ ++ ..
82
+ ++ bsatn(normalize(a)_n)
83
+ ```
84
+
85
+ where ` normalize(a) ` for ` a: ArrayValue ` converts ` a ` to a list of ` AlgebraicValue ` s.
64
86
65
- An instance of a buil-in type.
66
- Built-in types include booleans, integers, floats, strings and arrays.
67
- The BSATN encoding of ` BuiltinValue ` s defers to the encoding of each variant:
87
+ ### Strings
68
88
89
+ For strings, the encoding is defined as:
69
90
``` fsharp
70
- bsatn(BuiltinValue)
71
- = bsatn(Bool)
72
- | bsatn(U8) | bsatn(U16) | bsatn(U32) | bsatn(U64) | bsatn(U128)
73
- | bsatn(I8) | bsatn(I16) | bsatn(I32) | bsatn(I64) | bsatn(I128)
74
- | bsatn(F32) | bsatn(F64)
75
- | bsatn(String)
76
- | bsatn(Array)
91
+ bsatn(String(s)) = bsatn(len(s) as u32) ++ bsatn(utf8_to_bytes(s))
92
+ ```
93
+ That is, the BSATN encoding is the concatenation of
94
+ - the bsatn of the string's length as a ` u32 ` integer byte
95
+ - the utf8 representation of the string as a byte array
77
96
78
- bsatn(Bool(b)) = bsatn(b as u8)
97
+ ### Primitives
98
+
99
+ For the primitive variants of ` AlgebraicValue ` , the BSATN encodings are: s
100
+
101
+ ``` fsharp
102
+ bsatn(Bool(false)) = [0]
103
+ bsatn(Bool(true)) = [1]
79
104
bsatn(U8(x)) = [x]
80
105
bsatn(U16(x: u16)) = to_little_endian_bytes(x)
81
106
bsatn(U32(x: u32)) = to_little_endian_bytes(x)
82
107
bsatn(U64(x: u64)) = to_little_endian_bytes(x)
83
108
bsatn(U128(x: u128)) = to_little_endian_bytes(x)
109
+ bsatn(U256(x: u256)) = to_little_endian_bytes(x)
84
110
bsatn(I8(x: i8)) = to_little_endian_bytes(x)
85
111
bsatn(I16(x: i16)) = to_little_endian_bytes(x)
86
112
bsatn(I32(x: i32)) = to_little_endian_bytes(x)
87
113
bsatn(I64(x: i64)) = to_little_endian_bytes(x)
88
114
bsatn(I128(x: i128)) = to_little_endian_bytes(x)
115
+ bsatn(I256(x: i256)) = to_little_endian_bytes(x)
89
116
bsatn(F32(x: f32)) = bsatn(f32_to_raw_bits(x)) // lossless conversion
90
117
bsatn(F64(x: f64)) = bsatn(f64_to_raw_bits(x)) // lossless conversion
91
118
bsatn(String(s)) = bsatn(len(s) as u32) ++ bsatn(bytes(s))
92
- bsatn(Array(a)) = bsatn(len(a) as u32)
93
- ++ bsatn(normalize(a)_0) ++ .. ++ bsatn(normalize(a)_n)
94
119
```
95
120
96
121
Where
97
122
98
- - ` f32_to_raw_bits(x) ` is the raw transmute of ` x: f32 ` to ` u32 `
99
- - ` f64_to_raw_bits(x) ` is the raw transmute of ` x: f64 ` to ` u64 `
100
- - ` normalize(a) ` for ` a: ArrayValue ` converts ` a ` to a list of ` AlgebraicValue ` s
123
+ - ` f32_to_raw_bits(x) ` extracts the raw bits of ` x: f32 ` to ` u32 `
124
+ - ` f64_to_raw_bits(x) ` extracts the raw bits of ` x: f64 ` to ` u64 `
101
125
102
126
## Types
103
127
0 commit comments