jasmin-lang / jasmin

Language for high-assurance and high-speed cryptography
MIT License
268 stars 55 forks source link

Support for typedef #501

Open swarnpriya opened 1 year ago

swarnpriya commented 1 year ago

This is a feature request to support typedef in Jasmin. This will help the developers to create short aliases of long-type names which can be easily used throughout the program.

An example: Type like reg ptr u32[12] can be written as typedef reg ptr u32[12] MyArrayPtr.

After this type definition, the identifier MyArrayPtr can be used as an abbreviation for the type reg ptr u32[12].

For example: MyArrayPtr ap1.

MrDaiki commented 2 months ago

If I understand correctly, what you would like is a form of type aliasing. There is several possibilities of syntax for it :

alias x = reg ptr u32[12];
typedef x = reg ptr u32[12];
alias reg ptr u32[12] as x;
typedef reg ptr u32[12] as x;
typedef reg ptr u32[12] x;

I personally prefer the first two solutions because of their readability.

In term of semantic, it would be useful to decide whether or not we authorize alias of alias. I personally don't think that would be a good idea because it could ironically make user comprehension of a program harder.

bgregoir commented 2 months ago

I think rust syntax is type x = reg ptr u32[12]; Since we try to be clause to the rust syntax maybe it is better. The other question is: does reg ptr really part of the type. Maybe a first step will be to only allows type definition type x = u32[12]; I think the change in the parser will be much smaller.

vbgl commented 2 months ago

That’s right. Here in a use case, in which a single type is used with different storage attributes:

type Key = u8[33];

fn check(reg ptr Key key) // …

  stack Key key; // …
  check(key);
vbgl commented 2 months ago

Bejamin is right about Rust syntax: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#creating-type-synonyms-with-type-aliases

Can we avoid introducing a new key-word?

MrDaiki commented 2 months ago

I agree that rust syntax is really good. I also agree that storage type should not be part of the alias (at least for the moment). @vbgl presented a good example of alias with multiple storage types.

Another question I would like to ask about the feature is how do we resolve it's semantic. If I understand correctly how jasmin work, for the moment, we can't declare an array with size defined by a variable. For example :

fn test(reg u32[N] x) {
    reg u32 N;
    N=3;
    reg u32[N] x;
}

This code is not valid because N is not a top level param. With type alias, we could however encounter an issue. By extending this example a little :

param int N = 32;
type tab = u32[N];

fn test(reg u32[N] x) {
    reg u32 N;
    N=3;
    reg tab x;
}

We need to make sure that aliases are resolved within the correct scope.

Another thing we need to define is whether or not we authorize alias of alias. For example :

param int N = 32;
param int M = 32;
type vector = u32[N];
type matrix = vector[M];

It can create some loopholes while checking type correctness but the feature is interesting.

Finally, I think it would be preferable to only allow type aliases at top level and not inside functions (at least for the moment).