Secure multiparty Computing (MPC) from beginner to proficient: Frutta language

Source: Internet
Author: User
Tags abs arithmetic arithmetic operators bitwise bitwise operators mul

Introduction: In the previous section, "Secure Multiparty Computing (MPC) from getting started to mastering: a simple Tutorial", we have briefly introduced the Frutta language, Frutta is a programming language jugo developed for computational logic, the calculation logic in the MPC is to solve the specific business of the algorithm written. It is a class of C high-level language, support the majority of operators, data types, expression of implementation-300 gate circuit, only one line of code!
1. What is Frutta
Frutta is a specially customized programming language for the generation of algorithmic circuit files for secure multiparty computing. It is a high-level language and a process-oriented programming language. Frutta supports most operators and data types, and is flexible and practical to express. Currently only the matrix element provides an online IDE that can be compiled and run. The IDE can compile the Frutta file into a circuit file that can be executed on the Jugo platform.
2. Program Structure
Before we learn the basic building blocks of the Frutta language, let's take a look at a minimal Frutta program structure, which can be used as a reference in the following chapters.
Frutta language mainly includes the following parts:
Preprocessor directives
Input and output definition
Function
Variable
Statements & Expressions
Comments

    1. #parties 2/ two participants /
    2. #input 1 Int32/ Party 1, can be an underlying type, or it can be an array, struct, etc. complex type /
    3. #input 2 int32/ Party 2 /
    4. #output 1 Int32/ output side /
    5. / main function, complete the addition operation /
    6. function void Main ()
    7. {
    8. OUTPUT1 = input1 + input2;
    9. }

First, the basic grammar
The Frutta program consists of tokens, which can be keywords, identifiers, constants, string values, or a symbol.
Semicolon
In a Frutta program, a semicolon is a statement terminator. In other words, each statement must end with a semicolon. It indicates the end of a logical entity.
Comments
Comments are like help text in the program, and they are ignored by the compiler. They take/start with the characterTermination
You cannot set comments inline in comments, nor can annotations appear in string or character values.
Identifier
Identifiers are names used to identify variables, functions, or any other user-defined project. An identifier begins with a A-Z or a-Z or underscore, followed by 0 or more letters, underscores, and numbers (0-9).
Punctuation characters such as @, $, and% are not allowed in identifiers. Identifiers are case-sensitive. As a result, Manpower and Manpower are two different identifiers. Several valid identifiers are listed below:
Mohd Zara ABC Move_name a_123 myname50 _temp J a23b9 RetVal
Key words
The following table lists the reserved words in Frutta. These reserved words cannot be a constant name, variable name, or other identifier name. For input and output, follow the order, and then write in turn. The number of inputs is consistent with the number of Parties participating in the calculation. For example, there are 3 calculation parties, then please define input 1 xxx, input 2 xxx, input 3 xxx. The output is the same.
else typedef return for void if struct ABS max min
Int8 uint8 Int16 uint16 int32 uint32 Int64 uint64 bool
INPUT1 input2 ... inputn output1 output2 ... outputn
Space
A line that contains only spaces, called a blank line, may have comments, and the compiler ignores it completely.
In Frutta, spaces are used to describe whitespace characters, tab characters, line breaks, and comments. Spaces separate the parts of the statement so that the compiler can recognize where an element in the statement (such as int) ends and where the next element begins. Therefore, in the following statement: Type age;. Here, there must be at least one empty characters (usually a white space character) between type and age so that the compiler can differentiate between them. On the other hand, in the following statement:
Fruit = apples + oranges;
Copy
The space characters between fruit and =, or = and apples, are not required, but for readability, you can add as many spaces as you want.

