2020-03-28 21:25:06 +01:00
|
|
|
/*
|
|
|
|
Copyright 2018 0kims association.
|
|
|
|
|
|
|
|
This file is part of zksnark JavaScript library.
|
|
|
|
|
|
|
|
zksnark JavaScript library is a free software: you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
zksnark JavaScript library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
zksnark JavaScript library. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-10-12 17:12:41 +01:00
|
|
|
import * as chai from "chai";
|
2020-03-28 21:25:06 +01:00
|
|
|
|
2020-07-07 19:21:41 +02:00
|
|
|
import * as Scalar from "../src/scalar.js";
|
|
|
|
import ZqField from "../src/f1field.js";
|
|
|
|
import RatField from "../src/ratfield.js";
|
2020-03-28 21:25:06 +01:00
|
|
|
|
2020-04-18 20:07:29 +02:00
|
|
|
const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
2020-03-28 21:25:06 +01:00
|
|
|
const Z = new ZqField(q);
|
|
|
|
const R = new RatField(Z);
|
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
|
|
|
function r(a,b) {
|
2020-04-18 20:07:29 +02:00
|
|
|
return [Z.e(a), Z.e(b)];
|
2020-03-28 21:25:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
describe("Rational zq Field", () => {
|
|
|
|
it("Should compare correctly", () => {
|
|
|
|
assert( R.eq(r(3,5), r(6,10)));
|
|
|
|
assert(!R.eq(r(3,5), r(6,11)));
|
|
|
|
});
|
|
|
|
it("Should add correctly", () => {
|
|
|
|
const a = r(7,4);
|
|
|
|
const b = r(5,12);
|
|
|
|
|
|
|
|
assert(R.eq( R.add(a,b), r(13, 6)));
|
|
|
|
});
|
|
|
|
it("Should substract", () => {
|
|
|
|
const a = r(7,4);
|
|
|
|
const b = r(5,12);
|
|
|
|
|
|
|
|
assert(R.eq( R.sub(a,b), r(4, 3)));
|
|
|
|
});
|
|
|
|
it("Should multiply", () => {
|
|
|
|
const a = r(7,4);
|
|
|
|
const b = r(5,12);
|
|
|
|
|
|
|
|
assert(R.eq( R.mul(a,b), r(35, 48)));
|
|
|
|
});
|
|
|
|
it("Should div", () => {
|
|
|
|
const a = r(7,4);
|
|
|
|
const b = r(5,12);
|
|
|
|
|
|
|
|
assert(R.eq( R.div(a,b), r(7*12, 5*4)));
|
|
|
|
});
|
|
|
|
it("Should square", () => {
|
|
|
|
const a = r(7,4);
|
|
|
|
|
|
|
|
assert(R.eq( R.square(a), r(49, 16)));
|
|
|
|
});
|
|
|
|
it("Should affine", () => {
|
|
|
|
const a = r(12,4);
|
|
|
|
const aa = R.affine(a);
|
2020-04-18 20:07:29 +02:00
|
|
|
assert(Z.eq( aa[0], Z.e(3)));
|
2020-03-28 21:25:06 +01:00
|
|
|
assert(Z.eq( aa[1], Z.one));
|
|
|
|
});
|
|
|
|
it("Should convert from Z to R", () => {
|
2020-04-18 20:07:29 +02:00
|
|
|
const vz = Z.e(34);
|
2020-03-28 21:25:06 +01:00
|
|
|
const vr = R.fromF(vz);
|
|
|
|
|
|
|
|
assert(R.eq( vr, r(34,1)));
|
|
|
|
});
|
|
|
|
it("Should convert from R to Z", () => {
|
|
|
|
const vr = r(32, 2);
|
|
|
|
const vz = R.toF(vr);
|
|
|
|
|
2020-04-18 20:07:29 +02:00
|
|
|
assert(Z.eq( vz, Z.e(16)));
|
2020-03-28 21:25:06 +01:00
|
|
|
});
|
|
|
|
});
|