Arkworks Elliptic Curve utils overhaul (#1870)

- Removal of Arkworks unit tests. These tests were just testing the
arkworks upstream implementation which should be assumed correct. This
is not the place to test well known dependencies.
- Removal of some over-engineering. We just store the calls to Arkworks
in one file. Per-curve sources are not required.
- Docs formatting

---

I also took the opportunity to bump the `bandersnatch-vrfs` crate
revision internally providing some new shiny stuff.
This commit is contained in:
Davide Galassi
2023-10-16 10:43:52 +02:00
committed by GitHub
parent 19f38ca3aa
commit 38ef04eb53
15 changed files with 219 additions and 974 deletions
@@ -15,8 +15,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! The generic executions of the operations on arkworks elliptic curves
//! which get instantiatied by the corresponding curves.
//! Generic executions of the operations for *Arkworks* elliptic curves.
use ark_ec::{
pairing::{MillerLoopOutput, Pairing, PairingOutput},
short_weierstrass,
@@ -25,17 +25,18 @@ use ark_ec::{
twisted_edwards::TECurveConfig,
CurveConfig, VariableBaseMSM,
};
use ark_scale::hazmat::ArkScaleProjective;
use ark_std::vec::Vec;
use codec::{Decode, Encode};
use ark_scale::{
hazmat::ArkScaleProjective,
scale::{Decode, Encode},
};
use sp_std::vec::Vec;
const HOST_CALL: ark_scale::Usage = ark_scale::HOST_CALL;
type ArkScale<T> = ark_scale::ArkScale<T, HOST_CALL>;
// Scale codec type which is expected to be used by the host functions.
//
// Encoding is set to `HOST_CALL` which is a shortcut for "not-validated" and "not-compressed".
type ArkScale<T> = ark_scale::ArkScale<T, { ark_scale::HOST_CALL }>;
pub(crate) fn multi_miller_loop_generic<Curve: Pairing>(
g1: Vec<u8>,
g2: Vec<u8>,
) -> Result<Vec<u8>, ()> {
pub fn multi_miller_loop<Curve: Pairing>(g1: Vec<u8>, g2: Vec<u8>) -> Result<Vec<u8>, ()> {
let g1 = <ArkScale<Vec<<Curve as Pairing>::G1Affine>> as Decode>::decode(&mut g1.as_slice())
.map_err(|_| ())?;
let g2 = <ArkScale<Vec<<Curve as Pairing>::G2Affine>> as Decode>::decode(&mut g2.as_slice())
@@ -47,7 +48,7 @@ pub(crate) fn multi_miller_loop_generic<Curve: Pairing>(
Ok(result.encode())
}
pub(crate) fn final_exponentiation_generic<Curve: Pairing>(target: Vec<u8>) -> Result<Vec<u8>, ()> {
pub fn final_exponentiation<Curve: Pairing>(target: Vec<u8>) -> Result<Vec<u8>, ()> {
let target =
<ArkScale<<Curve as Pairing>::TargetField> as Decode>::decode(&mut target.as_slice())
.map_err(|_| ())?;
@@ -58,10 +59,7 @@ pub(crate) fn final_exponentiation_generic<Curve: Pairing>(target: Vec<u8>) -> R
Ok(result.encode())
}
pub(crate) fn msm_sw_generic<Curve: SWCurveConfig>(
bases: Vec<u8>,
scalars: Vec<u8>,
) -> Result<Vec<u8>, ()> {
pub fn msm_sw<Curve: SWCurveConfig>(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
let bases =
<ArkScale<Vec<short_weierstrass::Affine<Curve>>> as Decode>::decode(&mut bases.as_slice())
.map_err(|_| ())?;
@@ -78,10 +76,7 @@ pub(crate) fn msm_sw_generic<Curve: SWCurveConfig>(
Ok(result.encode())
}
pub(crate) fn msm_te_generic<Curve: TECurveConfig>(
bases: Vec<u8>,
scalars: Vec<u8>,
) -> Result<Vec<u8>, ()> {
pub fn msm_te<Curve: TECurveConfig>(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
let bases =
<ArkScale<Vec<twisted_edwards::Affine<Curve>>> as Decode>::decode(&mut bases.as_slice())
.map_err(|_| ())?;
@@ -97,7 +92,7 @@ pub(crate) fn msm_te_generic<Curve: TECurveConfig>(
Ok(result.encode())
}
pub(crate) fn mul_projective_generic<Group: SWCurveConfig>(
pub fn mul_projective_sw<Group: SWCurveConfig>(
base: Vec<u8>,
scalar: Vec<u8>,
) -> Result<Vec<u8>, ()> {
@@ -113,7 +108,7 @@ pub(crate) fn mul_projective_generic<Group: SWCurveConfig>(
Ok(result.encode())
}
pub(crate) fn mul_projective_te_generic<Group: TECurveConfig>(
pub fn mul_projective_te<Group: TECurveConfig>(
base: Vec<u8>,
scalar: Vec<u8>,
) -> Result<Vec<u8>, ()> {