Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ristretto255 #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
91 changes: 91 additions & 0 deletions crypto_core_ristretto255.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const ristretto255 = require('ristretto255')

const crypto_core_ristretto255_BYTES = 32
const crypto_core_ristretto255_HASHBYTES = 64
const crypto_core_ristretto255_SCALARBYTES = 32
const crypto_core_ristretto255_NONREDUCEDSCALARBYTES = 64

module.exports = {
crypto_core_ristretto255_BYTES,
crypto_core_ristretto255_HASHBYTES,
crypto_core_ristretto255_SCALARBYTES,
crypto_core_ristretto255_NONREDUCEDSCALARBYTES,
crypto_core_ristretto255_is_valid_point,
crypto_core_ristretto255_add,
crypto_core_ristretto255_sub,
crypto_core_ristretto255_from_hash,
crypto_core_ristretto255_random,
crypto_core_ristretto255_scalar_random,
crypto_core_ristretto255_scalar_invert,
crypto_core_ristretto255_scalar_negate,
crypto_core_ristretto255_scalar_add,
crypto_core_ristretto255_scalar_sub,
crypto_core_ristretto255_scalar_mul
}

function crypto_core_ristretto255_is_valid_point (p) {
check(p, crypto_core_ristretto255_BYTES)
return ristretto255.isValid(p)
}
function crypto_core_ristretto255_add (r, p, q) {
check(r, crypto_core_ristretto255_BYTES)
check(p, crypto_core_ristretto255_BYTES)
check(q, crypto_core_ristretto255_BYTES)
return r.set(ristretto255.add(p, q))
}
function crypto_core_ristretto255_sub (r, p, q) {
check(r, crypto_core_ristretto255_BYTES)
check(p, crypto_core_ristretto255_BYTES)
check(q, crypto_core_ristretto255_BYTES)
return r.set(ristretto255.sub(p, q))
}
function crypto_core_ristretto255_from_hash (p, r) {
check(p, crypto_core_ristretto255_BYTES)
check(r, crypto_core_ristretto255_HASHBYTES)
return p.set(ristretto255.fromHash(r))
}
// crypto_core_ristretto255_from_string () {}
// crypto_core_ristretto255_from_string_ro () {}
function crypto_core_ristretto255_random (p) {
check(p, crypto_core_ristretto255_BYTES)
return p.set(ristretto255.getRandom())
}
function crypto_core_ristretto255_scalar_random (r) {
check(r, crypto_core_ristretto255_SCALARBYTES)
return r.set(ristretto255.scalar.getRandom())
}
function crypto_core_ristretto255_scalar_invert (recip, s) {
check(recip, crypto_core_ristretto255_SCALARBYTES)
check(s, crypto_core_ristretto255_SCALARBYTES)
return recip.set(ristretto255.scalar.invert(s))
}
function crypto_core_ristretto255_scalar_negate (neg, s) {
check(neg, crypto_core_ristretto255_SCALARBYTES)
check(s, crypto_core_ristretto255_SCALARBYTES)
return neg.set(ristretto255.scalar.negate(s))
}
// function crypto_core_ristretto255_scalar_complement (comp, s) {}
function crypto_core_ristretto255_scalar_add (z, x, y) {
check(z, crypto_core_ristretto255_SCALARBYTES)
check(x, crypto_core_ristretto255_SCALARBYTES)
check(y, crypto_core_ristretto255_SCALARBYTES)
return z.set(ristretto255.scalar.add(x, y))
}
function crypto_core_ristretto255_scalar_sub (z, x, y) {
check(z, crypto_core_ristretto255_SCALARBYTES)
check(x, crypto_core_ristretto255_SCALARBYTES)
check(y, crypto_core_ristretto255_SCALARBYTES)
return z.set(ristretto255.scalar.sub(x, y))
}
function crypto_core_ristretto255_scalar_mul (z, x, y) {
check(z, crypto_core_ristretto255_SCALARBYTES)
check(x, crypto_core_ristretto255_SCALARBYTES)
check(y, crypto_core_ristretto255_SCALARBYTES)
return z.set(ristretto255.scalar.mul(x, y))
}
// function crypto_core_ristretto255_scalar_reduce (r, s) {}
// function crypto_core_ristretto255_scalar_is_canonical (s) {}

function check (buf, len) {
if (!buf || (len && buf.length < len)) throw new Error('Argument must be a buffer' + (len ? ' of length ' + len : ''))
}
31 changes: 30 additions & 1 deletion crypto_scalarmult.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable camelcase, one-var */
const ristretto255 = require('ristretto255')
const { crypto_core_ristretto255_BYTES, crypto_core_ristretto255_SCALARBYTES } = require('./crypto_core_ristretto255')
const { _9, _121665, gf, inv25519, pack25519, unpack25519, sel25519, A, M, Z, S } = require('./internal/ed25519')

const crypto_scalarmult_BYTES = 32
Expand All @@ -8,7 +10,34 @@ module.exports = {
crypto_scalarmult,
crypto_scalarmult_base,
crypto_scalarmult_BYTES,
crypto_scalarmult_SCALARBYTES
crypto_scalarmult_SCALARBYTES,
crypto_scalarmult_ristretto255,
crypto_scalarmult_ristretto255_base,
crypto_scalarmult_ristretto255_bytes,
crypto_scalarmult_ristretto255_scalarbytes,
crypto_scalarmult_ristretto255_BYTES: crypto_core_ristretto255_BYTES,
crypto_scalarmult_ristretto255_SCALARBYTES: crypto_core_ristretto255_SCALARBYTES
}

function crypto_scalarmult_ristretto255 (q, n, p) {
check(q, crypto_core_ristretto255_BYTES)
check(n, crypto_core_ristretto255_SCALARBYTES)
check(p, crypto_core_ristretto255_BYTES)
return q.set(ristretto255.scalarMult(n, p))
}

function crypto_scalarmult_ristretto255_base (q, n) {
check(q, crypto_core_ristretto255_BYTES)
check(n, crypto_core_ristretto255_SCALARBYTES)
return q.set(ristretto255.scalarMultBase(n))
}

function crypto_scalarmult_ristretto255_bytes () {
return crypto_scalarmult_ristretto255_BYTES
}

function crypto_scalarmult_ristretto255_scalarbytes () {
return crypto_scalarmult_ristretto255_SCALARBYTES
}

function crypto_scalarmult (q, n, p) {
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ forward(require('./crypto_kdf'))
forward(require('./crypto_kx'))
forward(require('./crypto_aead'))
forward(require('./crypto_onetimeauth'))
forward(require('./crypto_core_ristretto255'))
forward(require('./crypto_scalarmult'))
forward(require('./crypto_secretbox'))
forward(require('./crypto_shorthash'))
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"blake2b": "^2.1.1",
"chacha20-universal": "^1.0.4",
"nanoassert": "^2.0.0",
"ristretto255": "^0.1.2",
"sha256-universal": "^1.1.0",
"sha512-universal": "^1.1.0",
"siphash24": "^1.0.1",
Expand Down