Skip to content

Commit a8462e2

Browse files
committed
Overhaul tests to work with more networks
1 parent 89be0f7 commit a8462e2

35 files changed

+891
-659
lines changed

confidential-assets/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"build:types": "tsc -p tsconfig.build.json --outDir ./dist/types --declaration --emitDeclarationOnly",
3737
"build:cjs": "npx swc src -d ./dist/cjs --config-file .swcrc -C module.type=commonjs --strip-leading-paths --copy-files",
3838
"build:esm": "npx swc src -d ./dist/esm --config-file .swcrc -C module.type=es6 isModule=true --strip-leading-paths --copy-files",
39-
"test": "pnpm jest ./tests/**/*.test.ts --testPathIgnorePatterns={./tests/units/api,./tests/e2e} --passWithNoTests"
39+
"test:units": "pnpm jest tests/units",
40+
"test:e2e": "pnpm jest tests/e2e"
4041
},
4142
"dependencies": {
4243
"@aptos-labs/confidential-asset-wasm-bindings": "^0.0.2",

confidential-assets/src/confidentialAsset.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
// Copyright © Aptos Foundation
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { Account, AccountAddress, AccountAddressInput, AnyNumber, Aptos, AptosConfig, CommittedTransactionResponse, HexInput, InputGenerateSingleSignerRawTransactionArgs, InputGenerateTransactionPayloadData, LedgerVersionArg, MoveStructId, SimpleTransaction } from "@aptos-labs/ts-sdk";
4+
import {
5+
Account,
6+
AccountAddress,
7+
AccountAddressInput,
8+
AnyNumber,
9+
Aptos,
10+
AptosConfig,
11+
CommittedTransactionResponse,
12+
HexInput,
13+
InputGenerateSingleSignerRawTransactionArgs,
14+
InputGenerateTransactionPayloadData,
15+
LedgerVersionArg,
16+
MoveStructId,
17+
SimpleTransaction,
18+
} from "@aptos-labs/ts-sdk";
519
import { TwistedElGamalCiphertext } from "./twistedElGamal";
620
import { ConfidentialNormalization, CreateConfidentialNormalizationOpArgs } from "./confidentialNormalization";
721
import { ConfidentialKeyRotation, CreateConfidentialKeyRotationOpArgs } from "./confidentialKeyRotation";
@@ -11,7 +25,10 @@ import { ConfidentialAmount } from "./confidentialAmount";
1125
import { CreateConfidentialTransferOpArgs, ConfidentialTransfer } from "./confidentialTransfer";
1226
import { CreateConfidentialWithdrawOpArgs, ConfidentialWithdraw } from "./confidentialWithdraw";
1327
import { TwistedEd25519PublicKey, TwistedEd25519PrivateKey } from "./twistedEd25519";
14-
import { DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS, MODULE_NAME } from "./consts";
28+
29+
/** For now we only deploy to devnet as part of aptos-experimental, which lives at 0x7. */
30+
const DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS = "0x7";
31+
const MODULE_NAME = "confidential_asset";
1532

1633
export type ConfidentialBalanceResponse = {
1734
chunks: {
@@ -32,7 +49,12 @@ export class ConfidentialAsset {
3249
client: Aptos;
3350
confidentialAssetModuleAddress: string;
3451

35-
constructor(readonly config: AptosConfig, { confidentialAssetModuleAddress = DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS }: { confidentialAssetModuleAddress?: string } = {}) {
52+
constructor(
53+
readonly config: AptosConfig,
54+
{
55+
confidentialAssetModuleAddress = DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS,
56+
}: { confidentialAssetModuleAddress?: string } = {},
57+
) {
3658
this.client = new Aptos(config);
3759
this.confidentialAssetModuleAddress = confidentialAssetModuleAddress;
3860
}

confidential-assets/src/confidentialKeyRotation.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,12 @@ export class ConfidentialKeyRotation {
108108
const alpha2 = proofArr[3];
109109
const alpha3 = proofArr[4];
110110
const alpha4 = proofArr[5];
111-
const alpha5List = proofArr.slice(
112-
6,
113-
6 + ConfidentialAmount.CHUNKS_COUNT,
114-
);
115-
const X1 = proofArr[
116-
6 + ConfidentialAmount.CHUNKS_COUNT
117-
];
118-
const X2 = proofArr[
119-
7 + ConfidentialAmount.CHUNKS_COUNT
120-
];
121-
const X3 = proofArr[
122-
8 + ConfidentialAmount.CHUNKS_COUNT
123-
];
124-
const X4List = proofArr.slice(
125-
8 + ConfidentialAmount.CHUNKS_COUNT,
126-
8 + 2 * ConfidentialAmount.CHUNKS_COUNT,
127-
);
128-
const X5List = proofArr.slice(
129-
8 + 2 * ConfidentialAmount.CHUNKS_COUNT,
130-
);
111+
const alpha5List = proofArr.slice(6, 6 + ConfidentialAmount.CHUNKS_COUNT);
112+
const X1 = proofArr[6 + ConfidentialAmount.CHUNKS_COUNT];
113+
const X2 = proofArr[7 + ConfidentialAmount.CHUNKS_COUNT];
114+
const X3 = proofArr[8 + ConfidentialAmount.CHUNKS_COUNT];
115+
const X4List = proofArr.slice(8 + ConfidentialAmount.CHUNKS_COUNT, 8 + 2 * ConfidentialAmount.CHUNKS_COUNT);
116+
const X5List = proofArr.slice(8 + 2 * ConfidentialAmount.CHUNKS_COUNT);
131117

132118
return {
133119
alpha1List,
@@ -159,11 +145,11 @@ export class ConfidentialKeyRotation {
159145
ed25519modN(
160146
x1List.reduce((acc, el, i) => {
161147
const coef = 2n ** (BigInt(i) * ConfidentialAmount.CHUNK_BITS_BI);
162-
const x1i = el * coef
148+
const x1i = el * coef;
163149

164150
return acc + x1i;
165-
}, 0n)
166-
)
151+
}, 0n),
152+
),
167153
).add(
168154
this.currEncryptedBalance
169155
.reduce(
@@ -178,11 +164,11 @@ export class ConfidentialKeyRotation {
178164
const x1iG = RistrettoPoint.BASE.multiply(el);
179165
const x5iH = H_RISTRETTO.multiply(x5List[index]);
180166

181-
return x1iG.add(x5iH)
167+
return x1iG.add(x5iH);
182168
});
183169
const X5List = x5List.map((el) => {
184-
const Pnew = RistrettoPoint.fromHex(this.newDecryptionKey.publicKey().toUint8Array())
185-
return Pnew.multiply(el)
170+
const Pnew = RistrettoPoint.fromHex(this.newDecryptionKey.publicKey().toUint8Array());
171+
return Pnew.multiply(el);
186172
});
187173

188174
const p = genFiatShamirChallenge(
@@ -216,7 +202,7 @@ export class ConfidentialKeyRotation {
216202
const alpha5List = x5List.map((el, i) => {
217203
const pri = ed25519modN(p * this.randomness[i]);
218204

219-
return ed25519modN(el - pri)
205+
return ed25519modN(el - pri);
220206
});
221207

222208
return {
@@ -282,10 +268,11 @@ export class ConfidentialKeyRotation {
282268
const a1i = el * coef;
283269

284270
return acc + a1i;
285-
}, 0n)
286-
)
271+
}, 0n),
272+
),
287273
)
288-
.add(DOldSum.multiply(alpha2LE)).add(COldSum.multiply(p));
274+
.add(DOldSum.multiply(alpha2LE))
275+
.add(COldSum.multiply(p));
289276
const X2 = H_RISTRETTO.multiply(alpha3LE).add(pkOldRist.multiply(p));
290277
const X3 = H_RISTRETTO.multiply(alpha4LE).add(pkNewRist.multiply(p));
291278
const X4List = alpha1LEList.map((el, i) => {

confidential-assets/src/confidentialNormalization.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,9 @@ export class ConfidentialNormalization {
9797
const alpha1List = proofArr.slice(0, 3);
9898
const alpha2 = proofArr[3];
9999
const alpha3 = proofArr[4];
100-
const alpha4List = proofArr.slice(
101-
5,
102-
5 + ConfidentialAmount.CHUNKS_COUNT,
103-
);
104-
const X1 = proofArr[
105-
5 + 2 * ConfidentialAmount.CHUNKS_COUNT
106-
];
107-
const X2 = proofArr[
108-
5 + 2 * ConfidentialAmount.CHUNKS_COUNT + 1
109-
];
100+
const alpha4List = proofArr.slice(5, 5 + ConfidentialAmount.CHUNKS_COUNT);
101+
const X1 = proofArr[5 + 2 * ConfidentialAmount.CHUNKS_COUNT];
102+
const X2 = proofArr[5 + 2 * ConfidentialAmount.CHUNKS_COUNT + 1];
110103
const X3List = proofArr.slice(
111104
5 + 2 * ConfidentialAmount.CHUNKS_COUNT + 2,
112105
5 + 3 * ConfidentialAmount.CHUNKS_COUNT + 2,
@@ -143,11 +136,11 @@ export class ConfidentialNormalization {
143136
ed25519modN(
144137
x1List.reduce((acc, el, i) => {
145138
const coef = 2n ** (BigInt(i) * ConfidentialAmount.CHUNK_BITS_BI);
146-
const x1i = el * coef
139+
const x1i = el * coef;
147140

148-
return acc + x1i
149-
}, 0n)
150-
)
141+
return acc + x1i;
142+
}, 0n),
143+
),
151144
).add(
152145
this.unnormalizedEncryptedBalance
153146
.reduce(
@@ -162,7 +155,7 @@ export class ConfidentialNormalization {
162155

163156
const x4iH = H_RISTRETTO.multiply(x4List[index]);
164157

165-
return x1iG.add(x4iH)
158+
return x1iG.add(x4iH);
166159
});
167160
const X4List = x4List.map((el) =>
168161
RistrettoPoint.fromHex(this.decryptionKey.publicKey().toUint8Array()).multiply(el),
@@ -197,7 +190,7 @@ export class ConfidentialNormalization {
197190
const alpha4List = x4List.map((el, i) => {
198191
const pri = ed25519modN(p * this.randomness[i]);
199192

200-
return ed25519modN(el - pri)
193+
return ed25519modN(el - pri);
201194
});
202195

203196
return {
@@ -261,8 +254,10 @@ export class ConfidentialNormalization {
261254
const alpha1i = el * coef;
262255
return acc + alpha1i;
263256
}, 0n),
264-
)
265-
).add(alpha2D).add(pBalOld);
257+
),
258+
)
259+
.add(alpha2D)
260+
.add(pBalOld);
266261
const X2 = alpha3H.add(pP);
267262
const X3List = alpha1LEList.map((el, i) => {
268263
const a1iG = RistrettoPoint.BASE.multiply(el);
@@ -272,9 +267,9 @@ export class ConfidentialNormalization {
272267
});
273268
const X4List = alpha4LEList.map((el, i) => {
274269
const a4iP = RistrettoPoint.fromHex(publicKeyU8).multiply(el);
275-
const pDnew = opts.normalizedEncryptedBalance[i].D.multiply(p)
270+
const pDnew = opts.normalizedEncryptedBalance[i].D.multiply(p);
276271

277-
return a4iP.add(pDnew)
272+
return a4iP.add(pDnew);
278273
});
279274

280275
return (

confidential-assets/src/confidentialTransfer.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,11 @@ export class ConfidentialTransfer {
182182

183183
const alpha1List = baseProofArray.slice(0, half);
184184
const alpha2 = baseProofArray[half];
185-
const alpha3List = baseProofArray.slice(
186-
half + 1,
187-
half + 1 + ConfidentialAmount.CHUNKS_COUNT,
188-
)
185+
const alpha3List = baseProofArray.slice(half + 1, half + 1 + ConfidentialAmount.CHUNKS_COUNT);
189186
const alpha4List = baseProofArray.slice(
190187
half + 1 + ConfidentialAmount.CHUNKS_COUNT,
191188
half + 1 + ConfidentialAmount.CHUNKS_COUNT * 2,
192-
)
189+
);
193190
const alpha5 = baseProofArray[half + 1 + ConfidentialAmount.CHUNKS_COUNT * 2];
194191

195192
const X1 = baseProofArray[half + 1 + ConfidentialAmount.CHUNKS_COUNT * 3];
@@ -270,11 +267,11 @@ export class ConfidentialTransfer {
270267
ed25519modN(
271268
x1List.reduce((acc, el, i) => {
272269
const coef = 2n ** (BigInt(i) * ConfidentialAmount.CHUNK_BITS_BI);
273-
const x1i = el * coef
270+
const x1i = el * coef;
274271

275-
return acc + x1i
276-
}, 0n)
277-
)
272+
return acc + x1i;
273+
}, 0n),
274+
),
278275
)
279276
.add(DBal.multiply(x2))
280277
.subtract(DNewBal.multiply(x2))
@@ -298,7 +295,7 @@ export class ConfidentialTransfer {
298295
const x1iG = RistrettoPoint.BASE.multiply(el);
299296
const x3iH = H_RISTRETTO.multiply(x3List[idx]);
300297

301-
return x1iG.add(x3iH).toRawBytes()
298+
return x1iG.add(x3iH).toRawBytes();
302299
});
303300

