mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-04 06:43:07 +00:00
random inp
This commit is contained in:
parent
b28e3e0db7
commit
503cb8a99f
@ -1,6 +1,8 @@
|
||||
use std::ops::{Add, Mul, Neg};
|
||||
|
||||
use ethereum_types::U256;
|
||||
use rand::distributions::Standard;
|
||||
use rand::prelude::Distribution;
|
||||
use rand::Rng;
|
||||
|
||||
use crate::extension_tower::{FieldExt, Fp12, Fp2, Fp6, BN254};
|
||||
@ -23,6 +25,26 @@ impl<T: FieldExt> Curve<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Curve<T>
|
||||
where
|
||||
T: FieldExt,
|
||||
Curve<T>: CyclicGroup,
|
||||
{
|
||||
pub fn int(z: i32) -> Self {
|
||||
Curve::<T>::GENERATOR * z
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Distribution<Curve<T>> for Standard
|
||||
where
|
||||
T: FieldExt,
|
||||
Curve<T>: CyclicGroup,
|
||||
{
|
||||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Curve<T> {
|
||||
Curve::<T>::GENERATOR * rng.gen::<i32>()
|
||||
}
|
||||
}
|
||||
|
||||
/// Standard addition formula for elliptic curves, restricted to the cases
|
||||
/// https://en.wikipedia.org/wiki/Elliptic_curve#Algebraic_interpretation
|
||||
impl<T: FieldExt> Add for Curve<T> {
|
||||
@ -62,14 +84,14 @@ impl<T: FieldExt> Neg for Curve<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CurveGroup {
|
||||
pub trait CyclicGroup {
|
||||
const GENERATOR: Self;
|
||||
}
|
||||
|
||||
/// The BN curve consists of pairs
|
||||
/// (x, y): (BN254, BN254) | y^2 = x^3 + 2
|
||||
// with generator given by (1, 2)
|
||||
impl CurveGroup for Curve<BN254> {
|
||||
impl CyclicGroup for Curve<BN254> {
|
||||
const GENERATOR: Curve<BN254> = Curve {
|
||||
x: BN254 { val: U256::one() },
|
||||
y: BN254 {
|
||||
@ -81,7 +103,7 @@ impl CurveGroup for Curve<BN254> {
|
||||
impl<T> Mul<i32> for Curve<T>
|
||||
where
|
||||
T: FieldExt,
|
||||
Curve<T>: CurveGroup,
|
||||
Curve<T>: CyclicGroup,
|
||||
{
|
||||
type Output = Curve<T>;
|
||||
|
||||
@ -107,7 +129,6 @@ where
|
||||
exp >>= 1;
|
||||
x = x + x;
|
||||
}
|
||||
println!("result: {:?}", result);
|
||||
result
|
||||
}
|
||||
}
|
||||
@ -115,7 +136,7 @@ where
|
||||
/// The twisted curve consists of pairs
|
||||
/// (x, y): (Fp2<BN254>, Fp2<BN254>) | y^2 = x^3 + 3/(9 + i)
|
||||
/// with generator given as follows
|
||||
impl CurveGroup for Curve<Fp2<BN254>> {
|
||||
impl CyclicGroup for Curve<Fp2<BN254>> {
|
||||
const GENERATOR: Curve<Fp2<BN254>> = Curve {
|
||||
x: Fp2 {
|
||||
re: BN254 {
|
||||
|
||||
@ -4,7 +4,7 @@ use anyhow::Result;
|
||||
use ethereum_types::U256;
|
||||
use rand::Rng;
|
||||
|
||||
use crate::bn254_pairing::{final_exponent, gen_fp12_sparse, miller_loop, Curve, CurveGroup};
|
||||
use crate::bn254_pairing::{final_exponent, gen_fp12_sparse, miller_loop, Curve, CyclicGroup};
|
||||
use crate::cpu::kernel::interpreter::{
|
||||
run_interpreter_with_memory, Interpreter, InterpreterMemoryInitialization,
|
||||
};
|
||||
@ -213,7 +213,15 @@ fn pairing_input() -> Vec<U256> {
|
||||
fn test_bn_miller() -> Result<()> {
|
||||
let ptr: usize = 100;
|
||||
let out: usize = 106;
|
||||
let input = pairing_input();
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
let p: Curve<BN254> = rng.gen::<Curve<BN254>>();
|
||||
let q: Curve<Fp2<BN254>> = rng.gen::<Curve<Fp2<BN254>>>();
|
||||
|
||||
let p_stack: [U256; 2] = unsafe { transmute(p) };
|
||||
let q_stack: [U256; 4] = unsafe { transmute(q) };
|
||||
let mut input = p_stack.to_vec();
|
||||
input.extend(q_stack);
|
||||
|
||||
let setup = InterpreterMemoryInitialization {
|
||||
label: "bn254_miller".to_string(),
|
||||
@ -223,8 +231,7 @@ fn test_bn_miller() -> Result<()> {
|
||||
};
|
||||
let interpreter = run_interpreter_with_memory(setup).unwrap();
|
||||
let output: Vec<U256> = interpreter.extract_kernel_memory(BnPairing, out..out + 12);
|
||||
let expected =
|
||||
miller_loop(Curve::<BN254>::GENERATOR, Curve::<Fp2<BN254>>::GENERATOR).on_stack();
|
||||
let expected = miller_loop(p, q).on_stack();
|
||||
|
||||
assert_eq!(output, expected);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user