UNPKG

@stellot/crypto

Version:

Crypto libraries for front and backend

34 lines (33 loc) 1.01 kB
import { ed25519 } from './index'; import { randomScalar } from './utils'; export default class VoterSession { constructor(P, R, a, b) { this.a = a || randomScalar(); this.b = b || randomScalar(); this.P = ed25519.decodePoint(P); this.R = ed25519.decodePoint(R) .add(ed25519.curve.g.mul(this.a).add(this.P.mul(this.b))); } challenge(message) { return ed25519 .hashInt(ed25519.encodePoint(this.R), ed25519.encodePoint(this.P), message) .add(this.b) .umod(ed25519.curve.n); } proof() { return { a: this.a, b: this.b }; } signature(s) { const S = s.add(this.a).umod(ed25519.curve.n); const signature = ed25519.encodePoint(this.R).concat(ed25519.encodeInt(S)); return signature; } toJSON() { return { a: this.a, b: this.b, P: ed25519.encodePoint(this.P), R: ed25519.encodePoint(this.R), }; } }