Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion confidential-assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"build:types": "tsc -p tsconfig.build.json --outDir ./dist/types --declaration --emitDeclarationOnly",
"build:cjs": "npx swc src -d ./dist/cjs --config-file .swcrc -C module.type=commonjs --strip-leading-paths --copy-files",
"build:esm": "npx swc src -d ./dist/esm --config-file .swcrc -C module.type=es6 isModule=true --strip-leading-paths --copy-files",
"test": "pnpm jest ./tests/**/*.test.ts --testPathIgnorePatterns={./tests/units/api,./tests/e2e} --passWithNoTests"
"test:units": "pnpm jest tests/units",
"test:e2e": "pnpm jest tests/e2e"
},
"dependencies": {
"@aptos-labs/confidential-asset-wasm-bindings": "^0.0.2",
Expand Down
28 changes: 25 additions & 3 deletions confidential-assets/src/confidentialAsset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import { Account, AccountAddress, AccountAddressInput, AnyNumber, Aptos, AptosConfig, CommittedTransactionResponse, HexInput, InputGenerateSingleSignerRawTransactionArgs, InputGenerateTransactionPayloadData, LedgerVersionArg, MoveStructId, SimpleTransaction } from "@aptos-labs/ts-sdk";
import {
Account,
AccountAddress,
AccountAddressInput,
AnyNumber,
Aptos,
AptosConfig,
CommittedTransactionResponse,
HexInput,
InputGenerateSingleSignerRawTransactionArgs,
InputGenerateTransactionPayloadData,
LedgerVersionArg,
MoveStructId,
SimpleTransaction,
} from "@aptos-labs/ts-sdk";
import { TwistedElGamalCiphertext } from "./twistedElGamal";
import { ConfidentialNormalization, CreateConfidentialNormalizationOpArgs } from "./confidentialNormalization";
import { ConfidentialKeyRotation, CreateConfidentialKeyRotationOpArgs } from "./confidentialKeyRotation";
Expand All @@ -11,7 +25,10 @@ import { ConfidentialAmount } from "./confidentialAmount";
import { CreateConfidentialTransferOpArgs, ConfidentialTransfer } from "./confidentialTransfer";
import { CreateConfidentialWithdrawOpArgs, ConfidentialWithdraw } from "./confidentialWithdraw";
import { TwistedEd25519PublicKey, TwistedEd25519PrivateKey } from "./twistedEd25519";
import { DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS, MODULE_NAME } from "./consts";

/** For now we only deploy to devnet as part of aptos-experimental, which lives at 0x7. */
const DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS = "0x7";
const MODULE_NAME = "confidential_asset";

export type ConfidentialBalanceResponse = {
chunks: {
Expand All @@ -32,7 +49,12 @@ export class ConfidentialAsset {
client: Aptos;
confidentialAssetModuleAddress: string;

constructor(readonly config: AptosConfig, { confidentialAssetModuleAddress = DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS }: { confidentialAssetModuleAddress?: string } = {}) {
constructor(
readonly config: AptosConfig,
{
confidentialAssetModuleAddress = DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS,
}: { confidentialAssetModuleAddress?: string } = {},
) {
this.client = new Aptos(config);
this.confidentialAssetModuleAddress = confidentialAssetModuleAddress;
}
Expand Down
47 changes: 17 additions & 30 deletions confidential-assets/src/confidentialKeyRotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,12 @@ export class ConfidentialKeyRotation {
const alpha2 = proofArr[3];
const alpha3 = proofArr[4];
const alpha4 = proofArr[5];
const alpha5List = proofArr.slice(
6,
6 + ConfidentialAmount.CHUNKS_COUNT,
);
const X1 = proofArr[
6 + ConfidentialAmount.CHUNKS_COUNT
];
const X2 = proofArr[
7 + ConfidentialAmount.CHUNKS_COUNT
];
const X3 = proofArr[
8 + ConfidentialAmount.CHUNKS_COUNT
];
const X4List = proofArr.slice(
8 + ConfidentialAmount.CHUNKS_COUNT,
8 + 2 * ConfidentialAmount.CHUNKS_COUNT,
);
const X5List = proofArr.slice(
8 + 2 * ConfidentialAmount.CHUNKS_COUNT,
);
const alpha5List = proofArr.slice(6, 6 + ConfidentialAmount.CHUNKS_COUNT);
const X1 = proofArr[6 + ConfidentialAmount.CHUNKS_COUNT];
const X2 = proofArr[7 + ConfidentialAmount.CHUNKS_COUNT];
const X3 = proofArr[8 + ConfidentialAmount.CHUNKS_COUNT];
const X4List = proofArr.slice(8 + ConfidentialAmount.CHUNKS_COUNT, 8 + 2 * ConfidentialAmount.CHUNKS_COUNT);
const X5List = proofArr.slice(8 + 2 * ConfidentialAmount.CHUNKS_COUNT);

return {
alpha1List,
Expand Down Expand Up @@ -159,11 +145,11 @@ export class ConfidentialKeyRotation {
ed25519modN(
x1List.reduce((acc, el, i) => {
const coef = 2n ** (BigInt(i) * ConfidentialAmount.CHUNK_BITS_BI);
const x1i = el * coef
const x1i = el * coef;

return acc + x1i;
}, 0n)
)
}, 0n),
),
).add(
this.currEncryptedBalance
.reduce(
Expand All @@ -178,11 +164,11 @@ export class ConfidentialKeyRotation {
const x1iG = RistrettoPoint.BASE.multiply(el);
const x5iH = H_RISTRETTO.multiply(x5List[index]);

return x1iG.add(x5iH)
return x1iG.add(x5iH);
});
const X5List = x5List.map((el) => {
const Pnew = RistrettoPoint.fromHex(this.newDecryptionKey.publicKey().toUint8Array())
return Pnew.multiply(el)
const Pnew = RistrettoPoint.fromHex(this.newDecryptionKey.publicKey().toUint8Array());
return Pnew.multiply(el);
});

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

return ed25519modN(el - pri)
return ed25519modN(el - pri);
});

return {
Expand Down Expand Up @@ -282,10 +268,11 @@ export class ConfidentialKeyRotation {
const a1i = el * coef;

return acc + a1i;
}, 0n)
)
}, 0n),
),
)
.add(DOldSum.multiply(alpha2LE)).add(COldSum.multiply(p));
.add(DOldSum.multiply(alpha2LE))
.add(COldSum.multiply(p));
const X2 = H_RISTRETTO.multiply(alpha3LE).add(pkOldRist.multiply(p));
const X3 = H_RISTRETTO.multiply(alpha4LE).add(pkNewRist.multiply(p));
const X4List = alpha1LEList.map((el, i) => {
Expand Down
35 changes: 15 additions & 20 deletions confidential-assets/src/confidentialNormalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,9 @@ export class ConfidentialNormalization {
const alpha1List = proofArr.slice(0, 3);
const alpha2 = proofArr[3];
const alpha3 = proofArr[4];
const alpha4List = proofArr.slice(
5,
5 + ConfidentialAmount.CHUNKS_COUNT,
);
const X1 = proofArr[
5 + 2 * ConfidentialAmount.CHUNKS_COUNT
];
const X2 = proofArr[
5 + 2 * ConfidentialAmount.CHUNKS_COUNT + 1
];
const alpha4List = proofArr.slice(5, 5 + ConfidentialAmount.CHUNKS_COUNT);
const X1 = proofArr[5 + 2 * ConfidentialAmount.CHUNKS_COUNT];
const X2 = proofArr[5 + 2 * ConfidentialAmount.CHUNKS_COUNT + 1];
const X3List = proofArr.slice(
5 + 2 * ConfidentialAmount.CHUNKS_COUNT + 2,
5 + 3 * ConfidentialAmount.CHUNKS_COUNT + 2,
Expand Down Expand Up @@ -143,11 +136,11 @@ export class ConfidentialNormalization {
ed25519modN(
x1List.reduce((acc, el, i) => {
const coef = 2n ** (BigInt(i) * ConfidentialAmount.CHUNK_BITS_BI);
const x1i = el * coef
const x1i = el * coef;

return acc + x1i
}, 0n)
)
return acc + x1i;
}, 0n),
),
).add(
this.unnormalizedEncryptedBalance
.reduce(
Expand All @@ -162,7 +155,7 @@ export class ConfidentialNormalization {

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

return x1iG.add(x4iH)
return x1iG.add(x4iH);
});
const X4List = x4List.map((el) =>
RistrettoPoint.fromHex(this.decryptionKey.publicKey().toUint8Array()).multiply(el),
Expand Down Expand Up @@ -197,7 +190,7 @@ export class ConfidentialNormalization {
const alpha4List = x4List.map((el, i) => {
const pri = ed25519modN(p * this.randomness[i]);

return ed25519modN(el - pri)
return ed25519modN(el - pri);
});

return {
Expand Down Expand Up @@ -261,8 +254,10 @@ export class ConfidentialNormalization {
const alpha1i = el * coef;
return acc + alpha1i;
}, 0n),
)
).add(alpha2D).add(pBalOld);
),
)
.add(alpha2D)
.add(pBalOld);
const X2 = alpha3H.add(pP);
const X3List = alpha1LEList.map((el, i) => {
const a1iG = RistrettoPoint.BASE.multiply(el);
Expand All @@ -272,9 +267,9 @@ export class ConfidentialNormalization {
});
const X4List = alpha4LEList.map((el, i) => {
const a4iP = RistrettoPoint.fromHex(publicKeyU8).multiply(el);
const pDnew = opts.normalizedEncryptedBalance[i].D.multiply(p)
const pDnew = opts.normalizedEncryptedBalance[i].D.multiply(p);

return a4iP.add(pDnew)
return a4iP.add(pDnew);
});

return (
Expand Down
25 changes: 12 additions & 13 deletions confidential-assets/src/confidentialTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,11 @@ export class ConfidentialTransfer {

const alpha1List = baseProofArray.slice(0, half);
const alpha2 = baseProofArray[half];
const alpha3List = baseProofArray.slice(
half + 1,
half + 1 + ConfidentialAmount.CHUNKS_COUNT,
)
const alpha3List = baseProofArray.slice(half + 1, half + 1 + ConfidentialAmount.CHUNKS_COUNT);
const alpha4List = baseProofArray.slice(
half + 1 + ConfidentialAmount.CHUNKS_COUNT,
half + 1 + ConfidentialAmount.CHUNKS_COUNT * 2,
)
);
const alpha5 = baseProofArray[half + 1 + ConfidentialAmount.CHUNKS_COUNT * 2];

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

return acc + x1i
}, 0n)
)
return acc + x1i;
}, 0n),
),
)
.add(DBal.multiply(x2))
.subtract(DNewBal.multiply(x2))
Expand All @@ -298,7 +295,7 @@ export class ConfidentialTransfer {
const x1iG = RistrettoPoint.BASE.multiply(el);
const x3iH = H_RISTRETTO.multiply(x3List[idx]);

return x1iG.add(x3iH).toRawBytes()
return x1iG.add(x3iH).toRawBytes();
});

