arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/aux/calculate2roots.js
aux/calculate2roots.jsRaw
 1
 2const bigInt = require("../src/bigint.js");
 3const ZqField = require("../src/zqfield.js");
 4
 5
 6const r = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
 7const s = 28;
 8const nqr_to_t = bigInt("19103219067921713944291392827692070036145651957329286315305642004821462161904");
 9const t_minus_1_over_2 = bigInt("40770029410420498293352137776570907027550720424234931066070132305055");
10const root_unity = bigInt("19103219067921713944291392827692070036145651957329286315305642004821462161904");
11const t = bigInt("81540058820840996586704275553141814055101440848469862132140264610111");
12
13const F = new ZqField(r);
14
15function sqrt(a) {
16
17    let v = s;
18    let z = nqr_to_t;
19    let w = F.exp(a, t_minus_1_over_2);
20    let x = F.mul(a, w);
21    let b = F.mul(x, w);
22
23
24    // compute square root with Tonelli--Shanks
25    // (does not terminate if not a square!)
26
27    while (!F.equals(b, F.one))
28    {
29        let m = 0;
30        let b2m = b;
31        while (!F.equals(b2m, F.one))
32        {
33            /* invariant: b2m = b^(2^m) after entering this loop */
34            b2m = F.square(b2m);
35            m += 1;
36        }
37
38        let j = v-m-1;
39        w = z;
40        while (j > 0)
41        {
42            w = F.square(w);
43            --j;
44        } // w = z^2^(v-m-1)
45
46        z = F.square(w);
47        b = F.mul(b, z);
48        x = F.mul(x, w);
49        v = m;
50    }
51
52    return x;
53}
54
55const p_minus1= F.sub(r,bigInt(1));
56const gen = bigInt(bigInt(5));
57const twoto28= F.exp(bigInt(2), bigInt(28));
58const rem = F.div(p_minus1, twoto28);
59const w28 = F.exp(gen, rem);
60
61const one = F.exp(w28, twoto28);
62
63
64console.log(F.toString(w28));
65console.log(w28.toString(10));
66console.log(F.toString(one));