change ring for Fp2

This commit is contained in:
zhenfei
2023-11-29 19:05:12 -05:00
parent c5e8a91b48
commit 43a2575afe
3 changed files with 9 additions and 8 deletions

View File

@@ -3,7 +3,7 @@ Implementation of Goldilocks and its extension fields
This repo implements
- Goldilocks Field mod `2^64 - 2^32 + 1`
- Goldilocks quadratic extension over `x^2 + 1`
- Goldilocks quadratic extension over `x^2 - 7`
- Goldilocks cubic extension over `x^3 - x - 1`
Traits are compatible with `ff 0.13.0`.

View File

@@ -1,4 +1,4 @@
//! This module implements Goldilocks quadratic extension field mod x^2 + 1
//! This module implements Goldilocks quadratic extension field mod x^2 - 7
use crate::Goldilocks;
use core::iter::{Product, Sum};
@@ -8,15 +8,16 @@ use rand_core::RngCore;
use serde::{Deserialize, Serialize};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
/// Degree 3 Goldilocks extension field mod x^2 + 1
/// Degree 3 Goldilocks extension field mod x^2 - 7
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct GoldilocksExt2(pub [Goldilocks; 2]);
/// For a = (a1, a2) and b = (b1, b2)
/// The multiplication is define as
/// c := a * b = a(x) * b(x) % (x^2 + 1)
/// = x*a2*b1 + x*a1*b2
/// + a1*b1 - a2*b2
/// c := a * b = a(x) * b(x) % (x^2 - 7)
/// = x*a2*b1 + x*a1*b2
/// + a1*b1 + 7*a2*b2
/// This requires 9 multiplications and 6 1 additions
fn mul_internal(a: &GoldilocksExt2, b: &GoldilocksExt2) -> GoldilocksExt2 {
@@ -26,7 +27,7 @@ fn mul_internal(a: &GoldilocksExt2, b: &GoldilocksExt2) -> GoldilocksExt2 {
let a2b1 = a.0[1] * b.0[0];
let a2b2 = a.0[1] * b.0[1];
let c1 = a1b1 - a2b2;
let c1 = a1b1 + Goldilocks(7) * a2b2;
let c2 = a2b1 + a1b2;
GoldilocksExt2([c1, c2])
}

View File

@@ -13,6 +13,6 @@ fn test_field() {
fn known_answer_tests() {
let a = GoldilocksExt2([Goldilocks::from(1), Goldilocks::from(2)]);
let b = GoldilocksExt2([Goldilocks::from(3), Goldilocks::from(4)]);
let c = GoldilocksExt2([-Goldilocks::from(5), Goldilocks::from(10)]);
let c = GoldilocksExt2([Goldilocks::from(59), Goldilocks::from(10)]);
assert_eq!(a * b, c)
}