1115 const secp256k1_context* ctx,
1116 unsigned char* proof_out,
1118 const uint64_t* values,
1119 const unsigned char* blindings_flat,
1121 const secp256k1_pubkey* pk_base,
1122 const unsigned char* context_id
1129 if (m == 0)
return 0;
1130 if ((n & (n - 1)) != 0)
return 0;
1133 const size_t proof_size = 292 + 66 * rounds;
1134 if (proof_len) *proof_len = proof_size;
1139 secp256k1_pubkey* G_vec = (secp256k1_pubkey*)malloc(n *
sizeof(secp256k1_pubkey));
1140 secp256k1_pubkey* H_vec = (secp256k1_pubkey*)malloc(n *
sizeof(secp256k1_pubkey));
1141 secp256k1_pubkey* H_prime = (secp256k1_pubkey*)malloc(n *
sizeof(secp256k1_pubkey));
1142 unsigned char* al = (
unsigned char*)malloc(n * 32);
1143 unsigned char* ar = (
unsigned char*)malloc(n * 32);
1144 unsigned char* sl = (
unsigned char*)malloc(n * 32);
1145 unsigned char* sr = (
unsigned char*)malloc(n * 32);
1146 unsigned char* l_vec = (
unsigned char*)malloc(n * 32);
1147 unsigned char* r_vec = (
unsigned char*)malloc(n * 32);
1148 unsigned char* r1_vec = (
unsigned char*)malloc(n * 32);
1150 secp256k1_pubkey* L_vec = (secp256k1_pubkey*)malloc(rounds *
sizeof(secp256k1_pubkey));
1151 secp256k1_pubkey* R_vec = (secp256k1_pubkey*)malloc(rounds *
sizeof(secp256k1_pubkey));
1153 unsigned char* y_powers = (
unsigned char*)malloc(n * 32);
1154 unsigned char* z_j2 = (
unsigned char*)malloc(m * 32);
1156 if (!G_vec || !H_vec || !H_prime ||
1157 !al || !ar || !sl || !sr || !l_vec || !r_vec || !r1_vec ||
1159 !y_powers || !z_j2) {
1164 secp256k1_pubkey A, S, T1, T2, U;
1166 unsigned char alpha[32], rho[32];
1167 unsigned char tau1[32], tau2[32];
1168 unsigned char t1[32], t2[32];
1169 unsigned char t_hat[32], tau_x[32], mu[32];
1170 unsigned char a_final[32], b_final[32];
1172 unsigned char y[32], z[32], x[32];
1173 unsigned char z_sq[32], z_neg[32], x_sq[32];
1174 unsigned char ux_scalar[32];
1175 unsigned char ipa_transcript[32];
1177 unsigned char one[32] = {0}; one[31] = 1;
1179 unsigned char zero[32] = {0};
1185 secp256k1_pubkey U_arr[1];
1191 for (
size_t j = 0; j < m; j++) {
1192 uint64_t v = values[j];
1195 unsigned char* al_k = al + 32*k;
1196 unsigned char* ar_k = ar + 32*k;
1197 unsigned char* sl_k = sl + 32*k;
1198 unsigned char* sr_k = sr + 32*k;
1201 memcpy(al_k, one, 32);
1202 memset(ar_k, 0, 32);
1204 memset(al_k, 0, 32);
1205 memcpy(ar_k, minus_one, 32);
1221 secp256k1_pubkey tG, tH, tB;
1222 const secp256k1_pubkey* pts[3];
1227 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tB, alpha))
goto cleanup;
1228 pts[0] = &tB; pts[1] = &tG; pts[2] = &tH;
1229 if (!secp256k1_ec_pubkey_combine(ctx, &A, pts, 3))
goto cleanup;
1234 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tB, rho))
goto cleanup;
1235 pts[0] = &tB; pts[1] = &tG; pts[2] = &tH;
1236 if (!secp256k1_ec_pubkey_combine(ctx, &S, pts, 3))
goto cleanup;
1241 unsigned char A_ser[33], S_ser[33];
1245 if (!secp256k1_ec_pubkey_serialize(ctx, A_ser, &len, &A, SECP256K1_EC_COMPRESSED))
goto cleanup;
1247 if (!secp256k1_ec_pubkey_serialize(ctx, S_ser, &len, &S, SECP256K1_EC_COMPRESSED))
goto cleanup;
1253 SHA256_Update(&sha,
"MPT_BULLETPROOF_RANGE", 21);
1256 if (context_id) SHA256_Update(&sha, context_id, 32);
1259 for (
size_t i = 0; i < m; i++) {
1260 secp256k1_pubkey V_temp;
1261 unsigned char V_ser[33];
1265 if (!secp256k1_ec_pubkey_serialize(ctx, V_ser, &v_len, &V_temp, SECP256K1_EC_COMPRESSED))
goto cleanup;
1266 SHA256_Update(&sha, V_ser, 33);
1270 SHA256_Update(&sha, A_ser, 33);
1271 SHA256_Update(&sha, S_ser, 33);
1273 SHA256_Final(y, &sha);
1278 SHA256_Update(&sha,
"MPT_BULLETPROOF_RANGE", 21);
1279 if (context_id) SHA256_Update(&sha, context_id, 32);
1281 for (
size_t i = 0; i < m; i++) {
1283 secp256k1_pubkey V_temp;
1284 unsigned char V_ser[33];
1287 secp256k1_ec_pubkey_serialize(ctx, V_ser, &v_len, &V_temp, SECP256K1_EC_COMPRESSED);
1288 SHA256_Update(&sha, V_ser, 33);
1290 SHA256_Update(&sha, A_ser, 33);
1291 SHA256_Update(&sha, S_ser, 33);
1292 SHA256_Update(&sha, y, 32);
1293 SHA256_Final(z, &sha);
1295 memcpy(z_neg, z, 32);
1303 unsigned char ypow[32];
1304 memcpy(ypow, one, 32);
1306 for (
size_t k = 0; k < n; k++) {
1307 memcpy(y_powers + 32*k, ypow, 32);
1310 OPENSSL_cleanse(ypow, 32);
1317 for (
size_t block = 0; block < m; block++) {
1318 const unsigned char* zblk = z_j2 + 32*block;
1323 unsigned char* l0 = l_vec + 32*k;
1324 unsigned char* r0 = r_vec + 32*k;
1325 unsigned char* r1 = r1_vec + 32*k;
1327 const unsigned char* al_k = al + 32*k;
1328 const unsigned char* ar_k = ar + 32*k;
1329 const unsigned char* sr_k = sr + 32*k;
1330 const unsigned char* yk = y_powers + 32*k;
1332 unsigned char two_i[32] = {0};
1333 two_i[31 - (i >> 3)] = (
unsigned char)(1u << (i & 7));
1340 unsigned char tmp1[32], tmp2[32];
1354 OPENSSL_cleanse(tmp1, 32);
1355 OPENSSL_cleanse(tmp2, 32);
1365 unsigned char dot1[32], dot2[32];
1369 OPENSSL_cleanse(dot1, 32);
1370 OPENSSL_cleanse(dot2, 32);
1383 secp256k1_pubkey tG, tB;
1384 const secp256k1_pubkey* pts[2];
1386 if (memcmp(t1, zero, 32) == 0)
goto cleanup;
1388 if (!secp256k1_ec_pubkey_create(ctx, &tG, t1))
goto cleanup;
1392 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tB, tau1))
goto cleanup;
1394 pts[0] = &tG; pts[1] = &tB;
1395 if (!secp256k1_ec_pubkey_combine(ctx, &T1, pts, 2))
goto cleanup;
1400 secp256k1_pubkey tG, tB;
1401 const secp256k1_pubkey* pts[2];
1403 if (memcmp(t2, zero, 32) == 0)
goto cleanup;
1405 if (!secp256k1_ec_pubkey_create(ctx, &tG, t2))
goto cleanup;
1409 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tB, tau2))
goto cleanup;
1411 pts[0] = &tG; pts[1] = &tB;
1412 if (!secp256k1_ec_pubkey_combine(ctx, &T2, pts, 2))
goto cleanup;
1418 unsigned char A_ser[33], S_ser[33], T1_ser[33], T2_ser[33];
1422 if (!secp256k1_ec_pubkey_serialize(ctx, A_ser, &len, &A, SECP256K1_EC_COMPRESSED))
goto cleanup;
1424 if (!secp256k1_ec_pubkey_serialize(ctx, S_ser, &len, &S, SECP256K1_EC_COMPRESSED))
goto cleanup;
1426 if (!secp256k1_ec_pubkey_serialize(ctx, T1_ser, &len, &T1, SECP256K1_EC_COMPRESSED))
goto cleanup;
1428 if (!secp256k1_ec_pubkey_serialize(ctx, T2_ser, &len, &T2, SECP256K1_EC_COMPRESSED))
goto cleanup;
1431 if (context_id) SHA256_Update(&sha, context_id, 32);
1432 SHA256_Update(&sha, A_ser, 33);
1433 SHA256_Update(&sha, S_ser, 33);
1434 SHA256_Update(&sha, y, 32);
1435 SHA256_Update(&sha, z, 32);
1436 SHA256_Update(&sha, T1_ser, 33);
1437 SHA256_Update(&sha, T2_ser, 33);
1438 SHA256_Final(x, &sha);
1441 if (memcmp(x, zero, 32) == 0)
goto cleanup;
1445 for (
size_t k = 0; k < n; k++) {
1446 unsigned char tmp[32];
1456 OPENSSL_cleanse(tmp, 32);
1467 unsigned char tmp[32];
1472 for (
size_t j = 0; j < m; j++) {
1473 unsigned char add[32];
1476 OPENSSL_cleanse(add, 32);
1479 OPENSSL_cleanse(tmp, 32);
1484 unsigned char tmp[32];
1487 OPENSSL_cleanse(tmp, 32);
1503 unsigned char A_ser[33], S_ser[33], T1_ser[33], T2_ser[33];
1508 if (!secp256k1_ec_pubkey_serialize(ctx, A_ser, &ser_len, &A, SECP256K1_EC_COMPRESSED))
goto cleanup;
1510 if (!secp256k1_ec_pubkey_serialize(ctx, S_ser, &ser_len, &S, SECP256K1_EC_COMPRESSED))
goto cleanup;
1512 if (!secp256k1_ec_pubkey_serialize(ctx, T1_ser, &ser_len, &T1, SECP256K1_EC_COMPRESSED))
goto cleanup;
1514 if (!secp256k1_ec_pubkey_serialize(ctx, T2_ser, &ser_len, &T2, SECP256K1_EC_COMPRESSED))
goto cleanup;
1519 if (context_id) SHA256_Update(&sha, context_id, 32);
1522 SHA256_Update(&sha, A_ser, 33);
1523 SHA256_Update(&sha, S_ser, 33);
1524 SHA256_Update(&sha, T1_ser, 33);
1525 SHA256_Update(&sha, T2_ser, 33);
1528 SHA256_Update(&sha, y, 32);
1529 SHA256_Update(&sha, z, 32);
1530 SHA256_Update(&sha, x, 32);
1533 SHA256_Update(&sha, t_hat, 32);
1535 SHA256_Final(ipa_transcript, &sha);
1548 unsigned char y_inv[32];
1549 unsigned char y_inv_pow[32];
1550 if (memcmp(y, zero, 32) == 0)
goto cleanup;
1553 memcpy(y_inv_pow, one, 32);
1555 for (
size_t k = 0; k < n; k++) {
1556 H_prime[k] = H_vec[k];
1558 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &H_prime[k], y_inv_pow))
goto cleanup;
1562 OPENSSL_cleanse(y_inv, 32);
1563 OPENSSL_cleanse(y_inv_pow, 32);
1568 size_t rounds_used = 0;
1588 if (rounds_used != rounds)
goto cleanup;
1593 const size_t expected = 292 + 66 * rounds;
1596 if (proof_out == NULL) {
1597 if (proof_len) *proof_len = expected;
1602 if (proof_len == NULL)
goto cleanup;
1604 if (*proof_len < expected) {
1605 *proof_len = expected;
1609 unsigned char* ptr = proof_out;
1612#define SER_PT(P) do { \
1614 if (!secp256k1_ec_pubkey_serialize(ctx, ptr, &ser_len, &(P), SECP256K1_EC_COMPRESSED)) goto cleanup; \
1615 if (ser_len != 33) goto cleanup; \
1621 for (
size_t r = 0; r < rounds; r++)
SER_PT(L_vec[r]);
1622 for (
size_t r = 0; r < rounds; r++)
SER_PT(R_vec[r]);
1624 memcpy(ptr, a_final, 32); ptr += 32;
1625 memcpy(ptr, b_final, 32); ptr += 32;
1626 memcpy(ptr, t_hat, 32); ptr += 32;
1627 memcpy(ptr, tau_x, 32); ptr += 32;
1628 memcpy(ptr, mu, 32); ptr += 32;
1633 if ((
size_t)(ptr - proof_out) != expected)
goto cleanup;
1635 *proof_len = expected;
1643 if (al) OPENSSL_cleanse(al, n * 32);
1644 if (ar) OPENSSL_cleanse(ar, n * 32);
1645 if (sl) OPENSSL_cleanse(sl, n * 32);
1646 if (sr) OPENSSL_cleanse(sr, n * 32);
1647 if (l_vec) OPENSSL_cleanse(l_vec, n * 32);
1648 if (r_vec) OPENSSL_cleanse(r_vec, n * 32);
1649 if (r1_vec) OPENSSL_cleanse(r1_vec, n * 32);
1651 OPENSSL_cleanse(alpha, 32);
1652 OPENSSL_cleanse(rho, 32);
1653 OPENSSL_cleanse(tau1, 32);
1654 OPENSSL_cleanse(tau2, 32);
1655 OPENSSL_cleanse(t1, 32);
1656 OPENSSL_cleanse(t2, 32);
1657 OPENSSL_cleanse(t_hat, 32);
1658 OPENSSL_cleanse(tau_x, 32);
1659 OPENSSL_cleanse(mu, 32);
1661 OPENSSL_cleanse(y, 32);
1662 OPENSSL_cleanse(z, 32);
1663 OPENSSL_cleanse(x, 32);
1664 OPENSSL_cleanse(z_sq, 32);
1665 OPENSSL_cleanse(z_neg, 32);
1666 OPENSSL_cleanse(x_sq, 32);
1667 OPENSSL_cleanse(ux_scalar, 32);
1668 OPENSSL_cleanse(ipa_transcript, 32);
1670 if (G_vec) { OPENSSL_cleanse(G_vec, n *
sizeof(secp256k1_pubkey)); free(G_vec); }
1671 if (H_vec) { OPENSSL_cleanse(H_vec, n *
sizeof(secp256k1_pubkey)); free(H_vec); }
1672 if (H_prime) { OPENSSL_cleanse(H_prime, n *
sizeof(secp256k1_pubkey)); free(H_prime); }
1678 if (l_vec) free(l_vec);
1679 if (r_vec) free(r_vec);
1680 if (r1_vec) free(r1_vec);
1682 if (L_vec) { OPENSSL_cleanse(L_vec, rounds *
sizeof(secp256k1_pubkey)); free(L_vec); }
1683 if (R_vec) { OPENSSL_cleanse(R_vec, rounds *
sizeof(secp256k1_pubkey)); free(R_vec); }
1685 if (y_powers) { OPENSSL_cleanse(y_powers, n * 32); free(y_powers); }
1686 if (z_j2) { OPENSSL_cleanse(z_j2, m * 32); free(z_j2); }
1716 const secp256k1_context* ctx,
1717 const secp256k1_pubkey* G_vec,
1718 const secp256k1_pubkey* H_vec,
1719 const unsigned char* proof,
1721 const secp256k1_pubkey* commitment_C_vec,
1723 const secp256k1_pubkey* pk_base,
1724 const unsigned char* context_id
1727 if (!ctx || !G_vec || !H_vec || !proof || !commitment_C_vec || !pk_base)
return 0;
1728 if (m == 0)
return 0;
1730 if ((m & (m - 1)) != 0)
return 0;
1736 const size_t expected_len = 292 + 66 * rounds;
1737 if (proof_len != expected_len)
return 0;
1740 secp256k1_pubkey A, S, T1, T2;
1743 secp256k1_pubkey* L_vec = (secp256k1_pubkey*)malloc(rounds *
sizeof(secp256k1_pubkey));
1744 secp256k1_pubkey* R_vec = (secp256k1_pubkey*)malloc(rounds *
sizeof(secp256k1_pubkey));
1745 if (!L_vec || !R_vec) { free(L_vec); free(R_vec);
return 0; }
1747 unsigned char a_final[32], b_final[32];
1748 unsigned char t_hat[32], tau_x[32], mu[32];
1750 unsigned char y[32], z[32], x[32];
1751 unsigned char ux_scalar[32];
1752 unsigned char z_sq[32];
1754 const unsigned char* ptr = proof;
1756 if (!secp256k1_ec_pubkey_parse(ctx, &A, ptr, 33))
goto fail; ptr += 33;
1757 if (!secp256k1_ec_pubkey_parse(ctx, &S, ptr, 33))
goto fail; ptr += 33;
1758 if (!secp256k1_ec_pubkey_parse(ctx, &T1, ptr, 33))
goto fail; ptr += 33;
1759 if (!secp256k1_ec_pubkey_parse(ctx, &T2, ptr, 33))
goto fail; ptr += 33;
1761 for (
size_t i = 0; i < rounds; i++) {
1762 if (!secp256k1_ec_pubkey_parse(ctx, &L_vec[i], ptr, 33))
goto fail;
1766 for (
size_t i = 0; i < rounds; i++) {
1767 if (!secp256k1_ec_pubkey_parse(ctx, &R_vec[i], ptr, 33))
goto fail;
1771 memcpy(a_final, ptr, 32); ptr += 32;
1772 memcpy(b_final, ptr, 32); ptr += 32;
1773 memcpy(t_hat, ptr, 32); ptr += 32;
1774 memcpy(tau_x, ptr, 32); ptr += 32;
1775 memcpy(mu, ptr, 32); ptr += 32;
1778 if (!secp256k1_ec_seckey_verify(ctx, a_final))
goto fail;
1779 if (!secp256k1_ec_seckey_verify(ctx, b_final))
goto fail;
1780 if (!secp256k1_ec_seckey_verify(ctx, t_hat))
goto fail;
1781 if (!secp256k1_ec_seckey_verify(ctx, tau_x))
goto fail;
1782 if (!secp256k1_ec_seckey_verify(ctx, mu))
goto fail;
1786 secp256k1_pubkey U_arr[1];
1792 unsigned char A_ser[33], S_ser[33], T1_ser[33], T2_ser[33];
1796 if (!secp256k1_ec_pubkey_serialize(ctx, A_ser, &slen, &A, SECP256K1_EC_COMPRESSED))
goto fail;
1798 if (!secp256k1_ec_pubkey_serialize(ctx, S_ser, &slen, &S, SECP256K1_EC_COMPRESSED))
goto fail;
1800 if (!secp256k1_ec_pubkey_serialize(ctx, T1_ser, &slen, &T1, SECP256K1_EC_COMPRESSED))
goto fail;
1802 if (!secp256k1_ec_pubkey_serialize(ctx, T2_ser, &slen, &T2, SECP256K1_EC_COMPRESSED))
goto fail;
1807 SHA256_Update(&sha,
"MPT_BULLETPROOF_RANGE", 21);
1810 if (context_id) SHA256_Update(&sha, context_id, 32);
1813 for (
size_t i = 0; i < m; i++) {
1814 unsigned char C_ser[33];
1816 if (!secp256k1_ec_pubkey_serialize(ctx, C_ser, &c_len, &commitment_C_vec[i], SECP256K1_EC_COMPRESSED))
goto fail;
1817 SHA256_Update(&sha, C_ser, 33);
1821 SHA256_Update(&sha, A_ser, 33);
1822 SHA256_Update(&sha, S_ser, 33);
1823 SHA256_Final(y, &sha);
1828 SHA256_Update(&sha,
"MPT_BULLETPROOF_RANGE", 21);
1829 if (context_id) SHA256_Update(&sha, context_id, 32);
1830 for (
size_t i = 0; i < m; i++) {
1831 unsigned char C_ser[33];
1833 secp256k1_ec_pubkey_serialize(ctx, C_ser, &c_len, &commitment_C_vec[i], SECP256K1_EC_COMPRESSED);
1834 SHA256_Update(&sha, C_ser, 33);
1836 SHA256_Update(&sha, A_ser, 33);
1837 SHA256_Update(&sha, S_ser, 33);
1838 SHA256_Update(&sha, y, 32);
1839 SHA256_Final(z, &sha);
1842 if (!secp256k1_ec_seckey_verify(ctx, y) || !secp256k1_ec_seckey_verify(ctx, z))
goto fail;
1845 unsigned char* y_powers = (
unsigned char*)malloc(n * 32);
1846 unsigned char* y_inv_powers = (
unsigned char*)malloc(n * 32);
1847 if (!y_powers || !y_inv_powers) { free(y_powers); free(y_inv_powers);
goto fail; }
1849 unsigned char y_inv[32];
1856 if (context_id) SHA256_Update(&sha, context_id, 32);
1857 SHA256_Update(&sha, A_ser, 33);
1858 SHA256_Update(&sha, S_ser, 33);
1859 SHA256_Update(&sha, y, 32);
1860 SHA256_Update(&sha, z, 32);
1861 SHA256_Update(&sha, T1_ser, 33);
1862 SHA256_Update(&sha, T2_ser, 33);
1863 SHA256_Final(x, &sha);
1866 if (!secp256k1_ec_seckey_verify(ctx, x)) { free(y_powers); free(y_inv_powers);
goto fail; }
1877 unsigned char (*y_block_sum)[32] = (
unsigned char (*)[32])malloc(m * 32);
1878 if (!y_block_sum) { free(y_powers); free(y_inv_powers);
goto fail; }
1880 unsigned char two_sum[32];
1883 unsigned char delta[32] = {0};
1884 unsigned char sum_y_all[32] = {0};
1887 for (
size_t j = 0; j < m; j++) {
1893 unsigned char z_minus_z2[32], tmp[32];
1897 OPENSSL_cleanse(z_minus_z2, 32);
1898 OPENSSL_cleanse(tmp, 32);
1902 for (
size_t j = 0; j < m; j++) {
1903 unsigned char z_j3[32], tmp[32];
1908 OPENSSL_cleanse(z_j3, 32);
1909 OPENSSL_cleanse(tmp, 32);
1912 OPENSSL_cleanse(sum_y_all, 32);
1916 secp256k1_pubkey LHS;
1918 unsigned char zero32[32] = {0};
1919 int have_t = 0, have_tau = 0;
1920 secp256k1_pubkey tG, tauH;
1922 if (memcmp(t_hat, zero32, 32) != 0) {
1923 if (!secp256k1_ec_pubkey_create(ctx, &tG, t_hat)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
1926 if (memcmp(tau_x, zero32, 32) != 0) {
1928 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tauH, tau_x)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
1931 if (have_t && have_tau) {
1932 const secp256k1_pubkey* pts[2] = { &tG, &tauH };
1933 if (!secp256k1_ec_pubkey_combine(ctx, &LHS, pts, 2)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
1934 }
else if (have_t) {
1936 }
else if (have_tau) {
1939 free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail;
1944 secp256k1_pubkey RHS;
1946 secp256k1_pubkey acc, tmpP;
1948 unsigned char zero32[32] = {0};
1951 for (
size_t j = 0; j < m; j++) {
1952 unsigned char z_j2[32];
1955 if (memcmp(z_j2, zero32, 32) != 0) {
1956 tmpP = commitment_C_vec[j];
1957 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tmpP, z_j2)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
1958 if (!
add_term(ctx, &acc, &inited, &tmpP)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
1960 OPENSSL_cleanse(z_j2, 32);
1965 secp256k1_pubkey deltaG;
1966 if (!secp256k1_ec_pubkey_create(ctx, &deltaG, delta))
goto fail;
1967 if (!
add_term(ctx, &acc, &inited, &deltaG))
goto fail;
1973 if (memcmp(x, zero32, 32) != 0) {
1975 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tmpP, x)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
1976 if (!
add_term(ctx, &acc, &inited, &tmpP)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
1980 unsigned char x_sq[32];
1982 if (memcmp(x_sq, zero32, 32) != 0) {
1984 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tmpP, x_sq)) { OPENSSL_cleanse(x_sq,32); free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
1985 if (!
add_term(ctx, &acc, &inited, &tmpP)) { OPENSSL_cleanse(x_sq,32); free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
1987 OPENSSL_cleanse(x_sq, 32);
1995 printf(
"[VERIFY] Step3 polynomial identity failed\n");
1996 free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail;
2003 unsigned char ipa_transcript_id[32];
2006 unsigned char A_ser[33], S_ser[33], T1_ser[33], T2_ser[33];
2009 if (!secp256k1_ec_pubkey_serialize(ctx, A_ser, &len, &A, SECP256K1_EC_COMPRESSED))
goto fail;
2011 if (!secp256k1_ec_pubkey_serialize(ctx, S_ser, &len, &S, SECP256K1_EC_COMPRESSED))
goto fail;
2013 if (!secp256k1_ec_pubkey_serialize(ctx, T1_ser,&len, &T1, SECP256K1_EC_COMPRESSED))
goto fail;
2015 if (!secp256k1_ec_pubkey_serialize(ctx, T2_ser,&len, &T2, SECP256K1_EC_COMPRESSED))
goto fail;
2020 if (context_id) SHA256_Update(&sha, context_id, 32);
2021 SHA256_Update(&sha, A_ser, 33);
2022 SHA256_Update(&sha, S_ser, 33);
2023 SHA256_Update(&sha, T1_ser,33);
2024 SHA256_Update(&sha, T2_ser,33);
2025 SHA256_Update(&sha, y, 32);
2026 SHA256_Update(&sha, z, 32);
2027 SHA256_Update(&sha, x, 32);
2028 SHA256_Update(&sha, t_hat, 32);
2029 SHA256_Final(ipa_transcript_id, &sha);
2036 secp256k1_pubkey P = A;
2040 secp256k1_pubkey xS = S;
2041 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &xS, x)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
2044 secp256k1_pubkey newP;
2045 const secp256k1_pubkey* pts[2] = { &P, &xS };
2046 if (!secp256k1_ec_pubkey_combine(ctx, &newP, pts, 2))
goto fail;
2052 unsigned char neg_z[32];
2053 memcpy(neg_z, z, 32);
2054 if (!secp256k1_ec_seckey_negate(ctx, neg_z)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
2056 for (
size_t j = 0; j < m; j++) {
2057 unsigned char z_j2[32];
2060 for (
size_t i = 0; i < 64; i++) {
2061 const size_t k = j * 64 + i;
2065 secp256k1_pubkey Gi = G_vec[k];
2066 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &Gi, neg_z))
goto fail;
2068 secp256k1_pubkey newP;
2069 const secp256k1_pubkey* pts2[2] = { &P, &Gi };
2070 if (!secp256k1_ec_pubkey_combine(ctx, &newP, pts2, 2))
goto fail;
2075 unsigned char termH[32], tmp[32];
2076 unsigned char two_i[32] = {0};
2080 two_i[31 - (i / 8)] = (
unsigned char)(1u << (i % 8));
2085 secp256k1_pubkey Hi = H_vec[k];
2088 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &Hi, (
const unsigned char*)(y_inv_powers + 32*k)))
2092 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &Hi, termH))
2095 secp256k1_pubkey newP;
2096 const secp256k1_pubkey* pts2[2] = { &P, &Hi };
2097 if (!secp256k1_ec_pubkey_combine(ctx, &newP, pts2, 2))
goto fail;
2101 OPENSSL_cleanse(tmp, 32);
2102 OPENSSL_cleanse(termH, 32);
2105 OPENSSL_cleanse(z_j2, 32);
2111 unsigned char t_hat_ux[32];
2112 unsigned char zero32[32] = {0};
2115 if (memcmp(t_hat_ux, zero32, 32) != 0) {
2116 secp256k1_pubkey Q = U;
2117 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &Q, t_hat_ux)) { OPENSSL_cleanse(t_hat_ux,32); free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
2118 const secp256k1_pubkey* pts2[2] = { &P, &Q };
2119 secp256k1_pubkey newP;
2120 if (!secp256k1_ec_pubkey_combine(ctx, &newP, pts2, 2))
goto fail;
2124 OPENSSL_cleanse(t_hat_ux, 32);
2129 unsigned char neg_mu[32];
2130 memcpy(neg_mu, mu, 32);
2131 if (!secp256k1_ec_seckey_negate(ctx, neg_mu)) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
2133 secp256k1_pubkey mu_term = *pk_base;
2134 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &mu_term, neg_mu)) { OPENSSL_cleanse(neg_mu,32); free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
2135 OPENSSL_cleanse(neg_mu, 32);
2137 const secp256k1_pubkey* pts2[2] = { &P, &mu_term };
2138 secp256k1_pubkey newP;
2139 if (!secp256k1_ec_pubkey_combine(ctx, &newP, pts2, 2))
goto fail;
2145 secp256k1_pubkey* Hprime = (secp256k1_pubkey*)malloc(n *
sizeof(secp256k1_pubkey));
2146 if (!Hprime) { free(y_block_sum); free(y_powers); free(y_inv_powers);
goto fail; }
2148 for (
size_t k = 0; k < n; k++) {
2149 Hprime[k] = H_vec[k];
2150 if (!secp256k1_ec_pubkey_tweak_mul(ctx, &Hprime[k], (
const unsigned char*)(y_inv_powers + 32 * k))) {
2152 free(y_block_sum); free(y_powers); free(y_inv_powers);