Skip to content

Commit

Permalink
Different approach for dealing with [-Wincompatible-pointer-types]
Browse files Browse the repository at this point in the history
  • Loading branch information
etan-status committed Aug 9, 2024
1 parent 58258a8 commit 57b2af4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 36 deletions.
101 changes: 101 additions & 0 deletions blscurve/blst/blst+nim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright (c) 2024 Status Research & Development GmbH
* Licensed under either of
* * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
* * MIT license ([LICENSE-MIT](LICENSE-MIT))
* at your option.
* This file may not be copied, modified, or distributed except according to
* those terms.
*/

#ifndef BLST_NIM_H
#define BLST_NIM_H

// Nim does not support annotating pointer destinations with C `const`.
//
// This leads to errors on certain platforms and toolchains, e.g.:
// expected 'const blst_p1_affine * const*'
// but argument is of type 'blst_p1_affine **'
// [-Wincompatible-pointer-types]
//
// To prevent these issues, offending function signatures are replaced
// with ones that lack C `const` annotations.


#define blst_p1s_to_affine blst_p1s_to_affine_replaced
#define blst_p1s_add blst_p1s_add_replaced
#define blst_p1s_mult_wbits_precompute blst_p1s_mult_wbits_precompute_replaced
#define blst_p1s_mult_wbits blst_p1s_mult_wbits_replaced
#define blst_p1s_mult_pippenger blst_p1s_mult_pippenger_replaced
#define blst_p1s_tile_pippenger blst_p1s_tile_pippenger_replaced

#define blst_p2s_to_affine blst_p2s_to_affine_replaced
#define blst_p2s_add blst_p2s_add_replaced
#define blst_p2s_mult_wbits_precompute blst_p2s_mult_wbits_precompute_replaced
#define blst_p2s_mult_wbits blst_p2s_mult_wbits_replaced
#define blst_p2s_mult_pippenger blst_p2s_mult_pippenger_replaced
#define blst_p2s_tile_pippenger blst_p2s_tile_pippenger_replaced

#define blst_miller_loop_n blst_miller_loop_n_replaced

#include "../../vendor/blst/bindings/blst.h"

#undef blst_p1s_to_affine
#undef blst_p1s_add
#undef blst_p1s_mult_wbits_precompute
#undef blst_p1s_mult_wbits
#undef blst_p1s_mult_pippenger
#undef blst_p1s_tile_pippenger

#undef blst_p2s_to_affine
#undef blst_p2s_add
#undef blst_p2s_mult_wbits_precompute
#undef blst_p2s_mult_wbits
#undef blst_p2s_mult_pippenger
#undef blst_p2s_tile_pippenger

#undef blst_miller_loop_n

void blst_p1s_to_affine(blst_p1_affine dst[], blst_p1 *points[],
size_t npoints);
void blst_p1s_add(blst_p1 *ret, blst_p1_affine *points[],
size_t npoints);
void blst_p1s_mult_wbits_precompute(blst_p1_affine table[], size_t wbits,
blst_p1_affine *points[],
size_t npoints);
void blst_p1s_mult_wbits(blst_p1 *ret, const blst_p1_affine table[],
size_t wbits, size_t npoints,
byte *scalars[], size_t nbits,
limb_t *scratch);
void blst_p1s_mult_pippenger(blst_p1 *ret, blst_p1_affine *points[],
size_t npoints, byte *scalars[],
size_t nbits, limb_t *scratch);
void blst_p1s_tile_pippenger(blst_p1 *ret, blst_p1_affine *points[],
size_t npoints, byte *scalars[],
size_t nbits, limb_t *scratch,
size_t bit0, size_t window);

void blst_p2s_to_affine(blst_p2_affine dst[], blst_p2 *points[],
size_t npoints);
void blst_p2s_add(blst_p2 *ret, blst_p2_affine *points[],
size_t npoints);
void blst_p2s_mult_wbits_precompute(blst_p2_affine table[], size_t wbits,
blst_p2_affine *points[],
size_t npoints);
void blst_p2s_mult_wbits(blst_p2 *ret, const blst_p2_affine table[],
size_t wbits, size_t npoints,
byte *scalars[], size_t nbits,
limb_t *scratch);
void blst_p2s_mult_pippenger(blst_p2 *ret, blst_p2_affine *points[],
size_t npoints, byte *scalars[],
size_t nbits, limb_t *scratch);
void blst_p2s_tile_pippenger(blst_p2 *ret, blst_p2_affine *points[],
size_t npoints, byte *scalars[],
size_t nbits, limb_t *scratch,
size_t bit0, size_t window);

void blst_miller_loop_n(blst_fp12 *ret, blst_p2_affine *Qs[],
blst_p1_affine *Ps[],
size_t n);

#endif
2 changes: 1 addition & 1 deletion blscurve/blst/blst_abi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Manual edits
import std/[strutils, os]

const headerPath = currentSourcePath.rsplit({DirSep, AltSep}, 1)[0] & "/../../vendor/blst/bindings/blst.h"
const headerPath = currentSourcePath.rsplit({DirSep, AltSep}, 1)[0] & "/blst+nim.h"

{.pragma: blst, importc, header: headerPath.}
{.pragma: blstheader, header: headerPath.}
Expand Down
49 changes: 14 additions & 35 deletions blscurve/blst/blst_min_pubkey_sig_core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -567,21 +567,6 @@ func update*[T: char|byte](
aug = ""
)

template ignoringIncompatiblePointerTypes(body: untyped): untyped =
# Nim does not support annotating pointer destinations with C `const`
{.emit: """
#ifdef __clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#endif
""".}
body
{.emit: """
#ifdef __clang__
#pragma GCC diagnostic pop
#endif
""".}

func combine*(
secureRandomBytes: array[32, byte],
publicKeys: openArray[PublicKey],
Expand Down Expand Up @@ -635,29 +620,23 @@ func combine*(
signature {.noinit.}: Signature
block:
var combinedPublicKey {.noinit.}: AggregatePublicKey
ignoringIncompatiblePointerTypes:
# expected 'const blst_p1_affine * const*'
# but argument is of type 'blst_p1_affine **'
blst_p1s_mult_pippenger(
toCV(combinedPublicKey, cblst_p1),
publicKeysRef[0].unsafeAddr,
numEntries,
scalarsRef[0].unsafeAddr,
nbits = 64,
scratch[0].addr)
blst_p1s_mult_pippenger(
toCV(combinedPublicKey, cblst_p1),
publicKeysRef[0].unsafeAddr,
numEntries,
scalarsRef[0].unsafeAddr,
nbits = 64,
scratch[0].addr)
publicKey.finish combinedPublicKey
block:
var combinedSignature {.noinit.}: AggregateSignature
ignoringIncompatiblePointerTypes:
# expected 'const blst_p2_affine * const*'
# but argument is of type 'blst_p2_affine **'
blst_p2s_mult_pippenger(
toCV(combinedSignature, cblst_p2),
signaturesRef[0].unsafeAddr,
numEntries,
scalarsRef[0].unsafeAddr,
nbits = 64,
scratch[0].addr)
blst_p2s_mult_pippenger(
toCV(combinedSignature, cblst_p2),
signaturesRef[0].unsafeAddr,
numEntries,
scalarsRef[0].unsafeAddr,
nbits = 64,
scratch[0].addr)
signature.finish combinedSignature
(publicKey, signature)

Expand Down

0 comments on commit 57b2af4

Please sign in to comment.