304301
const X7List =
@@ -329,7 +326,9 @@ export class ConfidentialTransfer {
329326
const sLE = bytesToNumberLE(this.senderDecryptionKey.toUint8Array());
330327
const invertSLE = ed25519InvertN(sLE);
331328

332-
const alpha1List = x1List.map((x1, idx) => ed25519modN(x1 - ed25519modN(p * this.confidentialAmountAfterTransfer.amountChunks[idx])));
329+
const alpha1List = x1List.map((x1, idx) =>
330+
ed25519modN(x1 - ed25519modN(p * this.confidentialAmountAfterTransfer.amountChunks[idx])),
331+
);
333332
const alpha2 = ed25519modN(x2 - p * sLE);
334333
const alpha3List = x3List.map((el, idx) => ed25519modN(BigInt(el) - BigInt(p) * BigInt(this.randomness[idx])));
335334
const alpha4List = x4List
@@ -438,8 +437,8 @@ export class ConfidentialTransfer {
438437
const a1i = curr * coef;
439438

440439
return acc + a1i;
441-
}, 0n)
442-
)
440+
}, 0n),
441+
),
443442
)
444443
.add(oldDSum.multiply(alpha2LE))
445444
.subtract(newDSum.multiply(alpha2LE))

confidential-assets/src/confidentialWithdraw.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,20 @@ export class ConfidentialWithdraw {
144144
const x1i = el * coef;
145145

146146
return acc + x1i;
147-
}, 0n)
148-
)
149-
)
150-
.add(
151-
this.encryptedActualBalanceAmount.reduce((acc, el, i) => {
152-
const { D } = el;
153-
const coef = 2n ** (BigInt(i) * ConfidentialAmount.CHUNK_BITS_BI);
147+
}, 0n),
148+
),
149+
).add(
150+
this.encryptedActualBalanceAmount.reduce((acc, el, i) => {
151+
const { D } = el;
152+
const coef = 2n ** (BigInt(i) * ConfidentialAmount.CHUNK_BITS_BI);
154153

155-
const DCoef = D.multiply(coef);
154+
const DCoef = D.multiply(coef);
156155

157-
const DCoefX2 = DCoef.multiply(x2);
156+
const DCoefX2 = DCoef.multiply(x2);
158157

159-
return acc.add(DCoefX2);
160-
}, RistrettoPoint.ZERO),
161-
);
158+
return acc.add(DCoefX2);
159+
}, RistrettoPoint.ZERO),
160+
);
162161
const X2 = H_RISTRETTO.multiply(x3);
163162
const X3List = x1List.map((item, idx) => RistrettoPoint.BASE.multiply(item).add(H_RISTRETTO.multiply(x4List[idx])));
164163
const X4List = x4List.map((item) =>
@@ -185,8 +184,8 @@ export class ConfidentialWithdraw {
185184
const psInvert = ed25519modN(p * invertSLE);
186185

187186
const alpha1List = x1List.map((el, i) => {
188-
const pChunk = ed25519modN(p * this.confidentialAmountAfterWithdraw.amountChunks[i])
189-
return ed25519modN(el - pChunk)
187+
const pChunk = ed25519modN(p * this.confidentialAmountAfterWithdraw.amountChunks[i]);
188+
return ed25519modN(el - pChunk);
190189
});
191190
const alpha2 = ed25519modN(x2 - ps);
192191
const alpha3 = ed25519modN(x3 - psInvert);
@@ -255,8 +254,8 @@ export class ConfidentialWithdraw {
255254
const elCoef = el * coef;
256255

257256
return acc + elCoef;
258-
}, 0n)
259-
)
257+
}, 0n),
258+
),
260259
)
261260
.add(DOldSum.multiply(alpha2LE))
262261
.add(COldSum.multiply(p))
@@ -270,13 +269,12 @@ export class ConfidentialWithdraw {
270269
return a1iG.add(a4iH).add(pC);
271270
});
272271
const X4List = alpha4LEList.map((el, i) => {
273-
const a4iP = RistrettoPoint.fromHex(publicKeyU8).multiply(el)
272+
const a4iP = RistrettoPoint.fromHex(publicKeyU8).multiply(el);
274273

275-
const pDNew = opts.encryptedActualBalanceAfterWithdraw[i].D.multiply(p)
274+
const pDNew = opts.encryptedActualBalanceAfterWithdraw[i].D.multiply(p);
276275

277-
return a4iP.add(pDNew)
278-
},
279-
);
276+
return a4iP.add(pDNew);
277+
});
280278

