Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Mental Poker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
William KOCH
Mental Poker
Commits
d76c3150
Commit
d76c3150
authored
3 years ago
by
Oulawi
Browse files
Options
Downloads
Patches
Plain Diff
Main does something
parent
da21f26f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/astarzstar.rs
+11
-10
11 additions, 10 deletions
src/astarzstar.rs
src/crypto_system.rs
+10
-9
10 additions, 9 deletions
src/crypto_system.rs
src/main.rs
+15
-11
15 additions, 11 deletions
src/main.rs
src/quadratic_residues.rs
+8
-1
8 additions, 1 deletion
src/quadratic_residues.rs
with
44 additions
and
31 deletions
src/astarzstar.rs
+
11
−
10
View file @
d76c3150
mod
jacobi
;
mod
prime_gen
;
mod
gcd
;
use
num_bigint
::
BigUint
;
use
num_bigint
::
BigUint
;
use
super
::
prime_gen
;
use
super
::
gcd
;
use
super
::
jacobi
;
pub
fn
rand_astar
(
n
:
&
BigUInt
)
->
BigUInt
{
pub
fn
rand_astar
(
n
:
&
BigUint
)
->
BigUint
{
loop
{
loop
{
candidate
=
gen_large_number
(
n
.size
()
,
false
);
let
candidate
=
prime_gen
::
gen_large_number
(
n
.
bits
()
as
u
size
,
false
);
if
candidate
>=
n
{
if
&
candidate
>=
n
{
continue
;
continue
;
}
}
if
gcd
::
compute
(
n
,
&
candidate
)
!=
1
{
if
gcd
::
compute
(
n
,
&
candidate
)
!=
BigUint
::
from
(
1u32
)
{
continue
;
continue
;
}
}
if
jacobi
::
compute
(
&
candidate
,
n
)
==
-
1
{
if
jacobi
::
compute
(
&
candidate
,
n
)
==
-
1
{
...
@@ -21,11 +22,11 @@ pub fn rand_astar(n: &BigUInt) -> BigUInt {
...
@@ -21,11 +22,11 @@ pub fn rand_astar(n: &BigUInt) -> BigUInt {
pub
fn
rand_zstar
(
n
:
&
BigUint
)
->
BigUint
{
pub
fn
rand_zstar
(
n
:
&
BigUint
)
->
BigUint
{
loop
{
loop
{
candidate
=
gen_large_number
(
n
.size
()
,
false
);
let
candidate
=
prime_gen
::
gen_large_number
(
n
.
bits
()
as
u
size
,
false
);
if
candidate
>=
n
{
if
&
candidate
>=
n
{
continue
;
continue
;
}
}
if
gcd
::
compute
(
n
,
&
candidate
)
!=
1
{
if
gcd
::
compute
(
n
,
&
candidate
)
!=
BigUint
::
from
(
1u32
)
{
continue
;
continue
;
}
}
return
candidate
;
return
candidate
;
...
...
This diff is collapsed.
Click to expand it.
src/crypto_system.rs
+
10
−
9
View file @
d76c3150
use
super
::
astarzstar
;
use
num_bigint
::{
BigUint
};
use
num_bigint
::{
BigUint
};
use
rand
::
prelude
::
*
type
message
=
Vec
<
Bool
>
;
type
Message
=
Vec
<
bool
>
;
type
ciphertext
=
Vec
<
BigUint
>
;
type
Ciphertext
=
Vec
<
BigUint
>
;
pub
fn
encrypt
(
message
:
&
Message
,
n
:
&
BigUint
,
y
:
&
BigUint
)
->
Ciphertext
{
let
mut
res
:
Ciphertext
=
Vec
::
<
BigUint
>
::
with_capacity
(
message
.len
());
pub
fn
encrypt
(
message
:
&
message
,
n
:
&
BigUint
,
y
:
&
BigUint
)
{
let
mut
res
:
ciphertext
=
Vec
<
BigUint
>
::
with_capacity
(
message
.len
());
let
mut
rng
=
rand
::
thread_rng
();
for
i
in
0
..
message
.len
()
{
for
i
in
0
..
message
.len
()
{
let
xi
=
rand_z
n
(
n
);
let
xi
=
astarzstar
::
rand_z
star
(
n
);
if
message
[
i
]
{
if
message
[
i
]
{
res
[
i
]
=
(
y
*
xi
*
xi
)
%
n
;
res
.push
(
(
y
*
&
xi
*
&
xi
)
%
n
)
;
}
else
{
}
else
{
res
[
i
]
=
xi
.modpow
(
&
BigUint
::
from
(
2u32
),
n
);
res
.push
(
xi
.modpow
(
&
BigUint
::
from
(
2u32
),
n
)
)
;
}
}
}
}
return
res
;
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
15
−
11
View file @
d76c3150
mod
prime_gen
;
mod
prime_gen
;
use
rand
::
seq
::
SliceRandom
;
mod
astarzstar
;
mod
crypto_system
;
mod
gcd
;
mod
jacobi
;
mod
quadratic_residues
;
fn
main
()
{
fn
main
()
{
let
mut
rng
=
rand
::
thread_rng
();
let
p1
=
prime_gen
::
gen_prime
(
512
);
let
p1
=
prime_gen
::
gen_prime
(
512
);
let
mut
p2
=
prime_gen
::
gen_prime
(
512
);
let
mut
p2
=
prime_gen
::
gen_prime
(
512
);
while
p1
==
p2
{
while
&
p1
==
&
p2
{
p2
=
prime_gen
::
gen_prime
(
512
)
p2
=
prime_gen
::
gen_prime
(
512
)
}
}
//p1 and p2 are unequal primes (private key)
//p1 and p2 are unequal primes (private key)
let
n
=
p1
*
p2
;
//n is the public key together with some non-residue y to be computed
let
n
=
&
p1
*
&
p2
;
//n is the public key together with some non-residue y to be computed
let
mut
y
=
astar
(
n
)
.choose
(
rng
);
//astar (vec<bigUInt>) is the coprimes of n with jacobi symbol 1
let
mut
y
=
astar
zstar
::
rand_astar
(
&
n
);
//astar (vec<bigUInt>) is the coprimes of n with jacobi symbol 1
while
is_residue
(
n
,
y
)
{
while
quadratic_residues
::
is_quadratic_residue_n
(
&
y
,
&
p1
,
&
p2
)
{
//We regenerate y until its not a quadratic residue
//We regenerate y until its not a quadratic residue
y
=
astar
(
n
)
.choose
(
rng
);
y
=
astar
zstar
::
rand_astar
(
&
n
);
}
}
//Now (n, y) is the public key
//Now (n, y) is the public key
let
message
=
"Hello world"
.to_string
();
let
message
:
Vec
<
bool
>
=
vec!
[
true
,
true
,
false
,
false
,
true
];
let
cipher
=
encrypt
(
&
n
,
&
y
,
&
message
);
let
cipher
=
crypto_system
::
encrypt
(
&
message
,
&
n
,
&
y
);
for
i
in
0
..
5
{
println!
(
"{}"
,
cipher
[
i
]);
}
}
}
This diff is collapsed.
Click to expand it.
src/quadratic_residues.rs
+
8
−
1
View file @
d76c3150
...
@@ -4,5 +4,12 @@ use num_bigint::{BigUint};
...
@@ -4,5 +4,12 @@ use num_bigint::{BigUint};
// quadratic residue check
// quadratic residue check
pub
fn
is_quadratic_residue
(
a
:
&
BigUint
,
p
:
&
BigUint
)
->
bool
{
pub
fn
is_quadratic_residue
(
a
:
&
BigUint
,
p
:
&
BigUint
)
->
bool
{
a
.modpow
((
p
-
1
)
/
2
,
p
)
==
1
if
a
.modpow
(
&
((
p
-
1u32
)
/
2u32
),
p
)
==
BigUint
::
from
(
1u32
)
{
return
true
;
}
return
false
;
}
pub
fn
is_quadratic_residue_n
(
a
:
&
BigUint
,
p1
:
&
BigUint
,
p2
:
&
BigUint
)
->
bool
{
return
is_quadratic_residue
(
a
,
p1
)
&&
is_quadratic_residue
(
a
,
p2
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment