@andybry/crypto-testing
Version:
Utilities for expermenting with cryptography
132 lines (116 loc) • 3.01 kB
JavaScript
const { assert } = require('chai')
const curve = require('../../shared/curve')
// curve used in the tests is y^2 = x^3 + 2 * x + 3
// and the underlying field is Z17
const A = 2n
const B = 3n
const p = 17n
// for reference the following are all the points on the curve
// (excluding ZERO)
const pointsByX = {
0: [],
1: [],
2: [
[ ],
[ ],
],
3: [
[ ],
[ ],
],
4: [],
5: [
[ ],
[ ],
],
6: [],
7: [],
8: [
[ ],
[ ],
],
9: [
[ ],
[ ],
],
10: [],
11: [
[ ],
[ ],
],
12: [
[ ],
[ ],
],
13: [
[ ],
[ ],
],
14: [
[ ],
[ ],
],
15: [
[ ],
[ ],
],
16: [[16n, 0]],
}
describe('shared/curve', () => {
describe('lhs', () => {
it('should calculate y^2', () => {
assert.equal(curve.lhs(p, 8n), 13n)
})
})
describe('rhs', () => {
it('should calculate x^3 + A * x + B', () => {
assert.equal(curve.rhs(p, A, B, 2n), 15n)
})
})
describe('isOnCurve', () => {
it('should return true when the point is on the curve', () => {
assert.isTrue(curve.isOnCurve(p, A, B, [13n, 4n]))
})
it('should return false when the point is not on the curve', () => {
assert.isFalse(curve.isOnCurve(p, A, B, [13n, 1n]))
})
})
describe('add', () => {
it('should add two points on the curve', () => {
assert.deepEqual(curve.add(p, A, [2n, 10n], [12n, 15n]), [16n, 0n])
})
it('should return first point if second is ZERO', () => {
assert.deepEqual(curve.add(p, A, [2n, 10n], curve.ZERO), [2n, 10n])
})
it('should return second point if first is ZERO', () => {
assert.deepEqual(curve.add(p, A, curve.ZERO, [12n, 15n]), [12n, 15n])
})
})
describe('multiplyBy', () => {
it('should return ZERO if the multiplier is zero', () => {
assert.deepEqual(curve.multiplyBy(p, A, [2n, 10n], 0n), curve.ZERO)
})
it('should return the result of adding the point to iself k times', () => {
assert.deepEqual(curve.multiplyBy(p, A, [2n, 10n], 18n), [15n, 5n])
})
it('should return the ZERO if the point is ZERO', () => {
assert.deepEqual(curve.multiplyBy(p, A, curve.ZERO, 17n), curve.ZERO)
})
})
describe('pointsByX', () => {
it('should return the empty array if there are no points for this x', () => {
assert.deepEqual(curve.pointsByX(p, A, B, 1n), [])
})
it('should return both points even first if there are points', () => {
assert.deepEqual(curve.pointsByX(p, A, B, 2n), [
[ ],
[ ],
])
})
it('should return the single point if y is zero', () => {
assert.deepEqual(curve.pointsByX(p, A, B, 16n), [
[ ]
])
})
})
})