Cryptol function definition
Download learning reference materials: https://cryptol.net/documentation.html
Load file
Load and reload files You can use :load (or abbreviation: l) to load programs in Cryptol. The extension of Cryptol programs is usually .cry.
Function definition
-
Save the following code to the test.cry file, and load the file;
:l C:\Users\VikingsWu\Desktop\test.cry
increment : [8] -> [8] increment x = x+1
Test the function in the command line, pay attention to the data bit width :
Main> increment 3 0x04 Main> increment 255 0x00 Main> increment 912 [error] at <interactive>:1:1--1:14: Unsolvable constraint: 8 >= 10 arising from use of literal or demoted expression at <interactive>:1:11--1:14
Parameters can be parenthesized or not, but negative numbers must be added;
increment(3) increment(-2)
-
You can use let instead of load to define some simple functions;
Cryptol> let r = { a = True, b = False} Cryptol> r.a True
-
The use of where:
twoPlusXY : ([8], [8]) -> [8] twoPlusXY (x, y) = 2 + xy where xy = x * y //注意前面一定是有个tab的
Main> twoPlusXY(1,2) //这里一定要有括号,因为是元组类型 0x04
-
The definition of anonymous functions, or λ-expressions;
Cryptol> f 8 where f x = x+1 9 Cryptol> (\x -> x+1) 8 //这个的定义方法和上面的含义一致 9
-
The application of zero in the function;
all : {n, a} (fin n) => (a -> Bit) -> [n]a -> Bit
Cryptol> all eqTen [10, 10, 10, 10] where eqTen x = x == 10 True Cryptol> all eqTen [10, 10, 10, 5] where eqTen x = x == 10 False
-
Recursive function, currently not available;
-
Iteratively generate sequence, which can be used in sequence cipher:
ys = [i] # [ f (x, y) | x <- xs | y <- ys ]
Actually expressed as:
y0= i y1= f(x1, i) y2= f(x2, y1) y3= f(x3, y2) ... yn= f(xn, yn−1)
-
while loop:
-- C-like Pseudo-code! y = i; // running value, start with i idx = 0; // walk through the xs "array" using idx while(idx < length (xs)) { y = f (xs[idx], y); // compute next elt using the previous ++idx; } return y;
-