diff --git a/math/curve.sage b/math/curve.sage new file mode 100644 index 00000000..517c2b10 --- /dev/null +++ b/math/curve.sage @@ -0,0 +1,61 @@ +# our prime modulus +F101 = IntegerModRing(101) + +# A number 5 in our prime modulus, should be 5 +print(IntegerMod(F101, 5)) + +# Should be 96 +print(IntegerMod(F101, -5)) + +# should be 81 +print(IntegerMod(F101, 1/5)) + +# should be 20 +print(IntegerMod(F101, -1/5)) + +# should be 100 +print(IntegerMod(F101, -1)) + +# Lets make our elliptic curve +E = EllipticCurve(F101, [0, 3]) + +# lets print out the points, notice they print (x,y,z) the difference between homogenious points and affine points is that to use affine you just divide x,y by z. +# We can see here that for all points in the curve group z = 1 except the zero point at infinity. So for this field they are the same +print(E.points()) + +# Define polynomial ring +R. = PolynomialRing(F101) + +# Lets make an extension field +# niavely: we could pick x^2 + 1 but +# x^2 + 1 = x^2 + 100 = (x+10)(x-10) -> There is a root in the field +# lets pick x^2 + 2 which is irreducible in our field + +# Extended polynomial ring +K. = GF(101**2, modulus = x^2 + 2) + +# Curve group over polynomial ring +E2 = EllipticCurve(K, [0, 3]) +print(E2.points()) + +# G1 is the generator for E1 +G1 = E(1,2) +print(G1) + +# N is the order of the group E1 +N = 17 + +# G2 is the generator for E2 +G2 = E2([36, 31 *X]) +print(G2) + +# Now Lets generate the structured refrence string (SRS), +# we will use the "random" number 2 for the example but in practice it should be strong random. +# a circuit with n gates requires an SRS with at least +# n + 5 elements as below +# We will let it be of length 9, pythagorean triple uses 4 gates +g1SRS = [(2**i)*G1 for i in range(7)] +print(g1SRS) + +g2SRS = [(2**i)*G2 for i in range(2)] +print(g2SRS) \ No newline at end of file diff --git a/src/field/gf_101.rs b/src/field/gf_101.rs index b40f8f97..e44b2538 100644 --- a/src/field/gf_101.rs +++ b/src/field/gf_101.rs @@ -26,13 +26,8 @@ impl fmt::Display for GF101 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.value) } } -impl From for GF101 { - fn from(val: u32) -> Self { Self::new(val) } -} - impl GF101 { - // pub const fn new(value: u32) -> Self { Self { value: to_monty(value) } } - pub const fn new(value: u32) -> Self { Self { value: value % Self::ORDER } } + pub const fn new(value: u32) -> Self { Self { value: to_monty(value) } } } impl FiniteField for GF101 { @@ -119,8 +114,7 @@ impl SubAssign for GF101 { impl Mul for GF101 { type Output = Self; - // fn mul(self, rhs: Self) -> Self { Self { value: from_monty(self.value * rhs.value) } } - fn mul(self, rhs: Self) -> Self::Output { Self::new(self.value * rhs.value) } + fn mul(self, rhs: Self) -> Self { Self { value: from_monty(self.value * rhs.value) } } } impl MulAssign for GF101 { @@ -428,18 +422,6 @@ mod tests { #[test] fn primitive_root_of_unity() { - let n = 2; - let omega = GF101::primitive_root_of_unity(n); - println!("omega: {:?}", omega); - assert_eq!(omega, F::new(95)); - let omega_n = omega.pow(n); - for i in 1..n { - let omega_i = omega.pow(i); - println!("omega^{}: {:?}", i, omega_i); - assert_ne!(omega_i, F::new(1)); - } - assert_eq!(omega_n, F::new(1)); - let n = 5; let omega = GF101::primitive_root_of_unity(n); println!("omega: {:?}", omega); diff --git a/src/field/mod.rs b/src/field/mod.rs index 3a98a759..57455ed0 100644 --- a/src/field/mod.rs +++ b/src/field/mod.rs @@ -25,7 +25,6 @@ pub trait FiniteField: + DivAssign + Neg + Rem - + From + Hash + 'static { type Storage: From