@pbnjam/bandersnatch 0.7.2-rc3 → 0.8.0
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/package.json +1 -1
- package/src/__tests__/curve.test.ts +4 -4
- package/src/curve.ts +4 -4
- package/src/glv.ts +11 -16
- package/src/index.ts +5 -5
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { describe, expect, test } from 'bun:test'
|
|
2
|
-
import { Bandersnatch, BandersnatchCurve } from '../curve'
|
|
3
|
-
import { BANDERSNATCH_PARAMS } from '../config'
|
|
4
|
-
import { endomorphism, glvMultiply, scalarDecomposition } from '../glv'
|
|
5
|
-
import { mod } from '../math'
|
|
2
|
+
import { Bandersnatch, BandersnatchCurve } from '../curve.ts'
|
|
3
|
+
import { BANDERSNATCH_PARAMS } from '../config.ts'
|
|
4
|
+
import { endomorphism, glvMultiply, scalarDecomposition } from '../glv.ts'
|
|
5
|
+
import { mod } from '../math.ts'
|
|
6
6
|
|
|
7
7
|
describe('BandersnatchCurve Operations', () => {
|
|
8
8
|
test('Point addition: P + Q = Q + P (commutativity)', () => {
|
package/src/curve.ts
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
|
|
8
8
|
import { type EdwardsPoint, edwards } from '@noble/curves/abstract/edwards.js'
|
|
9
9
|
import { Field } from '@noble/curves/abstract/modular.js'
|
|
10
|
-
import { BANDERSNATCH_PARAMS } from './config'
|
|
11
|
-
import { glvMultiply } from './glv'
|
|
12
|
-
import { mod, modInverse, modSqrt } from './math'
|
|
10
|
+
import { BANDERSNATCH_PARAMS } from './config.ts'
|
|
11
|
+
import { glvMultiply } from './glv.ts'
|
|
12
|
+
import { mod, modInverse, modSqrt } from './math.ts'
|
|
13
13
|
|
|
14
14
|
// Elligator2 hash-to-curve moved to bandersnatch-vrf package
|
|
15
15
|
|
|
@@ -232,7 +232,7 @@ export class BandersnatchCurve {
|
|
|
232
232
|
}
|
|
233
233
|
|
|
234
234
|
if (scalar < 0n) {
|
|
235
|
-
const positiveScalar =
|
|
235
|
+
const positiveScalar = -scalar % BANDERSNATCH_PARAMS.CURVE_ORDER
|
|
236
236
|
if (positiveScalar === 0n) return Bandersnatch.ZERO
|
|
237
237
|
return point.negate().multiply(positiveScalar)
|
|
238
238
|
}
|
package/src/glv.ts
CHANGED
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import type { EdwardsPoint } from '@noble/curves/abstract/edwards.js'
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import { mod, modInverse } from './math'
|
|
17
|
+
import { BANDERSNATCH_PARAMS } from './config.ts'
|
|
18
|
+
import { Bandersnatch } from './curve.ts'
|
|
19
|
+
import { mod, modInverse } from './math.ts'
|
|
20
20
|
|
|
21
21
|
const P = BANDERSNATCH_PARAMS.FIELD_MODULUS
|
|
22
22
|
const R = BANDERSNATCH_PARAMS.CURVE_ORDER
|
|
@@ -32,7 +32,8 @@ const R = BANDERSNATCH_PARAMS.CURVE_ORDER
|
|
|
32
32
|
* transferred to TE coordinates.
|
|
33
33
|
*/
|
|
34
34
|
const AY4 = 0x1d46e71b2d28e06c42bc1f5a41f4a0156d070863689e8862eb12927f72f308c3n
|
|
35
|
-
const AY2Z2 =
|
|
35
|
+
const AY2Z2 =
|
|
36
|
+
0x20b21e58881722d68c92fa09709ea65d716e869843e94c821df033483694a51an
|
|
36
37
|
const AZ4 = 0x1373fe65dcb354e5209f902de5b37008d6c2721d8d6d5fb556e8b7e969c053c9n
|
|
37
38
|
const BY2 = 0x33937d60e9a0dd55ed1f9030e7c8b6fa9c42e1e41d2f1361a0fed9630f711caen
|
|
38
39
|
const BZ2 = 0x39a33e54438fe0155ae18e93205d4395acfe4be1127ca6458fc0270450b1b50dn
|
|
@@ -54,7 +55,7 @@ const M3 = 21482638764116277775478679919733259912n
|
|
|
54
55
|
function floorDiv(a: bigint, b: bigint): bigint {
|
|
55
56
|
const q = a / b
|
|
56
57
|
const r = a % b
|
|
57
|
-
if (r !== 0n &&
|
|
58
|
+
if (r !== 0n && r < 0n !== b < 0n) {
|
|
58
59
|
return q - 1n
|
|
59
60
|
}
|
|
60
61
|
return q
|
|
@@ -97,10 +98,7 @@ export function endomorphism(point: EdwardsPoint): EdwardsPoint {
|
|
|
97
98
|
const y2 = mod(y * y, P)
|
|
98
99
|
const y4 = mod(y2 * y2, P)
|
|
99
100
|
|
|
100
|
-
const xNumerator = mod(
|
|
101
|
-
x * mod(AY4 * y4 + AY2Z2 * y2 + AZ4, P),
|
|
102
|
-
P,
|
|
103
|
-
)
|
|
101
|
+
const xNumerator = mod(x * mod(AY4 * y4 + AY2Z2 * y2 + AZ4, P), P)
|
|
104
102
|
|
|
105
103
|
const denominator = mod(CY2 * y2 + CZ2, P)
|
|
106
104
|
const denominatorInv = modInverse(denominator, P)
|
|
@@ -129,7 +127,7 @@ export function scalarDecomposition(
|
|
|
129
127
|
const b1 = floorDiv(k * M2, R)
|
|
130
128
|
|
|
131
129
|
const s0 = k - b0 * M1 - b1 * M3
|
|
132
|
-
const s1 = -(b0 * M2 + b1 *
|
|
130
|
+
const s1 = -(b0 * M2 + b1 * -M1)
|
|
133
131
|
|
|
134
132
|
return [s0 >= 0n, abs(s0), s1 >= 0n, abs(s1)]
|
|
135
133
|
}
|
|
@@ -147,10 +145,7 @@ export function scalarDecomposition(
|
|
|
147
145
|
* @param point - The curve point P (must be in the prime-order subgroup)
|
|
148
146
|
* @param scalar - The scalar k (must be in range [1, r-1] after reduction)
|
|
149
147
|
*/
|
|
150
|
-
export function glvMultiply(
|
|
151
|
-
point: EdwardsPoint,
|
|
152
|
-
scalar: bigint,
|
|
153
|
-
): EdwardsPoint {
|
|
148
|
+
export function glvMultiply(point: EdwardsPoint, scalar: bigint): EdwardsPoint {
|
|
154
149
|
if (scalar === 0n) {
|
|
155
150
|
return Bandersnatch.ZERO
|
|
156
151
|
}
|
|
@@ -169,8 +164,8 @@ export function glvMultiply(
|
|
|
169
164
|
return Bandersnatch.ZERO
|
|
170
165
|
}
|
|
171
166
|
|
|
172
|
-
|
|
173
|
-
|
|
167
|
+
const b1 = sign1 ? point : point.negate()
|
|
168
|
+
const b2 = sign2 ? endomorphism(point) : endomorphism(point).negate()
|
|
174
169
|
|
|
175
170
|
if (absK1 === 0n) {
|
|
176
171
|
return absK2 === 1n ? b2 : b2.multiply(absK2)
|
package/src/index.ts
CHANGED
|
@@ -6,15 +6,15 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
// Export curve parameters
|
|
9
|
-
export { BANDERSNATCH_PARAMS } from './config'
|
|
9
|
+
export { BANDERSNATCH_PARAMS } from './config.ts'
|
|
10
10
|
// Export curve implementations
|
|
11
11
|
export {
|
|
12
12
|
Bandersnatch,
|
|
13
13
|
BandersnatchCurve,
|
|
14
|
-
} from './curve'
|
|
14
|
+
} from './curve.ts'
|
|
15
15
|
// Export GLV endomorphism
|
|
16
|
-
export { endomorphism, glvMultiply, scalarDecomposition } from './glv'
|
|
16
|
+
export { endomorphism, glvMultiply, scalarDecomposition } from './glv.ts'
|
|
17
17
|
// Export math utilities
|
|
18
|
-
export * from './math'
|
|
18
|
+
export * from './math.ts'
|
|
19
19
|
// Export types
|
|
20
|
-
export type { CurvePoint } from './types'
|
|
20
|
+
export type { CurvePoint } from './types.ts'
|