@esseyonsui/sui-sdk-v3 0.3.3 → 0.4.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.
Files changed (129) hide show
  1. package/dist/config.d.ts +3 -0
  2. package/dist/config.d.ts.map +1 -1
  3. package/dist/config.js +1 -0
  4. package/dist/errors.d.ts +3 -1
  5. package/dist/errors.d.ts.map +1 -1
  6. package/dist/errors.js +5 -0
  7. package/dist/essey.d.ts +157 -131
  8. package/dist/essey.d.ts.map +1 -1
  9. package/dist/essey.js +641 -823
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/v3/collateral/bodies.d.ts +23 -14
  14. package/dist/v3/collateral/bodies.d.ts.map +1 -1
  15. package/dist/v3/collateral/bodies.js +42 -35
  16. package/dist/v3/collateral/ext.d.ts +18 -13
  17. package/dist/v3/collateral/ext.d.ts.map +1 -1
  18. package/dist/v3/collateral/ext.js +5 -6
  19. package/dist/v3/collateral/health.d.ts +16 -10
  20. package/dist/v3/collateral/health.d.ts.map +1 -1
  21. package/dist/v3/collateral/health.js +55 -30
  22. package/dist/v3/collateral/math.d.ts +0 -2
  23. package/dist/v3/collateral/math.d.ts.map +1 -1
  24. package/dist/v3/collateral/math.js +0 -5
  25. package/dist/v3/collateral/openings.d.ts +5 -1
  26. package/dist/v3/collateral/openings.d.ts.map +1 -1
  27. package/dist/v3/collateral/openings.js +7 -12
  28. package/dist/v3/credential/membership.d.ts +16 -2
  29. package/dist/v3/credential/membership.d.ts.map +1 -1
  30. package/dist/v3/credential/membership.js +36 -3
  31. package/dist/v3/domains.d.ts +9 -0
  32. package/dist/v3/domains.d.ts.map +1 -1
  33. package/dist/v3/domains.js +5 -0
  34. package/dist/v3/fees.d.ts +13 -1
  35. package/dist/v3/fees.d.ts.map +1 -1
  36. package/dist/v3/fees.js +32 -2
  37. package/dist/v3/funds.d.ts +34 -0
  38. package/dist/v3/funds.d.ts.map +1 -0
  39. package/dist/v3/funds.js +60 -0
  40. package/dist/v3/keeper-payload.d.ts +29 -7
  41. package/dist/v3/keeper-payload.d.ts.map +1 -1
  42. package/dist/v3/keeper-payload.js +33 -22
  43. package/dist/v3/kinds.d.ts +6 -6
  44. package/dist/v3/kinds.d.ts.map +1 -1
  45. package/dist/v3/kinds.js +5 -13
  46. package/dist/v3/loans/bodies.d.ts +40 -68
  47. package/dist/v3/loans/bodies.d.ts.map +1 -1
  48. package/dist/v3/loans/bodies.js +50 -73
  49. package/dist/v3/loans/offer-payload.d.ts +109 -0
  50. package/dist/v3/loans/offer-payload.d.ts.map +1 -0
  51. package/dist/v3/loans/offer-payload.js +207 -0
  52. package/dist/v3/loans/offer-plan.d.ts +8 -60
  53. package/dist/v3/loans/offer-plan.d.ts.map +1 -1
  54. package/dist/v3/loans/offer-plan.js +19 -95
  55. package/dist/v3/loans/open-plan.d.ts +15 -5
  56. package/dist/v3/loans/open-plan.d.ts.map +1 -1
  57. package/dist/v3/loans/open-plan.js +18 -7
  58. package/dist/v3/loans/reclaim-plan.d.ts +31 -15
  59. package/dist/v3/loans/reclaim-plan.d.ts.map +1 -1
  60. package/dist/v3/loans/reclaim-plan.js +63 -32
  61. package/dist/v3/loans/refi-plan.d.ts +8 -34
  62. package/dist/v3/loans/refi-plan.d.ts.map +1 -1
  63. package/dist/v3/loans/refi-plan.js +30 -111
  64. package/dist/v3/loans/repay-plan.d.ts.map +1 -1
  65. package/dist/v3/loans/repay-plan.js +2 -2
  66. package/dist/v3/loans/settle-plan.d.ts +88 -53
  67. package/dist/v3/loans/settle-plan.d.ts.map +1 -1
  68. package/dist/v3/loans/settle-plan.js +141 -150
  69. package/dist/v3/locks.d.ts +21 -0
  70. package/dist/v3/locks.d.ts.map +1 -0
  71. package/dist/v3/locks.js +15 -0
  72. package/dist/v3/memo.d.ts +14 -4
  73. package/dist/v3/memo.d.ts.map +1 -1
  74. package/dist/v3/memo.js +14 -7
  75. package/dist/v3/notes/derive.d.ts +5 -0
  76. package/dist/v3/notes/derive.d.ts.map +1 -1
  77. package/dist/v3/notes/derive.js +5 -0
  78. package/dist/v3/pace.d.ts +0 -1
  79. package/dist/v3/pace.d.ts.map +1 -1
  80. package/dist/v3/pace.js +0 -1
  81. package/dist/v3/pads.d.ts +15 -0
  82. package/dist/v3/pads.d.ts.map +1 -0
  83. package/dist/v3/pads.js +25 -0
  84. package/dist/v3/prover.d.ts +1 -1
  85. package/dist/v3/prover.d.ts.map +1 -1
  86. package/dist/v3/prover.js +1 -0
  87. package/dist/v3/sales/auctioneer.d.ts +32 -27
  88. package/dist/v3/sales/auctioneer.d.ts.map +1 -1
  89. package/dist/v3/sales/auctioneer.js +67 -108
  90. package/dist/v3/sales/bid.d.ts +20 -49
  91. package/dist/v3/sales/bid.d.ts.map +1 -1
  92. package/dist/v3/sales/bid.js +28 -83
  93. package/dist/v3/sales/bodies.d.ts +4 -54
  94. package/dist/v3/sales/bodies.d.ts.map +1 -1
  95. package/dist/v3/sales/bodies.js +10 -52
  96. package/dist/v3/sales/claim.d.ts +0 -2
  97. package/dist/v3/sales/claim.d.ts.map +1 -1
  98. package/dist/v3/sales/claim.js +6 -8
  99. package/dist/v3/sales/index.d.ts +0 -1
  100. package/dist/v3/sales/index.d.ts.map +1 -1
  101. package/dist/v3/sales/index.js +0 -1
  102. package/dist/v3/sales/listing.d.ts +8 -6
  103. package/dist/v3/sales/listing.d.ts.map +1 -1
  104. package/dist/v3/sales/listing.js +27 -9
  105. package/dist/v3/sales/open-kind.d.ts +1 -3
  106. package/dist/v3/sales/open-kind.d.ts.map +1 -1
  107. package/dist/v3/sales/open-kind.js +2 -3
  108. package/dist/v3/sales/outcome.d.ts +5 -8
  109. package/dist/v3/sales/outcome.d.ts.map +1 -1
  110. package/dist/v3/sales/outcome.js +5 -9
  111. package/dist/v3/sales/package-gate.d.ts +3 -4
  112. package/dist/v3/sales/package-gate.d.ts.map +1 -1
  113. package/dist/v3/sales/package-gate.js +9 -7
  114. package/dist/v3/sales/rows.d.ts +9 -49
  115. package/dist/v3/sales/rows.d.ts.map +1 -1
  116. package/dist/v3/sales/rows.js +17 -91
  117. package/dist/v3/sales/seller.d.ts +2 -6
  118. package/dist/v3/sales/seller.d.ts.map +1 -1
  119. package/dist/v3/sales/seller.js +4 -11
  120. package/dist/v3/stage1.d.ts +8 -21
  121. package/dist/v3/stage1.d.ts.map +1 -1
  122. package/dist/v3/stage1.js +8 -76
  123. package/package.json +1 -1
  124. package/dist/v3/loans/lock-round.d.ts +0 -22
  125. package/dist/v3/loans/lock-round.d.ts.map +0 -1
  126. package/dist/v3/loans/lock-round.js +0 -20
  127. package/dist/v3/sales/reclaim.d.ts +0 -46
  128. package/dist/v3/sales/reclaim.d.ts.map +0 -1
  129. package/dist/v3/sales/reclaim.js +0 -57
