Skip to content

Commit

Permalink
Merge pull request #6 from sCrypt-Inc/ctc
Browse files Browse the repository at this point in the history
update doc for ctc
  • Loading branch information
xhliu committed Jul 28, 2021
2 parents 7b180fa + e3bc0c8 commit b76e5d9
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions docs/ctc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
=====================
Compile Time Constant
=====================
A compile time constant (CTC) is a value that can be computed at compile time. There are three types of compile time constants.
A compile time constant (CTC) is a value that can be computed at compile time. There are four types of compile time constants.

* literals
* static constants which are initialized with a literal
* static constants which are declared as contract properties and initialized with a literal
* static constants which are declared as function parameters and initialized with another CTC in a function call
* :ref:`induction variables<induction-var-label>`

There are four cases where only compile time constants are allowed.

* loop bound
* array size
* ``size`` in ``reverseBytes(bytes b, int size)``
* ``size`` in ``repeat(T e, int size)``
* ``size`` in ``reverseBytes(bytes b, static const int size)``
* ``size`` in ``repeat(T e, static const int size)``

.. code-block:: solidity
Expand All @@ -25,11 +26,25 @@ There are four cases where only compile time constants are allowed.
// A is not a CTC because the right hand size is an expression, not a literal
static const int A = 2 + 1;
// B is not a CTC because it is not static
const int B = 2;
const int B;
// FC is a CTC declared in function parameters
// it can be used within this function, including parameters after it & return type
function incArr(static const int FC, int[FC] x) : int[FC] {
loop(FC): i {
x[i] += i; // induction variable CTC
}
return x;
}
public function unlock(int y) {
int[N] arr = [1, 2, 3, 4];
int[N] arr0 = [1, 2, 3, 4];
// use `N` to initialize CTC parameter `FC` of function `incArr`
int[N] arr1 = this.incArr(N, repeat(1, N));
loop(N) : i {
require(arr0[i] == arr1[i]);
}
int z = 0;
loop (LOOPCOUNT)
{
Expand Down

0 comments on commit b76e5d9

Please sign in to comment.