Skip to content

Commit

Permalink
add tests of invVec and normalizeVec
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Sep 13, 2024
1 parent 97a52c9 commit 3f4c4d0
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions test/test-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ function powTest(Fcstr: any): void {
}
}

function invVecTest(cstr: any): void {
console.log('invVecTest')
const n = 10
const x = Array(n)
const y = Array(n)
x[0] = new cstr()
x[0].setStr('1232353525205982904')
for (let i = 1; i < n; i++) {
x[i] = mcl.sqr(x[i-1])
}
for (let i = 0; i < n; i++) {
y[i] = new cstr()
y[i].setStr(x[i].getStr())
}
mcl.invVec(x)
for (let i = 0; i < n; i++) {
if (x[i].isZero()) {
assert(y[i].isZero())
} else {
const t = mcl.mul(x[i], y[i])
assert(t.isOne())
}
}
}

function FrTest() {
const a = new mcl.Fr()
a.setInt(5)
Expand Down Expand Up @@ -160,6 +185,7 @@ function FrTest() {
assert(mcl.add(a, b).isEqual(c))
}
powTest(mcl.Fr)
invVecTest(mcl.Fr)
}

function FpTest() {
Expand Down Expand Up @@ -207,6 +233,7 @@ function FpTest() {
assert(mcl.add(a, b).isEqual(c))
}
powTest(mcl.Fp)
invVecTest(mcl.Fp)
}

function Fp2Test() {
Expand Down Expand Up @@ -279,6 +306,32 @@ function Fp2Test() {
assert(mcl.inv(x).isEqual(z))
}

function normalizeVecTest(cstr: any): void {
console.log('normalizeVecTest')
const n = 10
const P = Array(n)
const Q = Array(n)
P[0] = new cstr()
P[0].setHashOf('abc')
for (let i = 1; i < n; i++) {
P[i] = mcl.dbl(P[i-1])
}
P[n/2].clear()
for (let i = 0; i < n; i++) {
Q[i] = new cstr()
Q[i].setStr(P[i].getStr())
}
mcl.normalizeVec(P)
for (let i = 0; i < n; i++) {
assert(P[i].getZ().isZero() || P[i].getZ().isOne())
if (P[i].getZ().isZero()) {
assert(Q[i].isZero())
} else {
assert(P[i].isEqual(Q[i]))
}
}
}

function G1Test() {
const P = new mcl.G1()
assert(P.isZero())
Expand Down Expand Up @@ -314,6 +367,7 @@ function G1Test() {
R4.setZ(R1.getZ())
assert(R4.isValid())
assert(R1.isEqual(R4))
normalizeVecTest(mcl.G1)
}

function G2Test() {
Expand Down Expand Up @@ -351,6 +405,7 @@ function G2Test() {
R4.setZ(R1.getZ())
assert(R4.isValid())
assert(R1.isEqual(R4))
normalizeVecTest(mcl.G2)
}

function GTTest() {
Expand Down

0 comments on commit 3f4c4d0

Please sign in to comment.