Created
March 16, 2021 18:45
-
-
Save mskd12/845efec5ee246c1e900bb326bec9f47a to your computer and use it in GitHub Desktop.
Fast vector commitments
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
extern crate curve25519_dalek; | |
extern crate rand; | |
use curve25519_dalek::constants; | |
use curve25519_dalek::traits::MultiscalarMul; | |
use curve25519_dalek::scalar::Scalar; | |
use curve25519_dalek::edwards::EdwardsPoint; | |
use curve25519_dalek::edwards::VartimeEdwardsPrecomputation; | |
use curve25519_dalek::traits::VartimePrecomputedMultiscalarMul; | |
use std::time::Instant; | |
use rand::thread_rng; | |
fn construct_scalars(n: usize) -> Vec<Scalar> { | |
let mut rng = thread_rng(); | |
(0..n).map(|_| Scalar::random(&mut rng)).collect() | |
} | |
fn construct_points(n: usize) -> Vec<EdwardsPoint> { | |
let mut rng = thread_rng(); | |
(0..n) | |
.map(|_| &Scalar::random(&mut rng) * &constants::ED25519_BASEPOINT_TABLE) | |
.collect() | |
} | |
fn main() { | |
// Some points | |
let bases = construct_points(100); | |
let precomp = VartimeEdwardsPrecomputation::new(&bases); | |
// A1 = a*P + b*Q + c*R | |
// let before = Instant::now(); | |
// let A1 = EdwardsPoint::multiscalar_mul(&scalars, &bases); | |
// println!("Elapsed time: {:.2?}", before.elapsed()); | |
// Note: (&scalars).into_iter(): Iterator<Item=&Scalar> | |
// Some scalars | |
let scalars = construct_scalars(100); | |
let before = Instant::now(); | |
let A3 = precomp.vartime_multiscalar_mul(&scalars); | |
println!("Elapsed time: {:.2?}", before.elapsed()); | |
// A2 = (-a)*P + (-b)*Q + (-c)*R | |
// let minus_abc = scalars.iter().map(|x| -x); | |
// let A2 = EdwardsPoint::multiscalar_mul(minus_abc, &bases); | |
// Note: minus_abc.into_iter(): Iterator<Item=Scalar> | |
// assert_eq!(A1.compress(), (A3).compress()); | |
// println!("Success!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment