fancy verbosity
This commit is contained in:
parent
26fe9ed776
commit
96074f7fbb
|
@ -24,35 +24,36 @@ pub fn modular_exponentiation(
|
||||||
if verbose {
|
if verbose {
|
||||||
println!("args:\nbase {base}\nexp {exp}\nfield {field}\nverbose {verbose}");
|
println!("args:\nbase {base}\nexp {exp}\nfield {field}\nverbose {verbose}");
|
||||||
}
|
}
|
||||||
let mut instructions: Vec<bool> = bigint_to_bools(exp);
|
let mut instructions: Vec<bool> = bigint_to_bools(exp.clone());
|
||||||
// remove the signing bit
|
// remove the signing bit
|
||||||
if verbose {
|
|
||||||
println!("pre instructions {:?}",instructions);
|
|
||||||
}
|
|
||||||
|
|
||||||
instructions.reverse();
|
instructions.reverse();
|
||||||
|
|
||||||
if verbose {
|
if verbose {
|
||||||
println!("instructions {:?}",instructions);
|
println!("exponent to binary/bools (discard first bit):\n{:b}\n{:?}", exp, instructions);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut res = base.clone();
|
let mut res = base.clone();
|
||||||
for instr in instructions {
|
for (index, instr) in instructions.iter().enumerate() {
|
||||||
if verbose {
|
|
||||||
println!("current res: {res}");
|
|
||||||
}
|
|
||||||
if !instr {
|
if !instr {
|
||||||
// square
|
// square
|
||||||
if verbose {
|
if verbose {
|
||||||
println!("square");
|
print!("{index}. {instr} -> square:\nres = {res}^2 mod {field} = ");
|
||||||
}
|
}
|
||||||
res = res.pow(2) % &field;
|
res = res.pow(2) % &field;
|
||||||
|
if verbose {
|
||||||
|
println!("{res}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// square and multiply
|
// square and multiply
|
||||||
if verbose {
|
if verbose {
|
||||||
println!("square and multiply");
|
print!("{index}. {instr} -> square and multiply:\nres = {res}^2 * {base} mod {field} = ");
|
||||||
}
|
}
|
||||||
res = (res.pow(2) * &base) % &field;
|
res = (res.pow(2) * &base) % &field;
|
||||||
|
if verbose {
|
||||||
|
println!("{res}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue