【Mac】PBC库的vscode运行配置

3 篇文章 0 订阅
3 篇文章 0 订阅

【Mac】PBC库的vscode运行配置

首先从github下载好pbc并本地编译安装,然后将这些东西放入工程目录中。

├── misc
│   ├── darray.c
│   ├── darray.h
│   ├── darray.o
│   ├── extend_printf.c
│   ├── extend_printf.o
│   ├── get_time.c
│   ├── get_time.win32.c
│   ├── memory.c
│   ├── memory.o
│   ├── symtab.c
│   ├── symtab.h
│   ├── symtab.o
│   ├── utils.c
│   └── utils.o
├── param
│   ├── a.param
│   ├── a1.param
│   ├── d105171-196-185.param
│   ├── d159.param
│   ├── d201.param
│   ├── d224.param
│   ├── d277699-175-167.param
│   ├── d278027-190-181.param
│   ├── e.param
│   ├── f.param
│   ├── g149.param
│   └── i.param

tasks.json配置如下

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-O0",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-I./misc",
                "-L.","-lpbc","-lgmp"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

launch.json配置如下

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "clang - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": ["param/a.param"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang build active file"
        }
    ]
}

以官方自带的bls.c为例(略微修改)

#include <pbc/pbc.h>
#include <pbc/pbc_test.h>
int main(int argc, char **argv)
{
    pairing_t pairing;
    element_t g, h;
    element_t public_key, secret_key;
    element_t sig;
    element_t temp1, temp2;

    pbc_demo_pairing_init(pairing, argc, argv);
    if (!pairing_is_symmetric(pairing))
        pbc_die("pairing must be symmetric");

    element_init_G2(g, pairing);
    element_init_G2(public_key, pairing);
    element_init_G1(h, pairing);
    element_init_G1(sig, pairing);
    element_init_GT(temp1, pairing);
    element_init_GT(temp2, pairing);
    element_init_Zr(secret_key, pairing);

    // generate system parameters
    element_random(g);

    // generate a private key
    element_random(secret_key);

    // generate a public key
    element_pow_zn(public_key, g, secret_key);

    // Verify Signature
    element_from_hash(h, "hello", 5);
    element_pow_zn(sig, h, secret_key);
    pairing_apply(temp1, sig, g, pairing);
    pairing_apply(temp2, h, public_key, pairing);
    if (!element_cmp(temp1, temp2))
    {
        printf("signature verifies\n");
    }
    else
    {
        printf("signature does not verify\n");
    }
    // 打印所有element_t类型的变量
    element_printf("g = %B\n", g);
    element_printf("h = %B\n", h);
    element_printf("public_key = %B\n", public_key);
    element_printf("secret_key = %B\n", secret_key);
    element_printf("sig = %B\n", sig);
    element_printf("temp1 = %B\n", temp1);
    element_printf("temp2 = %B\n", temp2);

    // the signature could be converted to bytes for storage or transmission:
    int n = pairing_length_in_bytes_compressed_G1(pairing);
    // Alternative
    // int n = element_length_in_bytes_compressed(sig);
    unsigned char *data = malloc(n);
    element_to_bytes_compressed(data, sig);

    // On the other end, the signature must be decompressed:
    element_from_bytes_compressed(sig, data);

    // We can save more space by using the x-coordinate of the signature only
    int m = pairing_length_in_bytes_x_only_G1(pairing);
    // Alternative
    // int m = element_length_in_bytes_x_only(sig);
    unsigned char *data2 = malloc(m);
    element_to_bytes_compressed(data2, sig);
    // two different points may have the same x-coordinate.

    // 打印unsigned char*类型的变量
    printf("data = %s\n", data);
    printf("data2 = %s\n", data2);
    // element_printf("data = %B\n", data);
    // element_printf("data2 = %B\n", data2);

    return 0;
}

运行结果

coalbrother@MacBook-Air ~ % /bin/bash -c 'arch -arch arm64 "/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Resources/darwin-debug" --unix-socket=/tmp/wXU6WJ --arch=arm64 --working-dir "/Users/coalbrother/Downloads/mystudy/BLS" --disable-aslr --env="OS_ACTIVITY_DT_MODE=enable" --  "/Users/coalbrother/Downloads/mystudy/BLS/bls" "param/a.param" ; echo Process exited with status $? ; exit';exit

Launching: '/Users/coalbrother/Downloads/mystudy/BLS/bls'
Working directory: '/Users/coalbrother/Downloads/mystudy/BLS'
2 arguments:
argv[0] = '/Users/coalbrother/Downloads/mystudy/BLS/bls'
argv[1] = 'param/a.param'
signature verifies
g = [1382943752683876760312623002571026479942147710481053945644195097885274505937369319989860947088051878235795996778750515321388485598337204342736081590221935, 1308481955828876948130409698004072003616439085849852863819525781134579339280139863844951372916077211877960958245585343548042717584041668879494514441352921]
h = [7508008622687391414284635340560807695956550600619012687071792726610539336867926153372517772430204434002762313233625276147830901433321995607947772135640409, 3405655391505547936442475596823059751210807265275419089724969550356292683622390921439162134635381270466624373776151526505675770388334535645523271371923350]
public_key = [772578929734424998555228361851180907871599382233626627914617751054439920627609207123701924322788229863422414799719216079289844406660489954940799879596606, 6188083036730866005265858734997817518483961197947462990286358369686013290501493334352966108928358905878232712056929454238599272992086988844297063485447724]
secret_key = 28584690775597747679254887513909123074547294195
sig = [7630311469419017549730710697157124920989186372950193469209016709703607487655802142616871974176361709399767320853404228243877589180789521546751258743875888, 7693624774781248683760957457155937536090314280319311756431720909111102949246053131123587318877959501623279094131876720182817959566173873364143041025047210]
temp1 = [372331037400617545927376521728258524515425808066567753416453248637812222872381822878809365211943687674179280446583718787381478740522942108747484184777094, 6252739565177841502029901588831253444752862059581754100070569006210036063843565303166606269628225823843738765242629263735875662736407447996900554354194682]
temp2 = [372331037400617545927376521728258524515425808066567753416453248637812222872381822878809365211943687674179280446583718787381478740522942108747484184777094, 6252739565177841502029901588831253444752862059581754100070569006210036063843565303166606269628225823843738765242629263735875662736407447996900554354194682]
data = 
data2 = ??/????am\?????}?jITJtľ@?_?+{Y??G!?sN?Ӎ?o?ߏ?3?5?ls?SH??)0
Process exited with status 0

Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

后续将跟进其他数字签名算法的复现

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值