Skip to content

Commit

Permalink
fix(pvss): commented unused function
Browse files Browse the repository at this point in the history
  • Loading branch information
Luckydd99 authored and jaromil committed Aug 2, 2023
1 parent 5497c7f commit d0455ea
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 131 deletions.
1 change: 1 addition & 0 deletions src/zen_ecp.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ static int ecp_zcash_export(lua_State *L) {
END(1);
}

// See the generalised version commented inside zen_octet.c
static int ecp_zcash_import(lua_State *L){
BEGIN();
char *failed_msg = NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/zen_ecp2.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,8 @@ static int ecp2_zcash_export(lua_State *L) {
//
//}


// See the generalised version commented inside zen_octec.c
// TODO: remove magic numbers
// TODO: implement import for non compressed octets
static int ecp2_zcash_import(lua_State *L) {
Expand Down
264 changes: 133 additions & 131 deletions src/zen_octet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1799,139 +1799,141 @@ static int crc8(lua_State *L) {
return 1;
}

// The following function has been split up into zen_ecp.c and zen_ecp2.c

// TODO: remove magic numbers
// TODO: implement import for non compressed octets
static int zcash_topoint(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *o = o_arg(L, 1);
if(o == NULL) {
failed_msg = "Could not allocate octet";
goto end;
}

ecp2 *e2 = NULL;
ecp *e = NULL;

unsigned char m_byte = o->val[0] & 0xE0;
char c_bit;
char i_bit;
char s_bit;
if(m_byte == 0x20 || m_byte == 0x60 || m_byte == 0xE0) {
failed_msg = "Invalid octet header";
goto end;
}
c_bit = ((m_byte & 0x80) == 0x80);
i_bit = ((m_byte & 0x40) == 0x40);
s_bit = ((m_byte & 0x20) == 0x20);

if(c_bit) {
if(o->len != 96 && o->len != 48) {
failed_msg = "Invalid octet header";
goto end;
}
} else {
if(o->len != 192 && o->len != 96) {
failed_msg = "Invalid octet header";
goto end;
}
}

switch(o->len) {
case 192:
e2 = ecp2_new(L);
break;
case 48:
e = ecp_new(L);
break;
case 96:
if(c_bit) e2 = ecp2_new(L);
else e = ecp_new(L);
}

o->val[0] = o->val[0] & 0x1F;

if(i_bit) {
// TODO: check o->val is all 0
if(e == NULL) {
ECP2_inf(&e2->val);
} else {
ECP_inf(&e->val);
}
goto end;
}

if(c_bit) {
if(e == NULL) {
FP2 fx, fy;
octet x0 = {
.max = 48,
.len = 48,
.val = o->val
};
octet x1 = {
.max = 48,
.len = 48,
.val = o->val+48
};

big* bigx0 = big_new(L);
big* bigx1 = big_new(L);

_octet_to_big(L, bigx0, &x0);
_octet_to_big(L, bigx1, &x1);

FP2_from_BIGs(&fx, bigx1->val, bigx0->val);

if(!ECP2_setx(&e2->val, &fx)) {
failed_msg = "Invalid input octet: not a point on the curve";
goto end;
}

ECP2_get(&fx, &fy, &e2->val);

BIG by0,by1;
FP2_reduce(&fy);
FP_redc(by0,&(fy.a));
FP_redc(by1,&(fy.b));

if(gf2_sign(by0, by1) != s_bit) {
ECP2_neg(&e2->val);
}

lua_pop(L,1);
lua_pop(L,1);

} else {
BIG xpoint, ypoint;
big* bigx = big_new(L);
_octet_to_big(L, bigx, o);

if(!ECP_setx(&e->val, bigx->val, 0)) {
failed_msg = "Invalid input octet: not a point on the curve";
goto end;
}

ECP_get(xpoint, ypoint, &e->val);
if(gf_sign(ypoint) != s_bit) {
ECP_neg(&e->val);

}

lua_pop(L,1);
}

} else {
failed_msg = "Not yet implemented";
goto end;
}
end:
o_free(L, o);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
// static int zcash_topoint(lua_State *L) {
// BEGIN();
// char *failed_msg = NULL;
// octet *o = o_arg(L, 1);
// if(o == NULL) {
// failed_msg = "Could not allocate octet";
// goto end;
// }

// ecp2 *e2 = NULL;
// ecp *e = NULL;

// unsigned char m_byte = o->val[0] & 0xE0;
// char c_bit;
// char i_bit;
// char s_bit;
// if(m_byte == 0x20 || m_byte == 0x60 || m_byte == 0xE0) {
// failed_msg = "Invalid octet header";
// goto end;
// }
// c_bit = ((m_byte & 0x80) == 0x80);
// i_bit = ((m_byte & 0x40) == 0x40);
// s_bit = ((m_byte & 0x20) == 0x20);

// if(c_bit) {
// if(o->len != 96 && o->len != 48) {
// failed_msg = "Invalid octet header";
// goto end;
// }
// } else {
// if(o->len != 192 && o->len != 96) {
// failed_msg = "Invalid octet header";
// goto end;
// }
// }

// switch(o->len) {
// case 192:
// e2 = ecp2_new(L);
// break;
// case 48:
// e = ecp_new(L);
// break;
// case 96:
// if(c_bit) e2 = ecp2_new(L);
// else e = ecp_new(L);
// }

// o->val[0] = o->val[0] & 0x1F;

// if(i_bit) {
// // TODO: check o->val is all 0
// if(e == NULL) {
// ECP2_inf(&e2->val);
// } else {
// ECP_inf(&e->val);
// }
// goto end;
// }

// if(c_bit) {
// if(e == NULL) {
// FP2 fx, fy;
// octet x0 = {
// .max = 48,
// .len = 48,
// .val = o->val
// };
// octet x1 = {
// .max = 48,
// .len = 48,
// .val = o->val+48
// };

// big* bigx0 = big_new(L);
// big* bigx1 = big_new(L);

// _octet_to_big(L, bigx0, &x0);
// _octet_to_big(L, bigx1, &x1);

// FP2_from_BIGs(&fx, bigx1->val, bigx0->val);

// if(!ECP2_setx(&e2->val, &fx)) {
// failed_msg = "Invalid input octet: not a point on the curve";
// goto end;
// }

// ECP2_get(&fx, &fy, &e2->val);

// BIG by0,by1;
// FP2_reduce(&fy);
// FP_redc(by0,&(fy.a));
// FP_redc(by1,&(fy.b));

// if(gf2_sign(by0, by1) != s_bit) {
// ECP2_neg(&e2->val);
// }

// lua_pop(L,1);
// lua_pop(L,1);

// } else {
// BIG xpoint, ypoint;
// big* bigx = big_new(L);
// _octet_to_big(L, bigx, o);

// if(!ECP_setx(&e->val, bigx->val, 0)) {
// failed_msg = "Invalid input octet: not a point on the curve";
// goto end;
// }

// ECP_get(xpoint, ypoint, &e->val);
// if(gf_sign(ypoint) != s_bit) {
// ECP_neg(&e->val);

// }

// lua_pop(L,1);
// }

// } else {
// failed_msg = "Not yet implemented";
// goto end;
// }
// end:
// o_free(L, o);
// if(failed_msg) {
// THROW(failed_msg);
// }
// END(1);
// }

int luaopen_octet(lua_State *L) {
(void)L;
Expand Down

0 comments on commit d0455ea

Please sign in to comment.