cleaner file structure + python script to generate example values
This commit is contained in:
parent
9a9e565583
commit
950b64c4cd
6
gen_values.py
Normal file
6
gen_values.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from random import randint
|
||||||
|
p = randint(100,1000)
|
||||||
|
a = ["Integer::from("+str(p*randint(1,100)+randint(0,20))+"), " for _ in range(200)]
|
||||||
|
for b in a:
|
||||||
|
print(b)
|
||||||
|
print(p)
|
35
src/agcd.rs
Normal file
35
src/agcd.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use rug::Integer;
|
||||||
|
use crate::matrix::Matrix;
|
||||||
|
use crate::utils::abs;
|
||||||
|
use lll_rs::l2::bigl2;
|
||||||
|
|
||||||
|
pub fn agcd(ciphertexts: Vec<Integer>, noise_bits: usize) -> Integer {
|
||||||
|
// 1. Build lattice matrix basis
|
||||||
|
let basis_matrix = Matrix::new_lattice(noise_bits, ciphertexts.clone()).unwrap();
|
||||||
|
|
||||||
|
// 2. reduce with LLL
|
||||||
|
let mut lll_matrix = basis_matrix.to_lll_matrix();
|
||||||
|
println!("basis: {:?}", lll_matrix);
|
||||||
|
bigl2::lattice_reduce(&mut lll_matrix, 0.51, 0.75);
|
||||||
|
println!("basis after reduction: {:?}", lll_matrix);
|
||||||
|
|
||||||
|
// 3. Extract shortest vector
|
||||||
|
let shortest_vector = &lll_matrix[0];
|
||||||
|
println!("Shortest vector: {:?}", shortest_vector);
|
||||||
|
|
||||||
|
// 4. q0 candidate
|
||||||
|
let q0 = &shortest_vector[0] / (Integer::from(1) << (noise_bits + 1));
|
||||||
|
println!("q0: {}", q0);
|
||||||
|
|
||||||
|
// 5. Find p
|
||||||
|
// compute r0 = x0 (mod q0)
|
||||||
|
// and p = (x0 − r0)/q0.
|
||||||
|
let x0 = &ciphertexts[0];
|
||||||
|
println!("x0: {}", x0);
|
||||||
|
let r0 = x0 % q0.clone();
|
||||||
|
println!("r0: {}", r0);
|
||||||
|
let p_guess = abs((x0 - r0) / q0);
|
||||||
|
|
||||||
|
println!("Recovered p: {}", p_guess);
|
||||||
|
p_guess
|
||||||
|
}
|
67
src/main.rs
67
src/main.rs
@ -1,55 +1,32 @@
|
|||||||
use rug::Integer;
|
use rug::Integer;
|
||||||
use crate::matrix::Matrix;
|
|
||||||
use lll_rs::l2::bigl2;
|
|
||||||
mod lll;
|
mod lll;
|
||||||
mod matrix;
|
mod matrix;
|
||||||
|
mod utils;
|
||||||
fn abs(i: Integer) -> Integer {
|
mod agcd;
|
||||||
if i < 0 {
|
use crate::agcd::agcd;
|
||||||
-i
|
|
||||||
} else {
|
|
||||||
i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// 1. Build lattice matrix basis
|
// 1. Build lattice matrix basis
|
||||||
let ciphertexts = vec![
|
let ciphertexts = vec![
|
||||||
Integer::from(37459),
|
Integer::from(32219),
|
||||||
Integer::from(8227),
|
Integer::from(21254),
|
||||||
Integer::from(44119),
|
Integer::from(16764),
|
||||||
Integer::from(22575),
|
Integer::from(338),
|
||||||
Integer::from(9249),
|
Integer::from(29960),
|
||||||
Integer::from(38483),
|
Integer::from(23516),
|
||||||
Integer::from(26181),
|
Integer::from(7084),
|
||||||
|
Integer::from(26735),
|
||||||
|
Integer::from(23195),
|
||||||
|
Integer::from(11928),
|
||||||
|
Integer::from(985),
|
||||||
|
Integer::from(11916),
|
||||||
|
Integer::from(13217),
|
||||||
|
Integer::from(29966),
|
||||||
|
Integer::from(14171),
|
||||||
|
Integer::from(13211),
|
||||||
|
Integer::from(23514),
|
||||||
|
Integer::from(19643)
|
||||||
];
|
];
|
||||||
let noise_bits = 2;
|
let noise_bits = 2;
|
||||||
let basis_matrix = Matrix::new_lattice(noise_bits, ciphertexts.clone()).unwrap();
|
let _ = agcd(ciphertexts, noise_bits);
|
||||||
|
|
||||||
// println!("matrix: {:?}", basis_matrix);
|
|
||||||
|
|
||||||
// 2. reduce with LLL
|
|
||||||
let mut lll_matrix = basis_matrix.to_lll_matrix();
|
|
||||||
println!("matrix: {:?}", lll_matrix);
|
|
||||||
bigl2::lattice_reduce(&mut lll_matrix, 0.51, 0.75);
|
|
||||||
println!("matrix: {:?}", lll_matrix);
|
|
||||||
|
|
||||||
// 3. Extract shortest vector
|
|
||||||
let shortest_vector = &lll_matrix[0];
|
|
||||||
println!("Shortest vector: {:?}", shortest_vector);
|
|
||||||
|
|
||||||
// 4. q0 candidate
|
|
||||||
let q0 = &shortest_vector[0] / (Integer::from(1) << (noise_bits + 1));
|
|
||||||
println!("q0: {}", q0);
|
|
||||||
|
|
||||||
// 5. Find p
|
|
||||||
// compute r0 = x0 (mod q0)
|
|
||||||
// and p = (x0 − r0)/q0.
|
|
||||||
let x0 = &ciphertexts[0];
|
|
||||||
println!("x0: {}", x0);
|
|
||||||
let r0 = x0 % q0.clone();
|
|
||||||
println!("r0: {}", r0);
|
|
||||||
let p_guess = abs((x0 - r0) / q0);
|
|
||||||
|
|
||||||
println!("Recovered p: {}", p_guess);
|
|
||||||
}
|
}
|
||||||
|
9
src/utils.rs
Normal file
9
src/utils.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
use rug::Integer;
|
||||||
|
|
||||||
|
pub fn abs(i: Integer) -> Integer {
|
||||||
|
if i < 0 {
|
||||||
|
-i
|
||||||
|
} else {
|
||||||
|
i
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user