diff --git a/core/crypto/_fiat/field_curve25519/field.odin b/core/crypto/_fiat/field_curve25519/field.odin index faf8ae3f7..a8e0a0316 100644 --- a/core/crypto/_fiat/field_curve25519/field.odin +++ b/core/crypto/_fiat/field_curve25519/field.odin @@ -136,3 +136,52 @@ fe_carry_inv :: proc (out1: ^Tight_Field_Element, arg1: ^Loose_Field_Element) { mem.zero_explicit(&tmp1, size_of(tmp1)) } + +fe_zero :: proc "contextless" (out1: ^Tight_Field_Element) { + out1[0] = 0 + out1[1] = 0 + out1[2] = 0 + out1[3] = 0 + out1[4] = 0 +} + +fe_one :: proc "contextless" (out1: ^Tight_Field_Element) { + out1[0] = 1 + out1[1] = 0 + out1[2] = 0 + out1[3] = 0 + out1[4] = 0 +} + +fe_set :: proc "contextless" (out1, arg1: ^Tight_Field_Element) { + x1 := arg1[0] + x2 := arg1[1] + x3 := arg1[2] + x4 := arg1[3] + x5 := arg1[4] + out1[0] = x1 + out1[1] = x2 + out1[2] = x3 + out1[3] = x4 + out1[4] = x5 +} + +@(optimization_mode="none") +fe_cond_swap :: #force_no_inline proc "contextless" (out1, out2: ^Tight_Field_Element, arg1: int) { + mask := (u64(arg1) * 0xffffffffffffffff) + x := (out1[0] ~ out2[0]) & mask + x1, y1 := out1[0] ~ x, out2[0] ~ x + x = (out1[1] ~ out2[1]) & mask + x2, y2 := out1[1] ~ x, out2[1] ~ x + x = (out1[2] ~ out2[2]) & mask + x3, y3 := out1[2] ~ x, out2[2] ~ x + x = (out1[3] ~ out2[3]) & mask + x4, y4 := out1[3] ~ x, out2[3] ~ x + x = (out1[4] ~ out2[4]) & mask + x5, y5 := out1[4] ~ x, out2[4] ~ x + out1[0], out2[0] = x1, y1 + out1[1], out2[1] = x2, y2 + out1[2], out2[2] = x3, y3 + out1[3], out2[3] = x4, y4 + out1[4], out2[4] = x5, y5 +} diff --git a/core/crypto/_fiat/field_curve25519/field51.odin b/core/crypto/_fiat/field_curve25519/field51.odin index 1a731b31b..3cbc296b7 100644 --- a/core/crypto/_fiat/field_curve25519/field51.odin +++ b/core/crypto/_fiat/field_curve25519/field51.odin @@ -30,8 +30,6 @@ package field_curve25519 // // While the base implementation is provably correct, this implementation // makes no such claims as the port and optimizations were done by hand. -// At some point, it may be worth adding support to fiat-crypto for -// generating Odin output. // // TODO: // * When fiat-crypto supports it, using a saturated 64-bit limbs @@ -565,54 +563,3 @@ fe_carry_scmul_121666 :: proc (out1: ^Tight_Field_Element, arg1: ^Loose_Field_El out1[3] = x27 out1[4] = x32 } - -// The following routines were added by hand, and do not come from fiat-crypto. - -fe_zero :: proc "contextless" (out1: ^Tight_Field_Element) { - out1[0] = 0 - out1[1] = 0 - out1[2] = 0 - out1[3] = 0 - out1[4] = 0 -} - -fe_one :: proc "contextless" (out1: ^Tight_Field_Element) { - out1[0] = 1 - out1[1] = 0 - out1[2] = 0 - out1[3] = 0 - out1[4] = 0 -} - -fe_set :: proc "contextless" (out1, arg1: ^Tight_Field_Element) { - x1 := arg1[0] - x2 := arg1[1] - x3 := arg1[2] - x4 := arg1[3] - x5 := arg1[4] - out1[0] = x1 - out1[1] = x2 - out1[2] = x3 - out1[3] = x4 - out1[4] = x5 -} - -@(optimization_mode="none") -fe_cond_swap :: #force_no_inline proc "contextless" (out1, out2: ^Tight_Field_Element, arg1: int) { - mask := (u64(arg1) * 0xffffffffffffffff) - x := (out1[0] ~ out2[0]) & mask - x1, y1 := out1[0] ~ x, out2[0] ~ x - x = (out1[1] ~ out2[1]) & mask - x2, y2 := out1[1] ~ x, out2[1] ~ x - x = (out1[2] ~ out2[2]) & mask - x3, y3 := out1[2] ~ x, out2[2] ~ x - x = (out1[3] ~ out2[3]) & mask - x4, y4 := out1[3] ~ x, out2[3] ~ x - x = (out1[4] ~ out2[4]) & mask - x5, y5 := out1[4] ~ x, out2[4] ~ x - out1[0], out2[0] = x1, y1 - out1[1], out2[1] = x2, y2 - out1[2], out2[2] = x3, y3 - out1[3], out2[3] = x4, y4 - out1[4], out2[4] = x5, y5 -}