2022-10-03 15:35:35 +00:00
|
|
|
import * as bma from '#pkg'
|
|
|
|
|
2021-03-24 13:04:30 +00:00
|
|
|
describe('modInv', function () {
|
|
|
|
const inputs = [
|
|
|
|
{
|
|
|
|
a: BigInt(1),
|
|
|
|
n: BigInt(19),
|
|
|
|
modInv: BigInt(1)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: BigInt(2),
|
|
|
|
n: BigInt(5),
|
|
|
|
modInv: BigInt(3)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
a: BigInt(-2),
|
|
|
|
n: BigInt(5),
|
|
|
|
modInv: BigInt(2)
|
2021-03-24 17:30:45 +00:00
|
|
|
}]
|
|
|
|
|
|
|
|
const invalidInputs = [
|
2021-03-24 13:04:30 +00:00
|
|
|
{
|
|
|
|
a: BigInt(2),
|
2021-03-24 17:30:45 +00:00
|
|
|
n: BigInt(4)
|
2021-03-24 13:04:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
a: BigInt(0),
|
2021-03-24 17:30:45 +00:00
|
|
|
n: BigInt(0)
|
2021-03-24 13:04:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
a: BigInt(0),
|
2021-03-24 17:30:45 +00:00
|
|
|
n: BigInt(37)
|
2021-03-24 13:04:30 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
for (const input of inputs) {
|
|
|
|
describe(`modInv(${input.a}, ${input.n})`, function () {
|
|
|
|
it(`should return ${input.modInv}`, function () {
|
2022-10-03 15:35:35 +00:00
|
|
|
const ret = bma.modInv(input.a, input.n)
|
2021-03-24 13:04:30 +00:00
|
|
|
// chai.assert( String(ret) === String(input.modInv) );
|
|
|
|
chai.expect(String(ret)).to.be.equal(String(input.modInv))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2021-03-24 17:30:45 +00:00
|
|
|
for (const input of invalidInputs) {
|
|
|
|
describe(`modInv(${input.a}, ${input.n})`, function () {
|
|
|
|
it('should throw RangeError', function () {
|
|
|
|
try {
|
2022-10-03 15:35:35 +00:00
|
|
|
bma.modInv(input.a, input.n)
|
2021-03-24 17:30:45 +00:00
|
|
|
throw new Error('should have failed')
|
|
|
|
} catch (err) {
|
|
|
|
chai.expect(err).to.be.instanceOf(RangeError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2021-03-24 13:04:30 +00:00
|
|
|
})
|