move ed_point_add to urcrypt

This commit is contained in:
Paul Driver 2020-07-31 10:50:46 -07:00
parent 6cb81fe084
commit cc5998ae07
3 changed files with 48 additions and 47 deletions

View File

@ -2,64 +2,33 @@
**
*/
#include "all.h"
#include <ed25519.h>
#include <ge.h>
#include <urcrypt.h>
/* functions
*/
u3_noun
u3qc_point_add(u3_atom a,
u3_atom b)
{
c3_y met_w;
c3_w ate_w, bet_w;
met_w = u3r_met(3, a);
if (met_w > 32) {
return u3m_bail(c3__fail);
if ( ((ate_w = u3r_met(3, a)) > 32) ||
((bet_w = u3r_met(3, b)) > 32) ) {
return u3_none;
}
c3_y a_y[32];
memset(a_y, 0, 32);
u3r_bytes(0, met_w, a_y, a);
else {
c3_y a_y[32], b_y[32], out_y[32];
met_w = u3r_met(3, b);
if (met_w > 32) {
return u3m_bail(c3__fail);
memset(a_y, 0, 32);
memset(b_y, 0, 32);
u3r_bytes(0, ate_w, a_y, a);
u3r_bytes(0, bet_w, b_y, b);
return ( 0 == urcrypt_ed_point_add(a_y, b_y, out_y) )
? u3i_bytes(32, out_y)
: u3_none;
}
c3_y b_y[32];
memset(b_y, 0, 32);
u3r_bytes(0, met_w, b_y, b);
ge_p3 A;
if (ge_frombytes_negate_vartime(&A, a_y) != 0) {
return u3m_bail(c3__exit);
}
ge_p3 B;
if (ge_frombytes_negate_vartime(&B, b_y) != 0) {
return u3m_bail(c3__exit);
}
// Undo the negation from above. See add_scalar.c in the ed25519 distro.
fe_neg(A.X, A.X);
fe_neg(A.T, A.T);
fe_neg(B.X, B.X);
fe_neg(B.T, B.T);
ge_cached b_cached;
ge_p3_to_cached(&b_cached, &B);
ge_p1p1 sum;
ge_add(&sum, &A, &b_cached);
ge_p3 result;
ge_p1p1_to_p3(&result, &sum);
c3_y output_y[32];
ge_p3_tobytes(output_y, &result);
return u3i_bytes(32, output_y);
}
u3_noun

View File

@ -1,5 +1,36 @@
#include "urcrypt.h"
int
urcrypt_ed_point_add(uint8_t a[32], uint8_t b[32], uint8_t out[32])
{
ge_p3 A, B;
ge_cached b_cached;
ge_p1p1 sum;
ge_p3 result;
if ( ge_frombytes_negate_vartime(&A, a) != 0 ) {
return -1;
}
if ( ge_frombytes_negate_vartime(&B, b) != 0 ) {
return -1;
}
// Undo the negation from above. See add_scalar.c in the ed25519 distro.
fe_neg(A.X, A.X);
fe_neg(A.T, A.T);
fe_neg(B.X, B.X);
fe_neg(B.T, B.T);
ge_p3_to_cached(&b_cached, &B);
ge_add(&sum, &A, &b_cached);
ge_p1p1_to_p3(&result, &sum);
ge_p3_tobytes(out, &result);
return 0;
}
int
urcrypt_ed_scalarmult(uint8_t a[32], uint8_t b[32], uint8_t out[32])
{

View File

@ -6,6 +6,7 @@
#include <ed25519.h>
#include <ge-additions.h>
int urcrypt_ed_point_add(uint8_t a[32], uint8_t b[32], uint8_t out[32]);
int urcrypt_ed_scalarmult(uint8_t a[32], uint8_t b[32], uint8_t out[32]);
void urcrypt_ed_sign(uint8_t *message,
size_t length,