@metoncash/sdk-v1 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -0
- package/dist/constants.d.ts +13 -0
- package/dist/constants.js +128 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +41 -0
- package/dist/jubjub.d.ts +25 -0
- package/dist/jubjub.js +121 -0
- package/dist/mimc.d.ts +4 -0
- package/dist/mimc.js +21 -0
- package/dist/note.d.ts +38 -0
- package/dist/note.js +55 -0
- package/dist/prover.d.ts +79 -0
- package/dist/prover.js +144 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +10 -0
- package/dist/utils/keys.d.ts +23 -0
- package/dist/utils/keys.js +28 -0
- package/dist/utils/note.d.ts +30 -0
- package/dist/utils/note.js +27 -0
- package/dist/wrappers/Account.gen.d.ts +542 -0
- package/dist/wrappers/Account.gen.js +677 -0
- package/dist/wrappers/Factory.gen.d.ts +265 -0
- package/dist/wrappers/Factory.gen.js +401 -0
- package/dist/wrappers/JettonMinter.gen.d.ts +288 -0
- package/dist/wrappers/JettonMinter.gen.js +432 -0
- package/dist/wrappers/JettonWallet.gen.d.ts +308 -0
- package/dist/wrappers/JettonWallet.gen.js +429 -0
- package/dist/wrappers/Minter.gen.d.ts +511 -0
- package/dist/wrappers/Minter.gen.js +700 -0
- package/dist/wrappers/Wallet.gen.d.ts +588 -0
- package/dist/wrappers/Wallet.gen.js +720 -0
- package/dist/wrappers/index.d.ts +15 -0
- package/dist/wrappers/index.js +21 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Meton SDK
|
|
2
|
+
|
|
3
|
+
SDK to build **Groth16 proofs** for the Meton `deposit` / `withdraw` / `transfer`
|
|
4
|
+
circuits, plus the curve/commitment/encrypted-note helpers needed to construct
|
|
5
|
+
inputs and manage wallet state off-chain.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @metoncash/sdk-v1
|
|
11
|
+
# peer runtime dep used for proving:
|
|
12
|
+
npm install snarkjs
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { MetonSDK, deriveViewKey, randomScalar } from "meton-cash/sdk-v1";
|
|
19
|
+
|
|
20
|
+
// Point at a build directory laid out as <dir>/<c>/<c>_js/<c>.wasm + <dir>/<c>/<c>_final.zkey
|
|
21
|
+
const sdk = MetonSDK.fromBuildDir("./build");
|
|
22
|
+
|
|
23
|
+
// …or pass artifacts explicitly (paths or Uint8Array buffers):
|
|
24
|
+
// const sdk = new MetonSDK({
|
|
25
|
+
// deposit: { wasm, zkey },
|
|
26
|
+
// withdraw: { wasm, zkey },
|
|
27
|
+
// transfer: { wasm, zkey },
|
|
28
|
+
// });
|
|
29
|
+
|
|
30
|
+
// DEPOSIT — commit a public amount
|
|
31
|
+
const dep = await sdk.deposit({ amount: 1000n, accountId: 7n, seqNo: 1n });
|
|
32
|
+
// → { proof, publicSignals, commitment: [x,y], blinding } (persist `blinding`)
|
|
33
|
+
|
|
34
|
+
// WITHDRAW — prove balance ≥ amount
|
|
35
|
+
const w = await sdk.withdraw({
|
|
36
|
+
amount: 400n, balance: 1000n, oldBlind, accountId: 7n, seqNo: 2n,
|
|
37
|
+
});
|
|
38
|
+
// → { proof, publicSignals, newCommitment, remainder, newBlind }
|
|
39
|
+
|
|
40
|
+
// TRANSFER — hidden amount, encrypted to the receiver's VIEWING key
|
|
41
|
+
const recv = deriveViewKey(receiverSpendSecret); // recipient side
|
|
42
|
+
const t = await sdk.transfer({
|
|
43
|
+
amount: 300n, senderBalance: 1000n, senderOldBlind,
|
|
44
|
+
receiverViewPk: recv.pkView, accountId: 7n, seqNo: 3n,
|
|
45
|
+
});
|
|
46
|
+
// → { proof, publicSignals, newSenderCommitment, amountCommitment, note,
|
|
47
|
+
// remainder, senderNewBlind, amountBlind, ephScalar }
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`proof` + `publicSignals` are the snarkjs Groth16 outputs. Every result also returns the **secrets you must persist** (blindings,
|
|
51
|
+
note) to keep your local opening of the commitment spendable.
|
|
52
|
+
|
|
53
|
+
## Receiving / auditing
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { viewNote } from "meton-cash/sdk-v1";
|
|
57
|
+
|
|
58
|
+
// recipient (or a granted auditor) holding sk_view:
|
|
59
|
+
const seen = viewNote(recv.skView, note, amountCommitment[0], amountCommitment[1]);
|
|
60
|
+
// → { amount, amountBlind, valid } (valid: the note opens the on-chain amount_commit)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## What's exported
|
|
64
|
+
|
|
65
|
+
- **Prover:** `MetonSDK` (`deposit`, `withdraw`, `transfer`, `fromBuildDir`).
|
|
66
|
+
- **Keys / notes:** `deriveViewKey`, `derivePub`, `encryptNote`, `decryptNote`, `viewNote`.
|
|
67
|
+
- **Curve / commitments:** `commit`, `add`, `mul`, `mod`, `randomScalar`, `Point`, `Gx/Gy/Hx/Hy`, `r`, `d`, `L`.
|
|
68
|
+
- **Hash:** `mimc5perm`. **Constants:** `CONSTANTS`.
|
|
69
|
+
|
|
70
|
+
## Notes
|
|
71
|
+
|
|
72
|
+
- **Defaults:** `blinding`, `newBlind`, `senderNewBlind`, `ephScalar` default to fresh
|
|
73
|
+
randomness; `amountBlind` defaults to `oldBlind − newBlind (mod L)` (the homomorphic
|
|
74
|
+
relation the contract checks). The **ephemeral scalar must be fresh per transfer** —
|
|
75
|
+
reuse leaks amounts.
|
|
76
|
+
- Proving is CPU-heavy (transfer ≈ 30k constraints); run it off the UI thread / in a worker.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const CONSTANTS: {
|
|
2
|
+
readonly FIELD_ORDER: "52435875175126190479447740508185965837690552500527637822603658699938581184513";
|
|
3
|
+
readonly JUBJUB_A: "52435875175126190479447740508185965837690552500527637822603658699938581184512";
|
|
4
|
+
readonly JUBJUB_D: "19257038036680949359750312669786877991949435402254120286184196891950884077233";
|
|
5
|
+
readonly JUBJUB_L: "6554484396890773809930967563523245729705921265872317281365359162392183254199";
|
|
6
|
+
readonly JUBJUB_L_BITS: 252;
|
|
7
|
+
readonly GEN_Gx: "16883835295067924718820241258397066391974467372332466726257127763991993272033";
|
|
8
|
+
readonly GEN_Gy: "42588447725152514407835596888173212178050306123689774451881833705261369545076";
|
|
9
|
+
readonly GEN_Hx: "14653722800943284758809485580286192618140384108416844868180380591159549741996";
|
|
10
|
+
readonly GEN_Hy: "22457361691483092920063600365440163482248783119914970769616716996937239949217";
|
|
11
|
+
readonly MIMC_ROUNDS: 110;
|
|
12
|
+
readonly MIMC_C: readonly ["0", "21072141750850087383663205409324278169844061365381575277611939071655182312455", "37622882036515677186449363082300624937657621062639280655379771297919884650023", "15274812097476656601136231014142015644668649458816783056809062451554247021020", "8767595931832129140147118748525835374199314854827702346538260492390950457201", "4666328043292326148114258582552468160554388178179366325468263407968972958200", "32559525316041739305460488115858218089905460800305028902197049366621228533371", "40325229684193320545431924344472337434126627729542819419037149349591381066975", "32837126112535126653963378199414514687321665091148426554918645077447949264843", "25421044687323875993473836814276494183067960383591603411014344454297885111633", "47717453806610648745520960930109699696470303124994706835216839555665389322730", "26273160625355459716641178145747978492973118741650702001202299386543591382160", "51460907207940715536869034184790928747327283375411007478344425353940827988545", "20096566792041177016428803963893334487212473370749424653585896608141130797579", "12076038830969381723084035519204527200224283865445802734565783625529930892835", "43890258778541307158755992185589404868205618258791128432763388556403256488855", "30297775381552183772234027371224571299918809188945310102690843661296796809655", "42537817672679666417941047343589240146910212716724844414964302852458230400852", "7527690332948012001751125751348110559012301183562238619168746093580590881361", "20191892666322136807951749167805679040306511226473426101597742424070271809103", "20977549590821093825768184700982586624107298235112227977114198879891743122684", "27890398268344615765107421761578739390835511668219337021773480140254073952782", "25524611405167645437174955531627921539349561981626625092702865154703459942446", "46879565033944728664281252227796265246354633758405172383313206835958010079659", "29205522828954086221363799831503912468991941214893853374450247328999564862801", "18718289953542843490683728915889554109377225123061665821735001583922677053015", "16320180651169456710988600541447803729294211141032696453371327447578320977489", "41171902673930589445837112785377053206493106205839418656965923237609162674764", "7946925350257198829775701615740950146267379536757616212043819192604061264428", "43587172058556699925935837985002885772023354530445989610137665089264259776284", "6469041355578756418094425615737266068772593930438450669712604713741935959182", "20593052106517556803098698564178490768159984069383534632983137308152528377357", "32123614898978859885120496341913470252073538600549827408662421738168314313964", "31508168695690752201822527354303769756615415990417045839472411935372392906211", "9306137552982927830220469663874278004459737415064559687923659428553055147050", "22397225597038491362874618575991101248973382901759815572691355831766617844988", "1178806490892022673833323090308976985596364396566711554750713815758254072690", "41927892285248061432363306955866255305838715628751805028746018006777951603377", "33124782985539413353799984773013910198397627902540972607011609852332985782102", "8264242537959347912165349178037568181867260615047113060972229127549228548365", "30628513715208261973855213255594272164912399943824159064666852998516296199387", "36908760150162183065207198566931506093928926026865456431761710558483243139604", "38405966666827270644729081931103681823557909419620379125173841996270713894525", "48476804552735549433941266089354751638790676556988613986538565158153239239164", "25350178771977046624443438180419724789118651615276799792389214652654255594131", "7134321572191178094579535155557295338852752569276281817179291880310773996379", "44554968635275678940447084180516112940809564137965796587040405539413990634816", "6445862442836628135267244685092848004135769189115032325842639554004961639734", "8303085748935709685422419391867704987907830342668229962821502683082373019919", "9151182633953276628376259900907986711086966332220100784036778204432503911651", "3440877658974229701217515779898385791551793765029800816839378146726230681188", "9131399402386182529267153876830222412275604449939694975394353997182664760962", "29035099552731456390519691974029757503021957713327870969948615305607578130877", "51627100534146822121045598435538747837725002493105620408909127852556949189896", "39335306147391331211842927876952029339548702088679723228841089825203776118570", "23743804129565257267386139474966663581237278890712216590630104321275211212372", "287032936835344599096863063546577768987925419582549568237847424894140467071", "37228936729074779233573714335126141520843047140506021979041605513273035838991", "10309098046018929704151741131983202841869679900765782846924383953058749844821", "45221828053507162707077016391981032487491462903930916761611934183941302317208", "38948866739514671616686537718096956388124650811292354877353174029988293688888", "48932870416327357053546916204214452305460730360130423785696034930235152375139", "16921376837838649018345480077350907211734975869029201951648879778142456936643", "17379396587915935165324112901033406641837424684473773001672990719900168268228", "22876592527433053016044340473864694210094836262754087904683480558957464389035", "21913117512778578678692561447492001661339944596874676506003852226929205970227", "38681481997858898697298306595460439818766750768737856334925237334229489859132", "12025238718346321337090744037083621632450830949716157582041569775023334854094", "31039294197406004726040657094695059147166322590015814910670960581595031988542", "19498556691018005785990511495439695809176328244448954091084338751994675250715", "12843807018578951234069877954029510022541316494402221453774850778562820539037", "15943327644549331765413369573782149260002298189284846926844288935065608710030", "9803947623303216374576640831042570035406680134739084598498568665695989682522", "25741333210784506563755470514365803312623529539722910741093554747224178907168", "44798558478513255601218617071715797254097233317052669792998303897345110209061", "4715457658190842275913699504032331049234413297593889353294005839185180542274", "5810658147356805670312254787219449046883304193212330483208080425390564564068", "36061165646477692255810287754940448907185852426895462324333832220576208332290", "34669877101513817478578140283442517871553990921288039157434172997483881329246", "44951065949768072491385055264862256221991481983018844425917700835730640825877", "23267683346749599263840005271334624278779745307433939275931735649725462300424", "26789411971772149354013982304902937636914102562371517574370328720384182807415", "12096673427569632088402074700270493458661462998061021902156348555471897867931", "46273758524225398472816348123353967333474499677328295599186750176351194789613", "15532830437883878134485498025231323510127608765739530484981986710312225046972", "12348690817411982313891040676912426924358904180842701925104038695880882113204", "47075915545391480708996226682628403780963850012493155355762607172633829777412", "28904327600523304502824964276159846922591882400770038177356843808592521418290", "33663142465530376455407022891062827739033678427293776104705503868743332591997", "9161059607145599762195711292081516181727515715227708040556676925030837703685", "4904765645705232933226495667779443488792761822775881277363753251422411842968", "41599336890579004576517051409400238448212496621451231144538949957805417437112", "2251847377043924878247367391307443412902105898065907402853256639102490095641", "25394854752082723964587362578348727903795781589199087317305361689633750563471", "28784862225153357012161560797779066623166084910404928717145702053861513405269", "50139192946965239604786292644254651010489172995792159838065418700559357162075", "5430896141745505833372661383693947564449992570213083751165245573866706485359", "10028456652100503452315420116708682186940093356447395508204070080884896222269", "17201936194017332515668961485838523458115651063826087206019203062722657888001", "37710995439744348615031191450777582030826783739138996638870205607251527118221", "1409284232735744832092429647844153330266649735273273672334207947696665112375", "5837115092429472174539101355695313141042889507846268627869141583970641609342", "41926464152109552409408565187783234533392898906119526814448992008144737393386", "5227627551549322495316810249612662122620516724964106115518213687188720376589", "4696470924279142087658252792560081637393075998860534126204964030159623056928", "16870065764572024172355103150722558610428047686963550174636535910634732158210", "30634608983506388586852792328702713100553806060980967596791994556869004140861", "49410797512173516450548223976129742679725497892932837598338170674445689707469", "12076099973235753864246427644659675831412326682106139561081021455713042786464", "44386335335795300787517526291724124002557870724134250747000149200359903230213"];
|
|
13
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CONSTANTS = void 0;
|
|
4
|
+
// AUTO-GENERATED by scripts/generate_constants.ts - DO NOT EDIT (mirror of test/helpers/constants.json)
|
|
5
|
+
exports.CONSTANTS = {
|
|
6
|
+
"FIELD_ORDER": "52435875175126190479447740508185965837690552500527637822603658699938581184513",
|
|
7
|
+
"JUBJUB_A": "52435875175126190479447740508185965837690552500527637822603658699938581184512",
|
|
8
|
+
"JUBJUB_D": "19257038036680949359750312669786877991949435402254120286184196891950884077233",
|
|
9
|
+
"JUBJUB_L": "6554484396890773809930967563523245729705921265872317281365359162392183254199",
|
|
10
|
+
"JUBJUB_L_BITS": 252,
|
|
11
|
+
"GEN_Gx": "16883835295067924718820241258397066391974467372332466726257127763991993272033",
|
|
12
|
+
"GEN_Gy": "42588447725152514407835596888173212178050306123689774451881833705261369545076",
|
|
13
|
+
"GEN_Hx": "14653722800943284758809485580286192618140384108416844868180380591159549741996",
|
|
14
|
+
"GEN_Hy": "22457361691483092920063600365440163482248783119914970769616716996937239949217",
|
|
15
|
+
"MIMC_ROUNDS": 110,
|
|
16
|
+
"MIMC_C": [
|
|
17
|
+
"0",
|
|
18
|
+
"21072141750850087383663205409324278169844061365381575277611939071655182312455",
|
|
19
|
+
"37622882036515677186449363082300624937657621062639280655379771297919884650023",
|
|
20
|
+
"15274812097476656601136231014142015644668649458816783056809062451554247021020",
|
|
21
|
+
"8767595931832129140147118748525835374199314854827702346538260492390950457201",
|
|
22
|
+
"4666328043292326148114258582552468160554388178179366325468263407968972958200",
|
|
23
|
+
"32559525316041739305460488115858218089905460800305028902197049366621228533371",
|
|
24
|
+
"40325229684193320545431924344472337434126627729542819419037149349591381066975",
|
|
25
|
+
"32837126112535126653963378199414514687321665091148426554918645077447949264843",
|
|
26
|
+
"25421044687323875993473836814276494183067960383591603411014344454297885111633",
|
|
27
|
+
"47717453806610648745520960930109699696470303124994706835216839555665389322730",
|
|
28
|
+
"26273160625355459716641178145747978492973118741650702001202299386543591382160",
|
|
29
|
+
"51460907207940715536869034184790928747327283375411007478344425353940827988545",
|
|
30
|
+
"20096566792041177016428803963893334487212473370749424653585896608141130797579",
|
|
31
|
+
"12076038830969381723084035519204527200224283865445802734565783625529930892835",
|
|
32
|
+
"43890258778541307158755992185589404868205618258791128432763388556403256488855",
|
|
33
|
+
"30297775381552183772234027371224571299918809188945310102690843661296796809655",
|
|
34
|
+
"42537817672679666417941047343589240146910212716724844414964302852458230400852",
|
|
35
|
+
"7527690332948012001751125751348110559012301183562238619168746093580590881361",
|
|
36
|
+
"20191892666322136807951749167805679040306511226473426101597742424070271809103",
|
|
37
|
+
"20977549590821093825768184700982586624107298235112227977114198879891743122684",
|
|
38
|
+
"27890398268344615765107421761578739390835511668219337021773480140254073952782",
|
|
39
|
+
"25524611405167645437174955531627921539349561981626625092702865154703459942446",
|
|
40
|
+
"46879565033944728664281252227796265246354633758405172383313206835958010079659",
|
|
41
|
+
"29205522828954086221363799831503912468991941214893853374450247328999564862801",
|
|
42
|
+
"18718289953542843490683728915889554109377225123061665821735001583922677053015",
|
|
43
|
+
"16320180651169456710988600541447803729294211141032696453371327447578320977489",
|
|
44
|
+
"41171902673930589445837112785377053206493106205839418656965923237609162674764",
|
|
45
|
+
"7946925350257198829775701615740950146267379536757616212043819192604061264428",
|
|
46
|
+
"43587172058556699925935837985002885772023354530445989610137665089264259776284",
|
|
47
|
+
"6469041355578756418094425615737266068772593930438450669712604713741935959182",
|
|
48
|
+
"20593052106517556803098698564178490768159984069383534632983137308152528377357",
|
|
49
|
+
"32123614898978859885120496341913470252073538600549827408662421738168314313964",
|
|
50
|
+
"31508168695690752201822527354303769756615415990417045839472411935372392906211",
|
|
51
|
+
"9306137552982927830220469663874278004459737415064559687923659428553055147050",
|
|
52
|
+
"22397225597038491362874618575991101248973382901759815572691355831766617844988",
|
|
53
|
+
"1178806490892022673833323090308976985596364396566711554750713815758254072690",
|
|
54
|
+
"41927892285248061432363306955866255305838715628751805028746018006777951603377",
|
|
55
|
+
"33124782985539413353799984773013910198397627902540972607011609852332985782102",
|
|
56
|
+
"8264242537959347912165349178037568181867260615047113060972229127549228548365",
|
|
57
|
+
"30628513715208261973855213255594272164912399943824159064666852998516296199387",
|
|
58
|
+
"36908760150162183065207198566931506093928926026865456431761710558483243139604",
|
|
59
|
+
"38405966666827270644729081931103681823557909419620379125173841996270713894525",
|
|
60
|
+
"48476804552735549433941266089354751638790676556988613986538565158153239239164",
|
|
61
|
+
"25350178771977046624443438180419724789118651615276799792389214652654255594131",
|
|
62
|
+
"7134321572191178094579535155557295338852752569276281817179291880310773996379",
|
|
63
|
+
"44554968635275678940447084180516112940809564137965796587040405539413990634816",
|
|
64
|
+
"6445862442836628135267244685092848004135769189115032325842639554004961639734",
|
|
65
|
+
"8303085748935709685422419391867704987907830342668229962821502683082373019919",
|
|
66
|
+
"9151182633953276628376259900907986711086966332220100784036778204432503911651",
|
|
67
|
+
"3440877658974229701217515779898385791551793765029800816839378146726230681188",
|
|
68
|
+
"9131399402386182529267153876830222412275604449939694975394353997182664760962",
|
|
69
|
+
"29035099552731456390519691974029757503021957713327870969948615305607578130877",
|
|
70
|
+
"51627100534146822121045598435538747837725002493105620408909127852556949189896",
|
|
71
|
+
"39335306147391331211842927876952029339548702088679723228841089825203776118570",
|
|
72
|
+
"23743804129565257267386139474966663581237278890712216590630104321275211212372",
|
|
73
|
+
"287032936835344599096863063546577768987925419582549568237847424894140467071",
|
|
74
|
+
"37228936729074779233573714335126141520843047140506021979041605513273035838991",
|
|
75
|
+
"10309098046018929704151741131983202841869679900765782846924383953058749844821",
|
|
76
|
+
"45221828053507162707077016391981032487491462903930916761611934183941302317208",
|
|
77
|
+
"38948866739514671616686537718096956388124650811292354877353174029988293688888",
|
|
78
|
+
"48932870416327357053546916204214452305460730360130423785696034930235152375139",
|
|
79
|
+
"16921376837838649018345480077350907211734975869029201951648879778142456936643",
|
|
80
|
+
"17379396587915935165324112901033406641837424684473773001672990719900168268228",
|
|
81
|
+
"22876592527433053016044340473864694210094836262754087904683480558957464389035",
|
|
82
|
+
"21913117512778578678692561447492001661339944596874676506003852226929205970227",
|
|
83
|
+
"38681481997858898697298306595460439818766750768737856334925237334229489859132",
|
|
84
|
+
"12025238718346321337090744037083621632450830949716157582041569775023334854094",
|
|
85
|
+
"31039294197406004726040657094695059147166322590015814910670960581595031988542",
|
|
86
|
+
"19498556691018005785990511495439695809176328244448954091084338751994675250715",
|
|
87
|
+
"12843807018578951234069877954029510022541316494402221453774850778562820539037",
|
|
88
|
+
"15943327644549331765413369573782149260002298189284846926844288935065608710030",
|
|
89
|
+
"9803947623303216374576640831042570035406680134739084598498568665695989682522",
|
|
90
|
+
"25741333210784506563755470514365803312623529539722910741093554747224178907168",
|
|
91
|
+
"44798558478513255601218617071715797254097233317052669792998303897345110209061",
|
|
92
|
+
"4715457658190842275913699504032331049234413297593889353294005839185180542274",
|
|
93
|
+
"5810658147356805670312254787219449046883304193212330483208080425390564564068",
|
|
94
|
+
"36061165646477692255810287754940448907185852426895462324333832220576208332290",
|
|
95
|
+
"34669877101513817478578140283442517871553990921288039157434172997483881329246",
|
|
96
|
+
"44951065949768072491385055264862256221991481983018844425917700835730640825877",
|
|
97
|
+
"23267683346749599263840005271334624278779745307433939275931735649725462300424",
|
|
98
|
+
"26789411971772149354013982304902937636914102562371517574370328720384182807415",
|
|
99
|
+
"12096673427569632088402074700270493458661462998061021902156348555471897867931",
|
|
100
|
+
"46273758524225398472816348123353967333474499677328295599186750176351194789613",
|
|
101
|
+
"15532830437883878134485498025231323510127608765739530484981986710312225046972",
|
|
102
|
+
"12348690817411982313891040676912426924358904180842701925104038695880882113204",
|
|
103
|
+
"47075915545391480708996226682628403780963850012493155355762607172633829777412",
|
|
104
|
+
"28904327600523304502824964276159846922591882400770038177356843808592521418290",
|
|
105
|
+
"33663142465530376455407022891062827739033678427293776104705503868743332591997",
|
|
106
|
+
"9161059607145599762195711292081516181727515715227708040556676925030837703685",
|
|
107
|
+
"4904765645705232933226495667779443488792761822775881277363753251422411842968",
|
|
108
|
+
"41599336890579004576517051409400238448212496621451231144538949957805417437112",
|
|
109
|
+
"2251847377043924878247367391307443412902105898065907402853256639102490095641",
|
|
110
|
+
"25394854752082723964587362578348727903795781589199087317305361689633750563471",
|
|
111
|
+
"28784862225153357012161560797779066623166084910404928717145702053861513405269",
|
|
112
|
+
"50139192946965239604786292644254651010489172995792159838065418700559357162075",
|
|
113
|
+
"5430896141745505833372661383693947564449992570213083751165245573866706485359",
|
|
114
|
+
"10028456652100503452315420116708682186940093356447395508204070080884896222269",
|
|
115
|
+
"17201936194017332515668961485838523458115651063826087206019203062722657888001",
|
|
116
|
+
"37710995439744348615031191450777582030826783739138996638870205607251527118221",
|
|
117
|
+
"1409284232735744832092429647844153330266649735273273672334207947696665112375",
|
|
118
|
+
"5837115092429472174539101355695313141042889507846268627869141583970641609342",
|
|
119
|
+
"41926464152109552409408565187783234533392898906119526814448992008144737393386",
|
|
120
|
+
"5227627551549322495316810249612662122620516724964106115518213687188720376589",
|
|
121
|
+
"4696470924279142087658252792560081637393075998860534126204964030159623056928",
|
|
122
|
+
"16870065764572024172355103150722558610428047686963550174636535910634732158210",
|
|
123
|
+
"30634608983506388586852792328702713100553806060980967596791994556869004140861",
|
|
124
|
+
"49410797512173516450548223976129742679725497892932837598338170674445689707469",
|
|
125
|
+
"12076099973235753864246427644659675831412326682106139561081021455713042786464",
|
|
126
|
+
"44386335335795300787517526291724124002557870724134250747000149200359903230213"
|
|
127
|
+
]
|
|
128
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { CONSTANTS } from "./constants";
|
|
2
|
+
export { Point, r, d, L, Gx, Gy, Hx, Hy, mod, modpow, modinv, add, mul, commit, derivePub, randomScalar, } from "./jubjub";
|
|
3
|
+
export { mimc5perm } from "./mimc";
|
|
4
|
+
export { Note, deriveViewKey, encryptNote, decryptNote, viewNote } from "./note";
|
|
5
|
+
export { MetonSDK, CircuitArtifacts, MetonConfig, Proof, DepositArgs, WithdrawArgs, TransferArgs, DepositResult, WithdrawResult, TransferResult, } from "./prover";
|
|
6
|
+
export { WalletKeys, generateWalletKeys, walletKeysFromSpend, viewKeyFromSpend, SealNoteArgs, SealedNote, sealNote, openNote, } from "./utils";
|
|
7
|
+
export { Wallet, Minter, Factory } from "./wrappers";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Factory = exports.Minter = exports.Wallet = exports.openNote = exports.sealNote = exports.viewKeyFromSpend = exports.walletKeysFromSpend = exports.generateWalletKeys = exports.MetonSDK = exports.viewNote = exports.decryptNote = exports.encryptNote = exports.deriveViewKey = exports.mimc5perm = exports.randomScalar = exports.derivePub = exports.commit = exports.mul = exports.add = exports.modinv = exports.modpow = exports.mod = exports.Hy = exports.Hx = exports.Gy = exports.Gx = exports.L = exports.d = exports.r = exports.CONSTANTS = void 0;
|
|
4
|
+
var constants_1 = require("./constants");
|
|
5
|
+
Object.defineProperty(exports, "CONSTANTS", { enumerable: true, get: function () { return constants_1.CONSTANTS; } });
|
|
6
|
+
var jubjub_1 = require("./jubjub");
|
|
7
|
+
Object.defineProperty(exports, "r", { enumerable: true, get: function () { return jubjub_1.r; } });
|
|
8
|
+
Object.defineProperty(exports, "d", { enumerable: true, get: function () { return jubjub_1.d; } });
|
|
9
|
+
Object.defineProperty(exports, "L", { enumerable: true, get: function () { return jubjub_1.L; } });
|
|
10
|
+
Object.defineProperty(exports, "Gx", { enumerable: true, get: function () { return jubjub_1.Gx; } });
|
|
11
|
+
Object.defineProperty(exports, "Gy", { enumerable: true, get: function () { return jubjub_1.Gy; } });
|
|
12
|
+
Object.defineProperty(exports, "Hx", { enumerable: true, get: function () { return jubjub_1.Hx; } });
|
|
13
|
+
Object.defineProperty(exports, "Hy", { enumerable: true, get: function () { return jubjub_1.Hy; } });
|
|
14
|
+
Object.defineProperty(exports, "mod", { enumerable: true, get: function () { return jubjub_1.mod; } });
|
|
15
|
+
Object.defineProperty(exports, "modpow", { enumerable: true, get: function () { return jubjub_1.modpow; } });
|
|
16
|
+
Object.defineProperty(exports, "modinv", { enumerable: true, get: function () { return jubjub_1.modinv; } });
|
|
17
|
+
Object.defineProperty(exports, "add", { enumerable: true, get: function () { return jubjub_1.add; } });
|
|
18
|
+
Object.defineProperty(exports, "mul", { enumerable: true, get: function () { return jubjub_1.mul; } });
|
|
19
|
+
Object.defineProperty(exports, "commit", { enumerable: true, get: function () { return jubjub_1.commit; } });
|
|
20
|
+
Object.defineProperty(exports, "derivePub", { enumerable: true, get: function () { return jubjub_1.derivePub; } });
|
|
21
|
+
Object.defineProperty(exports, "randomScalar", { enumerable: true, get: function () { return jubjub_1.randomScalar; } });
|
|
22
|
+
var mimc_1 = require("./mimc");
|
|
23
|
+
Object.defineProperty(exports, "mimc5perm", { enumerable: true, get: function () { return mimc_1.mimc5perm; } });
|
|
24
|
+
var note_1 = require("./note");
|
|
25
|
+
Object.defineProperty(exports, "deriveViewKey", { enumerable: true, get: function () { return note_1.deriveViewKey; } });
|
|
26
|
+
Object.defineProperty(exports, "encryptNote", { enumerable: true, get: function () { return note_1.encryptNote; } });
|
|
27
|
+
Object.defineProperty(exports, "decryptNote", { enumerable: true, get: function () { return note_1.decryptNote; } });
|
|
28
|
+
Object.defineProperty(exports, "viewNote", { enumerable: true, get: function () { return note_1.viewNote; } });
|
|
29
|
+
var prover_1 = require("./prover");
|
|
30
|
+
Object.defineProperty(exports, "MetonSDK", { enumerable: true, get: function () { return prover_1.MetonSDK; } });
|
|
31
|
+
var utils_1 = require("./utils");
|
|
32
|
+
Object.defineProperty(exports, "generateWalletKeys", { enumerable: true, get: function () { return utils_1.generateWalletKeys; } });
|
|
33
|
+
Object.defineProperty(exports, "walletKeysFromSpend", { enumerable: true, get: function () { return utils_1.walletKeysFromSpend; } });
|
|
34
|
+
Object.defineProperty(exports, "viewKeyFromSpend", { enumerable: true, get: function () { return utils_1.viewKeyFromSpend; } });
|
|
35
|
+
Object.defineProperty(exports, "sealNote", { enumerable: true, get: function () { return utils_1.sealNote; } });
|
|
36
|
+
Object.defineProperty(exports, "openNote", { enumerable: true, get: function () { return utils_1.openNote; } });
|
|
37
|
+
// Contract wrappers — deploy/init Wallet, Minter, Factory through the SDK.
|
|
38
|
+
var wrappers_1 = require("./wrappers");
|
|
39
|
+
Object.defineProperty(exports, "Wallet", { enumerable: true, get: function () { return wrappers_1.Wallet; } });
|
|
40
|
+
Object.defineProperty(exports, "Minter", { enumerable: true, get: function () { return wrappers_1.Minter; } });
|
|
41
|
+
Object.defineProperty(exports, "Factory", { enumerable: true, get: function () { return wrappers_1.Factory; } });
|
package/dist/jubjub.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* jubjub.ts — Jubjub twisted-Edwards curve arithmetic (a = -1) over the
|
|
3
|
+
* BLS12-381 scalar field. Mirrors circuits/lib for off-chain input building.
|
|
4
|
+
*/
|
|
5
|
+
export type Point = [bigint, bigint];
|
|
6
|
+
export declare const r: bigint;
|
|
7
|
+
export declare const d: bigint;
|
|
8
|
+
export declare const L: bigint;
|
|
9
|
+
export declare const Gx: bigint;
|
|
10
|
+
export declare const Gy: bigint;
|
|
11
|
+
export declare const Hx: bigint;
|
|
12
|
+
export declare const Hy: bigint;
|
|
13
|
+
export declare const mod: (a: bigint, m?: bigint) => bigint;
|
|
14
|
+
export declare function modpow(base: bigint, exp: bigint, m?: bigint): bigint;
|
|
15
|
+
export declare const modinv: (a: bigint, m?: bigint) => bigint;
|
|
16
|
+
/** Complete twisted-Edwards addition (a = -1). */
|
|
17
|
+
export declare function add(x1: bigint, y1: bigint, x2: bigint, y2: bigint): Point;
|
|
18
|
+
/** Scalar multiplication: out = scalar · (x, y). */
|
|
19
|
+
export declare function mul(x: bigint, y: bigint, s: bigint): Point;
|
|
20
|
+
/** Pedersen commitment: value·G + blinding·H. */
|
|
21
|
+
export declare function commit(value: bigint, blinding: bigint): Point;
|
|
22
|
+
/** Public key for a secret scalar: pk = sk · G. */
|
|
23
|
+
export declare function derivePub(sk: bigint): Point;
|
|
24
|
+
/** Uniform random scalar in [1, 2^maxBits) reduced into the subgroup. */
|
|
25
|
+
export declare function randomScalar(maxBits?: number): bigint;
|
package/dist/jubjub.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* jubjub.ts — Jubjub twisted-Edwards curve arithmetic (a = -1) over the
|
|
4
|
+
* BLS12-381 scalar field. Mirrors circuits/lib for off-chain input building.
|
|
5
|
+
*/
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
+
}) : function(o, v) {
|
|
20
|
+
o["default"] = v;
|
|
21
|
+
});
|
|
22
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
23
|
+
var ownKeys = function(o) {
|
|
24
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
25
|
+
var ar = [];
|
|
26
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
return ownKeys(o);
|
|
30
|
+
};
|
|
31
|
+
return function (mod) {
|
|
32
|
+
if (mod && mod.__esModule) return mod;
|
|
33
|
+
var result = {};
|
|
34
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
35
|
+
__setModuleDefault(result, mod);
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
})();
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
exports.modinv = exports.mod = exports.Hy = exports.Hx = exports.Gy = exports.Gx = exports.L = exports.d = exports.r = void 0;
|
|
41
|
+
exports.modpow = modpow;
|
|
42
|
+
exports.add = add;
|
|
43
|
+
exports.mul = mul;
|
|
44
|
+
exports.commit = commit;
|
|
45
|
+
exports.derivePub = derivePub;
|
|
46
|
+
exports.randomScalar = randomScalar;
|
|
47
|
+
const crypto = __importStar(require("crypto"));
|
|
48
|
+
const constants_1 = require("./constants");
|
|
49
|
+
exports.r = BigInt(constants_1.CONSTANTS.FIELD_ORDER);
|
|
50
|
+
exports.d = BigInt(constants_1.CONSTANTS.JUBJUB_D);
|
|
51
|
+
exports.L = BigInt(constants_1.CONSTANTS.JUBJUB_L);
|
|
52
|
+
exports.Gx = BigInt(constants_1.CONSTANTS.GEN_Gx);
|
|
53
|
+
exports.Gy = BigInt(constants_1.CONSTANTS.GEN_Gy);
|
|
54
|
+
exports.Hx = BigInt(constants_1.CONSTANTS.GEN_Hx);
|
|
55
|
+
exports.Hy = BigInt(constants_1.CONSTANTS.GEN_Hy);
|
|
56
|
+
const mod = (a, m = exports.r) => ((a % m) + m) % m;
|
|
57
|
+
exports.mod = mod;
|
|
58
|
+
function modpow(base, exp, m = exports.r) {
|
|
59
|
+
base = (0, exports.mod)(base, m);
|
|
60
|
+
let result = 1n;
|
|
61
|
+
while (exp > 0n) {
|
|
62
|
+
if (exp & 1n)
|
|
63
|
+
result = (0, exports.mod)(result * base, m);
|
|
64
|
+
base = (0, exports.mod)(base * base, m);
|
|
65
|
+
exp >>= 1n;
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
const modinv = (a, m = exports.r) => modpow((0, exports.mod)(a, m), m - 2n, m);
|
|
70
|
+
exports.modinv = modinv;
|
|
71
|
+
/** Complete twisted-Edwards addition (a = -1). */
|
|
72
|
+
function add(x1, y1, x2, y2) {
|
|
73
|
+
const x1x2 = (0, exports.mod)(x1 * x2);
|
|
74
|
+
const y1y2 = (0, exports.mod)(y1 * y2);
|
|
75
|
+
const x1y2 = (0, exports.mod)(x1 * y2);
|
|
76
|
+
const y1x2 = (0, exports.mod)(y1 * x2);
|
|
77
|
+
const dx = (0, exports.mod)(exports.d * (0, exports.mod)(x1x2 * y1y2));
|
|
78
|
+
const xnum = (0, exports.mod)(x1y2 + y1x2);
|
|
79
|
+
const ynum = (0, exports.mod)(y1y2 + x1x2);
|
|
80
|
+
const xden = (0, exports.mod)(1n + dx);
|
|
81
|
+
const yden = (0, exports.mod)(1n + exports.r - dx);
|
|
82
|
+
return [(0, exports.mod)(xnum * (0, exports.modinv)(xden)), (0, exports.mod)(ynum * (0, exports.modinv)(yden))];
|
|
83
|
+
}
|
|
84
|
+
/** Scalar multiplication: out = scalar · (x, y). */
|
|
85
|
+
function mul(x, y, s) {
|
|
86
|
+
s = (0, exports.mod)(s, exports.L);
|
|
87
|
+
let rx = 0n;
|
|
88
|
+
let ry = 1n;
|
|
89
|
+
let tx = x;
|
|
90
|
+
let ty = y;
|
|
91
|
+
while (s > 0n) {
|
|
92
|
+
if (s & 1n)
|
|
93
|
+
[rx, ry] = add(rx, ry, tx, ty);
|
|
94
|
+
[tx, ty] = add(tx, ty, tx, ty);
|
|
95
|
+
s >>= 1n;
|
|
96
|
+
}
|
|
97
|
+
return [rx, ry];
|
|
98
|
+
}
|
|
99
|
+
/** Pedersen commitment: value·G + blinding·H. */
|
|
100
|
+
function commit(value, blinding) {
|
|
101
|
+
const [ax, ay] = mul(exports.Gx, exports.Gy, value);
|
|
102
|
+
const [bx, by] = mul(exports.Hx, exports.Hy, blinding);
|
|
103
|
+
return add(ax, ay, bx, by);
|
|
104
|
+
}
|
|
105
|
+
/** Public key for a secret scalar: pk = sk · G. */
|
|
106
|
+
function derivePub(sk) {
|
|
107
|
+
return mul(exports.Gx, exports.Gy, sk);
|
|
108
|
+
}
|
|
109
|
+
/** Uniform random scalar in [1, 2^maxBits) reduced into the subgroup. */
|
|
110
|
+
function randomScalar(maxBits = 252) {
|
|
111
|
+
const bytes = crypto.randomBytes(32);
|
|
112
|
+
let s = 0n;
|
|
113
|
+
for (let i = 0; i < 32; i++)
|
|
114
|
+
s = (s << 8n) | BigInt(bytes[i]);
|
|
115
|
+
s = (0, exports.mod)(s, exports.L);
|
|
116
|
+
const mask = (1n << BigInt(maxBits)) - 1n;
|
|
117
|
+
s = s & mask;
|
|
118
|
+
if (s === 0n)
|
|
119
|
+
s = 1n;
|
|
120
|
+
return s;
|
|
121
|
+
}
|
package/dist/mimc.d.ts
ADDED
package/dist/mimc.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* mimc.ts — MiMC-5 permutation over the BLS12-381 scalar field.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.mimc5perm = mimc5perm;
|
|
7
|
+
const constants_1 = require("./constants");
|
|
8
|
+
const jubjub_1 = require("./jubjub");
|
|
9
|
+
const ROUNDS = constants_1.CONSTANTS.MIMC_ROUNDS;
|
|
10
|
+
const RC = constants_1.CONSTANTS.MIMC_C.map((c) => BigInt(c));
|
|
11
|
+
function mimc5perm(input, key) {
|
|
12
|
+
let st = (0, jubjub_1.mod)(input);
|
|
13
|
+
key = (0, jubjub_1.mod)(key);
|
|
14
|
+
for (let i = 0; i < ROUNDS; i++) {
|
|
15
|
+
const t = (0, jubjub_1.mod)(st + key + RC[i]);
|
|
16
|
+
const t2 = (0, jubjub_1.mod)(t * t);
|
|
17
|
+
const t4 = (0, jubjub_1.mod)(t2 * t2);
|
|
18
|
+
st = (0, jubjub_1.mod)(t4 * t);
|
|
19
|
+
}
|
|
20
|
+
return (0, jubjub_1.mod)(st + key);
|
|
21
|
+
}
|
package/dist/note.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* note.ts — ECDH encrypted note + view/spend key derivation.
|
|
3
|
+
*
|
|
4
|
+
* Notes are encrypted to the receiver's VIEWING key (`pk_view`). Holding
|
|
5
|
+
* `sk_view` lets the receiver (or a granted auditor) read amounts but not spend.
|
|
6
|
+
*/
|
|
7
|
+
import { Point } from "./jubjub";
|
|
8
|
+
export interface Note {
|
|
9
|
+
Rx: bigint;
|
|
10
|
+
Ry: bigint;
|
|
11
|
+
enc_amount: bigint;
|
|
12
|
+
enc_blind: bigint;
|
|
13
|
+
eph_scalar: bigint;
|
|
14
|
+
shared_x: bigint;
|
|
15
|
+
}
|
|
16
|
+
/** Derive a viewing keypair one-way from the spend secret. */
|
|
17
|
+
export declare function deriveViewKey(spendSk: bigint): {
|
|
18
|
+
skView: bigint;
|
|
19
|
+
pkView: Point;
|
|
20
|
+
};
|
|
21
|
+
/** Encrypt (amount, amount_blind) to a receiver viewing key with ephemeral k. */
|
|
22
|
+
export declare function encryptNote(amount: bigint, amountBlind: bigint, receiverViewPkX: bigint, receiverViewPkY: bigint, ephScalar: bigint): Note;
|
|
23
|
+
/** Decrypt a note with a viewing secret (sk_view). */
|
|
24
|
+
export declare function decryptNote(skView: bigint, Rx: bigint, Ry: bigint, encAmount: bigint, encBlind: bigint): {
|
|
25
|
+
amount: bigint;
|
|
26
|
+
amountBlind: bigint;
|
|
27
|
+
};
|
|
28
|
+
/** Decrypt AND verify the note opens the on-chain amount_commit point. */
|
|
29
|
+
export declare function viewNote(skView: bigint, note: {
|
|
30
|
+
Rx: bigint;
|
|
31
|
+
Ry: bigint;
|
|
32
|
+
enc_amount: bigint;
|
|
33
|
+
enc_blind: bigint;
|
|
34
|
+
}, amountCommitX: bigint, amountCommitY: bigint): {
|
|
35
|
+
amount: bigint;
|
|
36
|
+
amountBlind: bigint;
|
|
37
|
+
valid: boolean;
|
|
38
|
+
};
|
package/dist/note.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* note.ts — ECDH encrypted note + view/spend key derivation.
|
|
4
|
+
*
|
|
5
|
+
* Notes are encrypted to the receiver's VIEWING key (`pk_view`). Holding
|
|
6
|
+
* `sk_view` lets the receiver (or a granted auditor) read amounts but not spend.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.deriveViewKey = deriveViewKey;
|
|
10
|
+
exports.encryptNote = encryptNote;
|
|
11
|
+
exports.decryptNote = decryptNote;
|
|
12
|
+
exports.viewNote = viewNote;
|
|
13
|
+
const jubjub_1 = require("./jubjub");
|
|
14
|
+
const mimc_1 = require("./mimc");
|
|
15
|
+
const DOMAIN_AMOUNT = 1n; // keystream block for amount
|
|
16
|
+
const DOMAIN_BLIND = 2n; // keystream block for amount_blind
|
|
17
|
+
const DOMAIN_VIEW = 7n; // viewing-key derivation tag
|
|
18
|
+
/** Derive a viewing keypair one-way from the spend secret. */
|
|
19
|
+
function deriveViewKey(spendSk) {
|
|
20
|
+
let skView = (0, jubjub_1.mod)((0, mimc_1.mimc5perm)(DOMAIN_VIEW, (0, jubjub_1.mod)(spendSk)), jubjub_1.L);
|
|
21
|
+
if (skView === 0n)
|
|
22
|
+
skView = 1n;
|
|
23
|
+
return { skView, pkView: (0, jubjub_1.mul)(jubjub_1.Gx, jubjub_1.Gy, skView) };
|
|
24
|
+
}
|
|
25
|
+
/** Encrypt (amount, amount_blind) to a receiver viewing key with ephemeral k. */
|
|
26
|
+
function encryptNote(amount, amountBlind, receiverViewPkX, receiverViewPkY, ephScalar) {
|
|
27
|
+
const [Rx, Ry] = (0, jubjub_1.mul)(jubjub_1.Gx, jubjub_1.Gy, ephScalar); // R = k·G
|
|
28
|
+
const [sx] = (0, jubjub_1.mul)(receiverViewPkX, receiverViewPkY, ephScalar); // shared = k·pk_view
|
|
29
|
+
const maskAmount = (0, mimc_1.mimc5perm)(DOMAIN_AMOUNT, sx);
|
|
30
|
+
const maskBlind = (0, mimc_1.mimc5perm)(DOMAIN_BLIND, sx);
|
|
31
|
+
return {
|
|
32
|
+
Rx,
|
|
33
|
+
Ry,
|
|
34
|
+
enc_amount: (0, jubjub_1.mod)(amount + maskAmount),
|
|
35
|
+
enc_blind: (0, jubjub_1.mod)(amountBlind + maskBlind),
|
|
36
|
+
eph_scalar: ephScalar,
|
|
37
|
+
shared_x: sx,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** Decrypt a note with a viewing secret (sk_view). */
|
|
41
|
+
function decryptNote(skView, Rx, Ry, encAmount, encBlind) {
|
|
42
|
+
const [sx] = (0, jubjub_1.mul)(Rx, Ry, skView); // shared = sk_view·R
|
|
43
|
+
const maskAmount = (0, mimc_1.mimc5perm)(DOMAIN_AMOUNT, sx);
|
|
44
|
+
const maskBlind = (0, mimc_1.mimc5perm)(DOMAIN_BLIND, sx);
|
|
45
|
+
return {
|
|
46
|
+
amount: (0, jubjub_1.mod)(encAmount + jubjub_1.r - maskAmount),
|
|
47
|
+
amountBlind: (0, jubjub_1.mod)(encBlind + jubjub_1.r - maskBlind),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** Decrypt AND verify the note opens the on-chain amount_commit point. */
|
|
51
|
+
function viewNote(skView, note, amountCommitX, amountCommitY) {
|
|
52
|
+
const { amount, amountBlind } = decryptNote(skView, note.Rx, note.Ry, note.enc_amount, note.enc_blind);
|
|
53
|
+
const [cx, cy] = (0, jubjub_1.commit)(amount, amountBlind);
|
|
54
|
+
return { amount, amountBlind, valid: cx === amountCommitX && cy === amountCommitY };
|
|
55
|
+
}
|
package/dist/prover.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* prover.ts — Build Groth16 proofs for the deposit / withdraw / transfer circuits.
|
|
3
|
+
*/
|
|
4
|
+
import { Point } from "./jubjub";
|
|
5
|
+
import { Note } from "./note";
|
|
6
|
+
/** A compiled circuit's artifacts: a wasm witness-calculator and a proving key. */
|
|
7
|
+
export interface CircuitArtifacts {
|
|
8
|
+
wasm: string | Uint8Array;
|
|
9
|
+
zkey: string | Uint8Array;
|
|
10
|
+
}
|
|
11
|
+
export interface MetonConfig {
|
|
12
|
+
deposit: CircuitArtifacts;
|
|
13
|
+
withdraw: CircuitArtifacts;
|
|
14
|
+
transfer: CircuitArtifacts;
|
|
15
|
+
}
|
|
16
|
+
export interface Proof {
|
|
17
|
+
proof: unknown;
|
|
18
|
+
publicSignals: string[];
|
|
19
|
+
}
|
|
20
|
+
export interface DepositArgs {
|
|
21
|
+
amount: bigint;
|
|
22
|
+
accountId: bigint;
|
|
23
|
+
seqNo: bigint;
|
|
24
|
+
blinding?: bigint;
|
|
25
|
+
}
|
|
26
|
+
export interface WithdrawArgs {
|
|
27
|
+
amount: bigint;
|
|
28
|
+
balance: bigint;
|
|
29
|
+
oldBlind: bigint;
|
|
30
|
+
accountId: bigint;
|
|
31
|
+
seqNo: bigint;
|
|
32
|
+
newBlind?: bigint;
|
|
33
|
+
}
|
|
34
|
+
export interface TransferArgs {
|
|
35
|
+
amount: bigint;
|
|
36
|
+
senderBalance: bigint;
|
|
37
|
+
senderOldBlind: bigint;
|
|
38
|
+
receiverViewPk: Point;
|
|
39
|
+
accountId: bigint;
|
|
40
|
+
seqNo: bigint;
|
|
41
|
+
senderNewBlind?: bigint;
|
|
42
|
+
amountBlind?: bigint;
|
|
43
|
+
ephScalar?: bigint;
|
|
44
|
+
}
|
|
45
|
+
export interface DepositResult extends Proof {
|
|
46
|
+
commitment: Point;
|
|
47
|
+
blinding: bigint;
|
|
48
|
+
}
|
|
49
|
+
export interface WithdrawResult extends Proof {
|
|
50
|
+
newCommitment: Point;
|
|
51
|
+
remainder: bigint;
|
|
52
|
+
newBlind: bigint;
|
|
53
|
+
}
|
|
54
|
+
export interface TransferResult extends Proof {
|
|
55
|
+
newSenderCommitment: Point;
|
|
56
|
+
amountCommitment: Point;
|
|
57
|
+
note: Note;
|
|
58
|
+
remainder: bigint;
|
|
59
|
+
senderNewBlind: bigint;
|
|
60
|
+
amountBlind: bigint;
|
|
61
|
+
ephScalar: bigint;
|
|
62
|
+
}
|
|
63
|
+
export declare class MetonSDK {
|
|
64
|
+
private readonly cfg;
|
|
65
|
+
constructor(cfg: MetonConfig);
|
|
66
|
+
/**
|
|
67
|
+
* Convenience: derive artifact paths from a build directory laid out as this
|
|
68
|
+
* repo's `npm run build` / `npm run setup` produce:
|
|
69
|
+
* <dir>/<c>/<c>_js/<c>.wasm and <dir>/<c>/<c>_final.zkey
|
|
70
|
+
*/
|
|
71
|
+
static fromBuildDir(dir: string): MetonSDK;
|
|
72
|
+
private prove;
|
|
73
|
+
/** Deposit a public `amount`, producing a commitment to it. */
|
|
74
|
+
deposit(args: DepositArgs): Promise<DepositResult>;
|
|
75
|
+
/** Withdraw a public `amount`, proving `balance ≥ amount`. */
|
|
76
|
+
withdraw(args: WithdrawArgs): Promise<WithdrawResult>;
|
|
77
|
+
/** Private transfer of a hidden `amount` to a receiver's viewing key. */
|
|
78
|
+
transfer(args: TransferArgs): Promise<TransferResult>;
|
|
79
|
+
}
|