@pbnjam/bandersnatch 0.7.2-rc0 → 0.7.2-rc1
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/LICENSE +3 -0
- package/package.json +5 -5
- package/src/curve.ts +21 -21
- package/src/index.ts +4 -4
- package/src/math.ts +12 -13
- package/src/types.ts +2 -3
package/LICENSE
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pbnjam/bandersnatch",
|
|
3
|
-
"version": "0.7.2-
|
|
3
|
+
"version": "0.7.2-rc1",
|
|
4
4
|
"description": "Bandersnatch elliptic curve implementation",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -19,13 +19,13 @@
|
|
|
19
19
|
"docs": "npx typedoc --out docs --entryPoints src/index.ts --entryPointStrategy expand --readme none --name '@pbnjam/bandersnatch' --includeVersion --tsconfig tsconfig.typedoc.json"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@noble/curves": "
|
|
23
|
-
"@noble/hashes": "
|
|
22
|
+
"@noble/curves": "2.0.1",
|
|
23
|
+
"@noble/hashes": "2.0.1"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@types/bun": "
|
|
26
|
+
"@types/bun": "1.3.5",
|
|
27
27
|
"@types/node": "^22.0.0",
|
|
28
|
-
"bun-types": "
|
|
28
|
+
"bun-types": "1.3.5",
|
|
29
29
|
"typescript": "5.9.3",
|
|
30
30
|
"typedoc": "0.28.15"
|
|
31
31
|
},
|
package/src/curve.ts
CHANGED
|
@@ -43,12 +43,12 @@ export const Bandersnatch = edwards(BANDERSNATCH_CURVE)
|
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Bandersnatch curve operations using @noble/curves.
|
|
46
|
-
*
|
|
46
|
+
*
|
|
47
47
|
* This class provides a high-level interface for working with points on the
|
|
48
48
|
* Bandersnatch elliptic curve. It implements all standard elliptic curve operations
|
|
49
49
|
* including point addition, scalar multiplication, and point compression/decompression
|
|
50
50
|
* compatible with arkworks serialization format.
|
|
51
|
-
*
|
|
51
|
+
*
|
|
52
52
|
* The Bandersnatch curve is a Twisted Edwards curve defined over the BLS12-381 scalar field,
|
|
53
53
|
* designed for efficient cryptographic operations in the JAM protocol.
|
|
54
54
|
*/
|
|
@@ -92,7 +92,7 @@ export class BandersnatchCurve {
|
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
94
|
* Decompresses arkworks-compatible point bytes to a Noble EdwardsPoint.
|
|
95
|
-
*
|
|
95
|
+
*
|
|
96
96
|
* This is the inverse operation of `pointToBytes`. It handles arkworks sign bit logic
|
|
97
97
|
* to reconstruct the full point from compressed bytes. The method:
|
|
98
98
|
* 1. Extracts the y-coordinate from the first 31 bytes (little-endian)
|
|
@@ -193,12 +193,12 @@ export class BandersnatchCurve {
|
|
|
193
193
|
|
|
194
194
|
/**
|
|
195
195
|
* Performs scalar multiplication on a curve point.
|
|
196
|
-
*
|
|
196
|
+
*
|
|
197
197
|
* Computes `scalar * point` on the Bandersnatch curve. Handles edge cases including:
|
|
198
198
|
* - Scalar 0: returns the identity point (infinity)
|
|
199
199
|
* - Negative scalars: negates the point and uses positive scalar
|
|
200
200
|
* - Scalars >= curve order: reduces modulo curve order
|
|
201
|
-
*
|
|
201
|
+
*
|
|
202
202
|
* @param point - The curve point to multiply
|
|
203
203
|
* @param scalar - The scalar multiplier (can be negative or >= curve order)
|
|
204
204
|
* @returns The result of scalar multiplication: `scalar * point`
|
|
@@ -234,10 +234,10 @@ export class BandersnatchCurve {
|
|
|
234
234
|
|
|
235
235
|
/**
|
|
236
236
|
* Adds two curve points together.
|
|
237
|
-
*
|
|
237
|
+
*
|
|
238
238
|
* Performs point addition on the Bandersnatch curve: `P + Q`.
|
|
239
239
|
* This operation is commutative: `add(P, Q) === add(Q, P)`.
|
|
240
|
-
*
|
|
240
|
+
*
|
|
241
241
|
* @param p1 - First curve point
|
|
242
242
|
* @param p2 - Second curve point
|
|
243
243
|
* @returns The sum of the two points: `p1 + p2`
|
|
@@ -249,10 +249,10 @@ export class BandersnatchCurve {
|
|
|
249
249
|
|
|
250
250
|
/**
|
|
251
251
|
* Doubles a curve point.
|
|
252
|
-
*
|
|
252
|
+
*
|
|
253
253
|
* Computes `2 * point` on the Bandersnatch curve. This is equivalent to
|
|
254
254
|
* `add(point, point)` but is typically more efficient.
|
|
255
|
-
*
|
|
255
|
+
*
|
|
256
256
|
* @param point - The curve point to double
|
|
257
257
|
* @returns The doubled point: `2 * point`
|
|
258
258
|
* @throws {Error} If the point is invalid or not on the curve
|
|
@@ -263,10 +263,10 @@ export class BandersnatchCurve {
|
|
|
263
263
|
|
|
264
264
|
/**
|
|
265
265
|
* Negates a curve point.
|
|
266
|
-
*
|
|
266
|
+
*
|
|
267
267
|
* Computes the additive inverse of a point on the Bandersnatch curve.
|
|
268
268
|
* The result satisfies: `add(point, negate(point)) === INFINITY`.
|
|
269
|
-
*
|
|
269
|
+
*
|
|
270
270
|
* @param point - The curve point to negate
|
|
271
271
|
* @returns The negated point: `-point`
|
|
272
272
|
* @throws {Error} If the point is invalid or not on the curve
|
|
@@ -277,10 +277,10 @@ export class BandersnatchCurve {
|
|
|
277
277
|
|
|
278
278
|
/**
|
|
279
279
|
* Checks if a point lies on the Bandersnatch curve.
|
|
280
|
-
*
|
|
280
|
+
*
|
|
281
281
|
* Validates that the point satisfies the Twisted Edwards curve equation:
|
|
282
282
|
* `a*x^2 + y^2 = 1 + d*x^2*y^2` where `a = -5` and `d` is the curve parameter.
|
|
283
|
-
*
|
|
283
|
+
*
|
|
284
284
|
* @param point - The curve point to validate
|
|
285
285
|
* @returns `true` if the point is on the curve, `false` otherwise
|
|
286
286
|
*/
|
|
@@ -290,10 +290,10 @@ export class BandersnatchCurve {
|
|
|
290
290
|
|
|
291
291
|
/**
|
|
292
292
|
* Gets the generator point (base point) of the Bandersnatch curve.
|
|
293
|
-
*
|
|
293
|
+
*
|
|
294
294
|
* The generator is a point on the curve that generates the prime subgroup G.
|
|
295
295
|
* All points in the prime subgroup can be expressed as scalar multiples of the generator.
|
|
296
|
-
*
|
|
296
|
+
*
|
|
297
297
|
* @returns The generator point G
|
|
298
298
|
*/
|
|
299
299
|
static get GENERATOR() {
|
|
@@ -302,10 +302,10 @@ export class BandersnatchCurve {
|
|
|
302
302
|
|
|
303
303
|
/**
|
|
304
304
|
* Gets the identity point (point at infinity) of the Bandersnatch curve.
|
|
305
|
-
*
|
|
305
|
+
*
|
|
306
306
|
* The identity point is the neutral element for point addition:
|
|
307
307
|
* `add(point, INFINITY) === point` for any point on the curve.
|
|
308
|
-
*
|
|
308
|
+
*
|
|
309
309
|
* @returns The identity point (point at infinity)
|
|
310
310
|
*/
|
|
311
311
|
static get INFINITY() {
|
|
@@ -314,11 +314,11 @@ export class BandersnatchCurve {
|
|
|
314
314
|
|
|
315
315
|
/**
|
|
316
316
|
* Converts a curve point to its byte representation.
|
|
317
|
-
*
|
|
317
|
+
*
|
|
318
318
|
* Serializes the point to bytes, typically used for challenge generation
|
|
319
319
|
* in cryptographic protocols. The output format matches the point compression
|
|
320
320
|
* format used by the curve implementation.
|
|
321
|
-
*
|
|
321
|
+
*
|
|
322
322
|
* @param point - The curve point to hash
|
|
323
323
|
* @returns Byte representation of the point
|
|
324
324
|
* @throws {Error} If the point is invalid
|
|
@@ -329,10 +329,10 @@ export class BandersnatchCurve {
|
|
|
329
329
|
|
|
330
330
|
/**
|
|
331
331
|
* Gets the order (cardinality) of the prime subgroup of the Bandersnatch curve.
|
|
332
|
-
*
|
|
332
|
+
*
|
|
333
333
|
* The curve order is the number of points in the prime subgroup G.
|
|
334
334
|
* For any point P in G, `scalarMultiply(P, CURVE_ORDER) === INFINITY`.
|
|
335
|
-
*
|
|
335
|
+
*
|
|
336
336
|
* @returns The curve order as a bigint
|
|
337
337
|
*/
|
|
338
338
|
static get CURVE_ORDER() {
|
package/src/index.ts
CHANGED
|
@@ -5,16 +5,16 @@
|
|
|
5
5
|
* with all necessary operations for cryptographic applications.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
// Export types
|
|
9
|
-
export type { CurvePoint } from './types'
|
|
10
8
|
// Export curve parameters
|
|
11
9
|
export { BANDERSNATCH_PARAMS } from './config'
|
|
12
10
|
// Elligator2 hash-to-curve is now available in bandersnatch-vrf package
|
|
13
11
|
// Export curve implementations
|
|
14
12
|
// Temporary alias for legacy compatibility
|
|
15
13
|
export {
|
|
16
|
-
BandersnatchCurve,
|
|
17
14
|
Bandersnatch,
|
|
15
|
+
BandersnatchCurve,
|
|
18
16
|
} from './curve'
|
|
19
17
|
// VRF functionality moved to bandersnatch-vrf package
|
|
20
|
-
export * from './math'
|
|
18
|
+
export * from './math'
|
|
19
|
+
// Export types
|
|
20
|
+
export type { CurvePoint } from './types'
|
package/src/math.ts
CHANGED
|
@@ -10,10 +10,10 @@ import {
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Computes the non-negative modular reduction.
|
|
13
|
-
*
|
|
13
|
+
*
|
|
14
14
|
* Returns `a mod m` where the result is always non-negative (0 <= result < m).
|
|
15
15
|
* This handles negative inputs correctly by adding the modulus to negative results.
|
|
16
|
-
*
|
|
16
|
+
*
|
|
17
17
|
* @param a - The value to reduce
|
|
18
18
|
* @param m - The modulus
|
|
19
19
|
* @returns Non-negative result of `a mod m`
|
|
@@ -25,10 +25,10 @@ export function mod(a: bigint, m: bigint): bigint {
|
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* Computes the modular inverse using the extended Euclidean algorithm.
|
|
28
|
-
*
|
|
28
|
+
*
|
|
29
29
|
* Finds the value `x` such that `(a * x) mod m = 1`. The modular inverse exists
|
|
30
30
|
* if and only if `gcd(a, m) = 1`.
|
|
31
|
-
*
|
|
31
|
+
*
|
|
32
32
|
* @param a - The value to find the inverse of
|
|
33
33
|
* @param m - The modulus
|
|
34
34
|
* @returns The modular inverse of `a` modulo `m`
|
|
@@ -53,10 +53,10 @@ export function modInverse(a: bigint, m: bigint): bigint {
|
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
* Computes the modular square root using the noble package FpSqrt.
|
|
56
|
-
*
|
|
56
|
+
*
|
|
57
57
|
* Finds a value `x` such that `x^2 mod p = value`. The square root exists
|
|
58
58
|
* if and only if `value` is a quadratic residue modulo `p`.
|
|
59
|
-
*
|
|
59
|
+
*
|
|
60
60
|
* @param value - The value to find the square root of
|
|
61
61
|
* @param p - The prime modulus
|
|
62
62
|
* @param field - The field implementation from @noble/curves
|
|
@@ -81,11 +81,10 @@ export function modSqrt(
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
export function numberToBytesLittleEndian(value: bigint): Uint8Array {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
return bytes
|
|
84
|
+
const bytes = new Uint8Array(32)
|
|
85
|
+
const hex = value.toString(16).padStart(64, '0')
|
|
86
|
+
for (let i = 0; i < 32; i++) {
|
|
87
|
+
bytes[i] = Number.parseInt(hex.slice(62 - i * 2, 64 - i * 2), 16)
|
|
90
88
|
}
|
|
91
|
-
|
|
89
|
+
return bytes
|
|
90
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Represents a point on an elliptic curve.
|
|
7
|
-
*
|
|
7
|
+
*
|
|
8
8
|
* This interface defines the structure for curve points used in Bandersnatch
|
|
9
9
|
* curve operations. Points can be either finite points with (x, y) coordinates
|
|
10
10
|
* or the point at infinity.
|
|
11
|
-
*
|
|
11
|
+
*
|
|
12
12
|
* @interface CurvePoint
|
|
13
13
|
*/
|
|
14
14
|
export interface CurvePoint {
|
|
@@ -19,4 +19,3 @@ export interface CurvePoint {
|
|
|
19
19
|
/** Whether this point is the point at infinity (identity element) */
|
|
20
20
|
isInfinity: boolean
|
|
21
21
|
}
|
|
22
|
-
|