Skip to content

Commit

Permalink
add desc for {i,j} variables for transfer sigma proof generation, fix…
Browse files Browse the repository at this point in the history
… typo for normalization
  • Loading branch information
lukachi committed Dec 16, 2024
1 parent ab84351 commit 700e6bf
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
12 changes: 7 additions & 5 deletions src/api/veiledCoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const VEILED_COIN_MODULE_ADDRESS = "0xcbd21318a3fe6eb6c01f3c371d9aca238a6cd7201d
export class VeiledCoin {
constructor(readonly config: AptosConfig) {}

static VEILED_COIN_MODULE_ADDRESS = VEILED_COIN_MODULE_ADDRESS;

async getBalance(args: {
accountAddress: AccountAddress;
tokenAddress: string;
Expand Down Expand Up @@ -197,19 +199,19 @@ export class VeiledCoin {
});

if (!isNormalized) {
const aliceBalances = await this.getBalance({
const accountBalance = await this.getBalance({
accountAddress: AccountAddress.from(args.sender),
tokenAddress: args.tokenAddress,
});

const aliceVB = await VeiledAmount.fromEncrypted(aliceBalances.actual, args.decryptionKey);
const accountVB = await VeiledAmount.fromEncrypted(accountBalance.actual, args.decryptionKey);

const normalizationTx = await VeiledCoin.buildNormalizationTxPayload({
decryptionKey: args.decryptionKey,
sender: args.sender,
tokenAddress: args.tokenAddress,
unnormilizedEncryptedBalance: aliceBalances.pending,
balanceAmount: aliceVB.amount,
unnormalizedEncryptedBalance: accountBalance.actual,
balanceAmount: accountVB.amount,
});
txList.push(normalizationTx);
}
Expand Down Expand Up @@ -460,7 +462,7 @@ export class VeiledCoin {
): Promise<InputGenerateTransactionPayloadData> {
const veiledNormalization = await VeiledNormalization.create({
decryptionKey: args.decryptionKey,
unnormilizedEncryptedBalance: args.unnormilizedEncryptedBalance,
unnormalizedEncryptedBalance: args.unnormalizedEncryptedBalance,
balanceAmount: args.balanceAmount,
randomness: args.randomness,
});
Expand Down
24 changes: 12 additions & 12 deletions src/core/crypto/veiled/veiledNormalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export type VeiledNormalizationSigmaProof = {

export type CreateVeiledNormalizationOpArgs = {
decryptionKey: TwistedEd25519PrivateKey;
unnormilizedEncryptedBalance: TwistedElGamalCiphertext[];
unnormalizedEncryptedBalance: TwistedElGamalCiphertext[];
balanceAmount: bigint;
randomness?: bigint[];
};

export class VeiledNormalization {
decryptionKey: TwistedEd25519PrivateKey;

unnormilizedEncryptedBalance: TwistedElGamalCiphertext[];
unnormalizedEncryptedBalance: TwistedElGamalCiphertext[];

balanceAmount: bigint;

Expand All @@ -41,13 +41,13 @@ export class VeiledNormalization {

constructor(args: {
decryptionKey: TwistedEd25519PrivateKey;
unnormilizedEncryptedBalance: TwistedElGamalCiphertext[];
unnormalizedEncryptedBalance: TwistedElGamalCiphertext[];
balanceAmount: bigint;
normalizedVeiledAmount: VeiledAmount;
randomness: bigint[];
}) {
this.decryptionKey = args.decryptionKey;
this.unnormilizedEncryptedBalance = args.unnormilizedEncryptedBalance;
this.unnormalizedEncryptedBalance = args.unnormalizedEncryptedBalance;
this.balanceAmount = args.balanceAmount;
this.normalizedVeiledAmount = args.normalizedVeiledAmount;
this.randomness = args.randomness;
Expand All @@ -61,7 +61,7 @@ export class VeiledNormalization {

return new VeiledNormalization({
decryptionKey: args.decryptionKey,
unnormilizedEncryptedBalance: args.unnormilizedEncryptedBalance,
unnormalizedEncryptedBalance: args.unnormalizedEncryptedBalance,
balanceAmount: args.balanceAmount,
normalizedVeiledAmount,
randomness,
Expand Down Expand Up @@ -132,7 +132,7 @@ export class VeiledNormalization {
const x5List = ed25519GenListOfRandom();

const X1 = RistrettoPoint.BASE.multiply(x1).add(
this.unnormilizedEncryptedBalance
this.unnormalizedEncryptedBalance
.reduce(
(acc, ciphertext, i) => acc.add(ciphertext.D.multiply(2n ** (BigInt(i) * VeiledAmount.CHUNK_BITS_BI))),
RistrettoPoint.ZERO,
Expand All @@ -150,7 +150,7 @@ export class VeiledNormalization {
RistrettoPoint.BASE.toRawBytes(),
H_RISTRETTO.toRawBytes(),
this.decryptionKey.publicKey().toUint8Array(),
...this.unnormilizedEncryptedBalance.map((el) => el.serialize()).flat(),
...this.unnormalizedEncryptedBalance.map((el) => el.serialize()).flat(),
...this.normalizedVeiledAmount.amountEncrypted!.map((el) => el.serialize()).flat(),
X1.toRawBytes(),
X2.toRawBytes(),
Expand Down Expand Up @@ -190,7 +190,7 @@ export class VeiledNormalization {
publicKey: TwistedEd25519PublicKey;
sigmaProof: VeiledNormalizationSigmaProof;

unnormilizedEncryptedBalance: TwistedElGamalCiphertext[];
unnormalizedEncryptedBalance: TwistedElGamalCiphertext[];
normalizedEncryptedBalance: TwistedElGamalCiphertext[];
}): boolean {
const publicKeyU8 = publicKeyToU8(opts.publicKey);
Expand All @@ -206,21 +206,21 @@ export class VeiledNormalization {
RistrettoPoint.BASE.toRawBytes(),
H_RISTRETTO.toRawBytes(),
publicKeyU8,
...opts.unnormilizedEncryptedBalance.map((el) => el.serialize()).flat(),
...opts.unnormalizedEncryptedBalance.map((el) => el.serialize()).flat(),
...opts.normalizedEncryptedBalance.map((el) => el.serialize()).flat(),
opts.sigmaProof.X1,
opts.sigmaProof.X2,
...opts.sigmaProof.X3List,
...opts.sigmaProof.X4List,
);
const alpha1G = RistrettoPoint.BASE.multiply(alpha1LE);
const alpha2D = opts.unnormilizedEncryptedBalance
const alpha2D = opts.unnormalizedEncryptedBalance
.reduce(
(acc, { D }, i) => acc.add(D.multiply(2n ** (BigInt(i) * VeiledAmount.CHUNK_BITS_BI))),
RistrettoPoint.ZERO,
)
.multiply(alpha2LE);
const pBlaOld = opts.unnormilizedEncryptedBalance
const pBalOld = opts.unnormalizedEncryptedBalance
.reduce((acc, ciphertext, i) => {
const chunk = ciphertext.C.multiply(2n ** (BigInt(i) * VeiledAmount.CHUNK_BITS_BI));
return acc.add(chunk);
Expand All @@ -229,7 +229,7 @@ export class VeiledNormalization {

const alpha3H = H_RISTRETTO.multiply(alpha3LE);
const pP = RistrettoPoint.fromHex(publicKeyU8).multiply(p);
const X1 = alpha1G.add(alpha2D).add(pBlaOld);
const X1 = alpha1G.add(alpha2D).add(pBalOld);
const X2 = alpha3H.add(pP);
const X3List = alpha4LEList.map((a, i) => {
const aG = RistrettoPoint.BASE.multiply(a);
Expand Down
1 change: 1 addition & 0 deletions src/core/crypto/veiled/veiledTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export class VeiledTransfer {
TwistedElGamal.encryptWithPK(chunk, this.senderDecryptionKey.publicKey(), this.randomness[i]),
);

// Prover selects random x1, x2, x3i[], x4j[], x5, x6i[], where i in {0, 3} and j in {0, 1}
const i = 4;
const j = 2;

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/api/veiledCoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ describe("Veiled balance api", () => {
const normalizeTx = await aptos.veiledCoin.normalizeUserBalance({
tokenAddress: TOKEN_ADDRESS,
decryptionKey: aliceDecryptionKey,
unnormilizedEncryptedBalance: unnormalizedAliceEncryptedBalance,
unnormalizedEncryptedBalance: unnormalizedAliceEncryptedBalance,
balanceAmount: unnormalizedVeiledAmount.amount,

sender: alice.accountAddress,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/veiled/api/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe("Normalize", () => {
const normalizeTx = await aptos.veiledCoin.normalizeUserBalance({
tokenAddress: TOKEN_ADDRESS,
decryptionKey: aliceVeiled,
unnormilizedEncryptedBalance: balances.pending.amountEncrypted!,
balanceAmount: balances.pending.amount,
unnormalizedEncryptedBalance: balances.actual.amountEncrypted!,
balanceAmount: balances.actual.amount,

sender: alice.accountAddress,
});
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/veiled/veiledProofs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe("Generate 'veiled coin' proofs", () => {
test("Generate normalization sigma proof", async () => {
veiledNormalization = await VeiledNormalization.create({
decryptionKey: aliceVeiledDecryptionKey,
unnormilizedEncryptedBalance: unnormalizedAliceVeiledAmount.amountEncrypted!,
unnormalizedEncryptedBalance: unnormalizedAliceVeiledAmount.amountEncrypted!,
balanceAmount: unnormalizedAliceVeiledAmount.amount,
});

Expand All @@ -309,7 +309,7 @@ describe("Generate 'veiled coin' proofs", () => {
const isValid = VeiledNormalization.verifySigmaProof({
publicKey: aliceVeiledDecryptionKey.publicKey(),
sigmaProof: veiledNormalizationSigmaProof,
unnormilizedEncryptedBalance: unnormalizedAliceVeiledAmount.amountEncrypted!,
unnormalizedEncryptedBalance: unnormalizedAliceVeiledAmount.amountEncrypted!,
normalizedEncryptedBalance: veiledNormalization.normalizedVeiledAmount!.amountEncrypted!,
});

Expand Down

0 comments on commit 700e6bf

Please sign in to comment.