-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1705 from rpanic/feature/scalar-field
ScalarField and ShiftedScalar conversions
- Loading branch information
Showing
5 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Fq } from '../../bindings/crypto/finite-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'; | ||
|
||
export { ScalarField }; | ||
|
||
/** | ||
* ForeignField representing the scalar field of Pallas and the base field of Vesta | ||
*/ | ||
class ScalarField extends createForeignField(Fq.modulus) { | ||
/** | ||
* Provable method to convert a {@link ScalarField} into a {@link Scalar} | ||
*/ | ||
public toScalar(): Scalar { | ||
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); | ||
} | ||
|
||
/** | ||
* Converts this {@link Scalar} into a {@link ScalarField} | ||
*/ | ||
static fromScalar(s: Scalar): ScalarField { | ||
if (s.lowBit.isConstant() && s.high254.isConstant()) { | ||
return new ScalarField(s.toBigInt()); | ||
} | ||
const field = Provable.witness(ScalarField.provable, () => { | ||
return s.toBigInt(); | ||
}); | ||
const foreignField = new ScalarField(field); | ||
const scalar = foreignField.toScalar(); | ||
Provable.assertEqual(Scalar, s, scalar); | ||
|
||
return foreignField; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters