From 8496020c5fe4378bff28a992fee8892170229728 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 7 Sep 2023 07:29:26 +0000 Subject: [PATCH 1/4] feat(stdlib): grumpkin scalar multiplication --- noir_stdlib/src/grumpkin_scalar.nr | 21 +++++++++++++++++++++ noir_stdlib/src/grumpkin_scalar_mul.nr | 6 ++++++ noir_stdlib/src/lib.nr | 2 ++ 3 files changed, 29 insertions(+) create mode 100644 noir_stdlib/src/grumpkin_scalar.nr create mode 100644 noir_stdlib/src/grumpkin_scalar_mul.nr diff --git a/noir_stdlib/src/grumpkin_scalar.nr b/noir_stdlib/src/grumpkin_scalar.nr new file mode 100644 index 00000000000..51b175ac552 --- /dev/null +++ b/noir_stdlib/src/grumpkin_scalar.nr @@ -0,0 +1,21 @@ +struct GrumpkinScalar { + high: Field, + low: Field, +} + +impl GrumpkinScalar { + fn new(high: Field, low: Field) -> Self { + // TODO: max value check + GrumpkinScalar { high, low } + } +} + +global GRUMPKIN_SCALAR_SERIALISED_LEN: Field = 2; + +fn deserialise_grumpkin_scalar(fields: [Field; GRUMPKIN_SCALAR_SERIALISED_LEN]) -> GrumpkinScalar { + GrumpkinScalar { high: fields[0], low: fields[1] } +} + +fn serialise_grumpkin_scalar(scalar: GrumpkinScalar) -> [Field; GRUMPKIN_SCALAR_SERIALISED_LEN] { + [scalar.high, scalar.low] +} diff --git a/noir_stdlib/src/grumpkin_scalar_mul.nr b/noir_stdlib/src/grumpkin_scalar_mul.nr new file mode 100644 index 00000000000..78fd3e692e8 --- /dev/null +++ b/noir_stdlib/src/grumpkin_scalar_mul.nr @@ -0,0 +1,6 @@ +use crate::grumpkin_scalar::GrumpkinScalar; +use crate::scalar_mul::fixed_base_embedded_curve; + +fn grumpkin_fixed_base(scalar: GrumpkinScalar) -> [Field; 2] { + fixed_base_embedded_curve(scalar.low) +} diff --git a/noir_stdlib/src/lib.nr b/noir_stdlib/src/lib.nr index f033334c140..224c3a03f21 100644 --- a/noir_stdlib/src/lib.nr +++ b/noir_stdlib/src/lib.nr @@ -6,6 +6,8 @@ mod schnorr; mod ecdsa_secp256k1; mod ecdsa_secp256r1; mod eddsa; +mod grumpkin_scalar; +mod grumpkin_scalar_mul; mod scalar_mul; mod sha256; mod sha512; From 8642fa67894cd67c5eb63c7f727259d208847614 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 7 Sep 2023 11:54:55 +0100 Subject: [PATCH 2/4] Update grumpkin_scalar.nr --- noir_stdlib/src/grumpkin_scalar.nr | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/noir_stdlib/src/grumpkin_scalar.nr b/noir_stdlib/src/grumpkin_scalar.nr index 51b175ac552..82717de276f 100644 --- a/noir_stdlib/src/grumpkin_scalar.nr +++ b/noir_stdlib/src/grumpkin_scalar.nr @@ -1,21 +1,21 @@ struct GrumpkinScalar { - high: Field, low: Field, + high: Field, } impl GrumpkinScalar { - fn new(high: Field, low: Field) -> Self { + fn new(low: Field, high: Field) -> Self { // TODO: max value check - GrumpkinScalar { high, low } + GrumpkinScalar { low, high } } } global GRUMPKIN_SCALAR_SERIALISED_LEN: Field = 2; fn deserialise_grumpkin_scalar(fields: [Field; GRUMPKIN_SCALAR_SERIALISED_LEN]) -> GrumpkinScalar { - GrumpkinScalar { high: fields[0], low: fields[1] } + GrumpkinScalar { low: fields[0], high: fields[1] } } fn serialise_grumpkin_scalar(scalar: GrumpkinScalar) -> [Field; GRUMPKIN_SCALAR_SERIALISED_LEN] { - [scalar.high, scalar.low] + [scalar.low, scalar.high] } From e2c4114e237940b0bae37fa52a8bfd83ed6318ff Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 7 Sep 2023 11:56:58 +0100 Subject: [PATCH 3/4] Update noir_stdlib/src/grumpkin_scalar.nr --- noir_stdlib/src/grumpkin_scalar.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noir_stdlib/src/grumpkin_scalar.nr b/noir_stdlib/src/grumpkin_scalar.nr index 82717de276f..3a74e974322 100644 --- a/noir_stdlib/src/grumpkin_scalar.nr +++ b/noir_stdlib/src/grumpkin_scalar.nr @@ -5,7 +5,7 @@ struct GrumpkinScalar { impl GrumpkinScalar { fn new(low: Field, high: Field) -> Self { - // TODO: max value check + // TODO: check that the low and high value fit within the grumpkin modulus GrumpkinScalar { low, high } } } From 5fc2815148f35a3e5bbd0b38aa92b2b7f198c87f Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 7 Sep 2023 11:57:40 +0100 Subject: [PATCH 4/4] Update noir_stdlib/src/grumpkin_scalar_mul.nr --- noir_stdlib/src/grumpkin_scalar_mul.nr | 1 + 1 file changed, 1 insertion(+) diff --git a/noir_stdlib/src/grumpkin_scalar_mul.nr b/noir_stdlib/src/grumpkin_scalar_mul.nr index 78fd3e692e8..b7774f0086d 100644 --- a/noir_stdlib/src/grumpkin_scalar_mul.nr +++ b/noir_stdlib/src/grumpkin_scalar_mul.nr @@ -2,5 +2,6 @@ use crate::grumpkin_scalar::GrumpkinScalar; use crate::scalar_mul::fixed_base_embedded_curve; fn grumpkin_fixed_base(scalar: GrumpkinScalar) -> [Field; 2] { + // TODO: this should use both the low and high limbs to do the scalar multiplication fixed_base_embedded_curve(scalar.low) }