

今研究で[TEPLA](http://www.cipher.risk.tsukuba.ac.jp/tepla/index.html)というオープンソースライブラリを利用して楕円曲線暗号を利用した署名の作成や検証を行うプログラムを書いています。  
度々Segmentation Faultエラーが出るのですが、今回はソースコードの`//Segmentation
Fault`の部分でSegmentation Faultまたはエラーや警告なしで終了するのですが、その要因になっていそうなどの変数(calcpair,
content.aggsig, g, ptmp)についても予期していない挙動は行っておりません。  
解決法やご指摘、意見など聞かせていただけると幸いです。よろしくおねがいします。

gdbを利用してデバッグした結果、該当の行での外部ライブラリ参照の際にGMPの関数?(`__gmpn_copyi`)を呼び出しているようなのですが、そこでSegmentation
faultを起こしているようです。  
aggregate.cを修正しました。ですが、エラーはそのままでした。

aggregate.c

[code]

    #include"integrated.h"
    
    void aggregate(int auth)
    {
    extern mdata *mod;
    extern cdata content;
    extern EC_POINT g;
    extern EC_PAIRING p;
    extern EC_POINT publickey;
    
    int i, j;
    int mnmb;
    unsigned char aggstr[65536] = { 0 };
    unsigned char pair[1024] = { 0 };
    unsigned char strbuff[1024] = { 0 };
    unsigned char gbuf[512] = { 0 };
    FILE *aggregatefile;
    FILE *pairing1st;
    FILE *pairing2nd;
    FILE *pkey;
    Element calcpair;
    Element pairbuff;
    Element verify;
    
    //Evacuate parameter g
    point_get_str(gbuf, g);
    
    //Initialize
    mnmb = content.nummodel;
    pairing_clear(p);
    pairing_init(p, "ECBN254a");
    point_init(g, p->g1);
    point_set_str(g, gbuf);
    point_init(content.aggsig, p->g2);
    point_set_infinity(content.aggsig);
    point_init(publickey, p->g1);
    element_init(calcpair, p->g3);
    element_set_one(calcpair);
    element_init(pairbuff, p->g3);
    element_set_one(pairbuff);
    element_init(verify, p->g3);
    
    for (i = 0; i < mnmb; i++)
    {
        point_init(mod[i].signature, p->g2);
        point_set_str(mod[i].signature, mod[i].sigstr);
        point_init(mod[i].hash, p->g2);
        point_set_str(mod[i].hash, mod[i].hashstr);
    }
    
    if (auth == 1)
    {
        aggregatefile = fopen("slot/aggregate_sig.txt", "w");
        pairing1st = fopen("slot/pairing1st.txt", "w");
    }
    else {
        aggregatefile = fopen("slot'/aggregate_sig.txt", "w");
        pairing1st = fopen("slot/pairing1st.txt", "r");
        pairing2nd = fopen("slot'/pairing2nd.txt", "w");
        pkey = fopen("slot/publickey.txt", "r");
    
        if (pairing1st == NULL)
        {
            printf("Failed to open file.\n");
            exit(1);
        }
    
        if (pkey == NULL)
        {
            printf("Failed to open file.\n");
            exit(1);
        }
    }
    
    printf("Try to make aggregate signature.\n");
    for (i = 0; i < mnmb; i++)
    {
        point_add(content.aggsig, content.aggsig, mod[i].signature);
    }
    
    point_get_str(aggstr, content.aggsig);
    fputs(aggstr, aggregatefile);
    printf("Make aggregate signature!\nSave to aggregate_sig.txt\n\n");
    
    printf("g->");
    point_print(g);
    printf("\n");
    printf("Aggregate signature->");
    point_print(content.aggsig);
    printf("\n\n");
    //printf("Element->");
    //element_print(calcpair);
    //printf("\n");
    
    printf("Calculate pairing.\n");
    if (auth == 1)
    {
        pairing_map(calcpair, content.aggsig, g, p);    //Segmentation fault
        element_get_str(pair, calcpair);
        fputs(pair, pairing1st);
    }
    else {
        fgets(strbuff, 1024, pkey);
        point_set_str(publickey, strbuff);
        fgets(strbuff, 1024, pairing1st);
        element_set_str(verify, strbuff);
    
        for (i = 0; i < mnmb; i++)
        {
            pairing_map(pairbuff, mod[i].hash, publickey, p);
            element_mul(calcpair, calcpair, pairbuff);
        }
    
        if (element_cmp(verify, calcpair) == 0)
        {
            printf("Signature is legal.\n");
        }
        else {
            printf("Signature is illegal.\n");
        }
    }
    
    fclose(aggregatefile);
    fclose(pairing1st);
    fclose(pairing2nd);
    fclose(pkey);
    
    printf("aggregate.c Finished.\n\n");
    }
    
[/code]

integrated.h

[code]

    #pragma once
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<time.h>
    #include<tepla/ec.h>
    
    //Path for various files
    #define KEYPATH "slot/publickey.txt"
    #define PMMDATA "share/sample.pmm"
    #define SETTINGPATH "slot/control_settings.txt"
    
    //model data structure
    typedef struct modeldata {
    int modelid;        //Model ID in content
    char janame[32];    //Model's Japasese name
    char enname[32];    //Model's English name
    char filepath[256];     //File path to model data
    int modify;         //Check flag for modification
    int change;         //Check flag for changable
    int delete;         //Check flag for deletable
    int add;            //Check flag for addable
    unsigned int permit;    //Permission for edit control
    EC_POINT hash;      //Buffer for caliculating value of hash
    char hashstr[1024];     //For saving hash data
    EC_POINT signature;     //Buffer for caliculating signature
    char sigstr[1024];      //For saving signature data
    } mdata;
    
    //content data structure
    typedef struct contentdata {
    int nummodel;
    EC_POINT aggsig;
    } cdata;
    
    extern void Init_tepla();
    extern void CLK_INIT();
    extern void CLK_START();
    extern void CLK_END();
    
[/code]

main.c

[code]

    #include"integrated.h"
    
    //Define global variables
    clock_t clk[2] = { 0,0 };
    EC_PAIRING p;
    EC_POINT g;
    EC_POINT publickey;
    gmp_randstate_t state;
    mpz_t grx;
    cdata content;
    mdata *mod;
    
    //Define external function
    extern void keygen();
    extern void dataextract();
    extern void siggen(int auth);
    extern void aggregate(int auth);
    void Init_tepla();
    void CLK_INIT();
    void CLK_START();
    void CLK_END();
    
    //main
    int main()
    {   
    unsigned int author = 0;
    
    //input
    scanf("%d", &author);
    
    CLK_INIT();
    CLK_START();
    keygen();
    dataextract();
    siggen(author);
    aggregate(author);
    
    free(mod);
    
    printf("Program finished.\n");
    CLK_END();
    
    return 0;
    }
    
    //To initialize tepla
    void Init_tepla()
    {
    printf("Initialize tepla\n");
    pairing_init(p, "ECBN254a");
    point_init(g, p->g1);
    point_random(g);
    
    mpz_init(grx);
    gmp_randinit_default(state);
    gmp_randseed_ui(state, (unsigned)time(NULL));
    mpz_urandomb(grx, state, 256);
    }
    
    //Functions to measure elapsed time
    void CLK_INIT()
    {
    clk[0] = clock();
    clk[1] = clock();
    }
    
    void CLK_START()
    {
    clk[0] = clock();
    }
    
    void CLK_END()
    {
    clk[1] = clock();
    double elapsed_time = (double)(clk[1] - clk[0]) / (CLOCKS_PER_SEC)*1000;
    printf("Elapsed time is %.2lf [ms]\n", elapsed_time);
    }
    
[/code]


CC BY-SA 4.0
2019-01-22T21:31:40.917
52218
2019-01-23T20:44:18.957
2019-01-23T20:44:18.957
31611
31611
question
c gcc cygwin
Segmentation Faultエラーの要因について