FStarLang / FStarLang/karamel

Compile buffers and structs which lengths are known to more precise types ?

Open
#33 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
OCaml
Stars
519
Forks
73
Avg merge
2h 58m
Merged PRs (30d)
1

Description

What would be a good way to make it so that I can write a type `type state = b:buffer uint32{length b = 16}` and that gets compile to `typedef uint32[16] state` ?

The usecase is the following: I have an algorithm that relies on a state with an "init", an "update" and a "finish" function. To run the algorithm I would call init the initialize the state, and then update a certain number of times, and then finish to cleanup to the state and return some value. This is very typical in cryptography.

In F* what I would do it that `init` would be in the `StackInline` effect, so that one calling the function gets in return a properly formatted, pre-filled state.
However at the C level this function will have been inline and thus will not be available. Which forces me to decouple the allocation of the state from its initialization (because it is stack-based so no mallocs).
Unfortunately I can't do `state st; init(st, key)` because `state` will just have been compiled to a pointer and not an array with a statically defined length. If the state is a complicated structure, this is even more painful because the C user need to allocated each element on the stack and then fill the struct that only contains pointers.

Is there a way to easily solve that ? I still want to complain about the lack of a mechanism to translate more information from the Core AST to the extracted AST (like the length of the buffers).

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue names no files or tests. Start by tracing how Core AST information is translated into the extracted AST and then into C declarations, focusing on buffer lengths, pointer types, and inline init functions. Done means a supported state declaration can preserve its known length in generated C so callers can allocate and initialize it directly.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, ocaml
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.