Skip to content

Commit

Permalink
Update elgamal syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
msinkec committed Oct 27, 2023
1 parent 381feff commit dc8a3b2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/contracts/elGamalHE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ export type CT = {

export class ElGamalHE extends SmartContract {
@prop(true)
salarySum: CT
expenseSum: CT

constructor(salarySum: CT) {
super(...arguments)
this.salarySum = salarySum
this.expenseSum = salarySum
}

@method()
public add(toAdd: CT) {
// Add encrypted value to the total sum.
this.salarySum = ElGamalHE.addCT(this.salarySum, toAdd)
this.expenseSum = ElGamalHE.addCT(this.expenseSum, toAdd)

const outputs =
this.buildStateOutput(this.ctx.utxo.value) +
this.buildChangeOutput()
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch')
}

// Add homomorphicly.
@method()
static addCT(ct0: CT, ct1: CT): CT {
const res: CT = {
Expand Down
10 changes: 5 additions & 5 deletions tests/elGamalHE.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ describe('Heavy: Test SmartContract `ElGamalHE`', () => {
const Q = SECP256K1.pubKey2Point(PubKey(pub.toByteString()))

// Encrypt initial value of 0.
const [salarySum] = encryptNumber(0n, Q)
const [expenseSum] = encryptNumber(0n, Q)

// Instantiate and deploy contract.
const instance = new ElGamalHE(salarySum)
const instance = new ElGamalHE(expenseSum)
await instance.connect(getDefaultSigner())

await instance.deploy(1)
Expand All @@ -66,8 +66,8 @@ describe('Heavy: Test SmartContract `ElGamalHE`', () => {

// Add encrypted amount (100) to the total sum of the contract.
const [toAdd] = encryptNumber(100n, Q)
nextInstance.salarySum = ElGamalHE.addCT(
currentInstance.salarySum,
nextInstance.expenseSum = ElGamalHE.addCT(
currentInstance.expenseSum,
toAdd
)

Expand All @@ -85,7 +85,7 @@ describe('Heavy: Test SmartContract `ElGamalHE`', () => {
}

// Decrypt and check end result.
const mG = decrypt(currentInstance.salarySum, k)
const mG = decrypt(currentInstance.expenseSum, k)
const _mG = SECP256K1.mulByScalar(SECP256K1.G, 500n)
expect(mG.x === _mG.x && mG.y === _mG.y).to.be.true

Expand Down

0 comments on commit dc8a3b2

Please sign in to comment.