|
| 1 | +const path = require("path"); |
| 2 | + |
| 3 | +const bigInt = require("big-integer"); |
| 4 | +const Scalar = require("ffjavascript").Scalar; |
| 5 | +const tester = require("circom").tester; |
| 6 | + |
| 7 | +const { splitToArray } = require("./util.js"); |
| 8 | + |
| 9 | + |
| 10 | +describe("montgomery reduction 64bits/32words", function () { |
| 11 | + this.timeout(100000); |
| 12 | + |
| 13 | + let circuit; |
| 14 | + before(async () => { |
| 15 | + circuit = await tester(path.join(__dirname, "circuits", "montgomery_64_32.circom")); |
| 16 | + }); |
| 17 | + |
| 18 | + it("64bits/4words. and mod = 52435875175126190479447740508185965837690552500527637822603658699938581184513", async () => { |
| 19 | + |
| 20 | + const modulus = bigInt("27333278531038650284292446400685983964543820405055158402397263907659995327446166369388984969315774410223081038389734916442552953312548988147687296936649645550823280957757266695625382122565413076484125874545818286099364801140117875853249691189224238587206753225612046406534868213180954324992542640955526040556053150097561640564120642863954208763490114707326811013163227280580130702236406906684353048490731840275232065153721031968704703853746667518350717957685569289022049487955447803273805415754478723962939325870164033644600353029240991739641247820015852898600430315191986948597672794286676575642204004244219381500407"); |
| 21 | + |
| 22 | + const m0inv = "12890617734997456953"; |
| 23 | + |
| 24 | + const sign = bigInt("27166015521685750287064830171899789431519297967327068200526003963687696216659347317736779094212876326032375924944649760206771585778103092909024744594654706678288864890801000499430246054971129440518072676833029702477408973737931913964693831642228421821166326489172152903376352031367604507095742732994611253344812562891520292463788291973539285729019102238815435155266782647328690908245946607690372534644849495733662205697837732960032720813567898672483741410294744324300408404611458008868294953357660121510817012895745326996024006347446775298357303082471522757091056219893320485806442481065207020262668955919408138704593"); |
| 25 | + |
| 26 | + // p_A = (2 ** (64 * 32)) % modulus |
| 27 | + const p_A = bigInt("8090504373870994679612627222357908172758809628981258425784309037819139630322144671528165987384165881554828460235243846620448472971597333672100823634983655619029776040825234445735663391689708464300724937302764614255809108270510144580635273488990507929690660037680329790107264933452413195230785359054440855482839030680434847591404383105898217683831262927377177125853366529615844393994132094172487120401275260427990790479346279705147628336449313356516920320620281725278025278479103610090304469418605506553142667456793187284519065351481384226307824293012777494286385249767131477166992943622060450204816237194204381391317"); |
| 28 | + |
| 29 | + const r = bigInt("32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656"); |
| 30 | + // p_a = (a * r) % modulus |
| 31 | + const p_a = sign.multiply(r).mod(modulus); |
| 32 | + |
| 33 | + |
| 34 | + const result = sign.multiply(sign).mod(modulus); |
| 35 | + |
| 36 | + // console.log("result", splitToArray(result, 64, 32)); |
| 37 | + |
| 38 | + var testCases = [{ |
| 39 | + description: "one word", |
| 40 | + input: { |
| 41 | + x: splitToArray(p_a, 64, 32), |
| 42 | + y: splitToArray(sign, 64, 32), |
| 43 | + modulus: splitToArray(modulus, 64, 32), |
| 44 | + m0inv: m0inv, |
| 45 | + }, |
| 46 | + output: { |
| 47 | + out: [ |
| 48 | + "6726338490320059920", |
| 49 | + "10912188918279059842", |
| 50 | + "9480684547274909370", |
| 51 | + "17957855819450938343", |
| 52 | + "14472935173278472155", |
| 53 | + "7079099867353219214", |
| 54 | + "16449430926296343172", |
| 55 | + "5104880238183543706", |
| 56 | + "10665479437761628207", |
| 57 | + "5855523454381758411", |
| 58 | + "10461162232393809384", |
| 59 | + "8506503615129027292", |
| 60 | + "713958493375598634", |
| 61 | + "6870643563889906329", |
| 62 | + "6496643502438963194", |
| 63 | + "3333526337816518494", |
| 64 | + "7059814241034669637", |
| 65 | + "11046528448239496056", |
| 66 | + "8821061847720768545", |
| 67 | + "8804814027843795770", |
| 68 | + "9550990298891609521", |
| 69 | + "4546832636732460115", |
| 70 | + "11005916837189288046", |
| 71 | + "6747247992486122322", |
| 72 | + "8062076261952398748", |
| 73 | + "11274417876012308159", |
| 74 | + "12082695046704314515", |
| 75 | + "9977902230884319566", |
| 76 | + "8117196317861485583", |
| 77 | + "6870310691445046553", |
| 78 | + "15178250919948156997", |
| 79 | + "13075022197409122935"] |
| 80 | + }, |
| 81 | + }] |
| 82 | + |
| 83 | + |
| 84 | + for (var i = 0; i < testCases.length; i++) { |
| 85 | + const witness = await circuit.calculateWitness(testCases[i].input, true); |
| 86 | + |
| 87 | + await circuit.assertOut(witness, testCases[i].output); |
| 88 | + } |
| 89 | + }); |
| 90 | +}); |
0 commit comments