Skip to content

Enable more instances of the C Abstract Struct pattern#1025

Merged
msprotz merged 20 commits intomainfrom
protz_abstract_struct
Mar 10, 2025
Merged

Enable more instances of the C Abstract Struct pattern#1025
msprotz merged 20 commits intomainfrom
protz_abstract_struct

Conversation

@msprotz
Copy link
Copy Markdown
Contributor

@msprotz msprotz commented Feb 19, 2025

This is with FStarLang/karamel#542

@msprotz msprotz requested a review from a team as a code owner February 19, 2025 20:23
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 19, 2025

[CI] Important!
The code in dist/gcc-compatible, dist/msvc-compatible, and dist/wasm
is tested on cryspen/hacl-packages.
dist is not automatically re-generated, be sure to do it manually.
(A fresh snapshot can also be downloaded from CI by going to the "artifacts" section of your run.)
Then check the following tests before merging this PR.
Always check the latest run, it corresponds to the most recent version of this branch.
All jobs are expected to be successful.
In some cases manual intervention is needed. Please ping the hacl-packages maintainers.

  • Build, Test, Benchmark: Build on Linux (x86, x64), Windows (x86, x64), MacOS (x64, arm64), s390x, Android (arm64) and test on Linux (x86, x64), Windows (x86, x64), MacOS (x64).
  • Performance Regression Tests: Navigate to the terminal output in “Run benchmarks”. The comparison with the main branch will be at the bottom. The run fails if the performance regresses too much.
  • OCaml bindings: Build & Tests
  • JS bindings: Build & Tests
  • Rust bindings: Build & Tests

Copy link
Copy Markdown
Contributor

@chris-eibl chris-eibl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most probably this is generated, but at least I marked the spots to #ifdef.

}
Hacl_Streaming_MD_state_64;

typedef struct Hacl_Streaming_Blake2_Types_two_vec128_s
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
typedef struct Hacl_Streaming_Blake2_Types_two_vec128_s
#if defined(HACL_CAN_COMPILE_VEC128)
typedef struct Hacl_Streaming_Blake2_Types_two_vec128_s

Lib_IntVector_Intrinsics_vec128 *snd;
}
Hacl_Streaming_Blake2_Types_two_vec128;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif

}
Hacl_Streaming_Blake2_Types_two_vec128;

typedef struct Hacl_Streaming_Blake2_Types_two_vec256_s
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
typedef struct Hacl_Streaming_Blake2_Types_two_vec256_s
#if defined(HACL_CAN_COMPILE_VEC256)
typedef struct Hacl_Streaming_Blake2_Types_two_vec256_s

Lib_IntVector_Intrinsics_vec256 *snd;
}
Hacl_Streaming_Blake2_Types_two_vec256;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif

}
Hacl_Streaming_Blake2_Types_block_state_blake2b_32;

typedef struct Hacl_Streaming_Blake2_Types_block_state_blake2b_256_s
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
typedef struct Hacl_Streaming_Blake2_Types_block_state_blake2b_256_s
#if defined(HACL_CAN_COMPILE_VEC256)
typedef struct Hacl_Streaming_Blake2_Types_block_state_blake2b_256_s

Hacl_Streaming_Blake2_Types_two_vec256 f3;
}
Hacl_Streaming_Blake2_Types_block_state_blake2b_256;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif

}
Hacl_Streaming_Blake2_Types_block_state_blake2s_32;

typedef struct Hacl_Streaming_Blake2_Types_block_state_blake2s_128_s
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
typedef struct Hacl_Streaming_Blake2_Types_block_state_blake2s_128_s
#if defined(HACL_CAN_COMPILE_VEC128)
typedef struct Hacl_Streaming_Blake2_Types_block_state_blake2s_128_s

Hacl_Streaming_Blake2_Types_two_vec128 f3;
}
Hacl_Streaming_Blake2_Types_block_state_blake2s_128;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif

@chris-eibl
Copy link
Copy Markdown
Contributor

I think, libintvector.h can now be moved to internal, since the goal was to hide it.
Are there more candidates to be moved?

@msprotz
Copy link
Copy Markdown
Contributor Author

msprotz commented Mar 3, 2025

Regarding the #ifdef: I think this doesn't work, because I still need these types in scope for Hacl_Streaming_HMAC.c to compile, since its agile type contains references to those types in any case (but with the new libintvector-shim.h that should not be a problem).

Regarding moving libintvector.h to internal: sadly there are other files in HACL* that do not have such a clean abstraction barrier (see e.g. Hacl_Hash_SHA3_Simd256.h) -- that latter file is used directly by other crypto implementations in other repositories, e.g. for SIMD ML-KEM.

I would like to fix that latter point, eventually.

@msprotz
Copy link
Copy Markdown
Contributor Author

msprotz commented Mar 3, 2025

The windows CI error is a legitimate one -- I need to fix up an abstraction.

@msprotz
Copy link
Copy Markdown
Contributor Author

msprotz commented Mar 3, 2025

Ok at this stage I feel confident that this is in the right shape for Python, modulo my comments above. I intend to merge, and fix things up as they arise during the Python integration process.

Thanks @chris-eibl for the feedback.

The Nix CI will come back red until I merge FStarLang/karamel#542

@chris-eibl
Copy link
Copy Markdown
Contributor

Regarding the #ifdef: I think this doesn't work, because I still need these types in scope for Hacl_Streaming_HMAC.c to compile, since its agile type contains references to those types in any case (but with the new libintvector-shim.h that should not be a problem).

