From 0818d7ed997b443fd3f272e7f2380c2968865d68 Mon Sep 17 00:00:00 2001 From: Pasquale Ambrosini Date: Sat, 26 May 2018 02:58:20 +0200 Subject: [PATCH] Added Entangled PoW (written in C), it is 5-6x faster than Swift --- IotaKit.xcodeproj/project.pbxproj | 154 +++++++++++++++++- .../xcschemes/IotaKit-Package.xcscheme | 2 +- Package.swift | 3 +- Sources/IotaKit/Iota.swift | 2 +- .../IotaTransactionExtension.swift | 4 +- Sources/IotaKit/Pow/CPow.swift | 31 ++++ Sources/cpow/_pow.c | 26 +++ Sources/cpow/curl.c | 109 +++++++++++++ Sources/cpow/curl_p.c | 61 +++++++ Sources/cpow/curl_p_ptrit.c | 74 +++++++++ Sources/cpow/hashcash.c | 23 +++ Sources/cpow/include/_pow.h | 18 ++ Sources/cpow/include/const.h | 12 ++ Sources/cpow/include/constants.h | 13 ++ Sources/cpow/include/curl.h | 18 ++ Sources/cpow/include/hash.h | 27 +++ Sources/cpow/include/hashcash.h | 16 ++ Sources/cpow/include/indices.h | 57 +++++++ Sources/cpow/include/module.modulemap | 4 + Sources/cpow/include/pcurl_ptrit.h | 19 +++ Sources/cpow/include/pearl_diver.h | 11 ++ Sources/cpow/include/pow.h | 18 ++ Sources/cpow/include/ptrit.h | 16 ++ Sources/cpow/include/ptrit_incr.h | 9 + Sources/cpow/include/search.h | 11 ++ Sources/cpow/include/stdint.h | 9 + Sources/cpow/include/trit.h | 19 +++ Sources/cpow/include/trit_ptrit.h | 11 ++ Sources/cpow/include/trit_tryte.h | 11 ++ Sources/cpow/include/trits.h | 8 + Sources/cpow/include/tryte.h | 8 + Sources/cpow/pearl_diver.c | 152 +++++++++++++++++ Sources/cpow/pow.c | 36 ++++ Sources/cpow/ptrit_incr.c | 37 +++++ Sources/cpow/trit_ptrit.c | 36 ++++ Sources/cpow/trit_tryte.c | 49 ++++++ Tests/IotaKitTests/IotaKitTests.swift | 9 +- 37 files changed, 1115 insertions(+), 8 deletions(-) create mode 100644 Sources/IotaKit/Pow/CPow.swift create mode 100755 Sources/cpow/_pow.c create mode 100755 Sources/cpow/curl.c create mode 100755 Sources/cpow/curl_p.c create mode 100755 Sources/cpow/curl_p_ptrit.c create mode 100755 Sources/cpow/hashcash.c create mode 100755 Sources/cpow/include/_pow.h create mode 100755 Sources/cpow/include/const.h create mode 100755 Sources/cpow/include/constants.h create mode 100755 Sources/cpow/include/curl.h create mode 100755 Sources/cpow/include/hash.h create mode 100755 Sources/cpow/include/hashcash.h create mode 100755 Sources/cpow/include/indices.h create mode 100755 Sources/cpow/include/module.modulemap create mode 100755 Sources/cpow/include/pcurl_ptrit.h create mode 100755 Sources/cpow/include/pearl_diver.h create mode 100755 Sources/cpow/include/pow.h create mode 100755 Sources/cpow/include/ptrit.h create mode 100755 Sources/cpow/include/ptrit_incr.h create mode 100755 Sources/cpow/include/search.h create mode 100755 Sources/cpow/include/stdint.h create mode 100755 Sources/cpow/include/trit.h create mode 100755 Sources/cpow/include/trit_ptrit.h create mode 100755 Sources/cpow/include/trit_tryte.h create mode 100755 Sources/cpow/include/trits.h create mode 100755 Sources/cpow/include/tryte.h create mode 100755 Sources/cpow/pearl_diver.c create mode 100755 Sources/cpow/pow.c create mode 100755 Sources/cpow/ptrit_incr.c create mode 100755 Sources/cpow/trit_ptrit.c create mode 100755 Sources/cpow/trit_tryte.c diff --git a/IotaKit.xcodeproj/project.pbxproj b/IotaKit.xcodeproj/project.pbxproj index cf32df8..a5eed38 100644 --- a/IotaKit.xcodeproj/project.pbxproj +++ b/IotaKit.xcodeproj/project.pbxproj @@ -43,6 +43,36 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ + 472A935420B8D11D00BA091C /* CPow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4749509920B8C648005CA71B /* CPow.swift */; }; + 472A935520B8D11E00BA091C /* CPow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4749509920B8C648005CA71B /* CPow.swift */; }; + 472A935620B8D11E00BA091C /* CPow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4749509920B8C648005CA71B /* CPow.swift */; }; + 4749506C20B8B587005CA71B /* hashcash.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749505D20B8B587005CA71B /* hashcash.c */; }; + 4749506D20B8B587005CA71B /* hashcash.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749505D20B8B587005CA71B /* hashcash.c */; }; + 4749506E20B8B587005CA71B /* hashcash.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749505D20B8B587005CA71B /* hashcash.c */; }; + 4749506F20B8B587005CA71B /* pearl_diver.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749506920B8B587005CA71B /* pearl_diver.c */; }; + 4749507020B8B587005CA71B /* pearl_diver.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749506920B8B587005CA71B /* pearl_diver.c */; }; + 4749507120B8B587005CA71B /* pearl_diver.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749506920B8B587005CA71B /* pearl_diver.c */; }; + 4749507220B8B587005CA71B /* _pow.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749506A20B8B587005CA71B /* _pow.c */; }; + 4749507320B8B587005CA71B /* _pow.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749506A20B8B587005CA71B /* _pow.c */; }; + 4749507420B8B587005CA71B /* _pow.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749506A20B8B587005CA71B /* _pow.c */; }; + 4749507520B8B587005CA71B /* trit_tryte.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749506B20B8B587005CA71B /* trit_tryte.c */; }; + 4749507620B8B587005CA71B /* trit_tryte.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749506B20B8B587005CA71B /* trit_tryte.c */; }; + 4749507720B8B587005CA71B /* trit_tryte.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749506B20B8B587005CA71B /* trit_tryte.c */; }; + 4749507B20B8B78D005CA71B /* ptrit_incr.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749507A20B8B78D005CA71B /* ptrit_incr.c */; }; + 4749507C20B8B78D005CA71B /* ptrit_incr.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749507A20B8B78D005CA71B /* ptrit_incr.c */; }; + 4749507D20B8B78D005CA71B /* ptrit_incr.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749507A20B8B78D005CA71B /* ptrit_incr.c */; }; + 4749508020B8B7C3005CA71B /* trit_ptrit.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749507F20B8B7C3005CA71B /* trit_ptrit.c */; }; + 4749508120B8B7C3005CA71B /* trit_ptrit.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749507F20B8B7C3005CA71B /* trit_ptrit.c */; }; + 4749508220B8B7C3005CA71B /* trit_ptrit.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749507F20B8B7C3005CA71B /* trit_ptrit.c */; }; + 4749508620B8B99B005CA71B /* pow.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749508520B8B99B005CA71B /* pow.c */; }; + 4749508720B8B99B005CA71B /* pow.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749508520B8B99B005CA71B /* pow.c */; }; + 4749508820B8B99B005CA71B /* pow.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749508520B8B99B005CA71B /* pow.c */; }; + 4749508A20B8BA78005CA71B /* curl_p_ptrit.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749508920B8BA78005CA71B /* curl_p_ptrit.c */; }; + 4749508B20B8BA78005CA71B /* curl_p_ptrit.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749508920B8BA78005CA71B /* curl_p_ptrit.c */; }; + 4749508C20B8BA78005CA71B /* curl_p_ptrit.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749508920B8BA78005CA71B /* curl_p_ptrit.c */; }; + 4749509620B8BD10005CA71B /* curl_p.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749509520B8BD10005CA71B /* curl_p.c */; }; + 4749509720B8BD10005CA71B /* curl_p.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749509520B8BD10005CA71B /* curl_p.c */; }; + 4749509820B8BD10005CA71B /* curl_p.c in Sources */ = {isa = PBXBuildFile; fileRef = 4749509520B8BD10005CA71B /* curl_p.c */; }; 50211AF12051EAD300685CE5 /* sha3.c in Sources */ = {isa = PBXBuildFile; fileRef = 50211AF02051EAD300685CE5 /* sha3.c */; }; 50211AF22051EAD300685CE5 /* sha3.c in Sources */ = {isa = PBXBuildFile; fileRef = 50211AF02051EAD300685CE5 /* sha3.c */; }; 50211AF32051EAD300685CE5 /* sha3.c in Sources */ = {isa = PBXBuildFile; fileRef = 50211AF02051EAD300685CE5 /* sha3.c */; }; @@ -124,6 +154,37 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 472A935120B8CDBA00BA091C /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; }; + 4749505D20B8B587005CA71B /* hashcash.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = hashcash.c; sourceTree = ""; }; + 4749505F20B8B587005CA71B /* const.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = const.h; sourceTree = ""; }; + 4749506020B8B587005CA71B /* hashcash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = hashcash.h; sourceTree = ""; }; + 4749506120B8B587005CA71B /* module.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; + 4749506220B8B587005CA71B /* pearl_diver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pearl_diver.h; sourceTree = ""; }; + 4749506320B8B587005CA71B /* _pow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _pow.h; sourceTree = ""; }; + 4749506420B8B587005CA71B /* stdint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = stdint.h; sourceTree = ""; }; + 4749506520B8B587005CA71B /* trit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = trit.h; sourceTree = ""; }; + 4749506620B8B587005CA71B /* trit_tryte.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = trit_tryte.h; sourceTree = ""; }; + 4749506720B8B587005CA71B /* trits.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = trits.h; sourceTree = ""; }; + 4749506820B8B587005CA71B /* tryte.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tryte.h; sourceTree = ""; }; + 4749506920B8B587005CA71B /* pearl_diver.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pearl_diver.c; sourceTree = ""; }; + 4749506A20B8B587005CA71B /* _pow.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = _pow.c; sourceTree = ""; }; + 4749506B20B8B587005CA71B /* trit_tryte.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = trit_tryte.c; sourceTree = ""; }; + 4749507820B8B5C9005CA71B /* ptrit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ptrit.h; sourceTree = ""; }; + 4749507920B8B784005CA71B /* ptrit_incr.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ptrit_incr.h; sourceTree = ""; }; + 4749507A20B8B78D005CA71B /* ptrit_incr.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ptrit_incr.c; sourceTree = ""; }; + 4749507E20B8B7A7005CA71B /* trit_ptrit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = trit_ptrit.h; sourceTree = ""; }; + 4749507F20B8B7C3005CA71B /* trit_ptrit.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = trit_ptrit.c; sourceTree = ""; }; + 4749508320B8B8AF005CA71B /* pcurl_ptrit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = pcurl_ptrit.h; sourceTree = ""; }; + 4749508420B8B993005CA71B /* pow.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = pow.h; sourceTree = ""; }; + 4749508520B8B99B005CA71B /* pow.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pow.c; sourceTree = ""; }; + 4749508920B8BA78005CA71B /* curl_p_ptrit.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = curl_p_ptrit.c; sourceTree = ""; }; + 4749508D20B8BA8E005CA71B /* indices.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = indices.h; sourceTree = ""; }; + 4749508E20B8BB76005CA71B /* curl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = curl.h; sourceTree = ""; }; + 4749508F20B8BB84005CA71B /* curl.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = curl.c; sourceTree = ""; }; + 4749509320B8BBDE005CA71B /* hash.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = hash.h; sourceTree = ""; }; + 4749509420B8BBF3005CA71B /* constants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = constants.h; sourceTree = ""; }; + 4749509520B8BD10005CA71B /* curl_p.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = curl_p.c; sourceTree = ""; }; + 4749509920B8C648005CA71B /* CPow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CPow.swift; sourceTree = ""; }; 50211AEE2051EAD300685CE5 /* sha3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sha3.h; sourceTree = ""; }; 50211AF02051EAD300685CE5 /* sha3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha3.c; sourceTree = ""; }; 50211AF42051EB5D00685CE5 /* module.modulemap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; @@ -182,6 +243,59 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 472A935020B8CDBA00BA091C /* Frameworks */ = { + isa = PBXGroup; + children = ( + 472A935120B8CDBA00BA091C /* CoreFoundation.framework */, + ); + name = Frameworks; + sourceTree = ""; + }; + 4749505C20B8B587005CA71B /* cpow */ = { + isa = PBXGroup; + children = ( + 4749505E20B8B587005CA71B /* include */, + 4749508F20B8BB84005CA71B /* curl.c */, + 4749509520B8BD10005CA71B /* curl_p.c */, + 4749505D20B8B587005CA71B /* hashcash.c */, + 4749508520B8B99B005CA71B /* pow.c */, + 4749508920B8BA78005CA71B /* curl_p_ptrit.c */, + 4749506920B8B587005CA71B /* pearl_diver.c */, + 4749506A20B8B587005CA71B /* _pow.c */, + 4749506B20B8B587005CA71B /* trit_tryte.c */, + 4749507A20B8B78D005CA71B /* ptrit_incr.c */, + 4749507F20B8B7C3005CA71B /* trit_ptrit.c */, + ); + name = cpow; + path = Sources/cpow; + sourceTree = ""; + }; + 4749505E20B8B587005CA71B /* include */ = { + isa = PBXGroup; + children = ( + 4749509420B8BBF3005CA71B /* constants.h */, + 4749508D20B8BA8E005CA71B /* indices.h */, + 4749507E20B8B7A7005CA71B /* trit_ptrit.h */, + 4749507920B8B784005CA71B /* ptrit_incr.h */, + 4749508320B8B8AF005CA71B /* pcurl_ptrit.h */, + 4749507820B8B5C9005CA71B /* ptrit.h */, + 4749505F20B8B587005CA71B /* const.h */, + 4749506020B8B587005CA71B /* hashcash.h */, + 4749506120B8B587005CA71B /* module.modulemap */, + 4749506220B8B587005CA71B /* pearl_diver.h */, + 4749506320B8B587005CA71B /* _pow.h */, + 4749506420B8B587005CA71B /* stdint.h */, + 4749506520B8B587005CA71B /* trit.h */, + 4749506620B8B587005CA71B /* trit_tryte.h */, + 4749506720B8B587005CA71B /* trits.h */, + 4749506820B8B587005CA71B /* tryte.h */, + 4749508420B8B993005CA71B /* pow.h */, + 4749508E20B8BB76005CA71B /* curl.h */, + 4749509320B8BBDE005CA71B /* hash.h */, + ); + path = include; + sourceTree = ""; + }; 50211AEC2051EAD300685CE5 /* sha3 */ = { isa = PBXGroup; children = ( @@ -238,6 +352,7 @@ 50F1DF94200EE7AD005D91C6 /* IotaLocalPow.swift */, 50F1DF95200EE7AD005D91C6 /* PearlDiver.swift */, 50F3317F2010182800DC6E5B /* PearlDiverLocalPow.swift */, + 4749509920B8C648005CA71B /* CPow.swift */, ); path = Pow; sourceTree = ""; @@ -279,12 +394,14 @@ OBJ_7 /* Sources */, OBJ_10 /* Tests */, OBJ_13 /* Products */, + 472A935020B8CDBA00BA091C /* Frameworks */, ); sourceTree = ""; }; OBJ_7 /* Sources */ = { isa = PBXGroup; children = ( + 4749505C20B8B587005CA71B /* cpow */, 50211AEC2051EAD300685CE5 /* sha3 */, OBJ_8 /* IotaKit */, ); @@ -461,9 +578,19 @@ isa = PBXSourcesBuildPhase; buildActionMask = 0; files = ( + 472A935420B8D11D00BA091C /* CPow.swift in Sources */, + 4749507220B8B587005CA71B /* _pow.c in Sources */, + 4749508A20B8BA78005CA71B /* curl_p_ptrit.c in Sources */, + 4749507520B8B587005CA71B /* trit_tryte.c in Sources */, + 4749509620B8BD10005CA71B /* curl_p.c in Sources */, 50F60D2C2050916200112585 /* IotaMultisig.swift in Sources */, + 4749507B20B8B78D005CA71B /* ptrit_incr.c in Sources */, 50211AF12051EAD300685CE5 /* sha3.c in Sources */, + 4749506C20B8B587005CA71B /* hashcash.c in Sources */, + 4749508620B8B99B005CA71B /* pow.c in Sources */, + 4749506F20B8B587005CA71B /* pearl_diver.c in Sources */, OBJ_21 /* Package.swift in Sources */, + 4749508020B8B7C3005CA71B /* trit_ptrit.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -472,35 +599,45 @@ buildActionMask = 0; files = ( OBJ_27 /* IotaKitTests.swift in Sources */, + 4749508B20B8BA78005CA71B /* curl_p_ptrit.c in Sources */, 50F33170200FB6D200DC6E5B /* Iota.swift in Sources */, 50F60D2D2050916200112585 /* IotaMultisig.swift in Sources */, 50E32CED2020FCE9001017D6 /* IotaUnits.swift in Sources */, 50F33169200FB6CD00DC6E5B /* IotaTransactionExtension.swift in Sources */, 50F331822010185300DC6E5B /* PearlDiverLocalPow.swift in Sources */, + 472A935520B8D11E00BA091C /* CPow.swift in Sources */, 50F33176200FB6D900DC6E5B /* IotaAPIServiceProtocol.swift in Sources */, + 4749507C20B8B78D005CA71B /* ptrit_incr.c in Sources */, 50F33164200FB6C800DC6E5B /* Kerl.swift in Sources */, 50F33175200FB6D900DC6E5B /* PAWS.swift in Sources */, 50F33174200FB6D200DC6E5B /* IotaSigning.swift in Sources */, 50211AF22051EAD300685CE5 /* sha3.c in Sources */, + 4749507020B8B587005CA71B /* pearl_diver.c in Sources */, 50F33177200FB6DD00DC6E5B /* WSUtils.swift in Sources */, 50F33165200FB6C800DC6E5B /* CurlSource.swift in Sources */, + 4749509720B8BD10005CA71B /* curl_p.c in Sources */, 50F3316C200FB6D200DC6E5B /* IotaNodeSelector.swift in Sources */, 50F3316A200FB6D200DC6E5B /* Sha3.swift in Sources */, 50F3318620110CB600DC6E5B /* APIsTests.swift in Sources */, + 4749508720B8B99B005CA71B /* pow.c in Sources */, 50211AFF2053108500685CE5 /* ConversionTests.swift in Sources */, 50F33171200FB6D200DC6E5B /* IotaAPIService.swift in Sources */, 50F3316E200FB6D200DC6E5B /* IotaConverter.swift in Sources */, 50F60D3020509C6E00112585 /* IotaMultisigTests.swift in Sources */, 50F33168200FB6CD00DC6E5B /* IotaBundleExtension.swift in Sources */, + 4749507320B8B587005CA71B /* _pow.c in Sources */, 50F33163200FB6C300DC6E5B /* PearlDiver.swift in Sources */, 50F33178200FB6DD00DC6E5B /* Utils.swift in Sources */, 50E32CF020210BE7001017D6 /* IotaUnitsTests.swift in Sources */, 50F3316B200FB6D200DC6E5B /* IotaChecksum.swift in Sources */, 50F3316F200FB6D200DC6E5B /* IotaConstants.swift in Sources */, 50F33166200FB6C800DC6E5B /* Curl.swift in Sources */, + 4749507620B8B587005CA71B /* trit_tryte.c in Sources */, 50F3316D200FB6D200DC6E5B /* IotaInputValidator.swift in Sources */, 50F33173200FB6D200DC6E5B /* IotaModels.swift in Sources */, 50F33167200FB6C800DC6E5B /* IotaLocalPow.swift in Sources */, + 4749508120B8B7C3005CA71B /* trit_ptrit.c in Sources */, + 4749506D20B8B587005CA71B /* hashcash.c in Sources */, 50F33172200FB6D200DC6E5B /* IotaAPIUtils.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -509,32 +646,42 @@ isa = PBXSourcesBuildPhase; buildActionMask = 0; files = ( + 4749508820B8B99B005CA71B /* pow.c in Sources */, 50211AF32051EAD300685CE5 /* sha3.c in Sources */, 504C6C242006AF8A007C6C7E /* Kerl.swift in Sources */, 50F1DF87200C26FE005D91C6 /* IotaBundleExtension.swift in Sources */, 503F6336200B66B700CE0F6F /* IotaNodeSelector.swift in Sources */, 50F60D2E2050916200112585 /* IotaMultisig.swift in Sources */, + 4749507D20B8B78D005CA71B /* ptrit_incr.c in Sources */, 50F3315F200D744800DC6E5B /* IotaConstants.swift in Sources */, + 4749508C20B8BA78005CA71B /* curl_p_ptrit.c in Sources */, 504C6C2E2006AF8A007C6C7E /* IotaConverter.swift in Sources */, 504C6C322006AF8A007C6C7E /* IotaAPIServiceProtocol.swift in Sources */, 504C6C252006AF8A007C6C7E /* WSUtils.swift in Sources */, 50F1DF96200EE7AD005D91C6 /* IotaLocalPow.swift in Sources */, 50F3315E200D73E300DC6E5B /* IotaInputValidator.swift in Sources */, 50F1DF97200EE7AD005D91C6 /* PearlDiver.swift in Sources */, + 4749506E20B8B587005CA71B /* hashcash.c in Sources */, 504C6C272006AF8A007C6C7E /* Iota.swift in Sources */, 504C6C282006AF8A007C6C7E /* IotaAPIUtils.swift in Sources */, + 4749509820B8BD10005CA71B /* curl_p.c in Sources */, 504C6C2C2006AF8A007C6C7E /* Sha3.swift in Sources */, 50F1DF8C200C2C62005D91C6 /* IotaTransactionExtension.swift in Sources */, + 4749508220B8B7C3005CA71B /* trit_ptrit.c in Sources */, 50E32CEE2020FCE9001017D6 /* IotaUnits.swift in Sources */, 504C6C262006AF8A007C6C7E /* Utils.swift in Sources */, + 4749507720B8B587005CA71B /* trit_tryte.c in Sources */, 50F33157200D34E800DC6E5B /* Curl.swift in Sources */, 504C6C2F2006AF8A007C6C7E /* IotaAPIService.swift in Sources */, + 4749507420B8B587005CA71B /* _pow.c in Sources */, 50F331812010185300DC6E5B /* PearlDiverLocalPow.swift in Sources */, 504C6C2D2006AF8A007C6C7E /* IotaChecksum.swift in Sources */, + 472A935620B8D11E00BA091C /* CPow.swift in Sources */, 50F33155200D308200DC6E5B /* CurlSource.swift in Sources */, 504C6C332006AF8A007C6C7E /* IotaModels.swift in Sources */, 504C6C312006AF8A007C6C7E /* PAWS.swift in Sources */, 504C6C2B2006AF8A007C6C7E /* IotaSigning.swift in Sources */, + 4749507120B8B587005CA71B /* pearl_diver.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -623,6 +770,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + CLANG_ENABLE_MODULES = NO; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PLATFORM_DIR)/Developer/Library/Frameworks", @@ -642,6 +790,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; + CLANG_ENABLE_MODULES = NO; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PLATFORM_DIR)/Developer/Library/Frameworks", @@ -727,7 +876,7 @@ PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; - SWIFT_INCLUDE_PATHS = "${SRCROOT}/Sources/sha3/**"; + SWIFT_INCLUDE_PATHS = "${SRCROOT}/Sources/sha3/** ${SRCROOT}/Sources/cpow"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 4.0; TARGET_NAME = IotaKit; @@ -756,8 +905,9 @@ PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)"; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; + SWIFT_COMPILATION_MODE = wholemodule; SWIFT_DISABLE_SAFETY_CHECKS = YES; - SWIFT_INCLUDE_PATHS = "${SRCROOT}/Sources/sha3/**"; + SWIFT_INCLUDE_PATHS = "${SRCROOT}/Sources/sha3/** ${SRCROOT}/Sources/cpow"; SWIFT_VERSION = 4.0; TARGET_NAME = IotaKit; }; diff --git a/IotaKit.xcodeproj/xcshareddata/xcschemes/IotaKit-Package.xcscheme b/IotaKit.xcodeproj/xcshareddata/xcschemes/IotaKit-Package.xcscheme index 8f9a165..50570c1 100644 --- a/IotaKit.xcodeproj/xcshareddata/xcschemes/IotaKit-Package.xcscheme +++ b/IotaKit.xcodeproj/xcshareddata/xcschemes/IotaKit-Package.xcscheme @@ -43,7 +43,7 @@ String { + let cTrytes = trytes.cString(using: .utf8) + if let resultC = iota_pow(cTrytes, UInt8(minWeightMagnitude)) { + let result = String(cString: resultC) + resultC.deallocate() + var tx = IotaTransaction(trytes: trytes) + tx.nonce = result + return tx.trytes + } + return "" + } + + public func performPoW(trytes: String, minWeightMagnitude: Int, result: @escaping (String) -> ()) { + + } + + +} diff --git a/Sources/cpow/_pow.c b/Sources/cpow/_pow.c new file mode 100755 index 0000000..de47edd --- /dev/null +++ b/Sources/cpow/_pow.c @@ -0,0 +1,26 @@ +#include "include/_pow.h" +#include +#include +#include "include/hashcash.h" +#include "include/trit_tryte.h" + + +#define NONCE_LENGTH 27 * 3 + + +char* do_pow(Curl* curl, const char* trits_in, size_t trits_len, uint8_t mwm) { + + tryte_t* nonce_trits = + (tryte_t*)calloc(NONCE_LENGTH + 1, sizeof(tryte_t)); + + curl_absorb(curl, trits_in, trits_len - HASH_LENGTH); + memcpy(curl->state, trits_in + trits_len - HASH_LENGTH, HASH_LENGTH); + + // FIXME(th0br0) deal with result value of `hashcash` call + hashcash(curl, BODY, HASH_LENGTH - NONCE_LENGTH, HASH_LENGTH, mwm); + + memcpy(nonce_trits, curl->state + HASH_LENGTH - NONCE_LENGTH, NONCE_LENGTH); + + return (char*)nonce_trits; +} + diff --git a/Sources/cpow/curl.c b/Sources/cpow/curl.c new file mode 100755 index 0000000..149a135 --- /dev/null +++ b/Sources/cpow/curl.c @@ -0,0 +1,109 @@ +/* + * (c) 2016 Paul Handy, based on code from come-from-beyond + */ + +#include "include/curl.h" +#include "include/hash.h" +#include "include/constants.h" +#include +#include +#include + +#define __TRUTH_TABLE 1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0 + +#define __INDEX_TABLE \ + 0, 364, 728, 363, 727, 362, 726, 361, 725, 360, 724, 359, 723, 358, 722,\ + 357, 721, 356, 720, 355, 719, 354, 718, 353, 717, 352, 716, 351, 715, 350,\ + 714, 349, 713, 348, 712, 347, 711, 346, 710, 345, 709, 344, 708, 343, 707,\ + 342, 706, 341, 705, 340, 704, 339, 703, 338, 702, 337, 701, 336, 700, 335,\ + 699, 334, 698, 333, 697, 332, 696, 331, 695, 330, 694, 329, 693, 328, 692,\ + 327, 691, 326, 690, 325, 689, 324, 688, 323, 687, 322, 686, 321, 685, 320,\ + 684, 319, 683, 318, 682, 317, 681, 316, 680, 315, 679, 314, 678, 313, 677,\ + 312, 676, 311, 675, 310, 674, 309, 673, 308, 672, 307, 671, 306, 670, 305,\ + 669, 304, 668, 303, 667, 302, 666, 301, 665, 300, 664, 299, 663, 298, 662,\ + 297, 661, 296, 660, 295, 659, 294, 658, 293, 657, 292, 656, 291, 655, 290,\ + 654, 289, 653, 288, 652, 287, 651, 286, 650, 285, 649, 284, 648, 283, 647,\ + 282, 646, 281, 645, 280, 644, 279, 643, 278, 642, 277, 641, 276, 640, 275,\ + 639, 274, 638, 273, 637, 272, 636, 271, 635, 270, 634, 269, 633, 268, 632,\ + 267, 631, 266, 630, 265, 629, 264, 628, 263, 627, 262, 626, 261, 625, 260,\ + 624, 259, 623, 258, 622, 257, 621, 256, 620, 255, 619, 254, 618, 253, 617,\ + 252, 616, 251, 615, 250, 614, 249, 613, 248, 612, 247, 611, 246, 610, 245,\ + 609, 244, 608, 243, 607, 242, 606, 241, 605, 240, 604, 239, 603, 238, 602,\ + 237, 601, 236, 600, 235, 599, 234, 598, 233, 597, 232, 596, 231, 595, 230,\ + 594, 229, 593, 228, 592, 227, 591, 226, 590, 225, 589, 224, 588, 223, 587,\ + 222, 586, 221, 585, 220, 584, 219, 583, 218, 582, 217, 581, 216, 580, 215,\ + 579, 214, 578, 213, 577, 212, 576, 211, 575, 210, 574, 209, 573, 208, 572,\ + 207, 571, 206, 570, 205, 569, 204, 568, 203, 567, 202, 566, 201, 565, 200,\ + 564, 199, 563, 198, 562, 197, 561, 196, 560, 195, 559, 194, 558, 193, 557,\ + 192, 556, 191, 555, 190, 554, 189, 553, 188, 552, 187, 551, 186, 550, 185,\ + 549, 184, 548, 183, 547, 182, 546, 181, 545, 180, 544, 179, 543, 178, 542,\ + 177, 541, 176, 540, 175, 539, 174, 538, 173, 537, 172, 536, 171, 535, 170,\ + 534, 169, 533, 168, 532, 167, 531, 166, 530, 165, 529, 164, 528, 163, 527,\ + 162, 526, 161, 525, 160, 524, 159, 523, 158, 522, 157, 521, 156, 520, 155,\ + 519, 154, 518, 153, 517, 152, 516, 151, 515, 150, 514, 149, 513, 148, 512,\ + 147, 511, 146, 510, 145, 509, 144, 508, 143, 507, 142, 506, 141, 505, 140,\ + 504, 139, 503, 138, 502, 137, 501, 136, 500, 135, 499, 134, 498, 133, 497,\ + 132, 496, 131, 495, 130, 494, 129, 493, 128, 492, 127, 491, 126, 490, 125,\ + 489, 124, 488, 123, 487, 122, 486, 121, 485, 120, 484, 119, 483, 118, 482,\ + 117, 481, 116, 480, 115, 479, 114, 478, 113, 477, 112, 476, 111, 475, 110,\ + 474, 109, 473, 108, 472, 107, 471, 106, 470, 105, 469, 104, 468, 103, 467,\ + 102, 466, 101, 465, 100, 464, 99, 463, 98, 462, 97, 461, 96, 460, 95,\ + 459, 94, 458, 93, 457, 92, 456, 91, 455, 90, 454, 89, 453, 88, 452,\ + 87, 451, 86, 450, 85, 449, 84, 448, 83, 447, 82, 446, 81, 445, 80,\ + 444, 79, 443, 78, 442, 77, 441, 76, 440, 75, 439, 74, 438, 73, 437,\ + 72, 436, 71, 435, 70, 434, 69, 433, 68, 432, 67, 431, 66, 430, 65,\ + 429, 64, 428, 63, 427, 62, 426, 61, 425, 60, 424, 59, 423, 58, 422,\ + 57, 421, 56, 420, 55, 419, 54, 418, 53, 417, 52, 416, 51, 415, 50,\ + 414, 49, 413, 48, 412, 47, 411, 46, 410, 45, 409, 44, 408, 43, 407,\ + 42, 406, 41, 405, 40, 404, 39, 403, 38, 402, 37, 401, 36, 400, 35,\ + 399, 34, 398, 33, 397, 32, 396, 31, 395, 30, 394, 29, 393, 28, 392,\ + 27, 391, 26, 390, 25, 389, 24, 388, 23, 387, 22, 386, 21, 385, 20,\ + 384, 19, 383, 18, 382, 17, 381, 16, 380, 15, 379, 14, 378, 13, 377,\ + 12, 376, 11, 375, 10, 374, 9, 373, 8, 372, 7, 371, 6, 370, 5,\ + 369, 4, 368, 3, 367, 2, 366, 1, 365, 0 + + +static const size_t TRUTH_TABLE[11] = {__TRUTH_TABLE}; +static const size_t INDEX[STATE_LENGTH+1] = {__INDEX_TABLE}; + +void transform(curl_t* ctx); + +void init_curl(curl_t* ctx) { + memset(ctx->state, 0, STATE_LENGTH * sizeof(char)); +} +int i = 0; +void absorb(curl_t* ctx, char* const trits, int length) { + int offset = 0; + do { + memcpy(ctx->state, trits + offset, + (length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(char)); + transform(ctx); + offset += HASH_LENGTH; + } while ((length -= HASH_LENGTH) > 0); +} + +void squeeze(curl_t* ctx, char* trits, int length) { + int offset = 0; + do { + // memcpy(trits+offset, ctx->state, (length < HASH_LENGTH? length: + // HASH_LENGTH) * sizeof(char)); + memcpy(&(trits[offset]), ctx->state, + (length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(char)); + transform(ctx); + offset += HASH_LENGTH; + } while ((length -= HASH_LENGTH) > 0); +} + +void transform(curl_t* ctx) { + int round, stateIndex; + char scratchpad[STATE_LENGTH]; + for (round = 0; round < NUMBER_OF_ROUNDS; round++) { + memcpy(scratchpad, ctx->state, STATE_LENGTH * sizeof(char)); + for (stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) { + ctx->state[stateIndex] = TRUTH_TABLE[scratchpad[INDEX[stateIndex]] + + (scratchpad[INDEX[stateIndex+1]]<<2) +5 ]; + } + } +} + +void reset(curl_t* ctx) { memset(ctx->state, 0, STATE_LENGTH * sizeof(char)); } diff --git a/Sources/cpow/curl_p.c b/Sources/cpow/curl_p.c new file mode 100755 index 0000000..dbceaa1 --- /dev/null +++ b/Sources/cpow/curl_p.c @@ -0,0 +1,61 @@ +/* + * (c) 2016 Paul Handy, based on code from come-from-beyond + */ + +#include +#include + +#include "include/indices.h" +#include "include/trit.h" + +#define __TRUTH_TABLE 1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0 + +static const size_t CURL_INDEX[STATE_LENGTH + 1] = {__INDEX_TABLE}; + +static const trit_t TRUTH_TABLE[11] = {__TRUTH_TABLE}; + +void transform(Curl *ctx); +void sbox(Curl *const, Curl *const); + +void init_curl(Curl *ctx) { memset(ctx->state, 0, sizeof(ctx->state)); } + +void curl_absorb(Curl *ctx, trit_t *const trits, size_t length) { + size_t numChunks = length / HASH_LENGTH + ((length % HASH_LENGTH) ? 1 : 0); + size_t i = 0; + for (; i < numChunks; ++i) { + memcpy(ctx->state, trits + i * HASH_LENGTH * sizeof(trit_t), + (length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(trit_t)); + transform(ctx); + length = length < HASH_LENGTH ? 0 : length - HASH_LENGTH; + } +} + +void curl_squeeze(Curl *ctx, trit_t *const trits, size_t length) { + size_t numChunks = length / HASH_LENGTH + ((length % HASH_LENGTH) ? 1 : 0); + size_t i = 0; + for (; i < numChunks; ++i) { + memcpy(trits + i * HASH_LENGTH * sizeof(trit_t), ctx->state, + (length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(trit_t)); + transform(ctx); + length = length < HASH_LENGTH ? 0 : length - HASH_LENGTH; + } +} + +void transform(Curl *const ctx) { + Curl s; + size_t round = 0; + for (; round < ctx->type; ++round) { + memcpy(s.state, ctx->state, sizeof(ctx->state)); + sbox(ctx, &s); + } +} + +void sbox(Curl *const c, Curl *const s) { + size_t i = 0; + for (; i < STATE_LENGTH; ++i) { + c->state[i] = TRUTH_TABLE[s->state[CURL_INDEX[i]] + + ((unsigned)s->state[CURL_INDEX[i + 1]] << 2) + 5]; + } +} + +void curl_reset(Curl *ctx) { memset(ctx->state, 0, sizeof(ctx->state)); } diff --git a/Sources/cpow/curl_p_ptrit.c b/Sources/cpow/curl_p_ptrit.c new file mode 100755 index 0000000..a6d01cc --- /dev/null +++ b/Sources/cpow/curl_p_ptrit.c @@ -0,0 +1,74 @@ +/* + * (c) 2016 Paul Handy, based on code from come-from-beyond + */ + +#include +#include + +#include "include/const.h" +#include "include/indices.h" +#include "include/pcurl_ptrit.h" + +static const size_t CURL_INDEX[STATE_LENGTH + 1] = {__INDEX_TABLE}; + +void ptrit_transform_round(PCurl *const, PCurl *const, size_t const); +void ptrit_sbox(ptrit_t *const, ptrit_t *const, size_t const); + +void init_ptrit_curl(PCurl *ctx) { ptrit_curl_reset(ctx); } + +void ptrit_curl_absorb(PCurl *ctx, ptrit_t *const trits, size_t length) { + memcpy(ctx->state, trits, + (length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(ptrit_t)); + ptrit_transform(ctx); + if (length <= HASH_LENGTH) { + return; + } + ptrit_curl_absorb(ctx, trits + HASH_LENGTH * sizeof(ptrit_t), + length - HASH_LENGTH); +} + +void ptrit_curl_squeeze(PCurl *ctx, ptrit_t *const trits, size_t length) { + memcpy(trits, ctx->state, + (length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(ptrit_t)); + + ptrit_transform(ctx); + if (length <= HASH_LENGTH) { + return; + } + ptrit_curl_squeeze(ctx, trits + HASH_LENGTH * sizeof(ptrit_t), + length - HASH_LENGTH); +} + +void ptrit_transform(PCurl *const ctx) { + PCurl s; + ptrit_transform_round(ctx, &s, ctx->type); + memcpy(ctx->state, s.state, sizeof(ptrit_t) * STATE_LENGTH); + ptrit_curl_reset(&s); +} + +void ptrit_transform_round(PCurl *const ctx, PCurl *const s, size_t const i) { + if (i == 0) { + return; + } + ptrit_sbox(s->state, ctx->state, 0); + ptrit_transform_round(s, ctx, i - 1); +} +void ptrit_sbox(ptrit_t *const c, ptrit_t *const s, size_t const i) { + if (i == STATE_LENGTH) { + return; + } + ptrit_s alpha, beta, gamma, delta; + alpha = s[CURL_INDEX[i]].low; + beta = s[CURL_INDEX[i]].high; + gamma = s[CURL_INDEX[i + 1]].high; + delta = (alpha | (~gamma)) & (s[CURL_INDEX[i + 1]].low ^ beta); + + c->low = ~delta; + c->high = (alpha ^ gamma) | delta; + + ptrit_sbox(&c[1], s, i + 1); +} + +void ptrit_curl_reset(PCurl *ctx) { + memset(ctx->state, 0, sizeof(ptrit_t) * STATE_LENGTH); +} diff --git a/Sources/cpow/hashcash.c b/Sources/cpow/hashcash.c new file mode 100755 index 0000000..dfa97c0 --- /dev/null +++ b/Sources/cpow/hashcash.c @@ -0,0 +1,23 @@ +#include + +#include "include/hashcash.h" +#include "include/ptrit.h" +#include "include/search.h" + +short test(PCurl *curl, unsigned short mwm) { + unsigned short i; + ptrit_s probe = HIGH_BITS; + for (i = HASH_LENGTH; i-- > HASH_LENGTH - mwm && probe != 0;) { + probe &= ~(curl->state[i].low ^ curl->state[i].high); + } + if (probe == 0) { + return -1; + } + return __builtin_ctzll(probe); +} + +PearlDiverStatus hashcash(Curl *const ctx, SearchType type, + unsigned short const offset, unsigned short const end, + unsigned short const min_weight) { + return pd_search(ctx, offset, end, &test, min_weight); +} diff --git a/Sources/cpow/include/_pow.h b/Sources/cpow/include/_pow.h new file mode 100755 index 0000000..34b7e60 --- /dev/null +++ b/Sources/cpow/include/_pow.h @@ -0,0 +1,18 @@ +#ifndef __CURL_POW_H +#define __CURL_POW_H + + +#include +#include "trit.h" + +#ifdef __cplusplus +extern "C" { +#endif +char* do_pow(Curl* curl, const char* trits, size_t trits_len, uint8_t mwm); + +#ifdef __cplusplus +} +#endif + + +#endif //__CURL_POW_H diff --git a/Sources/cpow/include/const.h b/Sources/cpow/include/const.h new file mode 100755 index 0000000..7b430a8 --- /dev/null +++ b/Sources/cpow/include/const.h @@ -0,0 +1,12 @@ +#ifndef __COMMON_CURL_P_CONST_H +#define __COMMON_CURL_P_CONST_H + +#define HASH_LENGTH 243 +#define STATE_LENGTH 3 * HASH_LENGTH + +typedef enum { + CURL_P_27 = 27, + CURL_P_81 = 81, +} CurlType; + +#endif diff --git a/Sources/cpow/include/constants.h b/Sources/cpow/include/constants.h new file mode 100755 index 0000000..ec4f61c --- /dev/null +++ b/Sources/cpow/include/constants.h @@ -0,0 +1,13 @@ +#ifndef __CONST_H__ +#define __CONST_H__ + +#define HASH_LENGTH 243 +#define NONCE_LENGTH HASH_LENGTH / 3 +#define MESSAGE_LENGTH 6561 +#define INT_LENGTH NONCE_LENGTH / 3 +#define VALUE_LENGTH 81 +#define VALUE_USABLE_LENGTH 33 +#define NUMBER_OF_ROUNDS 81 + +#endif //__CONST_H__ + diff --git a/Sources/cpow/include/curl.h b/Sources/cpow/include/curl.h new file mode 100755 index 0000000..f77f710 --- /dev/null +++ b/Sources/cpow/include/curl.h @@ -0,0 +1,18 @@ + +#ifndef CURL_H +#define CURL_H +#include "hash.h" +#include +#include +#include + + +typedef struct { char state[STATE_LENGTH]; } curl_t; + +EXPORT void init_curl(curl_t* ctx); + +EXPORT void absorb(curl_t* ctx, char* const trits, int length); +EXPORT void squeeze(curl_t* ctx, char* const trits, int length); +EXPORT void reset(curl_t* ctx); + +#endif diff --git a/Sources/cpow/include/hash.h b/Sources/cpow/include/hash.h new file mode 100755 index 0000000..4cdf707 --- /dev/null +++ b/Sources/cpow/include/hash.h @@ -0,0 +1,27 @@ +#ifndef HASH_H +#define HASH_H + +#ifndef EXPORT +#if defined(_WIN32) +#define EXPORT __declspec(dllexport) +#else +#define EXPORT +#endif +#endif +#if defined(_WIN32) && !defined(MINGW) +#else +#include +#endif + +#include +#define HASH_LENGTH 243 +#define STATE_LENGTH 3 * HASH_LENGTH +#define TRYTE_LENGTH 2673 +#define TRANSACTION_LENGTH TRYTE_LENGTH * 3 +typedef int64_t bc_trit_t; +typedef char trit_t; + +#ifndef DEBUG +#define DEBUG +#endif // DEBUG +#endif diff --git a/Sources/cpow/include/hashcash.h b/Sources/cpow/include/hashcash.h new file mode 100755 index 0000000..8bf000b --- /dev/null +++ b/Sources/cpow/include/hashcash.h @@ -0,0 +1,16 @@ +#ifndef __COMMON_CURL_P_HASHCASH_H_ +#define __COMMON_CURL_P_HASHCASH_H_ + +#include "pearl_diver.h" +#include "trit.h" + +typedef enum { + TAIL, + BODY, + HEAD, +} SearchType; + +PearlDiverStatus hashcash(Curl *const ctx, SearchType type, + unsigned short const offset, unsigned short const end, + unsigned short const min_weight); +#endif diff --git a/Sources/cpow/include/indices.h b/Sources/cpow/include/indices.h new file mode 100755 index 0000000..8b80972 --- /dev/null +++ b/Sources/cpow/include/indices.h @@ -0,0 +1,57 @@ +#ifndef __COMMON_CURL_P_INDEX_H_ +#define __COMMON_CURL_P_INDEX_H_ + +#define __INDEX_TABLE \ + 0, 364, 728, 363, 727, 362, 726, 361, 725, 360, 724, 359, 723, 358, 722, \ + 357, 721, 356, 720, 355, 719, 354, 718, 353, 717, 352, 716, 351, 715, \ + 350, 714, 349, 713, 348, 712, 347, 711, 346, 710, 345, 709, 344, 708, \ + 343, 707, 342, 706, 341, 705, 340, 704, 339, 703, 338, 702, 337, 701, \ + 336, 700, 335, 699, 334, 698, 333, 697, 332, 696, 331, 695, 330, 694, \ + 329, 693, 328, 692, 327, 691, 326, 690, 325, 689, 324, 688, 323, 687, \ + 322, 686, 321, 685, 320, 684, 319, 683, 318, 682, 317, 681, 316, 680, \ + 315, 679, 314, 678, 313, 677, 312, 676, 311, 675, 310, 674, 309, 673, \ + 308, 672, 307, 671, 306, 670, 305, 669, 304, 668, 303, 667, 302, 666, \ + 301, 665, 300, 664, 299, 663, 298, 662, 297, 661, 296, 660, 295, 659, \ + 294, 658, 293, 657, 292, 656, 291, 655, 290, 654, 289, 653, 288, 652, \ + 287, 651, 286, 650, 285, 649, 284, 648, 283, 647, 282, 646, 281, 645, \ + 280, 644, 279, 643, 278, 642, 277, 641, 276, 640, 275, 639, 274, 638, \ + 273, 637, 272, 636, 271, 635, 270, 634, 269, 633, 268, 632, 267, 631, \ + 266, 630, 265, 629, 264, 628, 263, 627, 262, 626, 261, 625, 260, 624, \ + 259, 623, 258, 622, 257, 621, 256, 620, 255, 619, 254, 618, 253, 617, \ + 252, 616, 251, 615, 250, 614, 249, 613, 248, 612, 247, 611, 246, 610, \ + 245, 609, 244, 608, 243, 607, 242, 606, 241, 605, 240, 604, 239, 603, \ + 238, 602, 237, 601, 236, 600, 235, 599, 234, 598, 233, 597, 232, 596, \ + 231, 595, 230, 594, 229, 593, 228, 592, 227, 591, 226, 590, 225, 589, \ + 224, 588, 223, 587, 222, 586, 221, 585, 220, 584, 219, 583, 218, 582, \ + 217, 581, 216, 580, 215, 579, 214, 578, 213, 577, 212, 576, 211, 575, \ + 210, 574, 209, 573, 208, 572, 207, 571, 206, 570, 205, 569, 204, 568, \ + 203, 567, 202, 566, 201, 565, 200, 564, 199, 563, 198, 562, 197, 561, \ + 196, 560, 195, 559, 194, 558, 193, 557, 192, 556, 191, 555, 190, 554, \ + 189, 553, 188, 552, 187, 551, 186, 550, 185, 549, 184, 548, 183, 547, \ + 182, 546, 181, 545, 180, 544, 179, 543, 178, 542, 177, 541, 176, 540, \ + 175, 539, 174, 538, 173, 537, 172, 536, 171, 535, 170, 534, 169, 533, \ + 168, 532, 167, 531, 166, 530, 165, 529, 164, 528, 163, 527, 162, 526, \ + 161, 525, 160, 524, 159, 523, 158, 522, 157, 521, 156, 520, 155, 519, \ + 154, 518, 153, 517, 152, 516, 151, 515, 150, 514, 149, 513, 148, 512, \ + 147, 511, 146, 510, 145, 509, 144, 508, 143, 507, 142, 506, 141, 505, \ + 140, 504, 139, 503, 138, 502, 137, 501, 136, 500, 135, 499, 134, 498, \ + 133, 497, 132, 496, 131, 495, 130, 494, 129, 493, 128, 492, 127, 491, \ + 126, 490, 125, 489, 124, 488, 123, 487, 122, 486, 121, 485, 120, 484, \ + 119, 483, 118, 482, 117, 481, 116, 480, 115, 479, 114, 478, 113, 477, \ + 112, 476, 111, 475, 110, 474, 109, 473, 108, 472, 107, 471, 106, 470, \ + 105, 469, 104, 468, 103, 467, 102, 466, 101, 465, 100, 464, 99, 463, 98, \ + 462, 97, 461, 96, 460, 95, 459, 94, 458, 93, 457, 92, 456, 91, 455, 90, \ + 454, 89, 453, 88, 452, 87, 451, 86, 450, 85, 449, 84, 448, 83, 447, 82, \ + 446, 81, 445, 80, 444, 79, 443, 78, 442, 77, 441, 76, 440, 75, 439, 74, \ + 438, 73, 437, 72, 436, 71, 435, 70, 434, 69, 433, 68, 432, 67, 431, 66, \ + 430, 65, 429, 64, 428, 63, 427, 62, 426, 61, 425, 60, 424, 59, 423, 58, \ + 422, 57, 421, 56, 420, 55, 419, 54, 418, 53, 417, 52, 416, 51, 415, 50, \ + 414, 49, 413, 48, 412, 47, 411, 46, 410, 45, 409, 44, 408, 43, 407, 42, \ + 406, 41, 405, 40, 404, 39, 403, 38, 402, 37, 401, 36, 400, 35, 399, 34, \ + 398, 33, 397, 32, 396, 31, 395, 30, 394, 29, 393, 28, 392, 27, 391, 26, \ + 390, 25, 389, 24, 388, 23, 387, 22, 386, 21, 385, 20, 384, 19, 383, 18, \ + 382, 17, 381, 16, 380, 15, 379, 14, 378, 13, 377, 12, 376, 11, 375, 10, \ + 374, 9, 373, 8, 372, 7, 371, 6, 370, 5, 369, 4, 368, 3, 367, 2, 366, 1, \ + 365, 0 + +#endif diff --git a/Sources/cpow/include/module.modulemap b/Sources/cpow/include/module.modulemap new file mode 100755 index 0000000..332723f --- /dev/null +++ b/Sources/cpow/include/module.modulemap @@ -0,0 +1,4 @@ +module cpow [system][extern_c]{ + header "pow.h" + export * +} diff --git a/Sources/cpow/include/pcurl_ptrit.h b/Sources/cpow/include/pcurl_ptrit.h new file mode 100755 index 0000000..6fe8ac9 --- /dev/null +++ b/Sources/cpow/include/pcurl_ptrit.h @@ -0,0 +1,19 @@ +#ifndef __COMMON_CURL_P_PTRIT_H_ +#define __COMMON_CURL_P_PTRIT_H_ + +#include "const.h" +#include "ptrit_incr.h" + +typedef struct { + ptrit_t state[STATE_LENGTH]; + CurlType type; +} PCurl; + +void init_ptrit_curl(PCurl* ctx); + +void ptrit_transform(PCurl*); +void ptrit_curl_absorb(PCurl*, ptrit_t* const, size_t); +void ptrit_curl_squeeze(PCurl*, ptrit_t* const, size_t); +void ptrit_curl_reset(PCurl*); + +#endif diff --git a/Sources/cpow/include/pearl_diver.h b/Sources/cpow/include/pearl_diver.h new file mode 100755 index 0000000..5a58a7f --- /dev/null +++ b/Sources/cpow/include/pearl_diver.h @@ -0,0 +1,11 @@ +#ifndef __COMMON_CURL_P_PEARL_DIVER_H_ +#define __COMMON_CURL_P_PEARL_DIVER_H_ + +typedef enum { + PEARL_DIVER_SUCCESS, + PEARL_DIVER_RUNNING, + PEARL_DIVER_INTERRUPTED, + PEARL_DIVER_ERROR +} PearlDiverStatus; + +#endif diff --git a/Sources/cpow/include/pow.h b/Sources/cpow/include/pow.h new file mode 100755 index 0000000..3ba75fa --- /dev/null +++ b/Sources/cpow/include/pow.h @@ -0,0 +1,18 @@ +#ifndef ENTANGLED_POW_H +#define ENTANGLED_POW_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif +char* iota_pow(const char* trytes, uint8_t mwm); + +#ifdef __cplusplus +} +#endif + + +#endif //ENTANGLED_POW_H diff --git a/Sources/cpow/include/ptrit.h b/Sources/cpow/include/ptrit.h new file mode 100755 index 0000000..7188385 --- /dev/null +++ b/Sources/cpow/include/ptrit.h @@ -0,0 +1,16 @@ +#ifndef __COMMON_TRINARY_PTRIT_H_ +#define __COMMON_TRINARY_PTRIT_H_ + +#include "stdint.h" + +#define HIGH_BITS 0xFFFFFFFFFFFFFFFF +#define LOW_BITS 0x0000000000000000 + +typedef int64_t ptrit_s; + +typedef struct { + ptrit_s low; + ptrit_s high; +} ptrit_t; + +#endif diff --git a/Sources/cpow/include/ptrit_incr.h b/Sources/cpow/include/ptrit_incr.h new file mode 100755 index 0000000..c8b01e6 --- /dev/null +++ b/Sources/cpow/include/ptrit_incr.h @@ -0,0 +1,9 @@ +#ifndef __COMMON_TRINARY_PTRIT_INCREMENT_H_ +#define __COMMON_TRINARY_PTRIT_INCREMENT_H_ + +#include "ptrit.h" + +void ptrit_increment(ptrit_t *const, size_t, size_t); +void ptrit_offset(ptrit_t *const trits, size_t length); + +#endif diff --git a/Sources/cpow/include/search.h b/Sources/cpow/include/search.h new file mode 100755 index 0000000..df613ee --- /dev/null +++ b/Sources/cpow/include/search.h @@ -0,0 +1,11 @@ +#ifndef __COMMON_CURL_P_SEARCH_H_ +#define __COMMON_CURL_P_SEARCH_H_ + +#include "pearl_diver.h" +#include "pcurl_ptrit.h" +#include "trit.h" + +PearlDiverStatus pd_search(Curl *, unsigned short const, unsigned short const, + short (*)(PCurl *, unsigned short), unsigned short); + +#endif diff --git a/Sources/cpow/include/stdint.h b/Sources/cpow/include/stdint.h new file mode 100755 index 0000000..f11e4b7 --- /dev/null +++ b/Sources/cpow/include/stdint.h @@ -0,0 +1,9 @@ +#ifndef __CONSTANTS_H__ +#define __CONSTANTS_H__ + +#if defined(_WIN32) && !defined(MINGW) +#else +#include +#endif +#include +#endif //__CONSTANTS_H diff --git a/Sources/cpow/include/trit.h b/Sources/cpow/include/trit.h new file mode 100755 index 0000000..2485a90 --- /dev/null +++ b/Sources/cpow/include/trit.h @@ -0,0 +1,19 @@ +#ifndef __COMMON_CURL_P_TRIT_H_ +#define __COMMON_CURL_P_TRIT_H_ + +#include "stdint.h" +#include "const.h" +#include "trits.h" + +typedef struct { + trit_t state[STATE_LENGTH]; + CurlType type; +} Curl; + +void init_curl(Curl* ctx); + +void curl_absorb(Curl* ctx, trit_t* const trits, size_t length); +void curl_squeeze(Curl* ctx, trit_t* const trits, size_t length); +void curl_reset(Curl* ctx); + +#endif diff --git a/Sources/cpow/include/trit_ptrit.h b/Sources/cpow/include/trit_ptrit.h new file mode 100755 index 0000000..dc5584f --- /dev/null +++ b/Sources/cpow/include/trit_ptrit.h @@ -0,0 +1,11 @@ +#ifndef __COMMON_TRINARY_TRIT_PTRIT_H_ +#define __COMMON_TRINARY_TRIT_PTRIT_H_ + +#include "stdint.h" +#include "ptrit_incr.h" +#include "trits.h" + +void trits_to_ptrits(trit_t*, ptrit_t*, size_t); +void ptrits_to_trits(ptrit_t*, trit_t*, size_t, size_t); + +#endif diff --git a/Sources/cpow/include/trit_tryte.h b/Sources/cpow/include/trit_tryte.h new file mode 100755 index 0000000..d051866 --- /dev/null +++ b/Sources/cpow/include/trit_tryte.h @@ -0,0 +1,11 @@ +#ifndef __COMMON_TRINARY_TRIT_TRYTE_H_ +#define __COMMON_TRINARY_TRIT_TRYTE_H_ + +#include "stdint.h" +#include "trits.h" +#include "tryte.h" + +void trits_to_trytes(trit_t*, tryte_t*, size_t); +void trytes_to_trits(tryte_t*, trit_t*, size_t); + +#endif diff --git a/Sources/cpow/include/trits.h b/Sources/cpow/include/trits.h new file mode 100755 index 0000000..4406e02 --- /dev/null +++ b/Sources/cpow/include/trits.h @@ -0,0 +1,8 @@ +#ifndef __COMMON_TRINARY_TRIT_H_ +#define __COMMON_TRINARY_TRIT_H_ + +#include "stdint.h" + +typedef int8_t trit_t; + +#endif diff --git a/Sources/cpow/include/tryte.h b/Sources/cpow/include/tryte.h new file mode 100755 index 0000000..ddc6188 --- /dev/null +++ b/Sources/cpow/include/tryte.h @@ -0,0 +1,8 @@ +#ifndef __COMMON_TRINARY_TRYTE_H_ +#define __COMMON_TRINARY_TRYTE_H_ + +#include "stdint.h" + +typedef int8_t tryte_t; + +#endif diff --git a/Sources/cpow/pearl_diver.c b/Sources/cpow/pearl_diver.c new file mode 100755 index 0000000..70e324c --- /dev/null +++ b/Sources/cpow/pearl_diver.c @@ -0,0 +1,152 @@ +#include +#include +#include +#include + +#include "include/ptrit_incr.h" +#include "include/trit_ptrit.h" + +#include "include/ptrit.h" +#include "include/search.h" + +typedef enum { SEARCH_RUNNING, SEARCH_INTERRUPT, SEARCH_FINISHED } SearchStatus; + +typedef struct { + unsigned short index; + unsigned short offset; + unsigned short end; + PCurl curl; + short (*test)(PCurl *, unsigned short); + unsigned short param; + SearchStatus *status; + pthread_rwlock_t *statusLock; +} SearchInstance; + +void *run_search_thread(void *); +void pt_start(pthread_t *const, SearchInstance *const, unsigned short); +short do_pd_search(short (*)(PCurl *, unsigned short), SearchInstance *const, + PCurl *const); +void init_inst(SearchInstance *const, SearchStatus *, pthread_rwlock_t*, unsigned short, + PCurl *const, unsigned short, unsigned short, unsigned short, + short (*)(PCurl *, unsigned short)); + +PearlDiverStatus pd_search(Curl *const ctx, unsigned short offset, + unsigned short end, + short (*test)(PCurl *, unsigned short), + unsigned short param) { + unsigned short found_thread = 0, n_procs = sysconf(_SC_NPROCESSORS_ONLN); + intptr_t found_index = -1; + SearchInstance inst[n_procs]; + SearchStatus status = SEARCH_RUNNING; + pthread_rwlock_t statusLock; + pthread_t tid[n_procs]; + + if(pthread_rwlock_init(&statusLock, NULL)) { + return PEARL_DIVER_ERROR; + } + + + { + PCurl curl; + trits_to_ptrits(ctx->state, curl.state, STATE_LENGTH); + ptrit_offset(&curl.state[offset], 4); + curl.type = ctx->type; + init_inst(inst, &status, &statusLock, n_procs, &curl, offset + 4, end, param, test); + } + + pt_start(tid, inst, n_procs - 1); + + for (int i = n_procs; --i >= 0;) { + if (tid[i] == 0) continue; + + if (found_index < 0) { + pthread_join(tid[i], (void *)&found_index); + + if (found_index >= 0) { + found_thread = i; + } + } else { + pthread_join(tid[i], NULL); + } + } + + switch (found_index) { + case -1: + return PEARL_DIVER_ERROR; + } + + pthread_rwlock_destroy(&statusLock); + + ptrits_to_trits(&inst[found_thread].curl.state[offset], &ctx->state[offset], + found_index, end - offset); + + return PEARL_DIVER_SUCCESS; +} + +void init_inst(SearchInstance *const inst, SearchStatus *status, pthread_rwlock_t *statusLock, + unsigned short index, PCurl *const curl, unsigned short offset, + unsigned short end, unsigned short param, + short (*test)(PCurl *, unsigned short)) { + if (index == 0) { + return; + } + *inst = (SearchInstance){.index = index - 1, + .offset = offset, + .end = end, + .param = param, + .status = status, + .statusLock = statusLock, + .test = test}; + memcpy(&(inst->curl), curl, sizeof(PCurl)); + init_inst(&inst[1], status, statusLock, index - 1, curl, offset, end, param, test); +} + +void pt_start(pthread_t *const tid, SearchInstance *const inst, + unsigned short index) { + if (pthread_create(&tid[index], NULL, run_search_thread, + ((void *)&inst[index]))) { + tid[index] = 0; + } + if (index == 0) { + return; + } + pt_start(tid, inst, index - 1); +} + +void *run_search_thread(void *data) { + unsigned short i; + PCurl copy; + + SearchInstance *inst = ((SearchInstance *)data); + + for (i = 0; i < inst->index; i++) { + ptrit_increment(inst->curl.state, inst->offset, HASH_LENGTH); + } + return (void *)(intptr_t)do_pd_search(inst->test, inst, ©); +} + +short do_pd_search(short (*test)(PCurl *, unsigned short), SearchInstance *inst, + PCurl *copy) { + short index; + + SearchStatus status = SEARCH_RUNNING; + while (status == SEARCH_RUNNING) { + memcpy(copy, &inst->curl, sizeof(PCurl)); + ptrit_transform(copy); + index = test(copy, inst->param); + if (index >= 0) { + pthread_rwlock_wrlock(inst->statusLock); + *inst->status = SEARCH_FINISHED; + pthread_rwlock_unlock(inst->statusLock); + + return index; + } + ptrit_increment(inst->curl.state, inst->offset + 1, HASH_LENGTH); + + pthread_rwlock_rdlock(inst->statusLock); + status = *inst->status; + pthread_rwlock_unlock(inst->statusLock); + } + return -1; + // return do_pd_search(test, inst, copy); +} diff --git a/Sources/cpow/pow.c b/Sources/cpow/pow.c new file mode 100755 index 0000000..56aa3b4 --- /dev/null +++ b/Sources/cpow/pow.c @@ -0,0 +1,36 @@ +#include + +#include "include/_pow.h" +#include "include/pow.h" +#include +#include "include/hashcash.h" +#include "include/trit_tryte.h" + + +#define NONCE_LENGTH 27 * 3 + + +char* iota_pow(const char* trytes_in, uint8_t mwm) { + Curl curl; + init_curl(&curl); + curl.type = CURL_P_81; + + int tryte_len = strlen(trytes_in); + int trits_len = tryte_len * 3; + + trit_t* trits = (trit_t*)malloc(sizeof(trit_t) * trits_len); + + trytes_to_trits((tryte_t*)trytes_in, trits, tryte_len); + tryte_t* nonce_trytes = + (tryte_t*)calloc(NONCE_LENGTH / 3 + 1, sizeof(tryte_t)); + + trit_t* nonce_trits = do_pow(&curl, trits,trits_len,mwm); + free(trits); + + trits_to_trytes(nonce_trits, nonce_trytes, + NONCE_LENGTH); + free(nonce_trits); + + return (char*)nonce_trytes; +} + diff --git a/Sources/cpow/ptrit_incr.c b/Sources/cpow/ptrit_incr.c new file mode 100755 index 0000000..cdae558 --- /dev/null +++ b/Sources/cpow/ptrit_incr.c @@ -0,0 +1,37 @@ +#include "include/ptrit_incr.h" + +#define LOW_0 0xDB6DB6DB6DB6DB6D +#define HIGH_0 0xB6DB6DB6DB6DB6DB +#define LOW_1 0xF1F8FC7E3F1F8FC7 +#define HIGH_1 0x8FC7E3F1F8FC7E3F +#define LOW_2 0x7FFFE00FFFFC01FF +#define HIGH_2 0xFFC01FFFF803FFFF +#define LOW_3 0xFFC0000007FFFFFF +#define HIGH_3 0x003FFFFFFFFFFFFF + +void ptrit_offset(ptrit_t *const trits, size_t length) { + if (length < 4) { + return; + } + trits->low = LOW_0; + trits->high = HIGH_0; + trits[1].low = LOW_1; + trits[1].high = HIGH_1; + trits[2].low = LOW_2; + trits[2].high = HIGH_2; + trits[3].low = LOW_3; + trits[3].high = HIGH_3; +} + +void ptrit_increment(ptrit_t *const trits, size_t offset, size_t end) { + size_t i; + ptrit_s carry = 1; + ptrit_t copy; + for (i = offset; i < end && carry != 0; i++) { + copy.low = trits[i].low; + copy.high = trits[i].high; + trits[i].low = copy.high ^ copy.low; + trits[i].high = copy.low; + carry = copy.high & (~copy.low); + } +} diff --git a/Sources/cpow/trit_ptrit.c b/Sources/cpow/trit_ptrit.c new file mode 100755 index 0000000..99df8cc --- /dev/null +++ b/Sources/cpow/trit_ptrit.c @@ -0,0 +1,36 @@ +#include "include/trit_ptrit.h" + +#define HIGH_BITS 0xFFFFFFFFFFFFFFFF +#define LOW_BITS 0x0000000000000000 + +void trits_to_ptrits(trit_t *trits, ptrit_t *ptrits, size_t length) { + if (length == 0) { + return; + } + switch (*trits) { + case 0: + ptrits->low = HIGH_BITS; + ptrits->high = HIGH_BITS; + break; + case 1: + ptrits->low = LOW_BITS; + ptrits->high = HIGH_BITS; + break; + default: + ptrits->low = HIGH_BITS; + ptrits->high = LOW_BITS; + } + trits_to_ptrits(&trits[1], &ptrits[1], length - 1); +} + +void ptrits_to_trits(ptrit_t *ptrits, trit_t *trits, size_t index, + size_t length) { + if (length == 0) { + return; + } + + trits[0] = (ptrits->low & (1uLL << index)) + ? ((ptrits->high & (1uLL << index)) ? 0 : -1) + : 1; + ptrits_to_trits(&ptrits[1], &trits[1], index, length - 1); +} diff --git a/Sources/cpow/trit_tryte.c b/Sources/cpow/trit_tryte.c new file mode 100755 index 0000000..ef3d51a --- /dev/null +++ b/Sources/cpow/trit_tryte.c @@ -0,0 +1,49 @@ +#include + +#include "include/trit_tryte.h" + +#define NUMBER_OF_TRITS_IN_A_TRYTE 3 +#define TRYTE_SPACE 27 +#define TRYTE_STRING "9ABCDEFGHIJKLMNOPQRSTUVWXYZ" +#define TRITS_TO_TRYTES_MAP \ + {0, 0, 0}, {1, 0, 0}, {-1, 1, 0}, {0, 1, 0}, {1, 1, 0}, {-1, -1, 1}, \ + {0, -1, 1}, {1, -1, 1}, {-1, 0, 1}, {0, 0, 1}, {1, 0, 1}, {-1, 1, 1}, \ + {0, 1, 1}, {1, 1, 1}, {-1, -1, -1}, {0, -1, -1}, {1, -1, -1}, \ + {-1, 0, -1}, {0, 0, -1}, {1, 0, -1}, {-1, 1, -1}, {0, 1, -1}, \ + {1, 1, -1}, {-1, -1, 0}, {0, -1, 0}, {1, -1, 0}, { \ + -1, 0, 0 \ + } + +const trit_t TRYTE_TO_TRITS_MAPPINGS[TRYTE_SPACE][NUMBER_OF_TRITS_IN_A_TRYTE] = + {TRITS_TO_TRYTES_MAP}; + +void trits_to_trytes(trit_t *trits, tryte_t *trytes, size_t length) { + int j = 0, + end = length < NUMBER_OF_TRITS_IN_A_TRYTE ? length + : NUMBER_OF_TRITS_IN_A_TRYTE, + i = end; + for (; i-- > 0;) { + j *= 3; + j += trits[i]; + } + // int j = trits[0] + trits[1] * 3 + trits[2] * 9; + if (j < 0) { + j += TRYTE_SPACE; + } + trytes[0] = TRYTE_STRING[(size_t)j]; + if (length <= NUMBER_OF_TRITS_IN_A_TRYTE) { + return; + } + trits_to_trytes(&trits[NUMBER_OF_TRITS_IN_A_TRYTE], &trytes[1], + length - NUMBER_OF_TRITS_IN_A_TRYTE); +} + +void trytes_to_trits(tryte_t *tryte, trit_t *trits, size_t length) { + if (length == 0) { + return; + } + memcpy(trits, + TRYTE_TO_TRITS_MAPPINGS[strchr(TRYTE_STRING, tryte[0]) - TRYTE_STRING], + NUMBER_OF_TRITS_IN_A_TRYTE * sizeof(trit_t)); + trytes_to_trits(&tryte[1], &trits[NUMBER_OF_TRITS_IN_A_TRYTE], length - 1); +} diff --git a/Tests/IotaKitTests/IotaKitTests.swift b/Tests/IotaKitTests/IotaKitTests.swift index 07be94d..b0eadbb 100644 --- a/Tests/IotaKitTests/IotaKitTests.swift +++ b/Tests/IotaKitTests/IotaKitTests.swift @@ -15,10 +15,17 @@ class IotaKitTests: XCTestCase { let input = "JUSTANOTHERJOTATEST99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ADQYBMQBOCGWQTAVXI9HYKPMMYKHTRHXMQOJFVGYTY9CZUZVQXAIFVZXZXLSOOOQKVORXZITSNGHCDJYD999999999999999999999999999JOUASPAM9999999999999999999QAG9RYD99999999999999999999FWVYCWPSHHZIW9AFIXUXLEHXVHAZABIUD9HVKF9URQUBFYJIECOQWFAYZQ9JTBJRRHIIT9EOYVGGDVDN9NITNCLECVUGELFBVGS9WGOKHYMVJUAYZBBDUHWTT9TXJBOLO9BLFQNRGTQTVBDJCHXAPQHGZGTPHZ9999HZZGWFPZSYCAEWTVTC9IJWVWO9EEWKTEUTVCFGN9SFNVBDIPKPGON9ICNLLRIEMHKGWYPPDLWAEVZ9999JOUASPAM9999999999999999999SAQHBPZJE999999999MMMMMMMMM999999999999999999999999999" let p = PearlDiver() let trits = IotaConverter.trits(fromString: input) - let tritsResult = p.search(transactionTrits: trits, minWeightMagnitude: 14, numberOfThreads: 4) + let tritsResult = p.search(transactionTrits: trits, minWeightMagnitude: 14, numberOfThreads: 7) let result = IotaConverter.trytes(trits: tritsResult) XCTAssertEqual(result, "JUSTANOTHERJOTATEST99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ADQYBMQBOCGWQTAVXI9HYKPMMYKHTRHXMQOJFVGYTY9CZUZVQXAIFVZXZXLSOOOQKVORXZITSNGHCDJYD999999999999999999999999999JOUASPAM9999999999999999999QAG9RYD99999999999999999999FWVYCWPSHHZIW9AFIXUXLEHXVHAZABIUD9HVKF9URQUBFYJIECOQWFAYZQ9JTBJRRHIIT9EOYVGGDVDN9NITNCLECVUGELFBVGS9WGOKHYMVJUAYZBBDUHWTT9TXJBOLO9BLFQNRGTQTVBDJCHXAPQHGZGTPHZ9999HZZGWFPZSYCAEWTVTC9IJWVWO9EEWKTEUTVCFGN9SFNVBDIPKPGON9ICNLLRIEMHKGWYPPDLWAEVZ9999JOUASPAM9999999999999999999SAQHBPZJE999999999MMMMMMMMMUUPYPSSRUNVNROAGIVNGKZNVKVT") } + + func testCPoW() { + let input = "JUSTANOTHERJOTATEST99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ADQYBMQBOCGWQTAVXI9HYKPMMYKHTRHXMQOJFVGYTY9CZUZVQXAIFVZXZXLSOOOQKVORXZITSNGHCDJYD999999999999999999999999999JOUASPAM9999999999999999999QAG9RYD99999999999999999999FWVYCWPSHHZIW9AFIXUXLEHXVHAZABIUD9HVKF9URQUBFYJIECOQWFAYZQ9JTBJRRHIIT9EOYVGGDVDN9NITNCLECVUGELFBVGS9WGOKHYMVJUAYZBBDUHWTT9TXJBOLO9BLFQNRGTQTVBDJCHXAPQHGZGTPHZ9999HZZGWFPZSYCAEWTVTC9IJWVWO9EEWKTEUTVCFGN9SFNVBDIPKPGON9ICNLLRIEMHKGWYPPDLWAEVZ9999JOUASPAM9999999999999999999SAQHBPZJE999999999MMMMMMMMM999999999999999999999999999" + let p = CPearlDiver() + let result = p.performPoW(trytes: input, minWeightMagnitude: 14) + XCTAssertEqual(result, "JUSTANOTHERJOTATEST99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999ADQYBMQBOCGWQTAVXI9HYKPMMYKHTRHXMQOJFVGYTY9CZUZVQXAIFVZXZXLSOOOQKVORXZITSNGHCDJYD999999999999999999999999999JOUASPAM9999999999999999999QAG9RYD99999999999999999999FWVYCWPSHHZIW9AFIXUXLEHXVHAZABIUD9HVKF9URQUBFYJIECOQWFAYZQ9JTBJRRHIIT9EOYVGGDVDN9NITNCLECVUGELFBVGS9WGOKHYMVJUAYZBBDUHWTT9TXJBOLO9BLFQNRGTQTVBDJCHXAPQHGZGTPHZ9999HZZGWFPZSYCAEWTVTC9IJWVWO9EEWKTEUTVCFGN9SFNVBDIPKPGON9ICNLLRIEMHKGWYPPDLWAEVZ9999JOUASPAM9999999999999999999SAQHBPZJE999999999MMMMMMMMMFUPYPSSRUKUNROAGIVX9H9NVKVT") + } static var allTests = [