Last active
October 17, 2024 22:33
-
-
Save Lohann/6656fbd2de2c36c424ed592ff8cbbde0 to your computer and use it in GitHub Desktop.
Algoritmo Repeating Squaring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fn modpow(g: u32, exp: u128, n: u32) -> u32 { | |
let mut exponente = exp; | |
let mut resultado = 1; | |
let mut double = g; | |
while exponente > 0 { | |
if exponente % 2 == 1 { | |
resultado *= double; | |
resultado %= n; | |
} | |
exponente /= 2; | |
double *= double; | |
double %= n; | |
} | |
resultado | |
} | |
fn main() { | |
const PRIME: u32 = 997; | |
const BASE: u32 = 3; | |
const ALICE_SECRET: u128 = 123; | |
const BOB_SECRET: u128 = 987; | |
let alice_public = modpow(BASE, ALICE_SECRET, PRIME); | |
println!("Public key of Alice: {alice_public}"); | |
let bob_public = modpow(BASE, BOB_SECRET, PRIME); | |
println!("Public key of Bob: {bob_public}"); | |
let alice_shared_secret = modpow(bob_public, ALICE_SECRET, PRIME); | |
println!("Shared secret of Alice: {alice_shared_secret}"); | |
let bob_shared_secret = modpow(alice_public, BOB_SECRET, PRIME); | |
println!("Shared secret of Bob: {bob_shared_secret}"); | |
assert_eq!(alice_shared_secret, bob_shared_secret); | |
println!("The shared secret key is: {alice_shared_secret}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment