Skip to content

Commit

Permalink
build: build
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestThePoet committed Apr 10, 2024
1 parent 645b421 commit f4fe789
Show file tree
Hide file tree
Showing 12 changed files with 12,603 additions and 2,289 deletions.
51 changes: 40 additions & 11 deletions docs/demos.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
"irUrl": "demos/arithmetic/egcd.ir",
"remark": "输入a和b(a≥b>0),输出(d,X,Y)使得d=gcd(a,b)且Xa+Yb=d"
},
{
"name": "模逆元计算",
"irUrl": "demos/arithmetic/inv_mod.ir",
"remark": "输入a和n,计算能使(a*x mod n)=1的x并输出x"
},
{
"name": "32位无符号整数的完整加法",
"irUrl": "demos/arithmetic/add_uint32.ir",
Expand Down Expand Up @@ -172,55 +177,79 @@
{
"name": "DH密钥交换:Alice",
"irUrl": "demos/cryptography/dh_alice.ir",
"remark": "Alice进行如下步骤:(1)生成并输出DH密钥交换参数(g,p),其中g为生成元,p=2q+1为31位的安全素数;(2)生成私钥x∈Zq,计算并输出公钥h_A=(g^x) mod p;(3)读取Bob的公钥h_B;(4)计算并输出共享密钥k_A=(h_B^x) mod p",
"remark": "Alice进行如下步骤:(1)选取生成元g和31位的安全素数p (p=2q+1),输出DH密钥交换参数(g,p);(2)均匀随机地选取私钥x∈Zq,计算并输出公钥h_A=(g^x) mod p;(3)读取Bob的公钥h_B;(4)计算并输出共享密钥k_A=(h_B^x) mod p",
"vmOptions": {
"maxExecutionStepCount": 50000000
}
},
{
"name": "DH密钥交换:Bob",
"irUrl": "demos/cryptography/dh_bob.ir",
"remark": "Bob进行如下步骤:(1)读取Alice生成的DH密钥交换参数(g,p)和公钥h_A;(2)生成私钥y∈Zq,计算并输出公钥h_B=(g^y) mod p;(3)计算并输出共享密钥k_B=(h_A^y) mod p",
"remark": "Bob进行如下步骤:(1)读取Alice生成的DH密钥交换参数(g,p)和公钥h_A;(2)均匀随机地选取私钥y∈Zq (p=2q+1),计算并输出公钥h_B=(g^y) mod p;(3)计算并输出共享密钥k_B=(h_A^y) mod p",
"vmOptions": {
"maxExecutionStepCount": 3000000
}
},
{
"name": "El Gamal:密钥对生成",
"irUrl": "demos/cryptography/elgamal_keygen.ir",
"remark": "程序选取生成元g和31位的安全素数p (p=2q+1),均匀随机地选取x∈Zq,计算h=(g^x) mod p。输出(g,p,h,x),其中(g,p,h)为公钥,(g,p,x)为私钥。",
"vmOptions": {
"maxExecutionStepCount": 50000000
}
},
{
"name": "El Gamal:消息加密",
"irUrl": "demos/cryptography/elgamal_enc.ir",
"remark": "输入公钥(g,p,h)和明文m,均匀随机地选取x∈Zq (p=2q+1),计算并输出密文c=(c1,c2)=((g^y) mod p, (h^y)*m mod p)",
"vmOptions": {
"maxExecutionStepCount": 5000000
}
},
{
"name": "El Gamal:消息解密",
"irUrl": "demos/cryptography/elgamal_dec.ir",
"remark": "输入私钥(g,p,x)和密文(c1,c2),计算并输出明文m=(c2/c1)=c2*c1^(-1) mod p",
"vmOptions": {
"maxExecutionStepCount": 3000000
}
},
{
"name": "Textbook RSA:密钥对生成",
"name": "Plain RSA:密钥对生成",
"irUrl": "demos/cryptography/rsa_keygen.ir",
"remark": "程序选取e=65537,生成(N,p,q),其中N为31位,p和q为素数,N=p*q,并有gcd(e,φ(N))=1 (φ(N)=(p-1)(q-1))。计算d=(e^(-1)) mod φ(N)。输出(N,e,d),其中(N,e)为公钥,(N,d)为私钥。",
"vmOptions": {
"maxExecutionStepCount": 3000000
}
},
{
"name": "Textbook RSA:消息加密",
"name": "Plain RSA:消息加密",
"irUrl": "demos/cryptography/rsa_enc.ir",
"remark": "输入公钥(N,e)和明文p,计算并输出密文c=(p^e) mod N。未对输入明文使用任何填充方案。",
"remark": "输入公钥(N,e)和明文p,计算并输出密文c=(p^e) mod N",
"vmOptions": {
"maxExecutionStepCount": 2000000
}
},
{
"name": "Textbook RSA:消息解密",
"name": "Plain RSA:消息解密",
"irUrl": "demos/cryptography/rsa_dec.ir",
"remark": "输入私钥(N,d)和密文c,计算并输出明文p=(c^d) mod N。不会检查明文的填充方案。",
"remark": "输入私钥(N,d)和密文c,计算并输出明文p=(c^d) mod N",
"vmOptions": {
"maxExecutionStepCount": 2000000
}
},
{
"name": "Textbook RSA:数字签名",
"name": "Plain RSA:数字签名",
"irUrl": "demos/cryptography/rsa_sign.ir",
"remark": "输入私钥(N,d)和消息m,计算并输出数字签名s=(m^d) mod N。未对输入消息使用任何填充方案。",
"remark": "输入私钥(N,d)和消息m,计算并输出数字签名s=(m^d) mod N",
"vmOptions": {
"maxExecutionStepCount": 2000000
}
},
{
"name": "Textbook RSA:验证数字签名",
"name": "Plain RSA:验证数字签名",
"irUrl": "demos/cryptography/rsa_verify.ir",
"remark": "输入公钥(N,e),消息m和消息的数字签名s,计算m1=(s^e) mod N。如果m1=m,输出1,否则输出0。不会检查消息的填充方案。",
"remark": "输入公钥(N,e),消息m和消息的数字签名s,计算m1=(s^e) mod N。如果m1=m,输出1,否则输出0。",
"vmOptions": {
"maxExecutionStepCount": 2000000
}
Expand Down
267 changes: 267 additions & 0 deletions docs/demos/arithmetic/inv_mod.cmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
/*
* Modular multiplicative inverse computing algorithm
* Input a and n, compute multiplicative inverse of a modulo n
* (denoted as x), such that (a*x mod n)=1.
*
* Part of this implementation is adapted from OpenSSL v3.3.0.
*/

int kTwoPowers[32]; // [31] is negative

int init_two_powers()
{
int init_two_powers_i = 0;
int init_two_powers_power = 1;

while (init_two_powers_i < 32)
{
kTwoPowers[init_two_powers_i] = init_two_powers_power;
init_two_powers_power = init_two_powers_power * 2;
init_two_powers_i = init_two_powers_i + 1;
}

return 0;
}

int mod(int mod_x, int mod_y)
{
return mod_x - mod_y * (mod_x / mod_y);
}

// uint32 operations

int rshift_uint32(int rshift_uint32_x, int rshift_uint32_usr_a)
{
if (rshift_uint32_usr_a >= 32 ||
(rshift_uint32_x >= 0 && rshift_uint32_usr_a == 31))
{
return 0;
}

if (rshift_uint32_x < 0 && rshift_uint32_usr_a > 0)
{
// clear msb
rshift_uint32_x = rshift_uint32_x + (-2147483648);
// perform normal right shift
rshift_uint32_x = rshift_uint32_x / 2;
// reset msb
rshift_uint32_x = rshift_uint32_x + 1073741824;
rshift_uint32_usr_a = rshift_uint32_usr_a - 1;
}

// Now a<31
return rshift_uint32_x / kTwoPowers[rshift_uint32_usr_a];
}

int get_bits_uint32(int get_bits_uint32_a)
{
int get_bits_uint32_bits = 0;
while (rshift_uint32(get_bits_uint32_a, get_bits_uint32_bits))
{
get_bits_uint32_bits = get_bits_uint32_bits + 1;
}

return get_bits_uint32_bits;
}

int cmp_uint32(int cmp_uint32_a, int cmp_uint32_b)
{
if ((cmp_uint32_a < 0 && cmp_uint32_b < 0) ||
(cmp_uint32_a >= 0 && cmp_uint32_b >= 0))
{
if (cmp_uint32_a > cmp_uint32_b)
{
return 1;
}
else if (cmp_uint32_a < cmp_uint32_b)
{
return -1;
}
else
{
return 0;
}
}
else if (cmp_uint32_a < 0 && cmp_uint32_b >= 0)
{
return 1;
}
else if (cmp_uint32_b < 0 && cmp_uint32_a >= 0)
{
return -1;
}
}

int nnmod(int nnmod_a, int nnmod_b)
{
int nnmod_m = mod(nnmod_a, nnmod_b);
if (nnmod_m < 0)
{
if (nnmod_b < 0)
{
nnmod_m = nnmod_m - nnmod_b;
}
else
{
nnmod_m = nnmod_m + nnmod_b;
}
}

return nnmod_m;
}

// GCD-Related

// Return 0 if no inv
int inverse_mod(int invmod_inv[1], int invmod_a, int invmod_n)
{
int invmod_A,
invmod_B,
invmod_X,
invmod_Y,
invmod_M,
invmod_D,
invmod_T,
invmod_R,
invmod_abits,
invmod_bbits,
invmod_tmp;

int invmod_sign;

if (invmod_n == 1 || invmod_n == 0)
{
return 0;
}

invmod_X = 1;
invmod_Y = 0;
invmod_B = invmod_a;
invmod_A = invmod_n;

if (invmod_A < 0)
{
invmod_A = -invmod_A;
}

if (invmod_B < 0 || cmp_uint32(invmod_B, invmod_A) >= 0)
{
invmod_B = nnmod(invmod_B, invmod_A);
}

invmod_sign = -1;

while (invmod_B)
{
invmod_abits = get_bits_uint32(invmod_A);
invmod_bbits = get_bits_uint32(invmod_B);

if (invmod_abits == invmod_bbits)
{
invmod_D = 1;
invmod_M = invmod_A - invmod_B;
}
else if (invmod_abits == invmod_bbits + 1)
{
invmod_T = invmod_B * 2;
if (cmp_uint32(invmod_A, invmod_T) < 0)
{
invmod_D = 1;
invmod_M = invmod_A - invmod_B;
}
else
{
invmod_M = invmod_A - invmod_T;
invmod_D = invmod_T + invmod_B;
if (cmp_uint32(invmod_A, invmod_D) < 0)
{
invmod_D = 2;
}
else
{
invmod_D = 3;
invmod_M = invmod_M - invmod_B;
}
}
}
else
{
invmod_D = invmod_A / invmod_B;
invmod_M = mod(invmod_A, invmod_B);
}

invmod_tmp = invmod_A;
invmod_A = invmod_B;
invmod_B = invmod_M;

if (invmod_D == 1)
{
invmod_tmp = invmod_X + invmod_Y;
}
else
{
if (invmod_D == 2)
{
invmod_tmp = invmod_X * 2;
}
else if (invmod_D == 4)
{
invmod_tmp = invmod_X * 4;
}
else
{
invmod_tmp = invmod_D * invmod_X;
}

invmod_tmp = invmod_tmp + invmod_Y;
}

invmod_M = invmod_Y;
invmod_Y = invmod_X;
invmod_X = invmod_tmp;
invmod_sign = -invmod_sign;
}

if (invmod_sign < 0)
{
invmod_Y = invmod_n - invmod_Y;
}

if (invmod_A == 1)
{
if (invmod_Y >= 0 && cmp_uint32(invmod_Y, invmod_n) < 0)
{
invmod_R = invmod_Y;
}
else
{
invmod_R = nnmod(invmod_Y, invmod_n);
}
}
else
{
return 0;
}

invmod_inv[0] = invmod_R;

return 1;
}

int main()
{
int a = read();
int n = read();
int inv[1];

init_two_powers();

if (!inverse_mod(inv, a, n))
{
return 1;
}

write(inv[0]);

return 0;
}
Loading

0 comments on commit f4fe789

Please sign in to comment.