@l.x/hashcash-native 1.0.2 ā 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NitroHashcashNative.podspec +56 -0
- package/README.md +24 -0
- package/android/CMakeLists.txt +29 -0
- package/android/build.gradle +141 -0
- package/android/fix-prefab.gradle +51 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/cpp-adapter.cpp +6 -0
- package/android/src/main/java/com/margelo/nitro/hashcashnative/HybridHashcash.kt +113 -0
- package/android/src/main/java/com/margelo/nitro/hashcashnative/NitroHashcashNativePackage.kt +18 -0
- package/ios/Bridge.h +8 -0
- package/ios/HybridHashcash.swift +133 -0
- package/lib/benchmark.d.ts +74 -0
- package/lib/benchmark.d.ts.map +1 -0
- package/lib/benchmark.js +92 -0
- package/lib/benchmark.js.map +1 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +10 -0
- package/lib/index.js.map +1 -0
- package/lib/specs/Hashcash.nitro.d.ts +59 -0
- package/lib/specs/Hashcash.nitro.d.ts.map +1 -0
- package/lib/specs/Hashcash.nitro.js +2 -0
- package/lib/specs/Hashcash.nitro.js.map +1 -0
- package/nitro.json +18 -0
- package/nitrogen/generated/.gitattributes +1 -0
- package/nitrogen/generated/android/NitroHashcashNative+autolinking.cmake +81 -0
- package/nitrogen/generated/android/NitroHashcashNative+autolinking.gradle +27 -0
- package/nitrogen/generated/android/NitroHashcashNativeOnLoad.cpp +44 -0
- package/nitrogen/generated/android/NitroHashcashNativeOnLoad.hpp +25 -0
- package/nitrogen/generated/android/c++/JFindProofParams.hpp +68 -0
- package/nitrogen/generated/android/c++/JHashcashChallenge.hpp +69 -0
- package/nitrogen/generated/android/c++/JHashcashProofResult.hpp +69 -0
- package/nitrogen/generated/android/c++/JHybridHashcashSpec.cpp +81 -0
- package/nitrogen/generated/android/c++/JHybridHashcashSpec.hpp +66 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/hashcashnative/FindProofParams.kt +44 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/hashcashnative/HashcashChallenge.kt +47 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/hashcashnative/HashcashProofResult.kt +47 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/hashcashnative/HybridHashcashSpec.kt +62 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/hashcashnative/NitroHashcashNativeOnLoad.kt +35 -0
- package/nitrogen/generated/ios/NitroHashcashNative+autolinking.rb +60 -0
- package/nitrogen/generated/ios/NitroHashcashNative-Swift-Cxx-Bridge.cpp +49 -0
- package/nitrogen/generated/ios/NitroHashcashNative-Swift-Cxx-Bridge.hpp +154 -0
- package/nitrogen/generated/ios/NitroHashcashNative-Swift-Cxx-Umbrella.hpp +55 -0
- package/nitrogen/generated/ios/NitroHashcashNativeAutolinking.mm +33 -0
- package/nitrogen/generated/ios/NitroHashcashNativeAutolinking.swift +25 -0
- package/nitrogen/generated/ios/c++/HybridHashcashSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridHashcashSpecSwift.hpp +92 -0
- package/nitrogen/generated/ios/swift/FindProofParams.swift +82 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__optional_HashcashProofResult_.swift +47 -0
- package/nitrogen/generated/ios/swift/HashcashChallenge.swift +69 -0
- package/nitrogen/generated/ios/swift/HashcashProofResult.swift +69 -0
- package/nitrogen/generated/ios/swift/HybridHashcashSpec.swift +57 -0
- package/nitrogen/generated/ios/swift/HybridHashcashSpec_cxx.swift +155 -0
- package/nitrogen/generated/shared/c++/FindProofParams.hpp +85 -0
- package/nitrogen/generated/shared/c++/HashcashChallenge.hpp +87 -0
- package/nitrogen/generated/shared/c++/HashcashProofResult.hpp +87 -0
- package/nitrogen/generated/shared/c++/HybridHashcashSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridHashcashSpec.hpp +69 -0
- package/package.json +73 -1
- package/react-native.config.js +16 -0
- package/src/benchmark.ts +152 -0
- package/src/index.ts +23 -0
- package/src/specs/Hashcash.nitro.ts +64 -0
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/src/benchmark.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hashcash Benchmark Utility
|
|
3
|
+
*
|
|
4
|
+
* Compares native vs JS hashcash implementation performance.
|
|
5
|
+
* Run this in your app to measure the speedup from native code.
|
|
6
|
+
*/
|
|
7
|
+
/** biome-ignore-all lint/suspicious/noConsole: dev only file */
|
|
8
|
+
|
|
9
|
+
import { HashcashNative } from './index'
|
|
10
|
+
|
|
11
|
+
interface BenchmarkResult {
|
|
12
|
+
implementation: 'native' | 'js'
|
|
13
|
+
difficulty: number
|
|
14
|
+
counter: string | null
|
|
15
|
+
attempts: number
|
|
16
|
+
timeMs: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface BenchmarkSummary {
|
|
20
|
+
native: BenchmarkResult
|
|
21
|
+
js: BenchmarkResult
|
|
22
|
+
speedup: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Run a single benchmark with the native implementation
|
|
27
|
+
*/
|
|
28
|
+
export async function benchmarkNative(difficulty: number): Promise<BenchmarkResult> {
|
|
29
|
+
const challenge = {
|
|
30
|
+
difficulty,
|
|
31
|
+
subject: 'Benchmark',
|
|
32
|
+
nonce: 'dGVzdC1ub25jZS1iZW5jaG1hcms=', // "test-nonce-benchmark" in base64
|
|
33
|
+
maxProofLength: 10_000_000,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const startTime = performance.now()
|
|
37
|
+
const result = await HashcashNative.findProof({ challenge })
|
|
38
|
+
const endTime = performance.now()
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
implementation: 'native',
|
|
42
|
+
difficulty,
|
|
43
|
+
counter: result?.counter ?? null,
|
|
44
|
+
attempts: result?.attempts ?? 0,
|
|
45
|
+
timeMs: endTime - startTime,
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Run a single benchmark with the JS implementation
|
|
51
|
+
* Requires passing in the findProof function from @l.x/sessions
|
|
52
|
+
*/
|
|
53
|
+
export async function benchmarkJS(
|
|
54
|
+
difficulty: number,
|
|
55
|
+
findProof: (params: {
|
|
56
|
+
challenge: {
|
|
57
|
+
difficulty: number
|
|
58
|
+
subject: string
|
|
59
|
+
algorithm: 'sha256'
|
|
60
|
+
nonce: string
|
|
61
|
+
max_proof_length: number
|
|
62
|
+
}
|
|
63
|
+
}) => { counter: string; attempts: number; timeMs: number } | null,
|
|
64
|
+
): Promise<BenchmarkResult> {
|
|
65
|
+
const challenge = {
|
|
66
|
+
difficulty,
|
|
67
|
+
subject: 'Benchmark',
|
|
68
|
+
algorithm: 'sha256' as const,
|
|
69
|
+
nonce: 'dGVzdC1ub25jZS1iZW5jaG1hcms=',
|
|
70
|
+
max_proof_length: 10_000_000,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const result = findProof({ challenge })
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
implementation: 'js',
|
|
77
|
+
difficulty,
|
|
78
|
+
counter: result?.counter ?? null,
|
|
79
|
+
attempts: result?.attempts ?? 0,
|
|
80
|
+
timeMs: result?.timeMs ?? 0,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Run a comparison benchmark between native and JS implementations
|
|
86
|
+
*/
|
|
87
|
+
export async function runBenchmark(
|
|
88
|
+
difficulty: number,
|
|
89
|
+
jsFindProof: (params: {
|
|
90
|
+
challenge: {
|
|
91
|
+
difficulty: number
|
|
92
|
+
subject: string
|
|
93
|
+
algorithm: 'sha256'
|
|
94
|
+
nonce: string
|
|
95
|
+
max_proof_length: number
|
|
96
|
+
}
|
|
97
|
+
}) => { counter: string; attempts: number; timeMs: number } | null,
|
|
98
|
+
): Promise<BenchmarkSummary> {
|
|
99
|
+
console.log(`\nš Starting hashcash benchmark (difficulty=${difficulty})...\n`)
|
|
100
|
+
|
|
101
|
+
// Run native first
|
|
102
|
+
console.log('ā±ļø Running native implementation...')
|
|
103
|
+
const nativeResult = await benchmarkNative(difficulty)
|
|
104
|
+
console.log(` Native: ${nativeResult.timeMs.toFixed(2)}ms (${nativeResult.attempts} attempts)`)
|
|
105
|
+
|
|
106
|
+
// Run JS
|
|
107
|
+
console.log('ā±ļø Running JS implementation...')
|
|
108
|
+
const jsResult = await benchmarkJS(difficulty, jsFindProof)
|
|
109
|
+
console.log(` JS: ${jsResult.timeMs.toFixed(2)}ms (${jsResult.attempts} attempts)`)
|
|
110
|
+
|
|
111
|
+
const speedup = jsResult.timeMs / nativeResult.timeMs
|
|
112
|
+
|
|
113
|
+
console.log(`\nš Results:`)
|
|
114
|
+
console.log(` Native: ${nativeResult.timeMs.toFixed(2)}ms`)
|
|
115
|
+
console.log(` JS: ${jsResult.timeMs.toFixed(2)}ms`)
|
|
116
|
+
console.log(` Speedup: ${speedup.toFixed(2)}x faster with native\n`)
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
native: nativeResult,
|
|
120
|
+
js: jsResult,
|
|
121
|
+
speedup,
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Run benchmarks at multiple difficulty levels
|
|
127
|
+
*/
|
|
128
|
+
export async function runFullBenchmark(
|
|
129
|
+
jsFindProof: (params: {
|
|
130
|
+
challenge: {
|
|
131
|
+
difficulty: number
|
|
132
|
+
subject: string
|
|
133
|
+
algorithm: 'sha256'
|
|
134
|
+
nonce: string
|
|
135
|
+
max_proof_length: number
|
|
136
|
+
}
|
|
137
|
+
}) => { counter: string; attempts: number; timeMs: number } | null,
|
|
138
|
+
): Promise<void> {
|
|
139
|
+
console.log('āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā')
|
|
140
|
+
console.log(' HASHCASH NATIVE vs JS BENCHMARK')
|
|
141
|
+
console.log('āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā')
|
|
142
|
+
|
|
143
|
+
const difficulties = [1, 2]
|
|
144
|
+
|
|
145
|
+
for (const difficulty of difficulties) {
|
|
146
|
+
await runBenchmark(difficulty, jsFindProof)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
console.log('āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā')
|
|
150
|
+
console.log(' BENCHMARK COMPLETE')
|
|
151
|
+
console.log('āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā\n')
|
|
152
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { NitroModules } from 'react-native-nitro-modules'
|
|
2
|
+
import type { Hashcash } from './specs/Hashcash.nitro'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Native hashcash proof-of-work solver.
|
|
6
|
+
*
|
|
7
|
+
* Uses platform-native SHA256 implementation for better performance
|
|
8
|
+
* than JavaScript-based hashing.
|
|
9
|
+
*/
|
|
10
|
+
export const HashcashNative = NitroModules.createHybridObject<Hashcash>('Hashcash')
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
benchmarkJS,
|
|
14
|
+
benchmarkNative,
|
|
15
|
+
runBenchmark,
|
|
16
|
+
runFullBenchmark,
|
|
17
|
+
} from './benchmark'
|
|
18
|
+
export type {
|
|
19
|
+
FindProofParams,
|
|
20
|
+
Hashcash,
|
|
21
|
+
HashcashChallenge,
|
|
22
|
+
HashcashProofResult,
|
|
23
|
+
} from './specs/Hashcash.nitro'
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Challenge parameters for hashcash proof-of-work
|
|
5
|
+
*/
|
|
6
|
+
export interface HashcashChallenge {
|
|
7
|
+
/** Number of zero bytes required at start of hash */
|
|
8
|
+
difficulty: number
|
|
9
|
+
/** Subject string (e.g., "Lx") */
|
|
10
|
+
subject: string
|
|
11
|
+
/** Base64-encoded nonce from backend */
|
|
12
|
+
nonce: string
|
|
13
|
+
/** Maximum counter value to try */
|
|
14
|
+
maxProofLength: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Result of a successful proof-of-work computation
|
|
19
|
+
*/
|
|
20
|
+
export interface HashcashProofResult {
|
|
21
|
+
/** The counter value that produced a valid hash */
|
|
22
|
+
counter: string
|
|
23
|
+
/** Base64-encoded SHA256 hash */
|
|
24
|
+
hashBase64: string
|
|
25
|
+
/** Number of hash attempts made */
|
|
26
|
+
attempts: number
|
|
27
|
+
/** Time taken in milliseconds */
|
|
28
|
+
timeMs: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Parameters for findProof
|
|
33
|
+
*/
|
|
34
|
+
export interface FindProofParams {
|
|
35
|
+
challenge: HashcashChallenge
|
|
36
|
+
rangeStart?: number
|
|
37
|
+
rangeSize?: number
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Native hashcash proof-of-work solver.
|
|
42
|
+
*
|
|
43
|
+
* Computes SHA256 hashes to find a counter value that produces
|
|
44
|
+
* a hash with the required number of leading zero bytes.
|
|
45
|
+
*/
|
|
46
|
+
export interface Hashcash
|
|
47
|
+
extends HybridObject<{
|
|
48
|
+
ios: 'swift'
|
|
49
|
+
android: 'kotlin'
|
|
50
|
+
}> {
|
|
51
|
+
/**
|
|
52
|
+
* Find a proof-of-work solution for the given challenge.
|
|
53
|
+
*
|
|
54
|
+
* @param params - Challenge parameters and optional range limits
|
|
55
|
+
* @returns ProofResult if found, undefined if cancelled or no solution in range
|
|
56
|
+
*/
|
|
57
|
+
findProof(params: FindProofParams): Promise<HashcashProofResult | undefined>
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Cancel any in-progress proof search.
|
|
61
|
+
* The current findProof call will return undefined.
|
|
62
|
+
*/
|
|
63
|
+
cancel(): void
|
|
64
|
+
}
|
package/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '@luxexchange/hashcash-native';
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('@luxexchange/hashcash-native');
|