Skip to content

Code generation API

Warning

In this version of Oraqle, the API is still prone to changes. Paths and names can change between any version.

The easiest way is using:

arithmetic_circuit.generate_code()

Arithmetic instructions

If you want to extend the oraqle compiler, or implement your own code generation, you can use the following instructions to do so.

Abstract instruction

ArithmeticInstruction

Bases: ABC

An abstract arithmetic instruction that computes an operation in an arithmetic circuit using a stack.

__init__(stack_index)

Initialize an instruction that writes it output to the stack at stack_index.

evaluate(stack, inputs) abstractmethod

Executes the instruction on plaintext inputs without using encryption, keeping track of the plaintext values in the stack.

generate_code(stack_initialized, decrypt_outputs) abstractmethod

Generates code for this instruction, keeping track of which places of the stack are already initialized.

InputInstruction

InputInstruction

Bases: ArithmeticInstruction

Writes an input to the stack.

__init__(stack_index, name)

Initialize an InputInstruction that places the input with the given name in the stack at index stack_index.

evaluate(stack, inputs)

generate_code(stack_initialized, _decrypt_outputs)

AdditionInstruction

AdditionInstruction

Bases: ArithmeticInstruction

Reads two elements from the stack, adds them, and writes the result to the stack.

__init__(stack_index, left_stack_index, right_stack_index)

Initialize an instruction that adds the elements at left_stack_index and right_stack_index, placing the result at stack_index.

evaluate(stack, _inputs)

generate_code(stack_initialized, _decrypt_outputs)

MultiplicationInstruction

MultiplicationInstruction

Bases: ArithmeticInstruction

Reads two elements from the stack, multiplies them, and writes the result to the stack.

__init__(stack_index, left_stack_index, right_stack_index)

Initialize an instruction that multiplies the elements at left_stack_index and right_stack_index, placing the result at stack_index.

evaluate(stack, _inputs)

generate_code(stack_initialized, _decrypt_outputs)

ConstantAdditionInstruction

ConstantAdditionInstruction

Bases: ArithmeticInstruction

Reads an element from the stack, adds a constant to it it, and writes the result to the stack.

__init__(stack_index, input_stack_index, constant)

Initialize an instruction that adds constant to the element at input_stack_index, placing the result at stack_index.

evaluate(stack, _inputs)

generate_code(stack_initialized, _decrypt_outputs)

ConstantMultiplicationInstruction

ConstantMultiplicationInstruction

Bases: ArithmeticInstruction

Reads an element from the stack, multiplies it with a constant, and writes the result to the stack.

__init__(stack_index, input_stack_index, constant)

Initialize an instruction that multiplies the element at input_stack_index with constant, placing the result at stack_index.

evaluate(stack, _inputs)

generate_code(stack_initialized, _decrypt_outputs)

OutputInstruction

OutputInstruction

Bases: ArithmeticInstruction

Outputs an element from the stack.

__init__(stack_index)

Initialize an instruction that writes it output to the stack at stack_index.

evaluate(stack, _inputs)

generate_code(stack_initialized, decrypt_outputs)

Generating arithmetic programs

ArithmeticProgram

An ArithmeticProgram represents an ordered set of arithmetic operations that compute an arithmetic circuit.

The easiest way to obtain an ArithmeticProgram of an ArithmeticCircuit is to call ArithmeticCircuit.generate_program().

__init__(instructions, stack_size, gf)

Initialize an ArithmeticProgram from a list of instructions.

The user must specify an upper bound on the stack_size required.

execute(inputs)

Executes the arithmetic program on plaintext inputs without using encryption.

Raises:

  • Exception

    If there were no outputs in this program.

Returns: The first output in this program.

generate_code(decrypt_outputs)

Generates HElib code for this program.

If decrypt_outputs is true, then the generated code will decrypt the outputs at the end of the circuit.

Returns:

  • str

    The generated code as a string.

Generating code for HElib

...