const X7List =
Expand Down Expand Up @@ -329,7 +326,9 @@ export class ConfidentialTransfer {
const sLE = bytesToNumberLE(this.senderDecryptionKey.toUint8Array());
const invertSLE = ed25519InvertN(sLE);

const alpha1List = x1List.map((x1, idx) => ed25519modN(x1 - ed25519modN(p * this.confidentialAmountAfterTransfer.amountChunks[idx])));
const alpha1List = x1List.map((x1, idx) =>
ed25519modN(x1 - ed25519modN(p * this.confidentialAmountAfterTransfer.amountChunks[idx])),
);
const alpha2 = ed25519modN(x2 - p * sLE);
const alpha3List = x3List.map((el, idx) => ed25519modN(BigInt(el) - BigInt(p) * BigInt(this.randomness[idx])));
const alpha4List = x4List
Expand Down Expand Up @@ -438,8 +437,8 @@ export class ConfidentialTransfer {
const a1i = curr * coef;

return acc + a1i;
}, 0n)
)
}, 0n),
),
)
.add(oldDSum.multiply(alpha2LE))
.subtract(newDSum.multiply(alpha2LE))
Expand Down
40 changes: 19 additions & 21 deletions confidential-assets/src/confidentialWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,20 @@ export class ConfidentialWithdraw {
const x1i = el * coef;

return acc + x1i;
}, 0n)
)
)
.add(
this.encryptedActualBalanceAmount.reduce((acc, el, i) => {
const { D } = el;
const coef = 2n ** (BigInt(i) * ConfidentialAmount.CHUNK_BITS_BI);
}, 0n),
),
).add(
this.encryptedActualBalanceAmount.reduce((acc, el, i) => {
const { D } = el;
const coef = 2n ** (BigInt(i) * ConfidentialAmount.CHUNK_BITS_BI);

const DCoef = D.multiply(coef);
const DCoef = D.multiply(coef);

const DCoefX2 = DCoef.multiply(x2);
const DCoefX2 = DCoef.multiply(x2);

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

const alpha1List = x1List.map((el, i) => {
const pChunk = ed25519modN(p * this.confidentialAmountAfterWithdraw.amountChunks[i])
return ed25519modN(el - pChunk)
const pChunk = ed25519modN(p * this.confidentialAmountAfterWithdraw.amountChunks[i]);
return ed25519modN(el - pChunk);
});
const alpha2 = ed25519modN(x2 - ps);
const alpha3 = ed25519modN(x3 - psInvert);
Expand Down Expand Up @@ -255,8 +254,8 @@ export class ConfidentialWithdraw {
const elCoef = el * coef;

return acc + elCoef;
}, 0n)
)
}, 0n),
),
)
.add(DOldSum.multiply(alpha2LE))
.add(COldSum.multiply(p))
Expand All @@ -270,13 +269,12 @@ export class ConfidentialWithdraw {
return a1iG.add(a4iH).add(pC);
});
const X4List = alpha4LEList.map((el, i) => {
const a4iP = RistrettoPoint.fromHex(publicKeyU8).multiply(el)
const a4iP = RistrettoPoint.fromHex(publicKeyU8).multiply(el);

const pDNew = opts.encryptedActualBalanceAfterWithdraw[i].D.multiply(p)
const pDNew = opts.encryptedActualBalanceAfterWithdraw[i].D.multiply(p);

return a4iP.add(pDNew)
},
);
return a4iP.add(pDNew);
});

