Skip to content

Commit

Permalink
add squareRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Oct 24, 2024
1 parent b528b15 commit fbbbae8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ EMCC_OPT+=-DCYBOZU_MINIMUM_EXCEPTION
EMCC_OPT+=-s ABORTING_MALLOC=0
EMCC_OPT+=-s STACK_SIZE=1MB
EMCC_OPT+=-sEXPORTED_FUNCTIONS=_malloc,_free,stackAlloc,stackSave,stackRestore
#EMCC_OPT+=-msimd128
EMCC_OPT+=-flto

all: $(MCL_JS)
Expand Down
25 changes: 25 additions & 0 deletions src/value-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ abstract class Common {
return z
}

/** @internal r = func(y, this) and return (true, b) if r = true else (false, null) */
_squareRoot (func: (yPos: number, xPos: number) => number): [boolean, any] {
const y = new (this.constructor as any)()
const stack = mod.stackSave()
const xPos = this._sallocAndCopy()
const yPos = y._salloc()
const r = func(yPos, xPos)
y._save(yPos)
mod.stackRestore(stack)
return r === 0 ? [true, y] : [false, null]
}

/** @internal devide Uint32Array a into n and chose the idx-th */
_getSubArray (idx: number, n: number): Uint32Array {
const d = this.a_.length / n
Expand Down Expand Up @@ -753,6 +765,19 @@ export const inv = <T extends Fp | Fr | GT | Fp2>(x: T): T => {
throw new Error('inv:bad type')
}

export const squareRoot = <T extends Fp | Fr | Fp2>(x: T): [boolean, T] => {
if (x instanceof Fp) {
return x._squareRoot(mod._mclBnFp_squareRoot)
}
if (x instanceof Fr) {
return x._squareRoot(mod._mclBnFr_squareRoot)
}
if (x instanceof Fp2) {
return x._squareRoot(mod._mclBnFp2_squareRoot)
}
throw new Error('squareRoot:bad type')
}

export const normalize = <T extends G1 | G2>(x: T): T => {
if (x instanceof G1) {
return x._op1(mod._mclBnG1_normalize)
Expand Down
6 changes: 6 additions & 0 deletions test/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ function benchAll () {
bench('Fp::mul', C2, () => { b = mcl.mul(b, a) })
bench('Fp::sqr', C2, () => { b = mcl.sqr(b) })
bench('Fp::inv', C2, () => { b = mcl.inv(b) })
b = mcl.sqr(b)
bench('Fp::squareRoot', C, () => { mcl.squareRoot(b) })
}
{
const a = new mcl.Fp2()
Expand All @@ -636,6 +638,8 @@ function benchAll () {
bench('Fp2::mul', C2, () => { b = mcl.mul(b, a) })
bench('Fp2::sqr', C2, () => { b = mcl.sqr(b) })
bench('Fp2::inv', C2, () => { b = mcl.inv(b) })
b = mcl.sqr(b)
bench('Fp2::squareRoot', C, () => { mcl.squareRoot(b) })
}
{
let b = new mcl.Fr()
Expand All @@ -646,6 +650,8 @@ function benchAll () {
bench('Fr::mul', C2, () => { b = mcl.mul(b, a) })
bench('Fr::sqr', C2, () => { b = mcl.sqr(b) })
bench('Fr::inv', C2, () => { b = mcl.inv(b) })
b = mcl.sqr(b)
bench('Fr::squareRoot', C, () => { mcl.squareRoot(b) })
}

{
Expand Down
26 changes: 26 additions & 0 deletions test/test-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,29 @@ function invVecTest(cstr: any): void {
// bench('invVecTime', 1000, () => { w = mcl.invVec(w) })
}

function squareRootTest1 (x, expectBool = undefined): boolean {
const [t, y] = mcl.squareRoot(x)
if (expectBool !== undefined) {
assert(t === expectBool)
}
if (t) {
assert(mcl.sqr(y).isEqual(x))
} else {
assert(y === null)
}
return t
}

function squareRootTest(x: any): void {
const n = 100
let d = x.clone()
for (let i = 0; i < n; i++) {
const dd = mcl.sqr(d)
squareRootTest1(dd, true)
d = dd
}
}

function FrTest() {
const a = new mcl.Fr()
a.setInt(5)
Expand Down Expand Up @@ -189,6 +212,7 @@ function FrTest() {
}
powTest(mcl.Fr)
invVecTest(mcl.Fr)
squareRootTest(a)
}

function FpTest() {
Expand Down Expand Up @@ -237,6 +261,7 @@ function FpTest() {
}
powTest(mcl.Fp)
invVecTest(mcl.Fp)
squareRootTest(a)
}

function Fp2Test() {
Expand Down Expand Up @@ -307,6 +332,7 @@ function Fp2Test() {
a.setInt(-5)
z.set_b(mcl.div(a, b))
assert(mcl.inv(x).isEqual(z))
squareRootTest(a)
}

function normalizeVecTest(cstr: any): void {
Expand Down

0 comments on commit fbbbae8

Please sign in to comment.