diff --git a/rfc/0005 New Scalable Blockchain Protocol.md b/rfc/0005 New Scalable Blockchain Protocol.md index 80016b107b48704144eae73897554edfe2f842fe..2d58554a6dfe437ccbe78bc6388896e97fa0407f 100644 --- a/rfc/0005 New Scalable Blockchain Protocol.md +++ b/rfc/0005 New Scalable Blockchain Protocol.md @@ -288,9 +288,14 @@ Contraints are expressed through a non-Turing-complete functionnal programming l ### Language definition -The language is only composed of functions returning other functions. Some primitive functions are -defined in the language to represent values and allow computations, and any other functions are -combinations of these primitive functions. +The language is only composed of items which can be : + +- a value +- a tuple (a fixed size group of values) +- a function returning an item + +Some primitive items are defined in the language to represent values and allow computations, and +any other items are maid from them. #### Functions @@ -318,6 +323,32 @@ f = f2 1 (f3 2 3) f a = f2 1 (f3 2 a) ``` +#### Tuples + +Tuples allow to store multiple items of differents types together. + +A tuple can be created with + +```txt +token = {1 [5, 12, 3] f1 (f2 b)} +``` + +Each item can be accessed by + +```txt +token@0 -- returns the first item +``` + +This index must be written in place and can't be an item. + +A tuple can be transformed into a simpler one with + +```txt +token@{0 2 3} -- create a new tuple with only these items +``` + +A tuple with only one item is automatically converted to an item of this type. + #### Arrays Arrays can be created as @@ -402,13 +433,12 @@ complex numbers additions or even elliptic curves points addition. f1 = + #Num 2 5 ``` -#### Function signatures +#### Item signatures -Each function has a signature describing what parameter it takes and what it returns. -System functions have fixed signatures, and new defined functions signatures can be infered from -the combinaison of system functions used. +Each item has a signature describing what parameter it takes and what it returns. System items have +fixed signatures, and new defined items signatures can be infered from context. -The value function `5` takes no parameter and represents the value `5`. Its signature is writen as : +The value `5` has a signature of : ```txt 5 :: V @@ -466,6 +496,24 @@ To make it more readable and to express it's an array, we also write it as : [0 1 2] :: [Int] ``` +A tuple signature is written as : + +```txt +token :: {Int [Int] (Int -> Bool ) } +``` + +Aliases can be created to simplify their usage. By writing + +```txt +MyAlias :: {foo:Int bar:(Int -> Bool) baz:Bool} +``` + +any tuple having this signature can be used as `tuple@foo` as if it were `tuple@0`, and +`tuple@{foo baz}` in place of `tuple@{0 2}`. + +> If 2 alisases are written for the same tuple signature and have a name collision, ambiguity can +> be removed by writting `tuple@MyAlias.foo` and `tuple@MyAlias.{foo baz}`. + #### Subscripts It's possible to use subscripts to separate code in multiple smaller pieces. It also allows @@ -519,20 +567,22 @@ Then, each fonction contains on of these opcodes. | Opcode | Name | Description |:-------|:-----|:--------- -| `0b00XXXXXX` | `valXXXXXX` | Use a value of `XXXXXX` (0-63). -| `0b01XXXXXX` | `sys[name]` | Use system function `XXXXXX` (0-63). -| `0b10XXXXXX` | `usrXXXXX` | Use user function `XXXXX` (0-63). -| `0b1100XXXX` | `parXXXX` | Use function parameter `XXXX` (0-15) -| `0b1101XXXX` | `grpXXXX+2` | Create a group of the following `XXXX + 2` items. The first item should be a function call, and the following items its parameters. -| `0b1110000X` | `datX+1` | Use data with its `size` stored in the next `X + 1` following bytes, and the data in the `size` next bytes. If the size is 0 a null-length value will be created. -| `0b1110001X` | `arrX+1` | Create an *array function* with a `size` stored in the next `X + 1` following bytes, then `size` opcodes defining each value. If the size is 0 an empty array will be created. -| `0b11100100` | `anf` | Create in place an anonymous function whose body is the next item. -| `0b111001.1` | | -| `0b1110011.` | | -| `0b111001..` | | *Unused opcodes. Their presence makes the script returns `true` to allow new opcodes additions with soft-forks.* -| `0b1111XXXX` | `anp` | Use annonymous function parameter `XXXX` (0-15) +| `0b000XXXXX` | `valXXXXX` | Use a value of `XXXXX` (0-32). +| `0b001XXXXX` | `sys[name]` | Use system function `XXXXX` (0-31). +| `0b010XXXXX` | `usrXXXXX` | Use user function `XXXXX` (0-31). +| `0b0110XXXX` | `parXXXX` | Use function parameter `XXXX` (0-15) +| `0b0111XXXX` | `anp` | Use annonymous function parameter `XXXX` (0-15) +| `0b1000XXXX` | `grpXXXX+2` | Create a group of the following `XXXX + 2` items. The first item should be a function call, and the following items its parameters. +| `0b1001XXXX` | `tplXXXX+1` | Create a tuple containing the next `XXXX+1` items. +| +| `0b1111101X` | `desX` | Destructure a tuple. Is followed by `X` bytes containing a bitmask of which tuple item to keep, followed by the tuple (item) to destruture. +| `0b1111110X` | `datX+1` | Use data with its `size` stored in the next `X + 1` following bytes, and the data in the `size` next bytes. If the size is 0 a null-length value will be created. +| `0b1111111X` | `arrX+1` | Create an *array function* with a `size` stored in the next `X + 1` following bytes, then `size` opcodes defining each value. If the size is 0 an empty array will be created. +| `0b11111110` | `anf` | Create in place an anonymous function whose body is the next item. | `0b11111111` | `use` | *Can only be used as the only opcode of a function*. Allow to refeer to a subscript by providing its hash stored in the next 32 bytes. When this function is used in the script, the subscript with the provided hash can be provided and its final function is called. If the subscript is not provided, the return value is the opcode following the hash. If this opcode is another `use`, then the subscript is mandatory and its absence prevent the script from running (making it invalid). +> Unused opcodes makes the script returns `true` to allow new opcodes additions with soft-forks. + ### System functions list | Hex | Signature | Description @@ -561,15 +611,10 @@ Then, each fonction contains on of these opcodes. | `??` | `merge :: parts:[[a]] -> [a]` | Merge all parts into one array. | | | | | | **Data fetching** -| `??` | `fetch_ec :: EC` | Returns the execution context of the token at this index. -| `??` | `fetch_aec :: AEC` | Returns the agregated execution context at this index, panic if out of bounds. -| `??` | `fetch_tkn :: Tkn` | Returns the token at this index, panic if out of bounds. -| `??` | `this_ec :: Int` | Returns this token execution context index. -| `??` | `this_aec :: [Int]` | Returns this token agregated execution context indexes (a token can be in multiple AECs). -| `??` | `this_tkn :: Int` | Returns this token index. +| `??` | `context :: Context` | Returns the script context (tokens used, (agregated) execution contexts, etc) | | | | | | **Cryptography** -| `??` | `hash :: HashAlgo -> mes::V -> V` | Returns the hash of `mes` with given algorithm. *An unknown hashing algorithm throw a "true panic", allowing new hashing algorithms via soft-forks.* +| `??` | `hash :: HashAlgo -> mes:V -> Hash` | Returns the hash of `mes` with given algorithm. *An unknown hashing algorithm throw a "true panic", allowing new hashing algorithms via soft-forks.* | `??` | `verify :: mes:V -> sig:Sig -> key:Key -> Bool` | Verify if `sig` is valid for the `message` and public `key`. *If `sig` and `key` "cryptographic system codes" are equal but undefined, the function returns true to allow new cryptographic systems via soft-forks.* > Any undeclared system function makes the script valid without executing it, allowing addition of @@ -737,30 +782,6 @@ data_extract data begin size = if (== size len (data_extract_partial data begin size)) (some (data_extract_partial data begin size)) none - -# Storage -store_cell_size :: Storage -> Int -> Int? -store_cell_size store index = data_extract store (* index 2) 2 - -## Cumulative size (do not call directly, can overflow on data if misused) -store_cell_shift :: Storage -> Int -> Int? -store_cell_shift store index = sum (for [0..index] (|i| unwrap (store_cell_size store i) 0)) - -store_cell :: Storage -> Int -> V? -store_cell store store_size index = ... TODO - -# Token -tkn_hash :: Tkn -> V -tkn_hash tkn = data_extract tkn 0 32 - -tkn_usage :: Tkn -> TknUsage -tkn_usage tkn = data_extract tkn 32 1 - -tkn_class_store_size :: Tkn -> Int -tkn_class_store_size tkn = data_extract tkn 33 2 - -tkn_inst_store_size :: Tkn -> Int -tkn_inst_store_size tkn = data_extract tkn 35 2 ``` ## 6. Applications