Skip to content

Commit

Permalink
Added static version of toScalar
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanic committed Jul 2, 2024
1 parent a43907c commit 3e19e75
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/lib/provable/scalar-field.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
Expand All @@ -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();
Expand Down

0 comments on commit 3e19e75

Please sign in to comment.