@@ -1,12 +1,8 @@
1
1
  import { type FeeCommitInput } from "../fees.ts";
2
2
  import type { CircuitInput } from "../inputs.ts";
3
- import { type OfferPlain } from "./offer-plan.ts";
4
- export declare const SETTLE_SLOTS = 16;
3
+ import type { KeeperTerms } from "../keeper-payload.ts";
5
4
  export declare const LOAN_SETTLE_KIND = 13;
6
- /** The 208-byte offer plaintext `offerPlaintext` writes. */
7
- export declare function decodeOffer(plain: Uint8Array): OfferPlain;
8
- /** `loan::opens`: an honest offer plaintext, the one `offer::slash` aborts 42_013 on. */
9
- export declare function offerOpens(plain: Uint8Array, field: bigint, commit: bigint, lock: bigint): boolean;
5
+ export declare const LOCK_LEVELS = 20;
10
6
  /** `crypto.mjs` interest: whole seconds of the term, and interest rounded up over 365 days of 10_000 bps. */
11
7
  export declare function interest(principal: bigint, rateBps: bigint, termMs: bigint): {
12
8
  qTerm: bigint;
@@ -15,94 +11,133 @@ export declare function interest(principal: bigint, rateBps: bigint, termMs: big
15
11
  rIntr: bigint;
16
12
  owed: bigint;
17
13
  };
18
- /** One stored offer slot: its commit and lock as the round holds them, and its opening when active. */
19
- export type SettleSlot = {
20
- commit: bigint;
21
- lock: bigint;
22
- active: boolean;
23
- open?: OfferPlain;
14
+ export type SettleRequest = {
15
+ principal: bigint;
16
+ maxRateBps: bigint;
17
+ termMs: bigint;
18
+ borrowerPk: bigint;
19
+ collAmt: bigint;
20
+ collBlind: bigint;
21
+ collNote: bigint;
22
+ salt: bigint;
23
+ };
24
+ /** The offer's lock opening from its payload (freeze §6.1): the lender's keys, `ns`, the blind and `unlock_s`. */
25
+ export type OfferOpening = {
26
+ lenderPk: bigint;
27
+ ns: bigint;
28
+ b: bigint;
29
+ unlockS: bigint;
30
+ encX: bigint;
31
+ encY: bigint;
32
+ };
33
+ /** The offer's leaf in the loan pool's tree: its index and depth-20 path to `root`. */
34
+ export type LockPath = {
35
+ root: bigint;
36
+ index: bigint;
37
+ path: bigint[];
38
+ };
39
+ export type SettlePrice = {
40
+ pClaim: bigint;
41
+ expo: bigint;
42
+ cDec: bigint;
43
+ lDec: bigint;
44
+ ltvBps: bigint;
45
+ };
46
+ type Keyed = {
47
+ pk: bigint;
48
+ blinding: bigint;
24
49
  };
25
50
  export type LoanSettleParams = {
26
- slots: SettleSlot[];
27
- request: {
28
- principal: bigint;
29
- maxRateBps: bigint;
30
- termMs: bigint;
31
- borrowerPk: bigint;
32
- collAmt: bigint;
33
- collBlind: bigint;
34
- collNote: bigint;
35
- salt: bigint;
36
- };
51
+ request: SettleRequest;
37
52
  loanKey: bigint;
38
- borrowerNote: {
39
- pk: bigint;
40
- blinding: bigint;
41
- };
53
+ offer: OfferOpening;
54
+ lock: LockPath;
55
+ borrowerNote: Keyed;
56
+ pads: [Keyed, Keyed];
57
+ dummySeed: bigint;
42
58
  e: bigint;
43
59
  nowS: bigint;
44
60
  extHash: bigint;
45
61
  fee: FeeCommitInput;
46
62
  eph: bigint;
63
+ price: SettlePrice;
47
64
  };
65
+ /** What settle and refinance both read. */
66
+ export type LockedParams = Omit<LoanSettleParams, "pads" | "dummySeed">;
67
+ /** `lib/merkle.circom`: bit k of the index puts path[k] on the left. */
68
+ export declare function lockRoot(leaf: bigint, index: bigint, path: bigint[]): bigint;
48
69
  /**
49
- * PRV-23 A: the winner is the round's pick, the one active slot. `loan::settle_rows` (`loan.move:507-513`) folds the
50
- * pick's commit alone with `actMask = 1 << pick`, so a witness with any other active slot fails the proof.
70
+ * What settle and refinance share (freeze §3.1, §3.2): the lock opened under `field` at the posted principal and
71
+ * found at `root`, the posted-rate terms, the LTV line on `owed`, the flat fee and the collateral delivery.
72
+ * Each refusal is one the circuit has no witness for, so it is said before a proof is spent.
51
73
  */
52
- export declare function pickedSlot(slots: SettleSlot[], maxRate: bigint): number;
53
- /** Witness and Move arguments of `circuits/v3/loan_settle/loan_settle.circom` (`ref.mjs` loanSettleCase, round 0). */
74
+ export declare function lockedTerms(p: LockedParams, field: bigint): Promise<{
75
+ input: CircuitInput;
76
+ terms: KeeperTerms;
77
+ hRest: bigint;
78
+ termsCommit: bigint;
79
+ lane: import("../fees.ts").FeeLaneResult;
80
+ d: [bigint, bigint, bigint, bigint];
81
+ nLock: bigint;
82
+ reqCommit: bigint;
83
+ priceDigest: bigint;
84
+ }>;
85
+ /** Witness and Move arguments of `circuits/v3/loan_settle/loan_settle.circom` (freeze §3.1), round 0. */
54
86
  export declare function loanSettle(p: LoanSettleParams): Promise<{
55
87
  input: CircuitInput;
56
88
  publics: bigint[];
57
- winner: number;
58
- terms: {
59
- principal: bigint;
60
- rateBps: bigint;
61
- owed: bigint;
62
- maturityS: bigint;
63
- lenderPk: bigint;
64
- lenderEncX: bigint;
65
- lenderEncY: bigint;
66
- salt: bigint;
67
- };
89
+ terms: KeeperTerms;
90
+ hRest: bigint;
68
91
  termsCommit: bigint;
69
92
  principalNote: bigint;
70
93
  principalValue: bigint;
71
- winNull: bigint;
94
+ nLock: bigint;
95
+ nDummy: bigint;
96
+ pads: [bigint, bigint];
72
97
  d: [bigint, bigint, bigint, bigint];
73
98
  feeNote: bigint;
74
99
  ephX: bigint;
75
100
  ephY: bigint;
76
101
  feeCt: bigint;
102
+ priceDigest: bigint;
77
103
  }>;
78
104
  /** `loan::action_hash`: blake2b256("ess3/act/" ‖ kind ‖ "/" ‖ bcs(loan_key) ‖ bcs(nonce) ‖ args) mod R. */
79
105
  export declare function actionHash(blake: (b: Uint8Array) => Uint8Array, loanKey: bigint, nonce: bigint, kind: string, args: Uint8Array): bigint;
80
- /** `settle::settle_args`: every argument in declaration order, shared objects by id, vectors and the proof as bcs. */
106
+ /**
107
+ * `settle::settle_args` (Z1): every argument in declaration order, shared objects by id, the reading as the bcs of
108
+ * the struct (`readingBytes`), vectors and the proof as bcs; the pass and the clock left out.
109
+ */
81
110
  export declare function settleArgs(a: {
82
111
  loan: string;
83
112
  poolL: string;
113
+ market: string;
114
+ reading: Uint8Array;
84
115
  proof: Uint8Array;
116
+ root: bigint;
117
+ nLock: bigint;
118
+ nDummy: bigint;
85
119
  termsCommit: bigint;
86
120
  principalNote: bigint;
87
121
  feeNote: bigint;
88
- winNull: bigint;
122
+ pad0: bigint;
123
+ pad1: bigint;
89
124
  d: bigint[];
90
125
  ephX: bigint;
91
126
  ephY: bigint;
92
127
  feeCt: bigint;
128
+ pClaim: bigint;
93
129
  keeperCt: Uint8Array;
94
130
  encs: Uint8Array[];
95
131
  nowS: bigint;
96
132
  memo: Uint8Array;
97
133
  }): Uint8Array;
98
- /** `membership::seal_offer(room, round, seq, viewer).bind`. */
99
- export declare const sealOfferBind: (blake: (b: Uint8Array) => Uint8Array, room: string, round: bigint, seq: bigint, viewer: string) => Uint8Array<ArrayBufferLike>;
100
- /** `refi::open_refi_args`: the loan and pool by id, `req_commit`, the sealed request as bcs, `close_ms`. */
134
+ /** `refi::open_refi_args`: the loan and pool by id, `req_commit`, the sealed request and memo as bcs, `close_ms`. */
101
135
  export declare const openRefiArgs: (a: {
102
136
  loan: string;
103
137
  poolL: string;
104
138
  reqCommit: bigint;
105
139
  reqSealed: Uint8Array;
140
+ memo: Uint8Array;
106
141
  closeMs: bigint;
107
142
  }) => Uint8Array;
108
143
  /** `liquidation::topup_args`: every `topup_by_borrower` argument after the pass, in order, by `settle_args`' rule. */
@@ -112,8 +147,6 @@ export declare function topupArgs(a: {
112
147
  market: string;
113
148
  lockProof: Uint8Array;
114
149
  fields: bigint[];
115
- encChange: Uint8Array;
116
- encFee: Uint8Array;
117
150
  keeperCt: Uint8Array;
118
151
  proof: Uint8Array;
119
152
  outs: bigint[];
@@ -122,17 +155,19 @@ export declare function topupArgs(a: {
122
155
  }): Uint8Array;
123
156
  /** `refi::cancel_refi_args`: the loan and pool by id. */
124
157
  export declare const cancelRefiArgs: (loan: string, poolL: string) => Uint8Array;
125
- /** `refi::settle_refi_args`: `settle_args`' rule with the market, the reading's bcs, the payoff pair and `p_claim`. */
158
+ /** `refi::settle_refi_args` (Z1): `settle_args`' rule with the payoff pair, the pad and one `encs` of 4. */
126
159
  export declare function settleRefiArgs(a: {
127
160
  loan: string;
128
161
  poolL: string;
129
162
  market: string;
130
163
  reading: Uint8Array;
131
164
  proof: Uint8Array;
165
+ root: bigint;
166
+ nLock: bigint;
167
+ nDummy: bigint;
132
168
  termsCommit: bigint;
133
169
  borrowerNote: bigint;
134
170
  feeNote: bigint;
135
- winNull: bigint;
136
171
  d: bigint[];
137
172
  ephX: bigint;
138
173
  ephY: bigint;
@@ -143,7 +178,6 @@ export declare function settleRefiArgs(a: {
143
178
  pClaim: bigint;
144
179
  keeperCt: Uint8Array;
145
180
  encs: Uint8Array[];
146
- encsP: Uint8Array[];
147
181
  nowS: bigint;
148
182
  memo: Uint8Array;
149
183
  }): Uint8Array;
@@ -156,4 +190,5 @@ export declare const readingBytes: (r: {
156
190
  expo: bigint;
157
191
  publishS: bigint;
158
192
  }) => Uint8Array;
193
+ export {};
159
194
  //# sourceMappingURL=settle-plan.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"settle-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/settle-plan.ts"],"names":[],"mappings":"AAIA,OAAO,EAAgC,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC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,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,UAAU,CAAC;IACrB,IAAI,EAAE,UAAU,EAAE,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,UAAU,CAAC;CAClB,GAAG,UAAU,CAiBb;AAED,+GAA+G;AAC/G,eAAO,MAAM,YAAY,GAAI,GAAG;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CAClB,KAAG,UAQD,CAAC"}
@@ -1,54 +1,16 @@
1
1
  import { poseidonHash } from "@mysten/sui/zklogin";
2
- import { DOM_A2, DOM_A2D, DOM_A2O, DOM_LNULL, DOM_TSALT, R } from "../domains.js";
2
+ import { EsseyError } from "../../errors.js";
3
+ import { valueAt } from "../collateral/math.js";
4
+ import { termsOuter, termsRest } from "../collateral/openings.js";
5
+ import { DOM_A2, DOM_A2D, DOM_A2O, DOM_PRICE, DOM_TSALT, R } from "../domains.js";
3
6
  import { feeWitness } from "../fee-witness.js";
4
- import { ecdhBlind, feeLane, fieldAdd } from "../fees.js";
7
+ import { ecdhBlind, fieldAdd, flatFeeLane } from "../fees.js";
8
+ import { lock5, lockField, offerBlind } from "../locks.js";
5
9
  import { noteCommitment } from "../notes/keys.js";
6
- import { lock4, lockField, offerCommit } from "./offer-plan.js";
7
- export const SETTLE_SLOTS = 16;
10
+ import { dummyNull } from "../pads.js";
8
11
  export const LOAN_SETTLE_KIND = 13;
12
+ export const LOCK_LEVELS = 20;
9
13
  const INTEREST_DENOM = 315360000000n;
10
- function readLe(bytes, at, size) {
11
- let v = 0n;
12
- for (let i = size - 1; i >= 0; i--)
13
- v = (v << 8n) | BigInt(bytes[at + i]);
14
- return v;
15
- }
16
- /** The 208-byte offer plaintext `offerPlaintext` writes. */
17
- export function decodeOffer(plain) {
18
- if (plain.length !== 208)
19
- throw new Error("the offer plaintext is not 208 bytes");
20
- return {
21
- rateBps: readLe(plain, 0, 8),
22
- salt: readLe(plain, 8, 32),
23
- lenderPk: readLe(plain, 40, 32),
24
- encX: readLe(plain, 72, 32),
25
- encY: readLe(plain, 104, 32),
26
- ns: readLe(plain, 136, 32),
27
- b: readLe(plain, 168, 32),
28
- amount: readLe(plain, 200, 8),
29
- };
30
- }
31
- const BJJ_A = 168700n;
32
- const BJJ_D = 168696n;
33
- /** `membership::valid_point`. */
34
- function validPoint(x, y) {
35
- if (x >= R || y >= R || (x === 0n && y === 1n))
36
- return false;
37
- const x2 = (x * x) % R;
38
- const y2 = (y * y) % R;
39
- return (BJJ_A * x2 + y2) % R === (1n + BJJ_D * ((x2 * y2) % R)) % R;
40
- }
41
- /** `loan::opens`: an honest offer plaintext, the one `offer::slash` aborts 42_013 on. */
42
- export function offerOpens(plain, field, commit, lock) {
43
- if (plain.length !== 208)
44
- return false;
45
- const o = decodeOffer(plain);
46
- if ([o.salt, o.lenderPk, o.encX, o.encY, o.ns, o.b].some((x) => x >= R))
47
- return false;
48
- return (offerCommit(o.rateBps, o.salt, o.lenderPk, o.encX, o.encY) === commit &&
49
- validPoint(o.encX, o.encY) &&
50
- lock4(field, o.lenderPk, o.ns, o.amount, o.b) === lock);
51
- }
52
14
  /** `crypto.mjs` interest: whole seconds of the term, and interest rounded up over 365 days of 10_000 bps. */
53
15
  export function interest(principal, rateBps, termMs) {
54
16
  const qTerm = termMs / 1000n;
@@ -61,87 +23,53 @@ export function interest(principal, rateBps, termMs) {
61
23
  owed: principal + num / INTEREST_DENOM,
62
24
  };
63
25
  }
26
+ const refuse = (why) => {
27
+ throw new EsseyError("INVALID_ARGUMENT", `refused: ${why}; nothing was proved or sent`);
28
+ };
29
+ /** `lib/merkle.circom`: bit k of the index puts path[k] on the left. */
30
+ export function lockRoot(leaf, index, path) {
31
+ if (path.length !== LOCK_LEVELS)
32
+ throw new Error("the lock path is not 20 levels");
33
+ return path.reduce((node, sib, k) => ((index >> BigInt(k)) & 1n ? poseidonHash([sib, node]) : poseidonHash([node, sib])), leaf);
34
+ }
64
35
  /**
65
- * PRV-23 A: the winner is the round's pick, the one active slot. `loan::settle_rows` (`loan.move:507-513`) folds the
66
- * pick's commit alone with `actMask = 1 << pick`, so a witness with any other active slot fails the proof.
36
+ * What settle and refinance share (freeze §3.1, §3.2): the lock opened under `field` at the posted principal and
37
+ * found at `root`, the posted-rate terms, the LTV line on `owed`, the flat fee and the collateral delivery.
38
+ * Each refusal is one the circuit has no witness for, so it is said before a proof is spent.
67
39
  */
68
- export function pickedSlot(slots, maxRate) {
69
- const live = slots.flatMap((s, i) => (s.active ? [i] : []));
70
- if (live.length !== 1)
71
- throw new Error("a settle witness has exactly one active slot, the pick");
72
- const open = slots[live[0]].open;
73
- if (!open || open.rateBps > maxRate)
74
- throw new Error("no eligible offer");
75
- return live[0];
76
- }
77
- /** Witness and Move arguments of `circuits/v3/loan_settle/loan_settle.circom` (`ref.mjs` loanSettleCase, round 0). */
78
- export async function loanSettle(p) {
79
- const { request: q, slots } = p;
80
- if (slots.length > SETTLE_SLOTS)
81
- throw new Error("more than 16 offers");
82
- const w = pickedSlot(slots, q.maxRateBps);
83
- const win = slots[w].open;
84
- const field = lockField(p.loanKey, 0n);
85
- for (const s of slots) {
86
- if (!s.active || !s.open)
87
- continue;
88
- const o = s.open;
89
- if (offerCommit(o.rateBps, o.salt, o.lenderPk, o.encX, o.encY) !== s.commit)
90
- throw new Error("an offer does not open its commit");
91
- if (lock4(field, o.lenderPk, o.ns, o.amount, o.b) !== s.lock)
92
- throw new Error("an offer does not open its lock");
93
- }
94
- const pad = (pick, zero) => Array.from({ length: SETTLE_SLOTS }, (_, i) => (slots[i] ? pick(slots[i]) : zero));
95
- const opened = (pick) => pad((s) => (s.open ? pick(s.open) : 0n), 0n);
96
- const commits = pad((s) => (s.active ? s.commit : 0n), 0n);
97
- const locks = pad((s) => s.lock, 0n);
98
- const actMask = slots.reduce((m, s, i) => (s.active ? m | (1n << BigInt(i)) : m), 0n);
99
- const term = interest(q.principal, win.rateBps, q.termMs);
100
- const maturityS = p.nowS + term.qTerm;
101
- const tSalt = poseidonHash([DOM_TSALT, field, win.ns]);
102
- const termsCommit = poseidonHash([
103
- q.principal,
104
- win.rateBps,
105
- term.owed,
106
- maturityS,
107
- q.borrowerPk,
108
- win.lenderPk,
109
- poseidonHash([win.encX, win.encY]),
110
- tSalt,
111
- ]);
112
- const lane = await feeLane({ ...p.fee, base: q.principal, eph: p.eph });
113
- const principalNote = noteCommitment(q.principal - lane.fee, p.borrowerNote.pk, p.borrowerNote.blinding);
114
- const winNull = poseidonHash([DOM_LNULL, win.ns, slots[w].lock]);
115
- const k = await ecdhBlind(win.encX, win.encY, p.e);
116
- const ct1 = fieldAdd(q.collAmt, poseidonHash([k.sx, 1n]));
117
- const ct2 = fieldAdd(q.collBlind, poseidonHash([k.sx, 2n]));
118
- const reqCommit = poseidonHash([q.principal, q.maxRateBps, q.termMs, q.borrowerPk, q.collNote, q.salt]);
119
- const d = [k.ephX, k.ephY, ct1, ct2];
120
- const publics = [
121
- poseidonHash(commits),
122
- poseidonHash(locks),
123
- poseidonHash([DOM_A2, reqCommit, field, actMask, p.nowS, lane.fee_commit, p.fee.version, p.extHash, 0n]),
124
- poseidonHash([DOM_A2O, termsCommit, principalNote, lane.fee_note, winNull]),
125
- poseidonHash([DOM_A2D, ...d, lane.ephX, lane.ephY, lane.fee_ct]),
40
+ export async function lockedTerms(p, field) {
41
+ const { request: q, offer: o, price } = p;
42
+ if (o.b !== offerBlind(o.ns, o.encX, o.encY))
43
+ refuse("the offer's lock blind does not bind its enc point");
44
+ const l = lock5({ field, ownerPk: o.lenderPk, ns: o.ns, amount: q.principal, b: o.b, unlockS: o.unlockS });
45
+ if (lockRoot(l.lock, p.lock.index, p.lock.path) !== p.lock.root)
46
+ refuse("the offer's lock is not at the root");
47
+ if (p.fee.bps !== 0n && q.principal < 10n * p.fee.min)
48
+ refuse("the principal is under 10 times the flat fee");
49
+ const term = interest(q.principal, q.maxRateBps, q.termMs);
50
+ const value = valueAt(q.collAmt, price.pClaim, { cDec: price.cDec, lDec: price.lDec, expo: price.expo });
51
+ if (term.owed * 10000n > value.q * price.ltvBps)
52
+ refuse("the debt is over the LTV line at the claimed price");
53
+ const terms = {
54
+ principal: q.principal,
55
+ rateBps: q.maxRateBps,
56
+ owed: term.owed,
57
+ maturityS: p.nowS + term.qTerm,
58
+ lenderPk: o.lenderPk,
59
+ lenderEncX: o.encX,
60
+ lenderEncY: o.encY,
61
+ salt: poseidonHash([DOM_TSALT, field, o.ns]),
62
+ };
63
+ const hRest = termsRest(terms);
64
+ const lane = await flatFeeLane({ ...p.fee, base: q.principal, eph: p.eph });
65
+ const k = await ecdhBlind(o.encX, o.encY, p.e);
66
+ const d = [
67
+ k.ephX,
68
+ k.ephY,
69
+ fieldAdd(q.collAmt, poseidonHash([k.sx, 1n])),
70
+ fieldAdd(q.collBlind, poseidonHash([k.sx, 2n])),
126
71
  ];
127
- const s = (xs) => xs.map(String);
128
72
  const input = {
129
- commits: s(commits),
130
- locks: s(locks),
131
- rate: s(opened((o) => o.rateBps)),
132
- salt: s(opened((o) => o.salt)),
133
- lenderPk: s(opened((o) => o.lenderPk)),
134
- encX: s(opened((o) => o.encX)),
135
- encY: s(opened((o) => o.encY)),
136
- actMask: actMask.toString(),
137
- winner: String(w),
138
- winRate: win.rateBps.toString(),
139
- ns_w: win.ns.toString(),
140
- b_w: win.b.toString(),
141
- q_term: term.qTerm.toString(),
142
- r_term: term.rTerm.toString(),
143
- q_intr: term.qIntr.toString(),
144
- r_intr: term.rIntr.toString(),
145
73
  principal: q.principal.toString(),
146
74
  max_rate_bps: q.maxRateBps.toString(),
147
75
  term_ms: q.termMs.toString(),
@@ -150,39 +78,101 @@ export async function loanSettle(p) {
150
78
  collBlind: q.collBlind.toString(),
151
79
  salt_req: q.salt.toString(),
152
80
  loanKey: p.loanKey.toString(),
81
+ lenderPk: o.lenderPk.toString(),
82
+ ns: o.ns.toString(),
83
+ b: o.b.toString(),
84
+ unlock_s: o.unlockS.toString(),
85
+ encX: o.encX.toString(),
86
+ encY: o.encY.toString(),
87
+ lockIndex: p.lock.index.toString(),
88
+ lockPath: p.lock.path.map(String),
89
+ root: p.lock.root.toString(),
153
90
  pkB: p.borrowerNote.pk.toString(),
154
91
  blindB: p.borrowerNote.blinding.toString(),
155
92
  e: p.e.toString(),
93
+ q_term: term.qTerm.toString(),
94
+ r_term: term.rTerm.toString(),
95
+ q_intr: term.qIntr.toString(),
96
+ r_intr: term.rIntr.toString(),
156
97
  now_s: p.nowS.toString(),
98
+ pClaim: price.pClaim.toString(),
99
+ expo: price.expo.toString(),
100
+ c_dec: price.cDec.toString(),
101
+ l_dec: price.lDec.toString(),
102
+ ltv_bps: price.ltvBps.toString(),
103
+ q_v: value.q.toString(),
104
+ r_v: value.r.toString(),
157
105
  extHash: p.extHash.toString(),
158
106
  relayer_fee: "0",
159
107
  ...feeWitness(p.fee, q.principal, p.eph, lane),
160
108
  };
109
+ return {
110
+ input,
111
+ terms,
112
+ hRest,
113
+ termsCommit: termsOuter(terms, q.borrowerPk, hRest),
114
+ lane,
115
+ d,
116
+ nLock: l.nullifier,
117
+ reqCommit: poseidonHash([q.principal, q.maxRateBps, q.termMs, q.borrowerPk, q.collNote, q.salt]),
118
+ priceDigest: poseidonHash([DOM_PRICE, price.pClaim, price.expo, price.cDec, price.lDec, price.ltvBps]),
119
+ };
120
+ }
121
+ /** Witness and Move arguments of `circuits/v3/loan_settle/loan_settle.circom` (freeze §3.1), round 0. */
122
+ export async function loanSettle(p) {
123
+ const field = lockField(p.loanKey, 0n);
124
+ const t = await lockedTerms(p, field);
125
+ const { lane } = t;
126
+ const principalValue = p.request.principal - lane.fee;
127
+ const principalNote = noteCommitment(principalValue, p.borrowerNote.pk, p.borrowerNote.blinding);
128
+ const pads = p.pads.map((x) => noteCommitment(0n, x.pk, x.blinding));
129
+ const nDummy = dummyNull(p.dummySeed);
130
+ const hCtx = poseidonHash([
131
+ DOM_A2,
132
+ t.reqCommit,
133
+ field,
134
+ p.nowS,
135
+ lane.fee_commit,
136
+ p.fee.version,
137
+ p.extHash,
138
+ 0n,
139
+ t.priceDigest,
140
+ ]);
141
+ const publics = [
142
+ p.lock.root,
143
+ t.nLock,
144
+ nDummy,
145
+ hCtx,
146
+ poseidonHash([DOM_A2O, t.termsCommit, principalNote, lane.fee_note, pads[0], pads[1]]),
147
+ poseidonHash([DOM_A2D, ...t.d, lane.ephX, lane.ephY, lane.fee_ct]),
148
+ ];
149
+ const input = {
150
+ ...t.input,
151
+ padPk0: p.pads[0].pk.toString(),
152
+ padBlind0: p.pads[0].blinding.toString(),
153
+ padPk1: p.pads[1].pk.toString(),
154
+ padBlind1: p.pads[1].blinding.toString(),
155
+ dummySeed: p.dummySeed.toString(),
156
+ };
161
157
  if (publics.some((x) => x >= R))
162
158
  throw new Error("a public signal is not a field element");
163
159
  return {
164
160
  input,
165
161
  publics,
166
- winner: w,
167
- terms: {
168
- principal: q.principal,
169
- rateBps: win.rateBps,
170
- owed: term.owed,
171
- maturityS,
172
- lenderPk: win.lenderPk,
173
- lenderEncX: win.encX,
174
- lenderEncY: win.encY,
175
- salt: tSalt,
176
- },
177
- termsCommit,
162
+ terms: t.terms,
163
+ hRest: t.hRest,
164
+ termsCommit: t.termsCommit,
178
165
  principalNote,
179
- principalValue: q.principal - lane.fee,
180
- winNull,
181
- d,
166
+ principalValue,
167
+ nLock: t.nLock,
168
+ nDummy,
169
+ pads,
170
+ d: t.d,
182
171
  feeNote: lane.fee_note,
183
172
  ephX: lane.ephX,
184
173
  ephY: lane.ephY,
185
174
  feeCt: lane.fee_ct,
175
+ priceDigest: t.priceDigest,
186
176
  };
187
177
  }
188
178
  const ascii = (s) => Uint8Array.from(Buffer.from(s, "ascii"));
@@ -201,31 +191,32 @@ function uleb(n) {
201
191
  }
202
192
  const bcsBytes = (b) => cat(uleb(b.length), b);
203
193
  const addressBytes = (id) => le(BigInt(id), 32).reverse();
194
+ const fields = (xs) => cat(uleb(xs.length), ...xs.map((x) => le(x, 32)));
195
+ const blobs = (xs) => cat(uleb(xs.length), ...xs.map(bcsBytes));
196
+ const words = (xs) => cat(...xs.map((x) => le(x, 32)));
204
197
  /** `loan::action_hash`: blake2b256("ess3/act/" ‖ kind ‖ "/" ‖ bcs(loan_key) ‖ bcs(nonce) ‖ args) mod R. */
205
198
  export function actionHash(blake, loanKey, nonce, kind, args) {
206
199
  const digest = blake(cat(ascii(`ess3/act/${kind}/`), le(loanKey, 32), le(nonce, 8), args));
207
200
  return BigInt(`0x${Buffer.from(digest).toString("hex")}`) % R;
208
201
  }
209
- /** `settle::settle_args`: every argument in declaration order, shared objects by id, vectors and the proof as bcs. */
202
+ /**
203
+ * `settle::settle_args` (Z1): every argument in declaration order, shared objects by id, the reading as the bcs of
204
+ * the struct (`readingBytes`), vectors and the proof as bcs; the pass and the clock left out.
205
+ */
210
206
  export function settleArgs(a) {
211
- return cat(addressBytes(a.loan), addressBytes(a.poolL), bcsBytes(a.proof), le(a.termsCommit, 32), le(a.principalNote, 32), le(a.feeNote, 32), le(a.winNull, 32), cat(uleb(a.d.length), ...a.d.map((x) => le(x, 32))), le(a.ephX, 32), le(a.ephY, 32), le(a.feeCt, 32), bcsBytes(a.keeperCt), cat(uleb(a.encs.length), ...a.encs.map(bcsBytes)), le(a.nowS, 8), bcsBytes(a.memo));
207
+ return cat(addressBytes(a.loan), addressBytes(a.poolL), addressBytes(a.market), a.reading, bcsBytes(a.proof), words([a.root, a.nLock, a.nDummy, a.termsCommit, a.principalNote, a.feeNote, a.pad0, a.pad1]), fields(a.d), words([a.ephX, a.ephY, a.feeCt]), le(a.pClaim, 8), bcsBytes(a.keeperCt), blobs(a.encs), le(a.nowS, 8), bcsBytes(a.memo));
212
208
  }
213
- /** `membership::seal_offer(room, round, seq, viewer).bind`. */
214
- export const sealOfferBind = (blake, room, round, seq, viewer) => blake(cat(ascii("essey-v3/seal/offer"), addressBytes(room), le(round, 8), le(seq, 8), addressBytes(viewer)));
215
- /** `refi::open_refi_args`: the loan and pool by id, `req_commit`, the sealed request as bcs, `close_ms`. */
216
- export const openRefiArgs = (a) => cat(addressBytes(a.loan), addressBytes(a.poolL), le(a.reqCommit, 32), bcsBytes(a.reqSealed), le(a.closeMs, 8));
209
+ /** `refi::open_refi_args`: the loan and pool by id, `req_commit`, the sealed request and memo as bcs, `close_ms`. */
210
+ export const openRefiArgs = (a) => cat(addressBytes(a.loan), addressBytes(a.poolL), le(a.reqCommit, 32), bcsBytes(a.reqSealed), bcsBytes(a.memo), le(a.closeMs, 8));
217
211
  /** `liquidation::topup_args`: every `topup_by_borrower` argument after the pass, in order, by `settle_args`' rule. */
218
212
  export function topupArgs(a) {
219
- const fields = (xs) => cat(uleb(xs.length), ...xs.map((x) => le(x, 32)));
220
- return cat(addressBytes(a.loan), addressBytes(a.poolC), addressBytes(a.market), bcsBytes(a.lockProof), ...a.fields.map((x) => le(x, 32)), bcsBytes(a.encChange), bcsBytes(a.encFee), bcsBytes(a.keeperCt), bcsBytes(a.proof), fields(a.outs), fields(a.delivery), cat(uleb(a.encs.length), ...a.encs.map(bcsBytes)));
213
+ return cat(addressBytes(a.loan), addressBytes(a.poolC), addressBytes(a.market), bcsBytes(a.lockProof), ...a.fields.map((x) => le(x, 32)), bcsBytes(a.keeperCt), bcsBytes(a.proof), fields(a.outs), fields(a.delivery), blobs(a.encs));
221
214
  }
222
215
  /** `refi::cancel_refi_args`: the loan and pool by id. */
223
216
  export const cancelRefiArgs = (loan, poolL) => cat(addressBytes(loan), addressBytes(poolL));
224
- /** `refi::settle_refi_args`: `settle_args`' rule with the market, the reading's bcs, the payoff pair and `p_claim`. */
217
+ /** `refi::settle_refi_args` (Z1): `settle_args`' rule with the payoff pair, the pad and one `encs` of 4. */
225
218
  export function settleRefiArgs(a) {
226
- const fields = (xs) => cat(uleb(xs.length), ...xs.map((x) => le(x, 32)));
227
- const blobs = (xs) => cat(uleb(xs.length), ...xs.map(bcsBytes));
228
- return cat(addressBytes(a.loan), addressBytes(a.poolL), addressBytes(a.market), a.reading, bcsBytes(a.proof), ...[a.termsCommit, a.borrowerNote, a.feeNote, a.winNull].map((x) => le(x, 32)), fields(a.d), ...[a.ephX, a.ephY, a.feeCt, a.payoffNote, a.pad].map((x) => le(x, 32)), fields(a.ephP), le(a.pClaim, 8), bcsBytes(a.keeperCt), blobs(a.encs), blobs(a.encsP), le(a.nowS, 8), bcsBytes(a.memo));
219
+ return cat(addressBytes(a.loan), addressBytes(a.poolL), addressBytes(a.market), a.reading, bcsBytes(a.proof), words([a.root, a.nLock, a.nDummy, a.termsCommit, a.borrowerNote, a.feeNote]), fields(a.d), words([a.ephX, a.ephY, a.feeCt, a.payoffNote, a.pad]), fields(a.ephP), le(a.pClaim, 8), bcsBytes(a.keeperCt), blobs(a.encs), le(a.nowS, 8), bcsBytes(a.memo));
229
220
  }
230
221
  /** The bcs of `price::Reading`: the oracle type name, the feed id, spot, EMA, the exponent and `publish_s`. */
231
222
  export const readingBytes = (r) => cat(bcsBytes(ascii(r.oracle)), addressBytes(r.feedId), le(r.spot, 8), le(r.ema, 8), Uint8Array.of(Number(r.expo)), le(r.publishS, 8));
@@ -0,0 +1,21 @@
1
+ export type Lock5 = {
2
+ field: bigint;
3
+ ownerPk: bigint;
4
+ ns: bigint;
5
+ amount: bigint;
6
+ b: bigint;
7
+ unlockS: bigint;
8
+ };
9
+ /** `loan::lock_field`: a loan round's lock field. A bid's field is the room's `sale_key`. */
10
+ export declare const lockField: (loanKey: bigint, round: bigint) => bigint;
11
+ /** `lib/lock.circom` Lock5: a note shape, so js4's first output with `outPk0 = lockPk` is the leaf. */
12
+ export declare function lock5(l: Lock5): {
13
+ lockPk: bigint;
14
+ lock: bigint;
15
+ nullifier: bigint;
16
+ };
17
+ /** Ruling R5: an offer's blind binds the lender's enc point (`loan_settle.circom`, `loan_refi.circom`). */
18
+ export declare const offerBlind: (ns: bigint, encX: bigint, encY: bigint) => bigint;
19
+ /** A bid's blind binds `ns` alone: no ECDH reads a bid key. */
20
+ export declare const bidBlind: (ns: bigint) => bigint;
21
+ //# sourceMappingURL=locks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"locks.d.ts","sourceRoot":"","sources":["../../src/v3/locks.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,KAAK,GAAG;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,CAAC,EAAE,MAAM,CAAC;IACV,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,6FAA6F;AAC7F,eAAO,MAAM,SAAS,GAAI,SAAS,MAAM,EAAE,OAAO,MAAM,WAA8C,CAAC;AAEvG,uGAAuG;AACvG,wBAAgB,KAAK,CAAC,CAAC,EAAE,KAAK,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAInF;AAED,2GAA2G;AAC3G,eAAO,MAAM,UAAU,GAAI,IAAI,MAAM,EAAE,MAAM,MAAM,EAAE,MAAM,MAAM,WACN,CAAC;AAE5D,+DAA+D;AAC/D,eAAO,MAAM,QAAQ,GAAI,IAAI,MAAM,WAAkC,CAAC"}