From 154e202241d8f617d6633cf0f41bdffd4c9cda1b Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 4 Jun 2016 19:16:38 -0400 Subject: [PATCH] added a simple working example --- Main.hs | 11 ++ README | 33 ------ cpptest.cabal | 15 +++ hlibWithC/clib/generalfuncs.c | 9 -- hlibWithC/clib/l.c | 9 -- hlibWithC/clib/tensorTypes.h | 17 --- hlibWithC/hlibWithC.cabal | 22 ---- hlibWithC/src/HLibWithC.hs | 14 --- hlibWithC/tests/Main.hs | 12 --- hlibWithCPP/clib/generalfuncs.cpp | 9 -- hlibWithCPP/clib/l.cpp | 9 -- hlibWithCPP/clib/tensorTypes.h | 15 --- hlibWithCPP/hlibWithCPP.cabal | 22 ---- hlibWithCPP/src/HLibWithCPP.hs | 14 --- hlibWithCPP/tests/Main.hs | 12 --- loltest/csrc/g.cpp | 165 ------------------------------ loltest/csrc/generalfuncs.cpp | 103 ------------------- loltest/csrc/l.cpp | 86 ---------------- loltest/csrc/tensorTypes.h | 152 --------------------------- loltest/lol.cabal | 52 ---------- loltest/src/CTensor.hs | 58 ----------- loltest/tests/Main.hs | 6 -- sandbox-init | 52 ---------- simplelib/SimpleLib.hs | 7 -- simplelib/simplelib.cabal | 20 ---- simplelib/tests/Main.hs | 11 -- src/header.h | 23 +++++ src/main.cpp | 10 ++ src/zq.cpp | 3 + 29 files changed, 62 insertions(+), 909 deletions(-) create mode 100644 Main.hs delete mode 100644 README create mode 100644 cpptest.cabal delete mode 100644 hlibWithC/clib/generalfuncs.c delete mode 100644 hlibWithC/clib/l.c delete mode 100644 hlibWithC/clib/tensorTypes.h delete mode 100644 hlibWithC/hlibWithC.cabal delete mode 100644 hlibWithC/src/HLibWithC.hs delete mode 100644 hlibWithC/tests/Main.hs delete mode 100644 hlibWithCPP/clib/generalfuncs.cpp delete mode 100644 hlibWithCPP/clib/l.cpp delete mode 100644 hlibWithCPP/clib/tensorTypes.h delete mode 100644 hlibWithCPP/hlibWithCPP.cabal delete mode 100644 hlibWithCPP/src/HLibWithCPP.hs delete mode 100644 hlibWithCPP/tests/Main.hs delete mode 100644 loltest/csrc/g.cpp delete mode 100644 loltest/csrc/generalfuncs.cpp delete mode 100644 loltest/csrc/l.cpp delete mode 100644 loltest/csrc/tensorTypes.h delete mode 100644 loltest/lol.cabal delete mode 100644 loltest/src/CTensor.hs delete mode 100644 loltest/tests/Main.hs delete mode 100755 sandbox-init delete mode 100644 simplelib/SimpleLib.hs delete mode 100644 simplelib/simplelib.cabal delete mode 100644 simplelib/tests/Main.hs create mode 100644 src/header.h create mode 100644 src/main.cpp create mode 100644 src/zq.cpp diff --git a/Main.hs b/Main.hs new file mode 100644 index 0000000..a6a026d --- /dev/null +++ b/Main.hs @@ -0,0 +1,11 @@ + +import Data.Int + +main :: IO () +main = do + let x = 123 + q = 7 + helloC x q + putStrLn $ "Hello from Haskell! " ++ (show x) ++ " = " ++ (show $ x `mod` q) ++ " mod " ++ (show q) + +foreign import ccall unsafe "helloC" helloC :: Int64 -> Int64 -> IO () \ No newline at end of file diff --git a/README b/README deleted file mode 100644 index 0fa3044..0000000 --- a/README +++ /dev/null @@ -1,33 +0,0 @@ -Run ./sandbox-init to run all examples and show the problem with 'cabal repl' and lol. - - -hlibWithC has a pure-C library linked to a Haskell library using cabal. -It compiles, builds the test, and the test executes successfully. I can also run 'cabal repl' - -hlibWithCPP is similar to hlibWithC, but the C library is modified slightly to be a C++ library. Linking still occurs with cabal thanks to n.m.'s helpful comment regarding the use of __cplusplus. -It compiles, builds the test, and the test executes successfully. I can also run 'cabal repl' - -simplelib is a pure Haskell library that depends on hlibWithCPP. -It compiles, builds the test, and the test executes successfully. I can also run 'cabal repl' - -loltest *should* be similar to hlibWithCPP: it is a trimmed down version of "lol", with a C++ library that is linked by cabal. -Unfortunately, 'cabal repl' fails with - -ghc: panic! (the 'impossible' happened) - (GHC version 7.10.3 for x86_64-unknown-linux): - Loading temp shared object failed: /tmp/ghc113133_0/libghc_1.so: undefined symbol: _ZN2Zq1qE - - -I'm not quite ready to report this as a GHC bug (despite the panic), because GHC also panics when a file is not found. Suspiciously, the last bit of 'cabal repl -v3' is: - -*** Deleting temp files: -Deleting: /tmp/ghc113237_0/ghc_2.rsp /tmp/ghc113237_0/libghc_1.so -*** Deleting temp dirs: -Deleting: /tmp/ghc113237_0 -ghc: panic! (the 'impossible' happened) - (GHC version 7.10.3 for x86_64-unknown-linux): - Loading temp shared object failed: /tmp/ghc113237_0/libghc_1.so: undefined symbol: _ZN2Zq1qE - - -so cabal deletes the directory "/tmp/ghc113237_0" -and then complains that it failed to load an object file in that directory... \ No newline at end of file diff --git a/cpptest.cabal b/cpptest.cabal new file mode 100644 index 0000000..a47a0e3 --- /dev/null +++ b/cpptest.cabal @@ -0,0 +1,15 @@ +name: cpptest +version: 0.3.0.0 +build-type: Simple +cabal-version: >= 1.10 + +-- For information on compiling C with cabal: http://blog.ezyang.com/2010/06/setting-up-cabal-the-ffi-and-c2hs/ + +executable cpptest + default-language: Haskell2010 + Include-dirs: src + C-sources: src/zq.cpp, src/main.cpp + main-is: Main.hs + extra-libraries: stdc++ + + build-depends: base diff --git a/hlibWithC/clib/generalfuncs.c b/hlibWithC/clib/generalfuncs.c deleted file mode 100644 index b09a770..0000000 --- a/hlibWithC/clib/generalfuncs.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "tensorTypes.h" - -hInt_t add1Int(hInt_t x) { - return x+1; -} - -double add1Double(double x) { - return x+1; -} \ No newline at end of file diff --git a/hlibWithC/clib/l.c b/hlibWithC/clib/l.c deleted file mode 100644 index 020e048..0000000 --- a/hlibWithC/clib/l.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "tensorTypes.h" - -hInt_t addCInt(hInt_t x) { - return add1Int(x); -} - -double addCDouble(double x) { - return add1Double(x); -} \ No newline at end of file diff --git a/hlibWithC/clib/tensorTypes.h b/hlibWithC/clib/tensorTypes.h deleted file mode 100644 index 7e7ca53..0000000 --- a/hlibWithC/clib/tensorTypes.h +++ /dev/null @@ -1,17 +0,0 @@ - -#ifndef TENSORTYPES_H_ -#define TENSORTYPES_H_ - -#include -#include -#include -#include -#include -#include - -typedef int64_t hInt_t ; - -hInt_t add1Int(hInt_t x); -double add1Double(double x); - -#endif /* TENSORTYPES_H_ */ diff --git a/hlibWithC/hlibWithC.cabal b/hlibWithC/hlibWithC.cabal deleted file mode 100644 index 4576f65..0000000 --- a/hlibWithC/hlibWithC.cabal +++ /dev/null @@ -1,22 +0,0 @@ -name: hlibWithC -version: 0.0.0.1 -synopsis: A test for C++ linking. -build-type: Simple -cabal-version: >= 1.10 - -library - default-language: Haskell2010 - hs-source-dirs: src - Include-dirs: clib - C-sources: clib/l.c, clib/generalfuncs.c - Includes: clib/tensorTypes.h - - exposed-modules: HLibWithC - build-depends: base - -test-suite test - type: exitcode-stdio-1.0 - hs-source-dirs: tests - default-language: Haskell2010 - main-is: Main.hs - build-depends: base, hlibWithC diff --git a/hlibWithC/src/HLibWithC.hs b/hlibWithC/src/HLibWithC.hs deleted file mode 100644 index 5b45257..0000000 --- a/hlibWithC/src/HLibWithC.hs +++ /dev/null @@ -1,14 +0,0 @@ - -module HLibWithC -( addHInt, addHDouble ) where - -import Data.Int - -addHInt :: Int64 -> IO Int64 -addHInt = addCInt - -addHDouble :: Double -> IO Double -addHDouble = addCDouble - -foreign import ccall unsafe "addCInt" addCInt :: Int64 -> IO Int64 -foreign import ccall unsafe "addCDouble" addCDouble :: Double -> IO Double diff --git a/hlibWithC/tests/Main.hs b/hlibWithC/tests/Main.hs deleted file mode 100644 index dc37a49..0000000 --- a/hlibWithC/tests/Main.hs +++ /dev/null @@ -1,12 +0,0 @@ - -import HLibWithC - -main :: IO () -main = do - let x = 1 - y = 2.0 - x' <- addHInt x - y' <- addHDouble y - if (x' == x+1) && (y' == y+1.0) - then print "SUCCESS" - else print "FAIL" \ No newline at end of file diff --git a/hlibWithCPP/clib/generalfuncs.cpp b/hlibWithCPP/clib/generalfuncs.cpp deleted file mode 100644 index 6690b8e..0000000 --- a/hlibWithCPP/clib/generalfuncs.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "tensorTypes.h" - -template T addCAny (T y) -{ - return y + 1; -} - -template hInt_t addCAny (hInt_t y); -template double addCAny (double y); diff --git a/hlibWithCPP/clib/l.cpp b/hlibWithCPP/clib/l.cpp deleted file mode 100644 index 1f10377..0000000 --- a/hlibWithCPP/clib/l.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "tensorTypes.h" - -extern "C" hInt_t addCInt(hInt_t x) { - return addCAny(x); -} - -extern "C" double addCDouble(double x) { - return addCAny(x); -} diff --git a/hlibWithCPP/clib/tensorTypes.h b/hlibWithCPP/clib/tensorTypes.h deleted file mode 100644 index 1cf07b8..0000000 --- a/hlibWithCPP/clib/tensorTypes.h +++ /dev/null @@ -1,15 +0,0 @@ - -#ifndef TENSORTYPES_H_ -#define TENSORTYPES_H_ - -#include -#include -#include - -typedef int64_t hInt_t ; - -#ifdef __cplusplus -template T addCAny (T y); -#endif - -#endif /* TENSORTYPES_H_ */ diff --git a/hlibWithCPP/hlibWithCPP.cabal b/hlibWithCPP/hlibWithCPP.cabal deleted file mode 100644 index 3cc8c9a..0000000 --- a/hlibWithCPP/hlibWithCPP.cabal +++ /dev/null @@ -1,22 +0,0 @@ -name: hlibWithCPP -version: 0.0.0.1 -synopsis: A test for C++ linking. -build-type: Simple -cabal-version: >= 1.10 - -library - default-language: Haskell2010 - hs-source-dirs: src - Include-dirs: clib - C-sources: clib/l.cpp, clib/generalfuncs.cpp - Includes: clib/tensorTypes.h - - exposed-modules: HLibWithCPP - build-depends: base - -test-suite test - type: exitcode-stdio-1.0 - hs-source-dirs: tests - default-language: Haskell2010 - main-is: Main.hs - build-depends: base, hlibWithCPP diff --git a/hlibWithCPP/src/HLibWithCPP.hs b/hlibWithCPP/src/HLibWithCPP.hs deleted file mode 100644 index 3edabd5..0000000 --- a/hlibWithCPP/src/HLibWithCPP.hs +++ /dev/null @@ -1,14 +0,0 @@ - -module HLibWithCPP -( addHInt, addHDouble ) where - -import Data.Int - -addHInt :: Int64 -> IO Int64 -addHInt = addCInt - -addHDouble :: Double -> IO Double -addHDouble = addCDouble - -foreign import ccall unsafe "addCInt" addCInt :: Int64 -> IO Int64 -foreign import ccall unsafe "addCDouble" addCDouble :: Double -> IO Double diff --git a/hlibWithCPP/tests/Main.hs b/hlibWithCPP/tests/Main.hs deleted file mode 100644 index 11c4209..0000000 --- a/hlibWithCPP/tests/Main.hs +++ /dev/null @@ -1,12 +0,0 @@ - -import HLibWithCPP - -main :: IO () -main = do - let x = 1 - y = 2.0 - x' <- addHInt x - y' <- addHDouble y - if (x' == x+1) && (y' == y+1.0) - then print "SUCCESS" - else print "FAIL" \ No newline at end of file diff --git a/loltest/csrc/g.cpp b/loltest/csrc/g.cpp deleted file mode 100644 index e1e2875..0000000 --- a/loltest/csrc/g.cpp +++ /dev/null @@ -1,165 +0,0 @@ -#include "tensorTypes.h" - -template void gPow (ring* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t p) -{ - if (p == 2) {return;} - hDim_t tmp1 = rts*(p-1); - hDim_t tmp2 = tmp1 - rts; - hDim_t blockOffset, modOffset; - hDim_t i; - for (blockOffset = 0; blockOffset < lts; ++blockOffset) { - hDim_t tmp3 = blockOffset * tmp1; - for (modOffset = 0; modOffset < rts; ++modOffset) { - hDim_t tensorOffset = tmp3 + modOffset; - ring last = y[(tensorOffset + tmp2)*tupSize]; - for (i = p-2; i != 0; --i) { - hDim_t idx = tensorOffset + i * rts; - y[idx*tupSize] += (last - y[(idx-rts)*tupSize]); - } - y[tensorOffset*tupSize] += last; - } - } -} - -template void gDec (ring* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t p) -{ - if (p == 2) {return;} - hDim_t tmp1 = rts*(p-1); - hDim_t blockOffset; - hDim_t modOffset; - hDim_t i; - - for (blockOffset = 0; blockOffset < lts; ++blockOffset) { - hDim_t tmp2 = blockOffset * tmp1; - for (modOffset = 0; modOffset < rts; ++modOffset) { - hDim_t tensorOffset = tmp2 + modOffset; - ring acc = y[tensorOffset*tupSize]; - for (i = p-2; i != 0; --i) { - hDim_t idx = tensorOffset + i * rts; - acc += y[idx*tupSize]; - y[idx*tupSize] -= y[(idx-rts)*tupSize]; - } - y[tensorOffset*tupSize] += acc; - } - } -} - -template void gInvPow (ring* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t p) -{ - if (p == 2) {return;} - hDim_t tmp1 = rts * (p-1); - hDim_t blockOffset, modOffset; - hDim_t i; - - for (blockOffset = 0; blockOffset < lts; ++blockOffset) { - hDim_t tmp2 = blockOffset * tmp1; - for (modOffset = 0; modOffset < rts; ++modOffset) { - hDim_t tensorOffset = tmp2 + modOffset; - ring lelts; - lelts = 0; - for (i = 0; i < p-1; ++i) { - lelts += y[(tensorOffset + i*rts)*tupSize]; - } - ring relts; - relts = 0; - for (i = p-2; i >= 0; --i) { - hDim_t idx = tensorOffset + i*rts; - ring z = y[idx*tupSize]; - ring lmul, rmul; - lmul = p-1-i; - rmul = i+1; - y[idx*tupSize] = lmul * lelts - rmul * relts; - lelts -= z; - relts += z; - } - } - } -} - -template void gInvDec (ring* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t p) -{ - if (p == 2) {return;} - hDim_t blockOffset; - hDim_t modOffset; - hDim_t i; - hDim_t tmp1 = rts*(p-1); - - for (blockOffset = 0; blockOffset < lts; ++blockOffset) { - hDim_t tmp2 = blockOffset*tmp1; - for (modOffset = 0; modOffset < rts; ++modOffset) { - hDim_t tensorOffset = tmp2 + modOffset; - ring lastOut; - lastOut = 0; - for (i=1; i < p; ++i) { - ring ri; - ri = i; - lastOut += (ri * y[(tensorOffset + (i-1)*rts)*tupSize]); - } - ring rp; - rp = p; - ring acc = lastOut / rp; - ASSERT ((acc * rp) == lastOut); // this line asserts that lastOut % p == 0, without calling % operator - for (i = p-2; i > 0; --i) { - hDim_t idx = tensorOffset + i*rts; - ring tmp = acc; - acc -= y[idx*tupSize]; // we already divided acc by p, do not multiply y[idx] by p - y[idx*tupSize] = tmp; - } - y[tensorOffset*tupSize] = acc; - } - } -} - -extern "C" void tensorGPowR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, gPow, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorGPowRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) -{ - tensorFuserPrime (y, tupSize, gPow, totm, peArr, sizeOfPE, qs); - canonicalizeZq(y,tupSize,totm,qs); -} - -extern "C" void tensorGPowC (hShort_t tupSize, Complex* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, gPow, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorGDecR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, gDec, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorGDecRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) -{ - tensorFuserPrime (y, tupSize, gDec, totm, peArr, sizeOfPE, qs); - canonicalizeZq(y,tupSize,totm,qs); -} - -extern "C" void tensorGInvPowR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, gInvPow, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorGInvPowRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) -{ - tensorFuserPrime (y, tupSize, gInvPow, totm, peArr, sizeOfPE, qs); - canonicalizeZq(y,tupSize,totm,qs); -} - -extern "C" void tensorGInvPowC (hShort_t tupSize, Complex* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, gInvPow, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorGInvDecR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, gInvDec, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorGInvDecRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) -{ - tensorFuserPrime (y, tupSize, gInvDec, totm, peArr, sizeOfPE, qs); - canonicalizeZq(y,tupSize,totm,qs); -} diff --git a/loltest/csrc/generalfuncs.cpp b/loltest/csrc/generalfuncs.cpp deleted file mode 100644 index f629400..0000000 --- a/loltest/csrc/generalfuncs.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "tensorTypes.h" - -//see note in zq.cc -hInt_t Zq::q; - -hDim_t ipow(hDim_t base, hShort_t exp) -{ - hDim_t result = 1; - while (exp) { - if (exp & 1) { - result *= base; - } - exp >>= 1; - base *= base; - } - return result; -} - -// a is the field size. we are looking for reciprocal of b -hInt_t reciprocal (hInt_t a, hInt_t b) -{ - hInt_t fieldSize = a; - - hInt_t y = 1; - hInt_t lasty = 0; - while (b != 0) { - hInt_t quotient = a / b; - hInt_t tmp = a % b; - a = b; - b = tmp; - tmp = y; - y = lasty - quotient*y; - lasty = tmp; - } - ASSERT (a==1); // if this one fails, then b is not invertible mod a - - // this actually returns EITHER the reciprocal OR reciprocal + fieldSize - hInt_t res = lasty + fieldSize; - return res; -} - -void canonicalizeZq (Zq* y, hShort_t tupSize, hDim_t totm, hInt_t* qs) { - for(int tupIdx = 0; tupIdx void tensorFuserPrime (ring* y, hShort_t tupSize, void (*f) (ring* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t p), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) -{ - hDim_t lts = totm; - hDim_t rts = 1; - hShort_t i; - - for (i = 0; i < sizeOfPE; ++i) { - PrimeExponent pe = peArr[i]; - hDim_t ipow_pe = ipow(pe.prime, (pe.exponent-1)); - hDim_t dim = (pe.prime-1) * ipow_pe; // the totient of pe - lts /= dim; - for(int tupIdx = 0; tupIdx < tupSize; tupIdx++) { - if(qs) { - Zq::q = qs[tupIdx]; // global update - } - (*f) (y+tupIdx, tupSize, lts*ipow_pe, rts, pe.prime); - } - rts *= dim; - } -} - -template void tensorFuserPrime (Zq* y, hShort_t tupSize, void (*f) (Zq* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t pe), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs); -template void tensorFuserPrime (hInt_t* y, hShort_t tupSize, void (*f) (hInt_t* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t pe), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs); -template void tensorFuserPrime (Complex* y, hShort_t tupSize, void (*f) (Complex* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t pe), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs); -template void tensorFuserPrime (double* y, hShort_t tupSize, void (*f) (double* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t pe), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs); - -template void tensorFuserCRT (ringy* y, hShort_t tupSize, void (*f) (ringy* y, hShort_t tupSize, hDim_t lts, hDim_t rts, PrimeExponent pe, ringru* ru), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, ringru** ru, hInt_t* qs) -{ - hDim_t lts = totm; - hDim_t rts = 1; - hShort_t i; - - for (i = 0; i < sizeOfPE; ++i) { - PrimeExponent pe = peArr[i]; - hDim_t ipow_pe = ipow(pe.prime, (pe.exponent-1)); - hDim_t dim = (pe.prime-1) * ipow_pe; // the totient of pe - lts /= dim; - for(int tupIdx = 0; tupIdx < tupSize; tupIdx++) { - if(qs) { - Zq::q = qs[tupIdx]; // global update - } - (*f) (y+tupIdx, tupSize, lts, rts, pe, ru[i]+tupIdx); - } - rts *= dim; - } -} -template void tensorFuserCRT (Zq* y, hShort_t tupSize, void (*f) (Zq* y, hShort_t tupSize, hDim_t lts, hDim_t rts, PrimeExponent pe, Zq* ru), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, Zq** ru, hInt_t* qs); -template void tensorFuserCRT (Complex* y, hShort_t tupSize, void (*f) (Complex* y, hShort_t tupSize, hDim_t lts, hDim_t rts, PrimeExponent pe, Complex* ru), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, Complex** ru, hInt_t* qs); -template void tensorFuserCRT (double* y, hShort_t tupSize, void (*f) (double* y, hShort_t tupSize, hDim_t lts, hDim_t rts, PrimeExponent pe, Complex* ru), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, Complex** ru, hInt_t* qs); diff --git a/loltest/csrc/l.cpp b/loltest/csrc/l.cpp deleted file mode 100644 index a7b0c58..0000000 --- a/loltest/csrc/l.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include "tensorTypes.h" - -template void lp (ring* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t p) -{ - hDim_t blockOffset; - hDim_t modOffset; - int i; - - if(p == 2) {return;} - - hDim_t tmp1 = rts*(p-1); - for (blockOffset = 0; blockOffset < lts; ++blockOffset) { - hDim_t tmp2 = blockOffset*tmp1; - for (modOffset = 0; modOffset < rts; ++modOffset) { - hDim_t idx = tmp2 + modOffset + rts; - for (i = 1; i < p-1; ++i) { - y[idx*tupSize] += y[(idx-rts)*tupSize]; - idx += rts; - } - } - } -} - -template void lpInv (ring* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t p) -{ - hDim_t blockOffset; - hDim_t modOffset; - int i; - - if(p == 2) {return;} - - hDim_t tmp1 = rts*(p-1); - for (blockOffset = 0; blockOffset < lts; ++blockOffset) { - hDim_t tmp2 = blockOffset*tmp1; - for (modOffset = 0; modOffset < rts; ++ modOffset) { - hDim_t tensorOffset = tmp2 + modOffset; - hDim_t idx = tensorOffset + (p-2) * rts; - for (i = p-2; i != 0; --i) { - y[idx*tupSize] -= y[(idx-rts)*tupSize] ; - idx -= rts; - } - } - } -} - -extern "C" void tensorLRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) -{ - tensorFuserPrime (y, tupSize, lp, totm, peArr, sizeOfPE, qs); - canonicalizeZq(y,tupSize,totm,qs); -} - -extern "C" void tensorLR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, lp, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorLDouble (hShort_t tupSize, double* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, lp, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorLC (hShort_t tupSize, Complex* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, lp, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorLInvRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) -{ - tensorFuserPrime (y, tupSize, lpInv, totm, peArr, sizeOfPE, qs); - canonicalizeZq(y,tupSize,totm,qs); -} - -extern "C" void tensorLInvR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, lpInv, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorLInvDouble (hShort_t tupSize, double* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, lpInv, totm, peArr, sizeOfPE, (hInt_t*)0); -} - -extern "C" void tensorLInvC (hShort_t tupSize, Complex* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) -{ - tensorFuserPrime (y, tupSize, lpInv, totm, peArr, sizeOfPE, (hInt_t*)0); -} diff --git a/loltest/csrc/tensorTypes.h b/loltest/csrc/tensorTypes.h deleted file mode 100644 index 675a89d..0000000 --- a/loltest/csrc/tensorTypes.h +++ /dev/null @@ -1,152 +0,0 @@ - -#ifndef TENSORTYPES_H_ -#define TENSORTYPES_H_ - -#include -#include -#include -#include -#include -#include - -#define ASSERT(EXP) { \ - if (!(EXP)) { \ - fprintf (stderr, "Assertion in file '%s' line %d : " #EXP " is false\n", __FILE__, __LINE__); \ - exit(-1); \ - } \ -} - -typedef int64_t hInt_t ; -typedef int32_t hDim_t ; -typedef int16_t hShort_t ; -typedef int8_t hByte_t ; - -typedef struct -{ - hDim_t prime; - hShort_t exponent; -} PrimeExponent; - -hInt_t reciprocal (hInt_t a, hInt_t b); - -#ifdef __cplusplus -//http://stackoverflow.com/a/4421719 -class Zq -{ -public: - hInt_t x; - - static hInt_t q; // declared here, defined in generalfuncs.cpp - - Zq& operator=(const hInt_t& c) - { - this->x = c % q; - return *this; - } - Zq& operator+=(const Zq& b) - { - this->x += b.x; - this->x %= q; - return *this; - } - Zq& operator-=(const Zq& b) - { - this->x -= b.x; - this->x %= q; - return *this; - } - Zq& operator*=(const Zq& b) - { - this->x *= b.x; - this->x %= q; - return *this; - } - Zq& operator/=(const Zq& b) - { - Zq binv; - binv = reciprocal(q,b.x); - *this *= binv; - return *this; - } -}; -inline char operator==(Zq a, const Zq& b) -{ - return (a.x == b.x); -} -inline Zq operator+(Zq a, const Zq& b) -{ - a += b; - return a; -} -inline Zq operator-(Zq a, const Zq& b) -{ - a -= b; - return a; -} -inline Zq operator*(Zq a, const Zq& b) -{ - a *= b; - return a; -} -inline Zq operator/(Zq a, const Zq& b) -{ - a /= b; - return a; -} - -void canonicalizeZq (Zq* y, hShort_t tupSize, hDim_t totm, hInt_t* qs); - -class Complex -{ -public: - double real; - double imag; - - Complex& operator=(const hInt_t& c) - { - this->real = c; - this->imag = 0; - return *this; - } - Complex& operator+=(const Complex& b) - { - this->real = this->real+b.real; - this->imag = this->imag+b.imag; - return *this; - } - Complex& operator-=(const Complex& b) - { - this->real = this->real-b.real; - this->imag = this->imag-b.imag; - return *this; - } - Complex& operator*=(const Complex& b) - { - double a = this->real; - this->real = (a*b.real)-(this->imag*b.imag); - this->imag = (a*b.imag)+(this->imag*b.real); - return *this; - } -}; -inline Complex operator+(Complex a, const Complex& b) -{ - a += b; - return a; -} -inline Complex operator-(Complex a, const Complex& b) -{ - a -= b; - return a; -} -inline Complex operator*(Complex a, const Complex& b) -{ - a *= b; - return a; -} -// calculates base ** exp -hDim_t ipow(hDim_t base, hShort_t exp); - -template void tensorFuserPrime (ring* y, hShort_t tupSize, void (*f) (ring* y, hShort_t tupSize, hDim_t lts, hDim_t rts, hDim_t p), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs); -template void tensorFuserCRT (ringy* y, hShort_t tupSize, void (*f) (ringy* y, hShort_t tupSize, hDim_t lts, hDim_t rts, PrimeExponent pe, ringru* ru), hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, ringru** ru, hInt_t* qs); -#endif /* __cplusplus */ -#endif /* TENSORTYPES_H_ */ diff --git a/loltest/lol.cabal b/loltest/lol.cabal deleted file mode 100644 index db55236..0000000 --- a/loltest/lol.cabal +++ /dev/null @@ -1,52 +0,0 @@ -name: lol --- The package version. See the Haskell package versioning policy (PVP) --- for standards guiding when and how versions should be incremented. --- http://www.haskell.org/haskellwiki/Package_versioning_policy --- PVP summary: +-+------- breaking API changes --- | | +----- non-breaking API additions --- | | | +--- code changes with no API change -version: 0.3.0.0 -synopsis: A library for lattice cryptography. -homepage: https://github.com/cpeikert/Lol -Bug-Reports: https://github.com/cpeikert/Lol/issues -author: Eric Crockett , Chris Peikert -maintainer: Eric Crockett -copyright: Eric Crockett, Chris Peikert -category: Crypto -stability: experimental -build-type: Simple -extra-source-files: README - Crypto/Lol/Cyclotomic/Tensor/CTensor/tensorTypes.h, - Crypto/Lol/Cyclotomic/Tensor/CTensor/*.cpp -cabal-version: >= 1.10 -description: - Λ ○ λ (Lol) is a general-purpose library for ring-based lattice cryptography. - For a detailed description of interfaces and functionality, see - . - For example cryptographic applications, see . -source-repository head - type: git - location: https://github.com/cpeikert/Lol - --- For information on compiling C with cabal: http://blog.ezyang.com/2010/06/setting-up-cabal-the-ffi-and-c2hs/ - -library - default-language: Haskell2010 - hs-source-dirs: src - Include-dirs: csrc - Includes: csrc/tensorTypes.h - C-sources: csrc/g.cpp, - csrc/generalfuncs.cpp, - csrc/l.cpp - - exposed-modules: CTensor - - build-depends: base==4.8.* - -test-suite test-lol - type: exitcode-stdio-1.0 - hs-source-dirs: tests - default-language: Haskell2010 - main-is: Main.hs - - build-depends: base, lol diff --git a/loltest/src/CTensor.hs b/loltest/src/CTensor.hs deleted file mode 100644 index 6a10c4b..0000000 --- a/loltest/src/CTensor.hs +++ /dev/null @@ -1,58 +0,0 @@ -{-# LANGUAGE ConstraintKinds, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, TypeFamilies, UndecidableInstances #-} - -module CTensor where - -import Data.Int -import Foreign.Ptr (Ptr, castPtr) -import Foreign.Storable (Storable (..)) -import GHC.Exts - -class (TElt t Double) => Tensor t where - type TElt t r :: Constraint - - scalarPow :: (Num r, TElt t r) => r -> t r - l, lInv :: (Num r, TElt t r) => t r -> t r - -data CT r = CT [r] deriving (Show) - -instance Tensor CT where - type TElt CT r = (Dispatch r) - scalarPow = error "SUCCESS!" - -data CPP = CPP {p' :: !Int32, e' :: !Int16} -instance Storable CPP -instance Show CPP where show (CPP p e) = "(" ++ show p ++ "," ++ show e ++ ")" - -data DoubleD -data Int64D - -type family CTypeOf x where - CTypeOf Double = DoubleD - CTypeOf Int64 = Int64D - --- | Single-argument synonym for @Dispatch'@. -type Dispatch r = (Dispatch' (CTypeOf r) r) - --- | Class to safely match Haskell types with the appropriate C function. -class (repr ~ CTypeOf r) => Dispatch' repr r where - -- | Equivalent to 'Tensor's @l@. - dl :: Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO () - -- | Equivalent to 'Tensor's @lInv@. - dlinv :: Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO () - -instance (CTypeOf r ~ DoubleD) => Dispatch' DoubleD r where - dl pout = - tensorLDouble 1 (castPtr pout) - dlinv pout = - tensorLInvDouble 1 (castPtr pout) - -instance (CTypeOf r ~ Int64D) => Dispatch' Int64D r where - dl pout = - tensorLR 1 (castPtr pout) - dlinv pout = - tensorLInvR 1 (castPtr pout) - -foreign import ccall unsafe "tensorLR" tensorLR :: Int16 -> Ptr Int64 -> Int64 -> Ptr CPP -> Int16 -> IO () -foreign import ccall unsafe "tensorLInvR" tensorLInvR :: Int16 -> Ptr Int64 -> Int64 -> Ptr CPP -> Int16 -> IO () -foreign import ccall unsafe "tensorLDouble" tensorLDouble :: Int16 -> Ptr Double -> Int64 -> Ptr CPP -> Int16 -> IO () -foreign import ccall unsafe "tensorLInvDouble" tensorLInvDouble :: Int16 -> Ptr Double -> Int64 -> Ptr CPP -> Int16 -> IO () \ No newline at end of file diff --git a/loltest/tests/Main.hs b/loltest/tests/Main.hs deleted file mode 100644 index 8f73638..0000000 --- a/loltest/tests/Main.hs +++ /dev/null @@ -1,6 +0,0 @@ - -import CTensor -import Data.Int - -main :: IO () -main = print $ show $ (scalarPow 2 :: CT Int64) \ No newline at end of file diff --git a/sandbox-init b/sandbox-init deleted file mode 100755 index f1e2cfe..0000000 --- a/sandbox-init +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/bash - -# a little cleanup -cd hlibWithC -#cabal clean -#cd ../hlibWithCPP -#cabal clean -#cd ../simplelib -#cabal clean -cd ../loltest -cabal clean -#cd .. -#rm -rf sandbox -# make a sandbox -#mkdir sandbox -#cd sandbox -#cabal sandbox init --sandbox . -#cabal sandbox add-source ../hlibWithC -#cabal sandbox add-source ../hlibWithCPP -#cabal sandbox add-source ../simplelib -#cabal sandbox add-source ../loltest -# build and test hlibWithC -#cd ../hlibWithC -#cabal sandbox init --sandbox ../sandbox -#cabal configure --enable-tests -#cabal build -#cabal test -#cabal repl -# install and test hlibWithCPP -#cd ../hlibWithCPP -#cabal sandbox init --sandbox ../sandbox -#cabal configure --enable-tests -#cabal install -#cabal test -#cabal repl -# build and test simplelib -#cd ../simplelib -#cabal sandbox init --sandbox ../sandbox -#cabal configure --enable-tests -#cabal build -#cabal test -#cabal repl -# build and test loltest -#cd ../loltest -#cabal sandbox init --sandbox ../sandbox -#cabal install dependencies-only -cabal configure --enable-tests -cabal build -cabal test -cabal repl -cd .. -exit \ No newline at end of file diff --git a/simplelib/SimpleLib.hs b/simplelib/SimpleLib.hs deleted file mode 100644 index 4b7da0f..0000000 --- a/simplelib/SimpleLib.hs +++ /dev/null @@ -1,7 +0,0 @@ - -module SimpleLib where - -import HLibWithCPP (addHInt, addHDouble) - -addHInt' = addHInt -addHDouble' = addHDouble \ No newline at end of file diff --git a/simplelib/simplelib.cabal b/simplelib/simplelib.cabal deleted file mode 100644 index 5969620..0000000 --- a/simplelib/simplelib.cabal +++ /dev/null @@ -1,20 +0,0 @@ -name: simplelib -version: 0.0.0.1 -build-type: Simple -cabal-version: >= 1.10 - -library - default-language: Haskell2010 - - exposed-modules: - SimpleLib - - build-depends: base==4.8.*, hlibWithCPP - -test-suite test - type: exitcode-stdio-1.0 - hs-source-dirs: tests - default-language: Haskell2010 - main-is: Main.hs - - build-depends: base, simplelib diff --git a/simplelib/tests/Main.hs b/simplelib/tests/Main.hs deleted file mode 100644 index 481f453..0000000 --- a/simplelib/tests/Main.hs +++ /dev/null @@ -1,11 +0,0 @@ -import SimpleLib - -main :: IO () -main = do - let x = 1 - y = 2.0 - x' <- addHInt' x - y' <- addHDouble' y - if (x' == x+1) && (y' == y+1.0) - then print "SUCCESS" - else print "FAIL" diff --git a/src/header.h b/src/header.h new file mode 100644 index 0000000..fad70fa --- /dev/null +++ b/src/header.h @@ -0,0 +1,23 @@ + +#ifndef TENSORTYPES_H_ +#define TENSORTYPES_H_ + +#include + +typedef int64_t hInt_t ; + +#ifdef __cplusplus +class Zq +{ +public: + hInt_t x; + static hInt_t q; // defined in zq.cpp + + Zq& operator=(const hInt_t& c) + { + this->x = c % q; + return *this; + } +}; +#endif /* __cplusplus */ +#endif /* TENSORTYPES_H_ */ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..886ec34 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,10 @@ +#include "header.h" +#include + +extern "C" void helloC(hInt_t x, hInt_t q) +{ + Zq::q = q; + Zq y; + y = x; + std::cout << "Hello from C world! " << x << " = " << y.x << " mod " << q << "\n"; +} diff --git a/src/zq.cpp b/src/zq.cpp new file mode 100644 index 0000000..691fa4c --- /dev/null +++ b/src/zq.cpp @@ -0,0 +1,3 @@ +#include "header.h" + +hInt_t Zq::q;