Second, pre-processor
The preprocessor is not part of the compiler, but it is a separate step in the compilation process. In short, the preprocessor is just a text substitution tool that instructs the compiler to do the preprocessing required before actually compiling.
All preprocessor commands begin with a pound sign (#). It must be the first non-null character, in order to enhance readability, the preprocessor directive should begin with the first column.
In Frutta, two types of pre-compiler directives are supported:
Include
Support includes external files, external files support the following types of external files:
Frutta file
Using the Frutta implementation of the file, the implementation of functions within the current file can be called. Note that only the current file is supported, not the current directory, such as # include "./lib/math.wir" is not supported, only the # include "Math.wir" is supported. A complete example is as follows:

  1. Math.wir
  2. function Int32 Add (Int32 X, Int32 y)
  3. {
  4. return x + y;
  5. }
  6. function Int32 Sub (int32 X, Int32 y)
  7. {
  8. return x-y;
  9. }
  10. function Int32 Mul (Int32 X, Int32 y)
  11. {
  12. return x * y;
  13. }
  14. function Int32 Div (Int32 X, Int32 y)
  15. {
  16. return x/y;
  17. }
  18. typedef struct POINT
  19. {
  20. Int32 x;
  21. Int32 y;
  22. }
  23. / Find the distance between two points /
  24. function Int32 dis (point p1, point p2)
  25. {
  26. Int32 x = Sub (p1.x, p2.x);
  27. Int32 y = Sub (p1.y, P2.Y);
  28. Return Add (Mul (x, X), Mul (y, y));
  29. }
  30. Include.wir
  31. #include "Math.wir"
  32. #parties 2
  33. #input 1 point
  34. #input 2 Point
  35. #output 1 Int32
  36. function void Main ()
  37. {
  38. OUTPUT1 = Dis (INPUT1, input2);
  39. }
    Define
    An identifier in a simple replacement program. Macro parameters are not supported, and # #连接符与 # characters.
  40. #define NAME Expression

Third, the data type
1. Basic types
In the Frutta language, any operation is represented as an integer operation, so the Frutta language has the following types of primitives:

type Number of digits range of Values
bool 8 Values are 0 and 1
int8 8 -128 ~ 127
Uint8 8 0 ~ 255
Int16 16 -32768 ~ 32767
UInt16 16 0 ~ 65535
Int32 32 -2147483648 ~ 2147483647
UInt32 32 0 ~ 4294967295
Int64 64 -9223372036854775808 ~ 9223372036854775807
UInt64 64 0 ~ 18446744073709551615

You can also define custom types with different byte numbers.
1.typedef signed bytelength type;
2.typedef unsigned bytelength utype;

2. Implicit conversion of data types
Frutta is implicitly converted in the following four scenarios:
A. In arithmetic expressions, a low type can be converted to a high type.
B. In an assignment expression, the value of the right-hand expression is automatically converted to the type of the left variable and assigned to him.
C. When an argument is passed in a function call, the system implicitly converts the argument to the type of the formal parameter and assigns the parameter.
D. When a function has a return value, the system implicitly converts the return expression type to the return value type and assigns a value to the calling function.
When different types of data are manipulated, they should first be converted to the same data type and then manipulated, and the conversion rules are converted from low-level to high-level.
The conversion rules are as follows:
Int8->uint8->int16->uint16->int32->uint32->int64->uint64.
Take an example of int8-uint8:

    1. int8 a =-41;
    2. int8 B =-90;
    3. Uint8 C = a + B;
    4. The final result is 125

3. struct (struct)
Frutta supports struct of Class C language to define struct body. Structs allow you to define variables that store different types of data items.
1) Define the structure

    1. typedef struct POINT
    2. {
    3. Int32 x;
    4. Int32 y;
    5. }
    6. typedef struct CIRCLE
    7. {
    8. Point P;
    9. UInt32 radius;
    10. }

The above definitions can use Point,circle to define struct-body variables.
2) access to structure members
The Frutta language supports array data structures, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a series of data, but it is often considered a series of variables of the same type.
1) declaring an array
To declare an array in Frutta, you need to specify the type of the element and the number of elements, as follows: Type arrayname [ArraySize]
Frutta supports multidimensional arrays as follows: Type arrayname [Row][col];
2) initializing an array
In Frutta, you can initialize an array individually, or you can use an initialization statement as follows: type balance[5] = {0, 0, 0, 0, 0};
The number of values between curly braces {} cannot be greater than the number of elements that we specify in square brackets [] When the array is declared.
3) Accessing array elements
Array elements can be accessed through the array name and index. The index of the element is placed inside the square brackets, followed by the array name. For example: type salary = balance[9];
The above statement assigns the value of the 10th element in the array to the salary variable.

Iv. Constants
In Frutta, there are integral constants and bool constants.
BOOL constants: In Frutta, the built-in two bool constants are true and false, and the binary represents 00000001 and 00000000, respectively. Such as:
BOOL flag = True.
Integer constants: Integer constants can be expressed in 10-and 16-binary notation. 16 binary needs to start with 0x. Such as:
UInt32 year = 365.

V. variables
In Frutta, a variable is simply a combination of multiple wire that the program can manipulate. Each variable in the Frutta has a different number of wire, and the operator can be applied to the variable.
The name of a variable can consist of letters, numbers, and underscore characters. It must start with a letter or an underscore. Uppercase and lowercase letters are different.
A variable definition specifies a data type (the data type must be declared inside a type definition) and contains a list of one or more variables of that type, such as:
Type variable_list;
Variables can be initialized at the time of declaration (specifying an initial value). The initializer consists of an equal sign followed by a constant expression, as follows:
Type variable_name = value;

