haskell-crypto / cryptonite

lowlevel set of cryptographic primitives for haskell
Other
226 stars 139 forks source link

master fails to build on macOS #358

Closed mouse07410 closed 2 years ago

mouse07410 commented 2 years ago

MacOS 11.6.2, Xcode-13.2.1.

Tried with Cabal-3.6.2.0 (GHC-9.0.1 and 9.0.2), and Stack-2.7.3 (GHC-8.10.7).

With Cabal, building tests fails:

$ cabal test
.  .  .
      Blake2b-512
        1:                                   OK
        2:                                   OK
        3:                                   dyld: lazy symbol binding failed: Symbol not found: __cryptonite_blake2s_init
  Referenced from: /Users/ur20980/src/cryptonite/dist-newstyle/build/x86_64-osx/ghc-9.0.1/cryptonite-0.29/build/libHScryptonite-0.29-inplace-ghc9.0.1.dylib
  Expected in: flat namespace

dyld: Symbol not found: __cryptonite_blake2s_init
  Referenced from: /Users/ur20980/src/cryptonite/dist-newstyle/build/x86_64-osx/ghc-9.0.1/cryptonite-0.29/build/libHScryptonite-0.29-inplace-ghc9.0.1.dylib
  Expected in: flat namespace

Test suite test-cryptonite: FAIL
Test suite logged to:
/Users/ur20980/src/cryptonite/dist-newstyle/build/x86_64-osx/ghc-9.0.1/cryptonite-0.29/t/test-cryptonite/test/cryptonite-0.29-test-cryptonite.log
0 of 1 test suites (0 of 1 test cases) passed.
cabal: Tests failed for test:test-cryptonite from cryptonite-0.29.
$ nm /Users/ur20980/src/cryptonite/dist-newstyle/build/x86_64-osx/ghc-9.0.1/cryptonite-0.29/build/libHScryptonite-0.29-inplace-ghc9.0.1.dylib | grep __cryptonite_blake2s_init
U __cryptonite_blake2s_init
U __cryptonite_blake2s_init_param

WIth Stack, linking tests fails:

Linking .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/test-cryptonite/test-cryptonite ...
Undefined symbols for architecture x86_64:
  "__cryptonite_blake2s_final", referenced from:
      _cryptonite_blake2s_finalize in libHScryptonite-0.29-5gTqpPHk8HTCvUfrUbEit5.a(cryptonite_blake2s.o)
  "__cryptonite_blake2s_init", referenced from:
      _cryptonite_blake2s_init in libHScryptonite-0.29-5gTqpPHk8HTCvUfrUbEit5.a(cryptonite_blake2s.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
`clang' failed in phase `Linker'. (Exit code: 1)
Progress 18/19            

--  While building package cryptonite-0.29 (scroll up to its section to see the error) using:
      /Users/ur20980/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_3.2.1.0_ghc-8.10.7 --builddir=.stack-work/dist/x86_64-osx/Cabal-3.2.1.0 build lib:cryptonite test:test-cryptonite --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

Would complete test log help? I'll post it if so.

Update

On newer Intel CPUs with extended instruction set, SSE gets turned on. Blake2 SSE code does not have the proper functions implemented, or doesn't name them properly - I did not dive into this.

Here's a workaround that makes all the tests run correctly even when you explicitly set support_sse to True (I also had to bump GHC resolver/version for Stack, because my environment does not include GHC-8.6.5). Note: I had to replace reference to non-working cbits/blake2/sse/blake2s.c with one to working bits/blake2/ref/blake2s-ref.c.

diff --git a/cryptonite.cabal b/cryptonite.cabal
index 83d100b..8b58753 100644
--- a/cryptonite.cabal
+++ b/cryptonite.cabal
@@ -76,12 +76,12 @@ Flag support_rdrand

 Flag support_pclmuldq
   Description:       Allow compilation with pclmuldq on architecture that supports it
-  Default:           False
+  Default:           True
   Manual:            True

 Flag support_sse
   Description:       Use SSE optimized version of (BLAKE2, ARGON2)
-  Default:           False
+  Default:           True
   Manual:            True

 Flag integer-gmp
@@ -361,7 +361,7 @@ Library
                    , cbits/cryptonite_aes.c

   if arch(x86_64) || flag(support_sse)
-    C-sources:      cbits/blake2/sse/blake2s.c
+    C-sources:      cbits/blake2/ref/blake2s-ref.c
                   , cbits/blake2/sse/blake2sp.c
                   , cbits/blake2/sse/blake2b.c
                   , cbits/blake2/sse/blake2bp.c
diff --git a/stack.yaml b/stack.yaml
index 0353574..56f9a1a 100644
--- a/stack.yaml
+++ b/stack.yaml
@@ -1,3 +1,3 @@
 # ~*~ auto-generated by haskell-ci with config : 4fdddfa41dd039e198b8d125a70471f7dd140fa01001d99126af56fb31429ece ~*~
-{ resolver: lts-14.27, packages: [ '.' ], extra-deps: [], flags: {} }
+{ resolver: lts-18.21, packages: [ '.' ], extra-deps: [], flags: {} }
mouse07410 commented 2 years ago

With workaround shown above, Cabal and Stack builds all pass tests:

$ stack test
.  .  .
All 1548 tests passed (16.48s)

cryptonite> Test suite test-cryptonite passed
Completed 2 action(s).
$
$ cabal test
.  .  .
Building test suite 'test-cryptonite' for cryptonite-0.29..
Linking /Users/ur20980/src/cryptonite/dist-newstyle/build/x86_64-osx/ghc-9.0.2/cryptonite-0.29/t/test-cryptonite/build/test-cryptonite/test-cryptonite ...
Running 1 test suites...
Test suite test-cryptonite: RUNNING...
Test suite test-cryptonite: PASS
Test suite logged to:
/Users/ur20980/src/cryptonite/dist-newstyle/build/x86_64-osx/ghc-9.0.2/cryptonite-0.29/t/test-cryptonite/test/cryptonite-0.29-test-cryptonite.log
1 of 1 test suites (1 of 1 test cases) passed.
iquerejeta commented 2 years ago

I think that the PR linked above should resolve this issues (PR #359 ). If you could check that it works for you as well, that'd be great.

mouse07410 commented 2 years ago

I confirm that the master builds successfully now, and passes the test. Thank you!

P.S. Now - to consider adding Post-Quantum algorithms from NIST PQC. My personal vote is on Lattice-based (Kyber, NTRU, Saber).

mouse07410 commented 2 years ago

Ping... Any chance to see, e.g., Kyber added to cryptonite?