Skip to content

Commit 7c01404

Browse files
committed
add translation for scrypt language
1 parent 34261d6 commit 7c01404

File tree

7 files changed

+616
-3
lines changed

7 files changed

+616
-3
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,151 @@
11
---
22
sidebar_position: 2
33
---
4+
45
# Basic Syntax
6+
7+
Introduction to the basic grammar and rules of the Scrypt language.
8+
9+
Reference: https://scryptdoc.readthedocs.io/en/latest/
10+
11+
## Constructor
12+
13+
Each contract has only one constructor, which is used to initialize member variables. If no constructor is specified, a
14+
default constructor will be generated to initialize all members.
15+
16+
![img.png](/img/scrypt-constructor.png)
17+
18+
## Require()
19+
20+
The `require` function specifies contract conditions and accepts a boolean parameter. If the parameter is false, the
21+
contract execution will be interrupted, and it will return a failure.
22+
23+
When the parameter is true, the verification will pass.
24+
25+
## Public Functions
26+
27+
Each contract has at least one public function. This function does not return a value (void return). It can be
28+
considered the locking script or the entry point of the entire contract. Only when all the `require` conditions in the
29+
public function are met and end normally, can the contract be unlocked.
30+
31+
In the case of multiple public functions, the contract can be considered to have multiple unlocking methods, each
32+
capable of unlocking the output.
33+
34+
## Basic Data Structures
35+
36+
- Bool: true or false
37+
- Int: Signed integers of any length in decimal or hexadecimal
38+
- Byte: Hexadecimal format starting with b and single quotes, or UTF-8 characters enclosed in double quotes
39+
- Array type: separated by commas, with a fixed length
40+
41+
```c
42+
bool[3] b = [false, false && true || false, true || (1 > 2)];
43+
int[3] c = [72, -4 - 1 - 40, 833 * (99 + 9901) + 8888];
44+
bytes[3] a = [b'ffee', b'11', b'22'];
45+
int[2][3] d = [[11, 12, 13], [21, 22, 23]];
46+
// array dimension can be omitted when declared
47+
int[] e = [1, 4, 2]; // e is of type int[3]
48+
int[][] f = [[11, 12, 13], [21, 22, 23]]; // f is of type int[2][3]
49+
```
50+
51+
Arrays can use the `repeat` function to set all values to the same.
52+
53+
```text
54+
// a == [0, 0, 0]
55+
int[3] a = repeat(0, 3);
56+
```
57+
58+
Arrays can be indexed using indices starting from 0. If out of bounds, the contract will fail.
59+
60+
Two arrays are considered equal if they have the same number of elements and each element is the same.
61+
62+
## Struct Type
63+
64+
A struct is a group of member variables, which can be basic data structures or nested structs.
65+
66+
When using arrays, members can be accessed directly using the dot (.) notation.
67+
68+
Type inference: The `auto` keyword can automatically infer basic data types.
69+
70+
Type alias: You can assign aliases to specific data types.
71+
72+
## Generics
73+
74+
Generics can be defined in libraries and structs. When using generics, the corresponding type must be explicitly
75+
declared.
76+
77+
```c
78+
library HashedMap<K, V> {
79+
// use them as function parameter's type
80+
function set(K k, V v, int idx) {
81+
...
82+
}
83+
}
84+
85+
struct ST<T, P> {
86+
T t;
87+
P p;
88+
}
89+
```
90+
91+
## Domain Types
92+
93+
Domain types are subtypes of basic types, mainly used for types related to Bitcoin transactions. Using domain types can
94+
enhance type safety.
95+
96+
- PublicKey: PubKey(b'0200112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100');
97+
- Sig: Sig(
98+
b'3045022100b71be3f1dc001e0a1ad65ed84e7a5a0bfe48325f2146ca1d677cf15e96e8b80302206d74605e8234eae3d4980fcd7b2fdc1c5b9374f0ce71dea38707fccdbd28cf7e41');
99+
- Ripemd160: Ripemd160(b'0011223344556677889999887766554433221100');
100+
- PubKeyHash: PubKeyHash(b'0011223344556677889999887766554433221100');
101+
- Sha1: Sha1(b'0011223344556677889999887766554433221100');
102+
- Sha256: Sha256(b'00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100');
103+
- SigHashType: SigHashType(b'01'); SigHashType s = SigHash.ALL | SigHash.ANYONECANPAY;
104+
- SigHashPreimage: SigHashPreimage(b'0100000028bcef7e73248aa273db19d73');
105+
- OpCodeType: OpCode.OP_DUP + OpCode.OP_ADD;
106+
- PrivKey: PrivKey(0x00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100);
107+
108+
## Constants
109+
110+
Constants cannot be modified once declared.
111+
112+
```text
113+
contract Test {
114+
const int x;
115+
116+
constructor(int x) {
117+
this.x = x;
118+
}
119+
120+
public function equal(const int y) {
121+
y = 1; // error
122+
const int a = 36;
123+
a = 11; // error
124+
}
125+
}
126+
```
127+
128+
## If Condition
129+
130+
The `if` condition is used to control the program logic flow. Besides bool, int, and bytes can also use `if`. Int 0 and
131+
byte b'', b'00' are considered false.
132+
133+
## Exit Method
134+
135+
The `exit` method ends the function logic early. The parameter can be true or false.
136+
137+
## Code Separator
138+
139+
Three or more asterisks (*) will insert an OP_CODESEPARATOR operation, which affects the signature calculation.
140+
141+
## Access Control
142+
143+
- Default: no keyword required
144+
- Private
145+
- Public: only applies to functions
146+
147+
| Access Level | Same Contract | Other Contract | Externally |
148+
|--------------|---------------|----------------|------------|
149+
| Default | Yes | Yes | No |
150+
| Private | Yes | No | No |
151+
| Public | Yes | Yes | Yes |
Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,62 @@
11
---
22
sidebar_position: 6
33
---
4-
# Compile Time Constant
4+
5+
# Compile Time Constants
6+
7+
An introduction to the concept and usage of Compile Time Constants (CTCs).
8+
9+
Compile Time Constants (CTCs) are values that can be calculated at compile time. There are four types of compile time
10+
constants:
11+
12+
- Literals
13+
- Induction variables
14+
- Static constant properties of a contract initialized with literals
15+
- Static constant function parameters
16+
17+
There are several cases where only compile time constants can be used:
18+
19+
- Loop boundaries
20+
- Array sizes
21+
- Writing to array elements using the indexing operator
22+
- Declaring static constant function parameters, such as `size` in `reverseBytes(bytes b, static const int size)`
23+
and `repeat(T e, static const int size)`
24+
25+
```c
26+
contract CTC {
27+
static const int N = 4;
28+
static const int LOOPCOUNT = 30;
29+
30+
// A is not a CTC because the right-hand side is an expression, not a literal
31+
static const int A = 2 + 1;
32+
// B is not a CTC because it is not static
33+
const int B;
34+
35+
// FC is declared as a CTC in the function parameter
36+
// It can be used within this function, including in parameters and return types declared after it
37+
function incArr(static const int FC, int[FC] x) : int[FC] {
38+
loop(FC): i {
39+
x[i] += i; // Induction variable CTC
40+
}
41+
return x;
42+
}
43+
44+
public function unlock(int y) {
45+
int[N] arr0 = [1, 2, 3, 4];
46+
// Using `N` to initialize the CTC parameter `FC` of the function `incArr`
47+
int[N] arr1 = this.incArr(N, repeat(1, N));
48+
loop(N): i {
49+
require(arr0[i] == arr1[i]);
50+
}
51+
52+
int z = 0;
53+
loop(LOOPCOUNT) {
54+
if (z < y) z += 4;
55+
}
56+
require(y == 1);
57+
}
58+
}
59+
```
60+
61+
Note: Order matters: when declaring function parameters, `const static` is not allowed, but it is allowed when declaring
62+
properties.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,67 @@
11
---
22
sidebar_position: 4
33
---
4+
45
# Functions
6+
7+
How to define and use user-defined functions.
8+
9+
## Private and Public Functions
10+
11+
Users can specify their own functions, but functions are private by default and can only be seen within the contract.
12+
13+
Public functions are specifically used to unlock the contract, and they return `true` by default. If the `require`
14+
conditions are not met, they will terminate and return `false`.
15+
16+
## Static Functions
17+
18+
Static functions can be called directly using the Contract name as a prefix or internally within the contract. Static
19+
functions do not modify any external state.
20+
21+
## `return` Keyword
22+
23+
Since the script does not support the `return` syntax, `return` must be placed at the end of the function. This
24+
restriction cannot be lifted for now, but there are ways to bypass it.
25+
26+
## Recursive Calls
27+
28+
Recursive calls are not permitted.
29+
30+
## Library Functions
31+
32+
Scrypt provides some convenient public library functions that can be called directly.
33+
34+
### Math
35+
36+
- `int abs(int a)`
37+
- `int min(int a, int b)`
38+
- `int max(int a, int b)`
39+
- `bool within(int x, int min, int max)`
40+
41+
### Hashing
42+
43+
- `Ripemd160 ripemd160(bytes b)`
44+
- `Sha1 sha1(bytes b)`
45+
- `Sha256 sha256(bytes b)`
46+
- `Ripemd160 hash160(bytes b)` - `ripemd160(sha256(b))`
47+
- `Sha256 hash256(bytes b)` - `sha256(sha256(b))`
48+
- `Sha256 flattenSha256(T a)`
49+
50+
### Signature Verification
51+
52+
- `bool checkSig(Sig sig, PubKey pk)`
53+
- Returns true if the signature matches the public key. Returns false if the signature is an empty byte array.
54+
Otherwise, the entire contract fails immediately due to the NULLFAIL rule.
55+
- `bool checkMultiSig(Sig[M] sigs, PubKey[N] pks)`
56+
- Returns true if and only if M signatures match M out of N public keys. Returns false if all signatures are an
57+
empty byte array. Otherwise, the entire contract fails immediately.
58+
59+
### Byte Operations
60+
61+
- Convert to and from `int`, using `unpack` or `pack` methods for conversion.
62+
- `bytes num2bin(int num, int size)`
63+
- `len()` - Returns the length of bytes.
64+
- Slicing Operator - `b[start:end]` - inclusive of start, exclusive of end. If `start` is omitted, it defaults to 0.
65+
If `end` is omitted, it defaults to the length.
66+
- Byte concatenation: `bytes b = b'00112233' + b'334455'` results in `b == b'00112233334455'`
67+
- `reverseBytes(bytes b, static const int size)`
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
---
22
sidebar_position: 1
33
---
4-
# Introduction to Scrypt Language
4+
5+
# Introduction to sCrypt Language
6+
7+
Introducing the programming language sCrypt for MVC contracts.
8+
9+
> Note: The latest version of the sCrypt language has adopted TypeScript as its underlying language. Therefore, the
10+
> syntax of the sCrypt language is similar to that of TypeScript. The TypeScript version is currently being integrated
11+
> into MVC, and the knowledge base will be updated according to the development progress of the new SDK.
12+
13+
sCrypt (pronounced "ess crypt") is a high-level smart contract language designed for the Bitcoin Virtual Machine.
14+
Bitcoin supports writing smart contracts using a Forth-like stack-based Script language. However, writing smart
15+
contracts in native Script is both cumbersome and error-prone. As the size and complexity of contracts increase, these
16+
issues become increasingly difficult to manage.
17+
18+
sCrypt aims to simplify the process of writing smart contracts that run on-chain.
19+
20+
It is easy to learn. Syntactically, sCrypt is similar to JavaScript and Solidity, making it more accessible and easier
21+
to use for existing web and smart contract developers.
22+
23+
It is statically typed. Type checking can detect many errors at compile time.

