This commit is contained in:
Dmitry Vagner 2023-01-21 14:26:38 +07:00
parent 769c615cf1
commit 8b670d54d1
2 changed files with 25 additions and 25 deletions

View File

@ -65,8 +65,8 @@ impl Div for Fp {
}
}
const ZERO_FP: Fp = Fp { val: U256::zero() };
const UNIT_FP: Fp = Fp { val: U256::one() };
pub const ZERO_FP: Fp = Fp { val: U256::zero() };
pub const UNIT_FP: Fp = Fp { val: U256::one() };
fn exp_fp(x: Fp, e: U256) -> Fp {
let mut current = x;
@ -148,12 +148,12 @@ impl Div for Fp2 {
}
}
const ZERO_FP2: Fp2 = Fp2 {
pub const ZERO_FP2: Fp2 = Fp2 {
re: ZERO_FP,
im: ZERO_FP,
};
const UNIT_FP2: Fp2 = Fp2 {
pub const UNIT_FP2: Fp2 = Fp2 {
re: UNIT_FP,
im: ZERO_FP,
};
@ -376,25 +376,6 @@ pub fn inv_fp12(f: Fp12) -> Fp12 {
UNIT_FP12 / f
}
pub fn sparse_embed(g000: Fp, g01: Fp2, g11: Fp2) -> Fp12 {
let g0 = Fp6 {
t0: Fp2 {
re: g000,
im: ZERO_FP,
},
t1: g01,
t2: ZERO_FP2,
};
let g1 = Fp6 {
t0: ZERO_FP2,
t1: g11,
t2: ZERO_FP2,
};
Fp12 { z0: g0, z1: g1 }
}
/// The nth frobenius endomorphism of a finite field F of order p^q is given by sending x: F to x^(p^n)
/// since any element x: F satisfies x^(p^q) = x = x^(p^0), these endomorphisms cycle modulo q
///
@ -827,7 +808,7 @@ const FROB_Z: [Fp2; 12] = [
},
];
fn gen_fp() -> Fp {
pub fn gen_fp() -> Fp {
let mut rng = thread_rng();
let x64 = rng.gen::<u64>();
let x256 = U256([x64, x64, x64, x64]) % BN_BASE;
@ -859,3 +840,22 @@ pub fn gen_fp12() -> Fp12 {
pub fn gen_fp12_sparse() -> Fp12 {
sparse_embed(gen_fp(), gen_fp2(), gen_fp2())
}
pub fn sparse_embed(g000: Fp, g01: Fp2, g11: Fp2) -> Fp12 {
let g0 = Fp6 {
t0: Fp2 {
re: g000,
im: ZERO_FP,
},
t1: g01,
t2: ZERO_FP2,
};
let g1 = Fp6 {
t0: ZERO_FP2,
t1: g11,
t2: ZERO_FP2,
};
Fp12 { z0: g0, z1: g1 }
}

View File

@ -1,7 +1,7 @@
use ethereum_types::U256;
use crate::bn254_arithmetic::{
frob_fp12, inv_fp12, make_fp, mul_fp_fp2, sparse_embed, Fp, Fp12, Fp2, UNIT_FP12,
frob_fp12, inv_fp12, make_fp, mul_fp_fp2, Fp, Fp12, Fp2, UNIT_FP12, sparse_embed
};
// The curve consists of pairs (x, y): (Fp, Fp) | y^2 = x^3 + 2