Skip to content

Commit

Permalink
Updated TwistedElGamalCiphertext
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausKidman committed Aug 26, 2024
1 parent 1d185b7 commit 334b908
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/core/crypto/twistedElGamal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { bytesToNumberLE, ensureBytes } from "@noble/curves/abstract/utils";
import { randomBytes } from "crypto";
import { HexInput } from "../../types";
import { TwistedEd25519PrivateKey, TwistedEd25519PublicKey } from "./twistedEd25519";
import { Hex } from "../hex";

export type RistPoint = InstanceType<typeof RistrettoPoint>

/**
* Twisted ElGamal encryption/decryption
Expand Down Expand Up @@ -69,12 +70,13 @@ export class TwistedElGamal {
const m = amount
const H = RistrettoPoint.fromHex(TwistedElGamal.HASH_BASE_POINT)
const r = mod(bytesToNumberLE(rBytes), ed25519.CURVE.n)
const D = RistrettoPoint.fromHex(publicKey.toUint8Array()).multiply(r).toRawBytes()
const rG = RistrettoPoint.BASE.multiply(r)
const mH = m === BigInt(0)
? RistrettoPoint.ZERO
: H.multiply(m)
const C = mH.add(rG).toRawBytes()

const D = RistrettoPoint.fromHex(publicKey.toUint8Array()).multiply(r)
const C = mH.add(rG)

return new TwistedElGamalCiphertext(C, D);
}
Expand All @@ -93,17 +95,18 @@ export class TwistedElGamal {
const { C, D } = ciphertext
const H = RistrettoPoint.fromHex(TwistedElGamal.HASH_BASE_POINT)
const modS = mod(bytesToNumberLE(privateKey.toUint8Array()), ed25519.CURVE.n)
const sD = RistrettoPoint.fromHex(D.toUint8Array()).multiply(modS)
const mH = RistrettoPoint.fromHex(C.toUint8Array()).subtract(sD)
const sD = RistrettoPoint.fromHex(D.toRawBytes()).multiply(modS)
const mH = RistrettoPoint.fromHex(C.toRawBytes()).subtract(sD)

let searchablePoint = H
let amount = startAmount ?? BigInt(0)
if (amount === BigInt(0)){
if (mH.equals(RistrettoPoint.ZERO)) return BigInt(0)

amount += BigInt(1)
}

let searchablePoint = H.multiply(amount)

while (!mH.equals(searchablePoint)) {
if (amount >= ed25519.CURVE.n) throw new Error("Error when decrypting the amount")

Expand All @@ -118,12 +121,16 @@ export class TwistedElGamal {
* Points of ciphertext encrypted by Twisted ElGamal
*/
export class TwistedElGamalCiphertext {
readonly C: Hex;
readonly C: RistPoint;

readonly D: Hex;
readonly D: RistPoint;

constructor(C: HexInput, D: HexInput) {
this.C = Hex.fromHexInput(C);
this.D = Hex.fromHexInput(D);
constructor(C: HexInput | RistPoint, D: HexInput | RistPoint) {
this.C = C instanceof RistrettoPoint
? C
: RistrettoPoint.fromHex(C);
this.D = D instanceof RistrettoPoint
? D
: RistrettoPoint.fromHex(D);
}
}

0 comments on commit 334b908

Please sign in to comment.