2024-07-03  2024-07-03    2371 字  5 分钟
- Test

测试一下codapi能不能用

Codapi 是专门为嵌入文章、产品文档、在线课程、移动应用程序等而设计的,所以它非常轻量级。可以通过很简单的方式将其集成到自己的网页中。下面测试一下自定义的代码块功能是否可用。(test标签中的文章是用来测试网页的功能是否可用,没有阅读价值)

我是正常的codeblock

为了不然所有的代码块都高亮显示,对代码块的渲染规则做了自定义,下面测试能否显示普通的代码块。

print("Hello python")
print("Hello python")
print("Hello python")

我是bash代码

测试高亮代码块格式能否使用。首先测试一种没在服务器上部署的代码类型,正常情况下,应该显示出代码高亮的样式,但是代码后面不显示运行按钮。

# 显示当前的Git配置
$ git config --list
# 定义当前用户所有提交使用的作者邮箱。
$ git config --global alias.<alias-name <git-command>
# 为Git命令创建一个快捷方式(别名)。
$ git config --system core.editor <editor>

我是python代码

测试一下代码块高亮功能和运行功能能否使用,然后测试python代码块。正常情况下,下面的python代码将被高亮,并且代码后面会出现一个 Run 按钮,点击可以运行代码块中的代码。

print("Hello python")
print("Hello codapi")

我是cpp代码

本次基准测试针对BLS12381曲线在底层安全沙盒环境下的执行效率进行了全方位评估,具体测试涵盖了四个核心维度。首先是群运算性能,包含了在单倍精度G1群和双倍精度G2群上分别进行点乘与点加的耗时统计,用以直观对比不同代数结构下的算力差异。其次是双线性配对开销,重点测试了从配对计算到最终幂运算的完整Ate-Pairing闭环流程,这是评估协议运行成本的关键指标。第三是目标群GT的域运算,包括在十二次扩展域上的元素乘法与幂运算效率。最后是哈希映射原语,测试了将随机字节流经过复杂算法安全映射至椭圆曲线点的全过程耗时,从而为开发者在构建聚合签名或零知识证明协议时提供详实的底层性能参考。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sys/time.h>
#include "randapi.h"
#include "bls_BLS12381.h"
#include "pair_BLS12381.h"

using namespace core;
using namespace BLS12381;
using namespace B384_58; 
using namespace std;

BIG order_Time;
csprng rng;

void randBigInt(BIG *big) {
    BIG mod;
    BIG_rcopy(mod, CURVE_Order);
    BIG_randtrunc(*big, mod, 2 * CURVE_SECURITY_BLS12381, &rng);
}

void hashtoStr384(char *str, octet *ct) {
    hash384 h;
    memset(str, 0, 48);
    HASH384_init(&h);
    for (int j = 0; j < ct->max; j++) {
        HASH384_process(&h, ct->val[j]);
    }
    HASH384_hash(&h, str);
}

void hashtoDStr384(char *str, octet *ct) {
    hash384 h;
    memset(str, 0, 48 * 2);
    HASH384_init(&h);
    for (int j = 0; j < ct->max; j++) {
        HASH384_process(&h, ct->val[j]);
    }
    HASH384_hash(&h, str);
    for (int j = 0; j < 48; j++) {
        HASH384_process(&h, str[j]);
    }
    HASH384_hash(&h, str + 48);
}

void testForBLS12381WithPairing() {
    cout << "------------------- testForBLS12381WithPairing-start\t ---------------------" << endl;
    BIG_rcopy(order_Time, CURVE_Order);

    ECP alpha;
    ECP_generator(&alpha);
    long totalTime = 0;
    int repeatedCount = 100;
    for (int i = 0; i < repeatedCount; i++) {
        BIG s;
        randBigInt(&s);
        struct timeval startTime;
        struct timeval endTime;
        gettimeofday(&startTime, NULL);
        ECP_mul(&alpha, s);
        gettimeofday(&endTime, NULL);
        if (ECP_isinf(&alpha)) {
            i--;
            printf("G1 SM error\n");
        } else {
            totalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);
        }
    }
    printf("G1 SM time:%ld us\n", totalTime / repeatedCount);

    ECP beta;
    ECP_generator(&beta);
    totalTime = 0;
    for (int i = 0; i < repeatedCount; i++) {
        BIG s;
        randBigInt(&s);
        struct timeval startTime;
        struct timeval endTime;
        ECP_mul(&alpha, s);
        randBigInt(&s);
        ECP_mul(&beta, s);
        gettimeofday(&startTime, NULL);
        ECP_add(&alpha, &beta);
        gettimeofday(&endTime, NULL);
        if (ECP_isinf(&alpha)) {
            i--;
            printf("G1 ADD error\n");
        } else {
            totalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);
        }
    }
    printf("G1 ADD time:%ld/1000 us\n", totalTime / repeatedCount);
    cout << endl;

    ECP2 alpha2;
    ECP2_generator(&alpha2);
    totalTime = 0;
    for (int i = 0; i < repeatedCount; i++) {
        BIG s;
        randBigInt(&s);
        struct timeval startTime;
        struct timeval endTime;
        gettimeofday(&startTime, NULL);
        ECP2_mul(&alpha2, s);
        gettimeofday(&endTime, NULL);
        if (ECP2_isinf(&alpha2)) {
            i--;
            printf("G2 SM error\n");
        } else {
            totalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);
        }
    }
    printf("G2 SM time:%ld us\n", totalTime / repeatedCount);

    ECP2 beta2;
    ECP2_generator(&beta2);
    totalTime = 0;
    for (int i = 0; i < repeatedCount; i++) {
        BIG s;
        randBigInt(&s);
        struct timeval startTime;
        struct timeval endTime;
        ECP2_mul(&alpha2, s);
        randBigInt(&s);
        ECP2_mul(&beta2, s);
        gettimeofday(&startTime, NULL);
        ECP2_add(&alpha2, &beta2);
        gettimeofday(&endTime, NULL);
        if (ECP2_isinf(&alpha2)) {
            i--;
            printf("G2 ADD error\n");
        } else {
            totalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);
        }
    }
    printf("G2 ADD time:%ld/1000 us\n", totalTime / repeatedCount);
    cout << endl;

    FP12 temp1;
    FP12 temp2;

    long pairTotalTime = 0;
    long fp12MULTime = 0;
    long fp12PowTime = 0;
    for (int i = 0; i < repeatedCount; i++) {
        BIG s;
        randBigInt(&s);
        ECP_mul(&alpha, s);
        randBigInt(&s);
        ECP2_mul(&alpha2, s);

        struct timeval startTime;
        struct timeval endTime;

        gettimeofday(&startTime, NULL);
        PAIR_ate(&temp1, &alpha2, &alpha);
        PAIR_fexp(&temp1);
        FP12_reduce(&temp1);
        gettimeofday(&endTime, NULL);
        if (FP12_isunity(&temp1) || FP12_iszilch(&temp1)) {
            printf("pairing error [temp1]\n");
        }
        pairTotalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);

        randBigInt(&s);
        ECP_mul(&alpha, s);
        randBigInt(&s);
        ECP2_mul(&alpha2, s);
        PAIR_ate(&temp2, &alpha2, &alpha);
        PAIR_fexp(&temp2);
        FP12_reduce(&temp2);
        if (FP12_isunity(&temp2) || FP12_iszilch(&temp2)) {
            printf("pairing error [temp2]\n");
        }

        gettimeofday(&startTime, NULL);
        FP12_mul(&temp1, &temp2);
        FP12_reduce(&temp1);
        gettimeofday(&endTime, NULL);
        if (FP12_isunity(&temp1) || FP12_iszilch(&temp1)) {
            printf("pairing error [mul]\n");
        }
        fp12MULTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);

        gettimeofday(&startTime, NULL);
        FP12_pow(&temp1, &temp2, s);
        FP12_reduce(&temp1);
        gettimeofday(&endTime, NULL);
        if (FP12_isunity(&temp1) || FP12_iszilch(&temp1)) {
            printf("pairing error [mul]\n");
        }
        fp12PowTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);
    }
    printf("PAIR time:%ld\n", pairTotalTime / repeatedCount);
    printf("GT MUL time:%ld\n", fp12MULTime / repeatedCount);
    printf("GT POW time:%ld\n", fp12PowTime / repeatedCount);
    cout << endl;

    totalTime = 0;
    for (int i = 0; i < repeatedCount; i++) {
        FP fp;
        FP_rand(&fp, &rng);
        struct timeval startTime;
        struct timeval endTime;

        gettimeofday(&startTime, NULL);
        ECP_map2point(&alpha, &fp);
        gettimeofday(&endTime, NULL);
        if (ECP_isinf(&alpha)) {
            i--;
            printf("G1 map2point error\n");
        } else {
            totalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);
        }
    }
    printf("G1 map2point time:%ld\n", totalTime / repeatedCount);

    totalTime = 0;
    for (int i = 0; i < repeatedCount; i++) {
        FP2 fp;
        FP2_rand(&fp, &rng);
        struct timeval startTime;
        struct timeval endTime;

        gettimeofday(&startTime, NULL);
        ECP2_map2point(&alpha2, &fp);
        gettimeofday(&endTime, NULL);
        if (ECP2_isinf(&alpha2)) {
            i--;
            printf("G2 map2point error\n");
        } else {
            totalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);
        }
    }
    printf("G2 map2point time:%ld\n", totalTime / repeatedCount);

    int creLength = BASEBITS_B384_58 * 3;
    totalTime = 0;
    octet idOct;
    idOct.val = (char *) malloc(creLength);
    idOct.max = creLength;
    idOct.len = creLength;
    for (int i = 0; i < repeatedCount; i++) {

        for (int j = 0; j < creLength; j++) {
            idOct.val[j] = RAND_byte(&rng);
        }
        char temp[48];
        ECP point;
        FP fp;
        struct timeval startTime;
        struct timeval endTime;

        gettimeofday(&startTime, NULL);
        hashtoStr384(temp, &idOct);
        FP_fromBytes(&fp, temp);
        ECP_map2point(&point, &fp);
        gettimeofday(&endTime, NULL);
        if (ECP_isinf(&point)) {
            i--;
            printf("G1 hash error\n");
        } else {
            totalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);
        }
    }
    free(idOct.val);
    printf("G1 hash id to point time:%ld\n", totalTime / repeatedCount);

    totalTime = 0;
    idOct.val = (char *) malloc(creLength);
    idOct.max = creLength;
    idOct.len = creLength;
    for (int i = 0; i < repeatedCount; i++) {

        for (int j = 0; j < creLength; j++) {
            idOct.val[j] = RAND_byte(&rng);
        }
        char temp[48 * 2];
        ECP2 point;
        FP2 fp;
        struct timeval startTime;
        struct timeval endTime;

        gettimeofday(&startTime, NULL);
        hashtoDStr384(temp, &idOct);
        FP2_fromBytes(&fp, temp);
        ECP2_map2point(&point, &fp);
        gettimeofday(&endTime, NULL);
        if (ECP2_isinf(&point)) {
            i--;
            printf("G2 hash error\n");
        } else {
            totalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);
        }

    }
    free(idOct.val);
    printf("G2 hash id to point time:%ld\n", totalTime / repeatedCount);

    totalTime = 0;
    idOct.val = (char *) malloc(creLength);
    idOct.max = creLength;
    idOct.len = creLength;
    for (int i = 0; i < repeatedCount; i++) {

        for (int j = 0; j < creLength; j++) {
            idOct.val[j] = RAND_byte(&rng);
        }
        char temp[48];

        struct timeval startTime;
        struct timeval endTime;

        gettimeofday(&startTime, NULL);
        hashtoStr384(temp, &idOct);
        gettimeofday(&endTime, NULL);

        totalTime += (endTime.tv_sec - startTime.tv_sec) * 1000000 + (endTime.tv_usec - startTime.tv_usec);

    }
    free(idOct.val);
    printf("hash time:%ld\n", totalTime / repeatedCount);

    cout << "------------------- testForBLS12381WithPairing-end\t ---------------------" << endl << endl;

}