Vi. operators
An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. The Frutta language has rich operators built into it and provides the following types of operators:
1. Arithmetic operators

operator Description
+ Add two operands
- Subtract a second operand from the first operand
* Multiply the two operands
/ Numerator divided by denominator
% Modulo operator, remainder after divisible

2. Relational operators

operator Description
== Check that the values of the two operands are equal, and if they are equal, the condition is true.
!= Check that the values of the two operands are equal, and the condition is true if they are not equal.
> Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true.
< Checks if the value of the left operand is less than the value of the right operand, or if the condition is true.
>= Checks whether the value of the left operand is greater than or equal to the value of the right operand, or if the condition is true.
<= Checks whether the value of the left operand is less than or equal to the value of the right operand, or if the condition is true.

3. Bitwise operators

operator Description
& If both are present in two operands, the binary and operator copies one to the result.
\ If it exists in either operand, the binary OR operator copies one into the result.
^ If it exists in one of the operands but does not simultaneously exist in two operands, the binary XOR operator copies one into the result.
<< Binary left shift operator. The value of the left operand moves the number of digits specified by the right operand to the left.
>> Binary right-shift operator. The value of the left operand moves the right operand to the specified number of digits.

4. Assignment operators

operator Description
= A simple assignment operator that assigns the value of the right operand to the left operand

Seven, operator
1. Take the line by position
Line that supports bitwise-taking variables
Supports var{start:length} to get the length root line of a variable from start, and the return value can be assigned a value
To a variable, you can also directly participate in the calculation.

    1. #parties 2
    2. #input 1 Int32
    3. #input 2 Int32
    4. #output 1 Int32
    5. function void Main ()
    6. {
    7. OUTPUT1 = Input1{0:8} + input2{0:8};//The above number starts with 0 and takes 8 bits to perform the operation
    8. }

Eight, control statements
1. Conditional Judgment Statement
Judging the structure requires the programmer to specify one or more conditions to evaluate or test, and the statement to execute when the condition is true (required) and the statement to be executed when the condition is false (optional).

Statement Description
If statement An If statement consists of a Boolean expression followed by one or more statements.
If...else statements An If statement can follow an optional else statement, and the Else statement executes when the Boolean expression is false.
Nested IF statements You can use another if or else if statement within an IF or else if statement.

2. Circular Judgment Statement
The loop statement allows us to execute a statement or group of statements multiple times, and Frutta only supports the FOR Loop statement, as follows:

    1. for (type i=0; i<max; i++)
    2. {
    3. }

Attention:
Break,continue statements are not supported
End condition I<max must not depend on input value, must be explicitly evaluated
External type declarations are not supported such as for (; i<max; i++) {}
Nine, function
1. Function definition
A function is a set of statements that perform a task together. Each Frutta program has at least one function, the main function main (), and all simple programs can define additional functions.
The functions of Frutta are defined as follows:

    1. function return_type foo (paramtype1 name1, Paramtype2 name2, paramtype3[5] name3)
      1. {
      2. return blah;
      3. }
      4. An example is as follows
      5. function Int32 Add (Int32 X, Int32 y)
      6. {
      7. return x + y;
      8. }
      9. Call
      10. OUTPUT1 = Add (input1, INPUT2);
        All components of a function:
        return type: A function can return a value. Return_type is the data type of the value returned by the function. Some functions perform the required operation without returning a value, in which case the return_type is the keyword void.
        Function Name: This is the actual name of the function. The function name and the parameter list together form the functional signature.
        Parameter: The parameter is like a placeholder. When a function is called, you pass a value to the parameter, which is called the actual parameter. The parameter list includes the type, order, and number of function parameters. Parameters are optional, that is, the function may not contain parameters.
        function Body: A function Body contains a set of statements that define functions to perform tasks.

2. Built-in functions
In Frutta, the following functions are built in:

function function Definition function Description
Abs Intxxx ABS (INTXXX) Absolute value, the input output is a character integer. as follows: Int32 y = ABS (x)
Min (u) intxxx min (u) intxxx, (U) intxxx) Take the minimum value of two numbers, the input can be both a character and an integer, if there are both characters and no characters, the character is automatically converted to no character, the return value is no character. as follows: Int16 min (x, y)
Max (u) intxxx max (U) intxxx, (U) intxxx) Take the maximum value of two numbers, the input can be both a character and an integer, if there are both characters and no characters, the character is automatically converted to no character, the return value is no character. as follows: Int16 max (x, y)

More information can be found in video: Safe multiparty computing MPC Video Course

Secure multiparty Computing (MPC) from beginner to proficient: Frutta language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.