From 3e19e75afc8fdb61d03868195f11807773c89ff2 Mon Sep 17 00:00:00 2001 From: rpanic Date: Tue, 2 Jul 2024 13:15:48 +0200 Subject: [PATCH] Added static version of toScalar --- src/lib/provable/scalar-field.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/lib/provable/scalar-field.ts b/src/lib/provable/scalar-field.ts index f8b21ca4f0..91c085a025 100644 --- a/src/lib/provable/scalar-field.ts +++ b/src/lib/provable/scalar-field.ts @@ -1,6 +1,5 @@ import { Fq } from '../../bindings/crypto/finite-field.js'; -import { createField } from './core/field-constructor.js'; -import { createForeignField } from './foreign-field.js'; +import { ForeignField, createForeignField } from './foreign-field.js'; import { field3ToShiftedScalar } from './gadgets/native-curve.js'; import { Provable } from './provable.js'; import { Scalar } from './scalar.js'; @@ -10,14 +9,21 @@ export { ScalarField }; /** * ForeignField representing the scalar field of Pallas and the base field of Vesta */ -class ScalarField extends createForeignField( - Fq.modulus -) { +class ScalarField extends createForeignField(Fq.modulus) { /** * Provable method to convert a {@link ScalarField} into a {@link Scalar} */ public toScalar(): Scalar { - const field3 = this.value; + return ScalarField.toScalar(this); + } + + public static toScalar(field: ForeignField) { + if (field.modulus !== Fq.modulus) { + throw new Error( + 'Only ForeignFields with Fq modulus are convertable into a scalar' + ); + } + const field3 = field.value; const shiftedScalar = field3ToShiftedScalar(field3); return Scalar.fromShiftedScalar(shiftedScalar); } @@ -27,12 +33,10 @@ class ScalarField extends createForeignField( */ static fromScalar(s: Scalar): ScalarField { if (s.lowBit.isConstant() && s.high254.isConstant()) { - return new ScalarField( - ScalarField.from(s.toBigInt()) - ); + return new ScalarField(s.toBigInt()); } const field = Provable.witness(ScalarField.provable, () => { - return s.toBigInt() + return s.toBigInt(); }); const foreignField = new ScalarField(field); const scalar = foreignField.toScalar();