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

Ristretto bindings #140

Closed
wants to merge 7 commits into from
Closed
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
224 changes: 224 additions & 0 deletions binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,209 @@ napi_value sn_crypto_core_ed25519_scalar_sub (napi_env env, napi_callback_info i
return NULL;
}

napi_value sn_crypto_scalarmult_ristretto255_base (napi_env env, napi_callback_info info) {
SN_ARGV(2, crypto_scalarmult_ristretto255_base)

SN_ARGV_TYPEDARRAY(q, 0)
SN_ARGV_TYPEDARRAY(n, 1)

SN_ASSERT_LENGTH(q_size, crypto_scalarmult_ristretto255_BYTES, "q")
SN_ASSERT_LENGTH(n_size, crypto_scalarmult_ristretto255_SCALARBYTES, "n")

SN_RETURN(crypto_scalarmult_ristretto255_base(q_data, n_data), "failed to derive public key")
}

napi_value sn_crypto_scalarmult_ristretto255 (napi_env env, napi_callback_info info) {
SN_ARGV(3, crypto_scalarmult_ristretto255)

SN_ARGV_TYPEDARRAY(q, 0)
SN_ARGV_TYPEDARRAY(n, 1)
SN_ARGV_TYPEDARRAY(p, 2)

SN_ASSERT_LENGTH(q_size, crypto_scalarmult_ristretto255_BYTES, "q")
SN_ASSERT_LENGTH(n_size, crypto_scalarmult_ristretto255_SCALARBYTES, "n")
SN_ASSERT_LENGTH(p_size, crypto_scalarmult_ristretto255_BYTES, "p")

SN_RETURN(crypto_scalarmult_ristretto255(q_data, n_data, p_data), "failed to derive shared secret")
}

napi_value sn_crypto_core_ristretto255_random (napi_env env, napi_callback_info info) {
SN_ARGV(1, crypto_core_ristretto255_random)

SN_ARGV_TYPEDARRAY(p, 0)

SN_ASSERT_LENGTH(p_size, crypto_core_ristretto255_BYTES, "p")

crypto_core_ristretto255_random(p_data);

return NULL;
}

napi_value sn_crypto_core_ristretto255_add (napi_env env, napi_callback_info info) {
SN_ARGV(3, crypto_core_ristretto255_add)

SN_ARGV_TYPEDARRAY(r, 0)
SN_ARGV_TYPEDARRAY(p, 1)
SN_ARGV_TYPEDARRAY(q, 2)

SN_ASSERT_LENGTH(r_size, crypto_core_ristretto255_BYTES, "r")
SN_ASSERT_LENGTH(p_size, crypto_core_ristretto255_BYTES, "p")
SN_ASSERT_LENGTH(q_size, crypto_core_ristretto255_BYTES, "q")

SN_RETURN(crypto_core_ristretto255_add(r_data, p_data, q_data), "could not add curve points")
}

napi_value sn_crypto_core_ristretto255_sub (napi_env env, napi_callback_info info) {
SN_ARGV(3, crypto_core_ristretto255_sub)

SN_ARGV_TYPEDARRAY(r, 0)
SN_ARGV_TYPEDARRAY(p, 1)
SN_ARGV_TYPEDARRAY(q, 2)

SN_ASSERT_LENGTH(r_size, crypto_core_ristretto255_BYTES, "r")
SN_ASSERT_LENGTH(p_size, crypto_core_ristretto255_BYTES, "p")
SN_ASSERT_LENGTH(q_size, crypto_core_ristretto255_BYTES, "q")

SN_RETURN(crypto_core_ristretto255_sub(r_data, p_data, q_data), "could not add curve points")
}

napi_value sn_crypto_core_ristretto255_scalar_random (napi_env env, napi_callback_info info) {
SN_ARGV(1, crypto_core_ristretto255_scalar_random)

SN_ARGV_TYPEDARRAY(r, 0)

SN_ASSERT_LENGTH(r_size, crypto_core_ristretto255_SCALARBYTES, "r")

crypto_core_ristretto255_scalar_random(r_data);

return NULL;
}

napi_value sn_crypto_core_ristretto255_scalar_reduce (napi_env env, napi_callback_info info) {
SN_ARGV(2, crypto_core_ristretto255_scalar_reduce)

SN_ARGV_TYPEDARRAY(r, 0)
SN_ARGV_TYPEDARRAY(s, 1)

SN_ASSERT_LENGTH(r_size, crypto_core_ristretto255_SCALARBYTES, "r")
SN_ASSERT_LENGTH(s_size, crypto_core_ristretto255_NONREDUCEDSCALARBYTES, "s")

crypto_core_ristretto255_scalar_reduce(r_data, s_data);

return NULL;
}

napi_value sn_crypto_core_ristretto255_scalar_invert (napi_env env, napi_callback_info info) {
SN_ARGV(2, crypto_core_ristretto255_scalar_invert)

SN_ARGV_TYPEDARRAY(recip, 0)
SN_ARGV_TYPEDARRAY(s, 1)

SN_ASSERT_LENGTH(recip_size, crypto_core_ristretto255_SCALARBYTES, "recip")
SN_ASSERT_LENGTH(s_size, crypto_core_ristretto255_SCALARBYTES, "s")

SN_RETURN(crypto_core_ristretto255_scalar_invert(recip_data, s_data), "point inversion failed")
}

napi_value sn_crypto_core_ristretto255_scalar_negate (napi_env env, napi_callback_info info) {
SN_ARGV(2, crypto_core_ristretto255_scalar_negate)

SN_ARGV_TYPEDARRAY(neg, 0)
SN_ARGV_TYPEDARRAY(s, 1)

SN_ASSERT_LENGTH(neg_size, crypto_core_ristretto255_SCALARBYTES, "neg")
SN_ASSERT_LENGTH(s_size, crypto_core_ristretto255_SCALARBYTES, "s")

crypto_core_ristretto255_scalar_negate(neg_data, s_data);

return NULL;
}

napi_value sn_crypto_core_ristretto255_scalar_complement (napi_env env, napi_callback_info info) {
SN_ARGV(2, crypto_core_ristretto255_scalar_complement)

SN_ARGV_TYPEDARRAY(comp, 0)
SN_ARGV_TYPEDARRAY(s, 1)

SN_ASSERT_LENGTH(comp_size, crypto_core_ristretto255_SCALARBYTES, "comp")
SN_ASSERT_LENGTH(s_size, crypto_core_ristretto255_SCALARBYTES, "s")

crypto_core_ristretto255_scalar_complement(comp_data, s_data);

return NULL;
}

napi_value sn_crypto_core_ristretto255_scalar_add (napi_env env, napi_callback_info info) {
SN_ARGV(3, crypto_core_ristretto255_scalar_add)

SN_ARGV_TYPEDARRAY(z, 0)
SN_ARGV_TYPEDARRAY(x, 1)
SN_ARGV_TYPEDARRAY(y, 2)

SN_ASSERT_LENGTH(z_size, crypto_core_ristretto255_SCALARBYTES, "z")
SN_ASSERT_LENGTH(x_size, crypto_core_ristretto255_SCALARBYTES, "x")
SN_ASSERT_LENGTH(y_size, crypto_core_ristretto255_SCALARBYTES, "y")

crypto_core_ristretto255_scalar_add(z_data, x_data, y_data);

return NULL;
}


napi_value sn_crypto_core_ristretto255_scalar_sub (napi_env env, napi_callback_info info) {
SN_ARGV(3, crypto_core_ristretto255_scalar_sub)

SN_ARGV_TYPEDARRAY(z, 0)
SN_ARGV_TYPEDARRAY(x, 1)
SN_ARGV_TYPEDARRAY(y, 2)

SN_ASSERT_LENGTH(z_size, crypto_core_ristretto255_SCALARBYTES, "z")
SN_ASSERT_LENGTH(x_size, crypto_core_ristretto255_SCALARBYTES, "x")
SN_ASSERT_LENGTH(y_size, crypto_core_ristretto255_SCALARBYTES, "y")

crypto_core_ristretto255_scalar_sub(z_data, x_data, y_data);

return NULL;
}

napi_value sn_crypto_core_ristretto255_scalar_mul (napi_env env, napi_callback_info info) {
SN_ARGV(3, crypto_core_ristretto255_scalar_mul)

SN_ARGV_TYPEDARRAY(z, 0)
SN_ARGV_TYPEDARRAY(x, 1)
SN_ARGV_TYPEDARRAY(y, 2)

SN_ASSERT_LENGTH(z_size, crypto_core_ristretto255_SCALARBYTES, "z")
SN_ASSERT_LENGTH(x_size, crypto_core_ristretto255_SCALARBYTES, "x")
SN_ASSERT_LENGTH(y_size, crypto_core_ristretto255_SCALARBYTES, "y")

crypto_core_ristretto255_scalar_mul(z_data, x_data, y_data);

return NULL;
}

