<commit_before>/*
 * Copyright (c) 2016, <NAME> <<EMAIL>>
 * See LICENSE for licensing details.
 */

#include "maps.h"

#ifdef DEBUG
#include <iostream>
#define ASSERT(X,Y) if (!(X)) { \
    std::cerr << "Error: Assert of " << #X << " at line " \
    << __LINE__ << " failed in " << __FILE__ << "!" << std::endl \
    << Y << std::endl; \
    throw Y; \
    }
#else
#define ASSERT(X,Y) /* noop */
#endif

namespace gsnizk {

G1 B1::extract(const CRS &crs) const {
    ASSERT(crs.type == CRS_TYPE_EXTRACT, "Wrong type of CRS");
    return _2 - (Fp::getUnit() / crs.j1) * _1;
}

B1 B1::commit(const Fp &el, const Fp &r, const CRS &crs) {
    return el * crs.u1 + r * crs.v1;
}

B1 B1::commit(const B1 &el, const Fp &r, const CRS &crs) {
    return el + r * crs.v1;
}

B1 B1::commit(const B1 &el, const Fp &r, const Fp &s, const CRS &crs) {
    if (crs.type & 1)
        return el + (r + crs.i1 * s) * crs.v1;
    return el + r * crs.v1 + s * crs.w1;
}

G2 B2::extract(const CRS &crs) const {
    ASSERT(crs.type == CRS_TYPE_EXTRACT, "Wrong type of CRS");
    return _2 - (Fp::getUnit() / crs.j2) * _1;
}

B2 B2::commit(const Fp &el, const Fp &r, const CRS &crs) {
    return el * crs.u2 + r * crs.v2;
}

B2 B2::commit(const B2 &el, const Fp &r, const CRS &crs) {
    return el + r * crs.v2;
}

B2 B2::commit(const B2 &el, const Fp &r, const Fp &s, const CRS &crs) {
    if (crs.type & 1)
        return el + (r + crs.i2 * s) * crs.v2;
    return el + r * crs.v2 + s * crs.w2;
}

GT BT::extract(const CRS &crs) const {
    ASSERT(crs.type == CRS_TYPE_EXTRACT, "Wrong type of CRS");
    Fp p = Fp(-1) / crs.j1;
    return (_22 * (_12 ^ p)) * ((_21 * (_11 ^ p)) ^ (Fp(-1) / crs.j2));
}

BT BT::pairing(const B1 &a, const B2 &b) {
    return BT(GT::pairing(a._1, b._1), GT::pairing(a._1, b._2),
              GT::pairing(a._2, b._1), GT::pairing(a._2, b._2));
}

BT BT::pairing(const std::vector< std::pair<B1,B2> > &lst) {
    if (lst.empty()) return BT();
    std::vector<std::pair<G1, G2> > p11, p12, p21, p22;
    p11.reserve(lst.size());
    p12.reserve(lst.size());
    p21.reserve(lst.size());
    p22.reserve(lst.size());
    for (const std::pair<B1, B2> p: lst) {
        p11.push_back(std::pair<G1, G2>(p.first._1, p.second._1));
        p12.push_back(std::pair<G1, G2>(p.first._1, p.second._2));
        p21.push_back(std::pair<G1, G2>(p.first._2, p.second._1));
        p22.push_back(std::pair<G1, G2>(p.first._2, p.second._2));
    }
    return BT(GT::pairing(p11), GT::pairing(p12),
              GT::pairing(p21), GT::pairing(p22));
}

CRS::CRS(bool binding) : v1(G1(), G1::getRand()), v2(G2(), G2::getRand()),
    type(binding ? CRS_TYPE_EXTRACT : CRS_TYPE_ZK),
    i1(Fp::getRand()), j1(Fp::getRand()),
    i2(Fp::getRand()), j2(Fp::getRand())
{
    computeElements();
}

void CRS::makePublic() {
    if (type != CRS_TYPE_PUBLIC) {
        i1 = Fp();
        i2 = Fp();
        j1 = Fp();
        j2 = Fp();
        type = CRS_TYPE_PUBLIC;
    }
}

CRS CRS::genPrivate(std::ostream &stream) const {
    if (type != CRS_TYPE_PUBLIC)
        throw "Unexpected use of gsnizk::genPrivate";
    CRS priv;
    priv.type = CRS_TYPE_PRIVATE;
    priv.v1 = v1;
    priv.i1 = Fp::getRand();
    priv.v2 = v2;
    priv.i2 = Fp::getRand();
    priv.computeElements(false);
    Fp r_rho = Fp::getRand(), r_sig = Fp::getRand();
    stream << (priv.i1 * u2 + r_rho * v2);
    stream << (priv.i2 * u1 + r_sig * v1);
    stream << (-r_rho) * v1;
    stream << (-r_sig) * v2;
    return priv;
}

bool CRS::checkPrivate(std::istream &stream, CRS priv) const {
    G2 c_rho_1, c_rho_2, p3, p4, g2;
    G1 c_sig_1, c_sig_2, p1, p2, g1;
    stream >> c_rho_1 >> c_rho_2;
    stream >> c_sig_1 >> c_sig_2;
    stream >> p1 >> p2 >> p3 >> p4;
    if (GT::pairing(priv.w1._1,u2._1) != GT::pairing(v1._1, c_rho_1) *
            GT::pairing(p1, v2._1))
        return false;
    if (GT::pairing(priv.w1._1,u2._2) != GT::pairing(v1._1, c_rho_2) *
            GT::pairing(p1, v2._2))
        return false;
    if (GT::pairing(priv.w1._2,u2._1) != GT::pairing(v1._2, c_rho_1) *
            GT::pairing(p2, v2._1))
        return false;
    if (GT::pairing(priv.w1._2,u2._2) != GT::pairing(v1._2, c_rho_2) *
            GT::pairing(p2, v2._2))
        return false;
    Fp c1_1 = Fp::getRand(), c1_2 = Fp::getRand();
    Fp c2_1 = Fp::getRand(), c2_2 = Fp::getRand();
    std::vector< std::pair<G1,G2> > pairs;
    pairs.reserve(3);
    g1 = -(c1_1 * priv.w1._1 + c1_2 * priv.w1._2);
    g2 = c2_1 * u2._1 + c2_2 * u2._2;
    pairs.push_back(std::pair<G1,G2>(g1, g2));
    g1 = c1_1 * v1._1 + c1_2 * v1._2;
    g2 = c2_1 * c_rho_1 + c2_2 * c_rho_2;
    pairs.push_back(std::pair<G1,G2>(g1, g2));
    g1 = c1_1 * p1 + c1_2 * p2;<commit_msg>Private CRS proof verification adjustement<commit_after>/*
 * Copyright (c) 2016, <NAME> <<EMAIL>>
 * See LICENSE for licensing details.
 */

#include "maps.h"

#ifdef DEBUG
#include <iostream>
#define ASSERT(X,Y) if (!(X)) { \
    std::cerr << "Error: Assert of " << #X << " at line " \
    << __LINE__ << " failed in " << __FILE__ << "!" << std::endl \
    << Y << std::endl; \
    throw Y; \
    }
#else
#define ASSERT(X,Y) /* noop */
#endif

namespace gsnizk {

G1 B1::extract(const CRS &crs) const {
    ASSERT(crs.type == CRS_TYPE_EXTRACT, "Wrong type of CRS");
    return _2 - (Fp::getUnit() / crs.j1) * _1;
}

B1 B1::commit(const Fp &el, const Fp &r, const CRS &crs) {
    return el * crs.u1 + r * crs.v1;
}

B1 B1::commit(const B1 &el, const Fp &r, const CRS &crs) {
    return el + r * crs.v1;
}

B1 B1::commit(const B1 &el, const Fp &r, const Fp &s, const CRS &crs) {
    if (crs.type & 1)
        return el + (r + crs.i1 * s) * crs.v1;
    return el + r * crs.v1 + s * crs.w1;
}

G2 B2::extract(const CRS &crs) const {
    ASSERT(crs.type == CRS_TYPE_EXTRACT, "Wrong type of CRS");
    return _2 - (Fp::getUnit() / crs.j2) * _1;
}

B2 B2::commit(const Fp &el, const Fp &r, const CRS &crs) {
    return el * crs.u2 + r * crs.v2;
}

B2 B2::commit(const B2 &el, const Fp &r, const CRS &crs) {
    return el + r * crs.v2;
}

B2 B2::commit(const B2 &el, const Fp &r, const Fp &s, const CRS &crs) {
    if (crs.type & 1)
        return el + (r + crs.i2 * s) * crs.v2;
    return el + r * crs.v2 + s * crs.w2;
}

GT BT::extract(const CRS &crs) const {
    ASSERT(crs.type == CRS_TYPE_EXTRACT, "Wrong type of CRS");
    Fp p = Fp(-1) / crs.j1;
    return (_22 * (_12 ^ p)) * ((_21 * (_11 ^ p)) ^ (Fp(-1) / crs.j2));
}

BT BT::pairing(const B1 &a, const B2 &b) {
    return BT(GT::pairing(a._1, b._1), GT::pairing(a._1, b._2),
              GT::pairing(a._2, b._1), GT::pairing(a._2, b._2));
}

BT BT::pairing(const std::vector< std::pair<B1,B2> > &lst) {
    if (lst.empty()) return BT();
    std::vector<std::pair<G1, G2> > p11, p12, p21, p22;
    p11.reserve(lst.size());
    p12.reserve(lst.size());
    p21.reserve(lst.size());
    p22.reserve(lst.size());
    for (const std::pair<B1, B2> p: lst) {
        p11.push_back(std::pair<G1, G2>(p.first._1, p.second._1));
        p12.push_back(std::pair<G1, G2>(p.first._1, p.second._2));
        p21.push_back(std::pair<G1, G2>(p.first._2, p.second._1));
        p22.push_back(std::pair<G1, G2>(p.first._2, p.second._2));
    }
    return BT(GT::pairing(p11), GT::pairing(p12),
              GT::pairing(p21), GT::pairing(p22));
}

CRS::CRS(bool binding) : v1(G1(), G1::getRand()), v2(G2(), G2::getRand()),
    type(binding ? CRS_TYPE_EXTRACT : CRS_TYPE_ZK),
    i1(Fp::getRand()), j1(Fp::getRand()),
    i2(Fp::getRand()), j2(Fp::getRand())
{
    computeElements();
}

void CRS::makePublic() {
    if (type != CRS_TYPE_PUBLIC) {
        i1 = Fp();
        i2 = Fp();
        j1 = Fp();
        j2 = Fp();
        type = CRS_TYPE_PUBLIC;
    }
}

CRS CRS::genPrivate(std::ostream &stream) const {
    if (type != CRS_TYPE_PUBLIC)
        throw "Unexpected use of gsnizk::genPrivate";
    CRS priv;
    priv.type = CRS_TYPE_PRIVATE;
    priv.v1 = v1;
    priv.i1 = Fp::getRand();
    priv.v2 = v2;
    priv.i2 = Fp::getRand();
    priv.computeElements(false);
    Fp r_rho = Fp::getRand(), r_sig = Fp::getRand();
    stream << (priv.i1 * u2 + r_rho * v2);
    stream << (priv.i2 * u1 + r_sig * v1);
    stream << (-r_rho) * v1;
    stream << (-r_sig) * v2;
    return priv;
}

bool CRS::checkPrivate(std::istream &stream, CRS priv) const {
    if ((v1 != priv.v1) || (v2 != priv.v2))
        return false;
    G2 c_rho_1, c_rho_2, p3, p4, g2;
    G1 c_sig_1, c_sig_2, p1, p2, g1;
    stream >> c_rho_1 >> c_rho_2;
    stream >> c_sig_1 >> c_sig_2;
    stream >> p1 >> p2 >> p3 >> p4;
    Fp c1_1 = Fp::getRand(), c1_2 = Fp::getRand();
    Fp c2_1 = Fp::getRand(), c2_2 = Fp::getRand();
    std::vector< std::pair<G1,G2> > pairs;
    pairs.reserve(3);
    g1 = -(c1_1 * priv.w1._1 + c1_2 * priv.w1._2);
    g2 = c2_1 * u2._1 + c2_2 * u2._2;
    pairs.push_back(std::pair<G1,G2>(g1, g2));
    g1 = c1_1 * v1._1 + c1_2 * v1._2;
    g2 = c2_1 * c_rho_1 + c2_2 * c_rho_2;
    pairs.push_back(std::pair<G1,G2>(g1, g2));
    g1 = c1_1 * p1 + c1_2 * p2;
{'fraction_non_alphanumeric': 0.20106465565043807, 'fraction_numerical': 0.055894421647998224, 'mean_word_length': 2.7748011720385097, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2314665', 'n_tokens_mistral': 4228, 'n_tokens_neox': 4140, 'n_words': 884}