docs/contract/scrypt-language/loop.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,64 @@
11
---
22
sidebar_position: 3
33
---
4-
# Loop
4+
5+
# Loops
6+
7+
Introduction to using loops in sCrypt contracts.
8+
9+
Bitcoin Script does not provide built-in loop functionality. sCrypt implements loops by repeating the loop's internal
10+
data script n times.
11+
12+
```c
13+
loop (maxLoopCount) [: loopIndex]{
14+
loopBody
15+
}
16+
17+
loop (10) {x = x * 2;} // Equivalent to unfolding x = x * 2; ten times
18+
```
19+
20+
Since loop unfolding occurs at compile time, the number of iterations must be known at compile time and must be a
21+
constant.
22+
23+
## Loop Increment
24+
25+
Loop increment refers to the operation of incrementing or decrementing the loop variable within the loop body.
26+
27+
```c
28+
// int[3][4] matrix;
29+
// i & j are induction variables
30+
loop (3) : i {
31+
// i is the outer loop index
32+
loop (4) : j {
33+
// j is the inner loop index
34+
matrix[i][j] = i + j;
35+
}
36+
}
37+
```
38+
39+
## Adding Conditions Inside the Loop
40+
41+
```c
42+
loop (3) {
43+
// place condition here
44+
if (x < 8) {
45+
x = x * 2;
46+
}
47+
}
48+
```
49+
50+
## Exiting the Loop
51+
52+
To exit the loop within the loop body, use the following method:
53+
54+
```c
55+
bool done = false;
56+
loop (3) {
57+
if (!done) {
58+
x = x * 2;
59+
if (x >= 8) {
60+
done = true;
61+
}
62+
}
63+
}
64+
```

0 commit comments

Comments
 (0)