If my analysis is correct, this means, that

  • even though in the Python example blake2module.c does no longer need a compiler that supports the intrinsics without compiling for an AVX architecture (since the abstraction now hides the "problematic" intrinsics)
  • the intrinsics are needed for all HACL_*_.c files that include internal/Hacl_Streaming_Types.h, e.g. Hacl_Hash_SHA1.c. This was not the case, when Python last downstreamed (https://github.com/hacl-star/hacl-star/tree/f218923ef2417d963d7efc7951593ae6aef613f7)
  • IOW, a binary that does run on hosts which do not support AVX, can no longer be created with such compilers

Hopefully, I am mistaken?

@msprotz
Copy link
Copy Markdown
Contributor Author

msprotz commented Mar 4, 2025

the intrinsics are needed for all HACL_*_.c files that include internal/Hacl_Streaming_Types.h, e.g. Hacl_Hash_SHA1.c. This was not the case, when Python last downstreamed

ok I did not realize about this side-effect, thanks for pointing it out

in my opinion then, the best option is to split the Hacl_Streaming_Types.h header into three files Hacl_Streaming_Types{,_Simd128,_Simd256} -- and then inclusion is more precise and does not have that unpleasant side effect

what do you think?

thanks again for your detailed review

@chris-eibl
Copy link
Copy Markdown
Contributor

in my opinion then, the best option is to split the Hacl_Streaming_Types.h header into three files Hacl_Streaming_Types{,_Simd128,_Simd256} -- and then inclusion is more precise and does not have that unpleasant side effect

Sounds even better than my #ifdef approach. I just thought that would have been the easiest solution :)

thanks again for your detailed review

Thanks for all your efforts!

@chris-eibl
Copy link
Copy Markdown
Contributor

Just a thought: include files with the same name only in different paths (e.g. Hacl_Streaming_Types.h and internal/Hacl_Streaming_Types.h) can be a root of trouble. Since most of the code is generated, would it be easily possible to e.g. postfix the internal ones with _internal (or whatever deems more appropriate)?

If this introduces churn or is not easily doable in the code generators - just forget about it :)

@msprotz
Copy link
Copy Markdown
Contributor Author

msprotz commented Mar 4, 2025

@chris-eibl just pushed a change to avoid the issue you described with SHA1 -- the SIMD types are now in their own separate files, only included by the corresponding Blake2 modules.

Can you say more about the issue about the filenames? It's changeable, but I'm curious what issues you ran into.

@chris-eibl
Copy link
Copy Markdown
Contributor

Can you say more about the issue about the filenames? It's changeable, but I'm curious what issues you ran into.

Oh sorry to have given that impression. I did not run into problems. Having the same filenames just can be a problem. If it is easy to avoid the same names - that would be just a "nice to have".

@msprotz
Copy link
Copy Markdown
Contributor Author

msprotz commented Mar 4, 2025

Let me merge this first then make a note of the filename issue -- this PR has been open for a while and I'm kind of eager to get this going to see if it would solve Python's issues.

@msprotz
Copy link
Copy Markdown
Contributor Author

msprotz commented Mar 4, 2025

I pushed one last simplification to the file structure that had become un-necessary with my latest changes to better support forward struct decls. Now the types that contains references to libintvector live in their respective Hacl_Hash_Blake2{b256,s128} files and are properly hidden behind the public header.

If you don't mind taking one last look and confirming that this is good for Python, I'll go ahead and merge.

@chris-eibl
Copy link
Copy Markdown
Contributor

I've just had a quick look - sorry, it's late here and I am busy the next days.

AFAICT, when doing some find-in-files, etc, and quickly rushing over the last commits - this looks good to me.

Sorry that I don't have more time right now. Don't feel blocked - but if you're in doubt about merging, maybe you can just start off a PR in Python based on your last commit and during working on it switch over?

@msprotz
Copy link
Copy Markdown
Contributor Author

msprotz commented Mar 7, 2025

I caught some incorrect cross-algorithm dependencies with e.g. Hacl_MAC_Poly1305 including Hacl_Blake2b_32.h, which I eventually fixed with FStarLang/karamel#550

With that, everything is now green and I feel confident about merging. Thanks everyone

@chris-eibl
Copy link
Copy Markdown
Contributor

I did a draft PR based on 809c320 and can confirm that for clang-cl on Windows the issue is fixed 👍

Thank you so much for your efforts. @msprotz Maybe you can do a review there?

@msprotz
Copy link
Copy Markdown
Contributor Author

msprotz commented Mar 7, 2025

for clang-cl on Windows the issue is fixed 👍

terrific news!

of course very happy to do a review there, cheers

Comment thread Makefile
-add-include 'Hacl_P256:"lib_intrinsics.h"' \
-add-include 'Hacl_Bignum:"lib_intrinsics.h"' \
-add-include 'Hacl_Bignum_Base:"lib_intrinsics.h"' \
-add-include 'Hacl_Bignum_Base.h:"lib_intrinsics.h"' \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit fixes the build (currently broken on main) after FStarLang/karamel#542. I can't really comment on the rest, the high-level idea makes sense to me but I'm not too familiar with the structure of the build here.

@msprotz msprotz enabled auto-merge March 10, 2025 21:45
@msprotz msprotz merged commit 322f6d5 into main Mar 10, 2025
@msprotz msprotz deleted the protz_abstract_struct branch March 10, 2025 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants