From 8b670d54d1676493a7ac7105a7af5ee6778a35cc Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Sat, 21 Jan 2023 14:26:38 +0700 Subject: [PATCH] meh --- evm/src/bn254_arithmetic.rs | 48 ++++++++++++++++++------------------- evm/src/bn254_pairing.rs | 2 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/evm/src/bn254_arithmetic.rs b/evm/src/bn254_arithmetic.rs index b921d862..5d52c452 100644 --- a/evm/src/bn254_arithmetic.rs +++ b/evm/src/bn254_arithmetic.rs @@ -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::(); 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 } +} diff --git a/evm/src/bn254_pairing.rs b/evm/src/bn254_pairing.rs index 0a5766b6..71da964c 100644 --- a/evm/src/bn254_pairing.rs +++ b/evm/src/bn254_pairing.rs @@ -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