napi_value sn_crypto_core_ristretto255_is_valid_point (napi_env env, napi_callback_info info) {
SN_ARGV(1, crypto_core_ristretto255_is_valid_point)

SN_ARGV_TYPEDARRAY(p, 0)

SN_ASSERT_LENGTH(p_size, crypto_core_ristretto255_BYTES, "p")

SN_RETURN_BOOLEAN_FROM_1(crypto_core_ristretto255_is_valid_point(p_data))
}

napi_value sn_crypto_core_ristretto255_from_hash (napi_env env, napi_callback_info info) {
SN_ARGV(2, crypto_core_ristretto255_from_hash)

SN_ARGV_TYPEDARRAY(p, 0)
SN_ARGV_TYPEDARRAY(r, 1)

SN_ASSERT_LENGTH(p_size, crypto_core_ristretto255_BYTES, "p")
SN_ASSERT_LENGTH(r_size, crypto_core_ristretto255_HASHBYTES, "r")

SN_RETURN(crypto_core_ristretto255_from_hash(p_data, r_data), "could not generate curve point from input")
}

napi_value sn_crypto_shorthash (napi_env env, napi_callback_info info) {
SN_ARGV(3, crypto_shorthash)

Expand Down Expand Up @@ -2953,6 +3156,21 @@ static napi_value create_sodium_native(napi_env env) {
SN_EXPORT_FUNCTION(crypto_core_ed25519_scalar_complement, sn_crypto_core_ed25519_scalar_complement)
SN_EXPORT_FUNCTION(crypto_core_ed25519_scalar_add, sn_crypto_core_ed25519_scalar_add)
SN_EXPORT_FUNCTION(crypto_core_ed25519_scalar_sub, sn_crypto_core_ed25519_scalar_sub)
SN_EXPORT_FUNCTION(crypto_scalarmult_ristretto255_base, sn_crypto_scalarmult_ristretto255_base)
SN_EXPORT_FUNCTION(crypto_scalarmult_ristretto255, sn_crypto_scalarmult_ristretto255)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_random, sn_crypto_core_ristretto255_random)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_add, sn_crypto_core_ristretto255_add)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_sub, sn_crypto_core_ristretto255_sub)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_scalar_random, sn_crypto_core_ristretto255_scalar_random)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_scalar_reduce, sn_crypto_core_ristretto255_scalar_reduce)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_scalar_invert, sn_crypto_core_ristretto255_scalar_invert)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_scalar_negate, sn_crypto_core_ristretto255_scalar_negate)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_scalar_complement, sn_crypto_core_ristretto255_scalar_complement)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_scalar_add, sn_crypto_core_ristretto255_scalar_add)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_scalar_sub, sn_crypto_core_ristretto255_scalar_sub)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_scalar_mul, sn_crypto_core_ristretto255_scalar_mul)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_from_hash, sn_crypto_core_ristretto255_from_hash)
SN_EXPORT_FUNCTION(crypto_core_ristretto255_is_valid_point, sn_crypto_core_ristretto255_is_valid_point)
SN_EXPORT_FUNCTION(crypto_shorthash, sn_crypto_shorthash)
SN_EXPORT_FUNCTION(crypto_kdf_keygen, sn_crypto_kdf_keygen)
SN_EXPORT_FUNCTION(crypto_kdf_derive_from_key, sn_crypto_kdf_derive_from_key)
Expand Down Expand Up @@ -3096,6 +3314,12 @@ static napi_value create_sodium_native(napi_env env) {
SN_EXPORT_UINT32(crypto_core_ed25519_NONREDUCEDSCALARBYTES, crypto_core_ed25519_NONREDUCEDSCALARBYTES)
SN_EXPORT_UINT32(crypto_scalarmult_ed25519_BYTES, crypto_scalarmult_ed25519_BYTES)
SN_EXPORT_UINT32(crypto_scalarmult_ed25519_SCALARBYTES, crypto_scalarmult_ed25519_SCALARBYTES)
SN_EXPORT_UINT32(crypto_core_ristretto255_BYTES, crypto_core_ristretto255_BYTES)
SN_EXPORT_UINT32(crypto_core_ristretto255_HASHBYTES, crypto_core_ristretto255_HASHBYTES)
SN_EXPORT_UINT32(crypto_core_ristretto255_SCALARBYTES, crypto_core_ristretto255_SCALARBYTES)
SN_EXPORT_UINT32(crypto_core_ristretto255_NONREDUCEDSCALARBYTES, crypto_core_ristretto255_NONREDUCEDSCALARBYTES)
SN_EXPORT_UINT32(crypto_scalarmult_ristretto255_BYTES, crypto_scalarmult_ristretto255_BYTES)
SN_EXPORT_UINT32(crypto_scalarmult_ristretto255_SCALARBYTES, crypto_scalarmult_ristretto255_SCALARBYTES)
SN_EXPORT_UINT32(crypto_aead_xchacha20poly1305_ietf_ABYTES, crypto_aead_xchacha20poly1305_ietf_ABYTES)
SN_EXPORT_UINT32(crypto_aead_xchacha20poly1305_ietf_KEYBYTES, crypto_aead_xchacha20poly1305_ietf_KEYBYTES)
SN_EXPORT_UINT32(crypto_aead_xchacha20poly1305_ietf_NPUBBYTES, crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)
Expand Down
48 changes: 48 additions & 0 deletions examples/ristretto255.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var sodium = require('..')

// Perform a secure two-party computation of f(x) = k*p(x).
//
// x is the input sent to the second party by the first party after blinding it using a random invertible scalar r,
// and k is a secret key only known by the second party. p(x) is a hash-to-group function.

// -------- First party -------- Send `a` to second party
var x = Buffer.alloc(sodium.crypto_core_ristretto255_HASHBYTES)
sodium.randombytes_buf(x)

// Compute p = p(x), a group element derived from x
var p = Buffer.alloc(sodium.crypto_core_ristretto255_BYTES)
sodium.crypto_core_ristretto255_from_hash(p, x)

// Compute a = p + rg
var r = Buffer.alloc(sodium.crypto_core_ristretto255_SCALARBYTES)
var rg = Buffer.alloc(sodium.crypto_core_ristretto255_BYTES)
var a = Buffer.alloc(sodium.crypto_core_ristretto255_BYTES)
sodium.crypto_core_ristretto255_scalar_random(r)
sodium.crypto_scalarmult_ristretto255_base(rg, r)
sodium.crypto_core_ristretto255_add(a, p, rg)

// -------- Second party -------- Send v=kg and b=ka to first party
var k = Buffer.alloc(sodium.crypto_core_ristretto255_SCALARBYTES)
sodium.randombytes_buf(k)

// Compute v = kg
var v = Buffer.alloc(sodium.crypto_core_ristretto255_BYTES)
sodium.crypto_scalarmult_ristretto255_base(v, k)

// Compute b = ka
var b = Buffer.alloc(sodium.crypto_core_ristretto255_BYTES)
sodium.crypto_scalarmult_ristretto255(b, k, a)

// -------- First party -------- Unblind f(x)
// Compute irv = -rv
var ir = Buffer.alloc(sodium.crypto_core_ristretto255_SCALARBYTES)
var irv = Buffer.alloc(sodium.crypto_core_ristretto255_BYTES)
sodium.crypto_core_ristretto255_scalar_negate(ir, r)
sodium.crypto_scalarmult_ristretto255(irv, ir, v)

// Compute f(x) = b + (-rv) = k(p + rg) - r(kg)
// = k(p + g) - kg = kp
var fx = Buffer.alloc(sodium.crypto_core_ristretto255_BYTES)
sodium.crypto_core_ristretto255_add(fx, b, irv)

console.log('f(x) =', fx)
2 changes: 1 addition & 1 deletion example.js → examples/sodium.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var sodium = require('./')
var sodium = require('..')

var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
var key = sodium.sodium_malloc(sodium.crypto_secretbox_KEYBYTES)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"prebuildify": "^4.2.1",
"sodium-test": "^0.8.0",
"sodium-vectors": "^1.0.0",
"standard": "^14.3.1",
"standard": "^16.0.4",
"tape": "^5.3.1"
},
"scripts": {
Expand Down
Loading