2024-10-12 17:12:41 +01:00
|
|
|
import * as chai from "chai";
|
2022-08-31 12:43:44 -07:00
|
|
|
import * as ScalarN from "../src/scalar.js";
|
|
|
|
import * as utilsN from "../src/utils.js";
|
2020-04-23 13:29:17 +02:00
|
|
|
|
2024-10-12 17:12:41 +01:00
|
|
|
const assert = chai.assert;
|
|
|
|
|
2020-04-23 13:29:17 +02:00
|
|
|
describe("Utils native", () => {
|
2022-06-08 15:37:12 -07:00
|
|
|
const num = ScalarN.e("21888242871839275222246405745257275088614511777268538073601725287587578984328");
|
2020-04-23 13:29:17 +02:00
|
|
|
|
|
|
|
it("Should convert integer to buffer little-endian", () => {
|
|
|
|
const buff = utilsN.leInt2Buff(num, 32);
|
|
|
|
const numFromBuff = utilsN.leBuff2int(buff);
|
2020-07-07 19:21:41 +02:00
|
|
|
|
2020-04-23 13:29:17 +02:00
|
|
|
assert(ScalarN.eq(num, numFromBuff), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should convert integer to buffer big-endian", () => {
|
|
|
|
const buff = utilsN.beInt2Buff(num, 32);
|
|
|
|
const numFromBuff = utilsN.beBuff2int(buff);
|
2020-07-07 19:21:41 +02:00
|
|
|
|
2020-04-23 13:29:17 +02:00
|
|
|
assert(ScalarN.eq(num, numFromBuff), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should stringify bigInt", () => {
|
|
|
|
const str = utilsN.stringifyBigInts(num);
|
|
|
|
const numFromStr = utilsN.unstringifyBigInts(str);
|
2020-07-07 19:21:41 +02:00
|
|
|
|
2020-04-23 13:29:17 +02:00
|
|
|
assert(ScalarN.eq(num, numFromStr), true);
|
|
|
|
});
|
2022-10-22 00:06:08 +08:00
|
|
|
|
|
|
|
it("Should generate buffer little-endian without trailing non-zero element", () => {
|
|
|
|
for (let i = 1; i < 33; i++) {
|
|
|
|
var buff = utilsN.leInt2Buff(BigInt(42), i);
|
|
|
|
for (let t = 1; t < buff.length; t++){
|
|
|
|
assert(buff[t] === 0, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should generate buffer big-endian without trailing non-zero element", () => {
|
|
|
|
for (let i = 1; i < 33; i++) {
|
|
|
|
var buff = utilsN.beInt2Buff(BigInt(42), i);
|
|
|
|
for (let t = 0; t < buff.length - 1; t++){
|
|
|
|
assert(buff[t] === 0, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-08-31 12:43:44 -07:00
|
|
|
});
|