Skip to content

Instantly share code, notes, and snippets.

Created February 8, 2018 15:48
Show Gist options
  • Select an option

  • Save anonymous/7547c439d03e342bc262887902f143e5 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/7547c439d03e342bc262887902f143e5 to your computer and use it in GitHub Desktop.
[package]
name = "bigint"
version = "0.1.0"
authors = ["Anon"]
[dependencies]
ramp = "0.3.11"
lazy_static = "1.0.0"
#[macro_use]
extern crate lazy_static;
extern crate ramp;
use ramp::Int;
lazy_static! {
static ref THREE: Int = Int::from(3);
static ref P: Int = {
Int::from_str_radix(
"115792089237316195423570985008687907853269984665640564039457584007908834671663",
10,
).unwrap()
};
static ref P2: Int = {
&*P - Int::from(2)
};
}
#[derive(PartialEq, Clone, Debug)]
struct Point {
x: Int,
y: Int,
}
fn point_add(p: &Point, q: &Point) -> Point {
let lam = if p.x == q.x && p.y == q.y {
&*THREE * &p.x.pow(2) * ((&p.y + &p.y) % &*P).pow_mod(&*P2, &*P)
} else {
(&q.x - &p.x).pow_mod(&*P2, &*P) * (&q.y - &p.y) % &*P
};
let rx = (lam.pow(2) - &p.x - &q.x) % &*P;
let mut ry = (&lam * (&p.x - &rx) - &p.y) % &*P;
if ry < 0 {
ry += &*P;
}
Point { x: rx, y: ry }
}
fn point_mul(p: &Point, mut d: u32) -> Point {
let mut n = p.clone();
let mut q = None;
for _ in 0..256 {
let last_bit = d & 1 == 1;
d >>= 1;
if last_bit {
q = Some(q.map_or_else(|| n.clone(), |q| point_add(&q, &n)));
}
n = point_add(&n, &n);
}
q.unwrap()
}
fn main() {
let g = Point {
x: Int::from_str_radix(
"79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
16,
).unwrap(),
y: Int::from_str_radix(
"483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8",
16,
).unwrap(),
};
let res = point_mul(&g, 125);
println!(" {}", res.x);
println!(" {}", res.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment