command line, read numbers from file + python script generate test values
This commit is contained in:
parent
950b64c4cd
commit
64c58265c4
@ -6,5 +6,6 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = { version = "4.5.35", features = ["derive"] }
|
||||||
lll-rs = "0.2.0"
|
lll-rs = "0.2.0"
|
||||||
rug = "1.27.0"
|
rug = "1.27.0"
|
||||||
|
@ -1,6 +1,28 @@
|
|||||||
from random import randint
|
import sys
|
||||||
p = randint(100,1000)
|
import random
|
||||||
a = ["Integer::from("+str(p*randint(1,100)+randint(0,20))+"), " for _ in range(200)]
|
import argparse
|
||||||
for b in a:
|
|
||||||
print(b)
|
def generate_test_file(noise_bits, number):
|
||||||
print(p)
|
|
||||||
|
p = random.randint(100, 1000)
|
||||||
|
while p % 2 == 0:
|
||||||
|
p = random.randint(100, 1000)
|
||||||
|
|
||||||
|
max_noise = (1 << noise_bits) - 1 # 2^noise_bits - 1
|
||||||
|
|
||||||
|
a = [str(p * random.randint(1, 100) + random.randint(0, max_noise)) for _ in range(number)]
|
||||||
|
|
||||||
|
print(noise_bits)
|
||||||
|
|
||||||
|
for b in a:
|
||||||
|
print(b)
|
||||||
|
|
||||||
|
print(f"// True p: {p}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description='Generate test input for AGCD computation')
|
||||||
|
parser.add_argument('--noise-bits', type=int, default=5, help='Number of noise bits (default: 5)')
|
||||||
|
parser.add_argument('--number', type=int, default=20, help='Number of numbers to generate (default: 20)')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
generate_test_file(args.noise_bits, args.number)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use rug::Integer;
|
|
||||||
use crate::matrix::Matrix;
|
use crate::matrix::Matrix;
|
||||||
use crate::utils::abs;
|
use crate::utils::abs;
|
||||||
use lll_rs::l2::bigl2;
|
use lll_rs::l2::bigl2;
|
||||||
|
use rug::Integer;
|
||||||
|
|
||||||
pub fn agcd(ciphertexts: Vec<Integer>, noise_bits: usize) -> Integer {
|
pub fn agcd(ciphertexts: Vec<Integer>, noise_bits: usize) -> Integer {
|
||||||
// 1. Build lattice matrix basis
|
// 1. Build lattice matrix basis
|
||||||
|
52
src/file.rs
Normal file
52
src/file.rs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
use rug::Integer;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
pub struct AgcdInput {
|
||||||
|
pub noise_bits: usize,
|
||||||
|
pub numbers: Vec<Integer>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the input file, return noise_bits and numbers.
|
||||||
|
pub fn parse_file(path: &Path) -> io::Result<AgcdInput> {
|
||||||
|
let content = fs::read_to_string(path)?;
|
||||||
|
let mut lines = content.lines();
|
||||||
|
|
||||||
|
// Parse noise_bits from first non comment line
|
||||||
|
let noise_bits = loop {
|
||||||
|
match lines.next() {
|
||||||
|
Some(first_line) => {
|
||||||
|
let trimmed = first_line.trim();
|
||||||
|
if trimmed.starts_with("//") {
|
||||||
|
continue; // Skip comment lines
|
||||||
|
}
|
||||||
|
break trimmed.parse::<usize>()
|
||||||
|
.map_err(|e| io::Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
format!("First non-comment line must be noise_bits (usize): {}", e)
|
||||||
|
))?;
|
||||||
|
},
|
||||||
|
None => return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
"Input file is empty or contains only comments"
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Parse numbers from remaining lines
|
||||||
|
let numbers: Vec<Integer> = lines
|
||||||
|
.filter(|line| !line.trim().starts_with("//") && !line.trim().is_empty()) // ignore comment and empty lines
|
||||||
|
.filter_map(|line| line.trim().parse::<u64>().ok())
|
||||||
|
.map(Integer::from)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if numbers.len() < 2 {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
"Need at least 2 numbers to compute AGCD"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(AgcdInput { noise_bits, numbers })
|
||||||
|
}
|
64
src/main.rs
64
src/main.rs
@ -1,32 +1,42 @@
|
|||||||
use rug::Integer;
|
mod agcd;
|
||||||
mod lll;
|
mod lll;
|
||||||
mod matrix;
|
mod matrix;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod agcd;
|
mod file;
|
||||||
use crate::agcd::agcd;
|
|
||||||
|
|
||||||
fn main() {
|
use crate::agcd::agcd;
|
||||||
// 1. Build lattice matrix basis
|
use crate::file::parse_file;
|
||||||
let ciphertexts = vec![
|
use clap::{Parser, Subcommand};
|
||||||
Integer::from(32219),
|
use std::path::{Path, PathBuf};
|
||||||
Integer::from(21254),
|
|
||||||
Integer::from(16764),
|
#[derive(Parser)]
|
||||||
Integer::from(338),
|
#[command(author, version, about, long_about = None)]
|
||||||
Integer::from(29960),
|
#[command(propagate_version = true)]
|
||||||
Integer::from(23516),
|
struct Cli {
|
||||||
Integer::from(7084),
|
#[command(subcommand)]
|
||||||
Integer::from(26735),
|
command: Commands,
|
||||||
Integer::from(23195),
|
}
|
||||||
Integer::from(11928),
|
|
||||||
Integer::from(985),
|
#[derive(Subcommand)]
|
||||||
Integer::from(11916),
|
enum Commands {
|
||||||
Integer::from(13217),
|
/// approximate GCD
|
||||||
Integer::from(29966),
|
Agcd {
|
||||||
Integer::from(14171),
|
/// (default './input.txt')
|
||||||
Integer::from(13211),
|
path: Option<PathBuf>,
|
||||||
Integer::from(23514),
|
},
|
||||||
Integer::from(19643)
|
}
|
||||||
];
|
|
||||||
let noise_bits = 2;
|
fn main() -> std::io::Result<()> {
|
||||||
let _ = agcd(ciphertexts, noise_bits);
|
let cli = Cli::parse();
|
||||||
|
|
||||||
|
match &cli.command {
|
||||||
|
Commands::Agcd { path } => {
|
||||||
|
let path = path.as_deref().unwrap_or(Path::new("./input.txt"));
|
||||||
|
let input = parse_file(path)?;
|
||||||
|
|
||||||
|
let result = agcd(input.numbers, input.noise_bits);
|
||||||
|
println!("Approximate GCD with noise_bits={}: {}", input.noise_bits, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user