int main() {
    char raw[100];
    octet RAW = {0, sizeof(raw), raw};
    for (int i = 0; i < 100; i++) RAW.val[i] = i; 
    CREATE_CSPRNG(&rng, &RAW);

    testForBLS12381WithPairing();

    KILL_CSPRNG(&rng);
    return 0;
}

创建 codeapi 的 template

上面的代码运行需要将所有的代码都写在代码块中,某些情况下,我们可能不需要展示所有的代码,只展示代码中的核心部分,但是整个代码必须还能够正常运行,这里通过两种方式实现了这个功能,下面分别测试这个功能是否可用。

1. 提前定义模板

可以预定义一些模板,在代码块运行的时候之间使用模板,例如,这里预定义了 java 类 public class Main{ ##CODE## },代码块中的部分会替换掉模板中的 ##CODE## 部分。

public static void main(String[] args){
    System.out.println("我是使用Template main.java的Java代码");
    System.out.println("运行我所需的其他代码隐藏了");
}

2. 写博客时自定义模板

预定义模板的方式不够灵活,下面这种方式可以实现跟灵活的模板使用。我们可以通过下面的方式在写markdown文件的时候自定义模板的内容,例如,通过下面的语法预定义一个模板,并在下面的代码块中调用它,代码块中的部分会替换掉模板中的 ##CODE## 部分。

可以使用下面框框内的语法创建codapi 的 template,
其中 name 表示下面codapi代码中的 $template
<codapi-snippet sandbox={{.Type}} editor="base" template="{{$template}}">
</codapi-snippet>
---------------------------------------------------------------------
    |       ```template {name="temp.java"}                      |
    |       public class Main{                                  |
    |           public static void main(String[] args){         |
    |               ##CODE##                                    |
    |           }                                               |
    |       }                                                   |
    |       ```                                                 |
---------------------------------------------------------------------

在上述的模板定义中,模板的name被定义为 temp.java , 因此,这里创建的代码块的时候传入一个 {template=“temp.java”} 来使用这个模板即可。

    System.out.println("我是使用Template temp.java的Java代码");
    System.out.println("运行我所需的其他代码隐藏了");