return (
X1.equals(RistrettoPoint.fromHex(opts.sigmaProof.X1)) &&
Expand Down
4 changes: 0 additions & 4 deletions confidential-assets/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ export const SIGMA_PROOF_TRANSFER_SIZE = PROOF_CHUNK_SIZE * 33; // bytes
export const SIGMA_PROOF_KEY_ROTATION_SIZE = PROOF_CHUNK_SIZE * 23; // bytes

export const SIGMA_PROOF_NORMALIZATION_SIZE = PROOF_CHUNK_SIZE * 21; // bytes

/** For now we only deploy to devnet as part of aptos-experimental, which lives at 0x7. */
export const DEFAULT_CONFIDENTIAL_COIN_MODULE_ADDRESS = "0x7";
export const MODULE_NAME = "confidential_asset";
24 changes: 12 additions & 12 deletions confidential-assets/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export * from './confidentialAmount'
export * from './confidentialAsset'
export * from './confidentialKeyRotation'
export * from './confidentialNormalization'
export * from './confidentialTransfer'
export * from './confidentialWithdraw'
export * from './consts'
export * from './helpers'
export * from './rangeProof'
export * from './twistedEd25519'
export * from './twistedElGamal'
export * from './utils'
export * from "./confidentialAmount";
export * from "./confidentialAsset";
export * from "./confidentialKeyRotation";
export * from "./confidentialNormalization";
export * from "./confidentialTransfer";
export * from "./confidentialWithdraw";
export * from "./consts";
export * from "./helpers";
export * from "./rangeProof";
export * from "./twistedEd25519";
export * from "./twistedElGamal";
export * from "./utils";
Loading
Loading