281279
return (
282280
X1.equals(RistrettoPoint.fromHex(opts.sigmaProof.X1)) &&

confidential-assets/src/consts.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,3 @@ export const SIGMA_PROOF_TRANSFER_SIZE = PROOF_CHUNK_SIZE * 33; // bytes
77
export const SIGMA_PROOF_KEY_ROTATION_SIZE = PROOF_CHUNK_SIZE * 23; // bytes
88

99
export const SIGMA_PROOF_NORMALIZATION_SIZE = PROOF_CHUNK_SIZE * 21; // bytes
10-
11-
/** For now we only deploy to devnet as part of aptos-experimental, which lives at 0x7. */
12-
export const DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS = "0x7";
13-
export const MODULE_NAME = "confidential_asset";

confidential-assets/src/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
export * from './confidentialAmount'
2-
export * from './confidentialAsset'
3-
export * from './confidentialKeyRotation'
4-
export * from './confidentialNormalization'
5-
export * from './confidentialTransfer'
6-
export * from './confidentialWithdraw'
7-
export * from './consts'
8-
export * from './helpers'
9-
export * from './rangeProof'
10-
export * from './twistedEd25519'
11-
export * from './twistedElGamal'
12-
export * from './utils'
1+
export * from "./confidentialAmount";
2+
export * from "./confidentialAsset";
3+
export * from "./confidentialKeyRotation";
4+
export * from "./confidentialNormalization";
5+
export * from "./confidentialTransfer";
6+
export * from "./confidentialWithdraw";
7+
export * from "./consts";
8+
export * from "./helpers";
9+
export * from "./rangeProof";
10+
export * from "./twistedEd25519";
11+
export * from "./twistedElGamal";
12+
export * from "./utils";

0 commit comments

Comments
 (0)