32 lines
629 B
Plaintext
32 lines
629 B
Plaintext
|
/* search for a random prime with the specified amount of bits */
|
||
|
\\ randomprime(bits:small=127)=
|
||
|
\\ {
|
||
|
\\ local(r:int=4);
|
||
|
\\ while(!isprime(r),
|
||
|
\\ r = bitor(2^(bits-1) + random(2^(bits-1)-1), 1);
|
||
|
\\ );
|
||
|
\\ r;
|
||
|
\\ }
|
||
|
|
||
|
smc_hextodec(s:str) =
|
||
|
{
|
||
|
local(v:vecsmall = Vecsmall(s), ret:int = 0);
|
||
|
for(i = 1, #v,
|
||
|
ret = (ret<<4) + if(v[i]<=57 && v[i]>=48, v[i]-48, v[i]<=70 && v[i]>=65, v[i]-55, v[i]<=102 && v[i]>=97, v[i]-87, error("invalid input format"))
|
||
|
);
|
||
|
ret;
|
||
|
}
|
||
|
|
||
|
smc_genfield(p:int, g)=
|
||
|
{
|
||
|
local(q:int);
|
||
|
q = (p - 1) / 2;
|
||
|
[Mod(g^2, p), q];
|
||
|
}
|
||
|
|
||
|
smc_genkeypair(field:vec)=
|
||
|
{
|
||
|
local(x:int=random(field[2]));
|
||
|
[x, field[1]^x];
|
||
|
}
|