diff options
author | James Crayne <jim.crayne@gmail.com> | 2019-09-28 13:43:29 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 19:27:53 -0500 |
commit | 11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch) | |
tree | 5716463275c2d3e902889db619908ded2a73971c /dht/src/Crypto/XEd25519 | |
parent | add2c76bced51fde5e9917e7449ef52be70faf87 (diff) |
Factor out some new libraries
word64-map:
Data.Word64Map
network-addr:
Network.Address
tox-crypto:
Crypto.Tox
lifted-concurrent:
Control.Concurrent.Lifted.Instrument
Control.Concurrent.Async.Lifted.Instrument
psq-wrap:
Data.Wrapper.PSQInt
Data.Wrapper.PSQ
minmax-psq:
Data.MinMaxPSQ
tasks:
Control.Concurrent.Tasks
kad:
Network.Kademlia
Network.Kademlia.Bootstrap
Network.Kademlia.Routing
Network.Kademlia.CommonAPI
Network.Kademlia.Persistence
Network.Kademlia.Search
Diffstat (limited to 'dht/src/Crypto/XEd25519')
-rw-r--r-- | dht/src/Crypto/XEd25519/FieldElement.hs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/dht/src/Crypto/XEd25519/FieldElement.hs b/dht/src/Crypto/XEd25519/FieldElement.hs new file mode 100644 index 00000000..7a916107 --- /dev/null +++ b/dht/src/Crypto/XEd25519/FieldElement.hs | |||
@@ -0,0 +1,49 @@ | |||
1 | {-# LANGUAGE DataKinds #-} | ||
2 | {-# LANGUAGE TypeOperators #-} | ||
3 | module Crypto.XEd25519.FieldElement where | ||
4 | |||
5 | import Crypto.Error | ||
6 | import qualified Crypto.PubKey.Curve25519 as X25519 | ||
7 | import qualified Crypto.PubKey.Ed25519 as Ed25519 | ||
8 | import Data.ByteArray as BA (pack,unpack,Bytes) | ||
9 | import Data.Modular | ||
10 | import Data.Word | ||
11 | |||
12 | -- 2^255 - 19 | ||
13 | type P25519 = 57896044618658097711785492504343953926634992332820282019728792003956564819949 | ||
14 | |||
15 | newtype FieldElement = FE (ℤ / P25519) | ||
16 | |||
17 | |||
18 | fe_frombytes :: X25519.PublicKey -> FieldElement | ||
19 | fe_frombytes pub = FE $ toMod $ decodeLittleEndian $ BA.unpack pub | ||
20 | |||
21 | fe_tobytes :: FieldElement -> Ed25519.PublicKey | ||
22 | fe_tobytes (FE x) = throwCryptoError $ Ed25519.publicKey (b :: Bytes) | ||
23 | where | ||
24 | b = BA.pack $ take 32 $ (encodeLittleEndian $ unMod x) ++ repeat 0 | ||
25 | |||
26 | fe_1 :: FieldElement | ||
27 | fe_1 = FE $ toMod 1 | ||
28 | |||
29 | fe_sub :: FieldElement -> FieldElement -> FieldElement | ||
30 | fe_sub (FE x) (FE y) = FE $ x - y | ||
31 | |||
32 | fe_add :: FieldElement -> FieldElement -> FieldElement | ||
33 | fe_add (FE x) (FE y) = FE $ x + y | ||
34 | |||
35 | fe_invert :: FieldElement -> FieldElement | ||
36 | fe_invert (FE x) = FE $ inv x | ||
37 | |||
38 | fe_mul :: FieldElement -> FieldElement -> FieldElement | ||
39 | fe_mul (FE x) (FE y) = FE (x * y) | ||
40 | |||
41 | decodeLittleEndian :: [Word8] -> Integer | ||
42 | decodeLittleEndian [] = 0 | ||
43 | decodeLittleEndian (x:xs) = fromIntegral x + 256 * decodeLittleEndian xs | ||
44 | |||
45 | encodeLittleEndian :: Integer -> [Word8] | ||
46 | encodeLittleEndian 0 = [] | ||
47 | encodeLittleEndian x = let (bs,b) = divMod x 256 | ||
48 | in fromIntegral b : encodeLittleEndian bs | ||
49 | |||