Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: use two limbs for scalar mul #2602

Merged
merged 10 commits into from
Sep 7, 2023
Merged
21 changes: 7 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ tower = "0.4"
url = "2.2.0"
wasm-bindgen = { version = "=0.2.86", features = ["serde-serialize"] }
wasm-bindgen-test = "0.3.33"
base64 = "0.21.2"
base64 = "0.21.2"


[patch.crates-io]
acvm = { git = "https://github.com/noir-lang/acvm", branch = "kw/fix-scalar-mul" }
Binary file modified crates/nargo_cli/tests/acir_artifacts/6/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/7/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/7_function/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/7_function/target/witness.gz
Binary file not shown.
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/array_len/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/array_sort/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/brillig_slices/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/eddsa/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/eddsa/target/witness.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/keccak256/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/merkle_insert/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/scalar_mul/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/schnorr/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/schnorr/target/witness.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/sha256/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/simple_radix/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/simple_shield/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/strings/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/to_be_bytes/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/to_be_bytes/target/witness.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/to_le_bytes/target/acir.gz
Binary file not shown.
Binary file modified crates/nargo_cli/tests/acir_artifacts/to_le_bytes/target/witness.gz
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ fn main(x: Field) {
let bytes = x.to_be_bytes(32);

let hash = std::hash::pedersen([x]);
let _p1 = std::scalar_mul::fixed_base_embedded_curve(x);
let _p1 = std::scalar_mul::fixed_base_embedded_curve(x,0);
kevaundray marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ unconstrained fn main(
pub_x = b_pub_x;
pub_y = b_pub_y;
}
let res = std::scalar_mul::fixed_base_embedded_curve(priv_key);
let res = std::scalar_mul::fixed_base_embedded_curve(priv_key,0);
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
assert(res[0] == pub_x);
assert(res[1] == pub_y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main(
pub_x = b_pub_x;
pub_y = b_pub_y;
}
let res = std::scalar_mul::fixed_base_embedded_curve(priv_key);
let res = std::scalar_mul::fixed_base_embedded_curve(priv_key, 0);
assert(res[0] == pub_x);
assert(res[1] == pub_y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main(
to_pubkey_y: Field,
) -> pub [Field; 2] {
// Compute public key from private key to show ownership
let pubkey = std::scalar_mul::fixed_base_embedded_curve(priv_key);
let pubkey = std::scalar_mul::fixed_base_embedded_curve(priv_key,0);
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
let pubkey_x = pubkey[0];
let pubkey_y = pubkey[1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ pub(crate) fn convert_black_box_call(
}
BlackBoxFunc::FixedBaseScalarMul => {
if let (
[RegisterOrMemory::RegisterIndex(scalar)],
[RegisterOrMemory::RegisterIndex(low), RegisterOrMemory::RegisterIndex(high)],
[RegisterOrMemory::HeapArray(result_array)],
) = (function_arguments, function_results)
{
brillig_context.black_box_op_instruction(BlackBoxOp::FixedBaseScalarMul {
input: *scalar,
low: *low,
high: *high,
result: *result_array,
});
} else {
Expand Down
3 changes: 2 additions & 1 deletion crates/noirc_evaluator/src/brillig/brillig_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,8 @@ pub(crate) mod tests {
}
fn fixed_base_scalar_mul(
&self,
_input: &FieldElement,
_low: &FieldElement,
_high: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
Ok((4_u128.into(), 5_u128.into()))
}
Expand Down
7 changes: 4 additions & 3 deletions crates/noirc_evaluator/src/brillig/brillig_ir/debug_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,12 @@ impl DebugShow {
result
);
}
BlackBoxOp::FixedBaseScalarMul { input, result } => {
BlackBoxOp::FixedBaseScalarMul { low, high, result } => {
debug_println!(
self.enable_debug_trace,
" FIXED_BASE_SCALAR_MUL {} -> {}",
input,
" FIXED_BASE_SCALAR_MUL {} {} -> {}",
low,
high,
result
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,8 @@ fn execute_brillig(
}
fn fixed_base_scalar_mul(
&self,
_input: &FieldElement,
_low: &FieldElement,
_high: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
Err(BlackBoxResolutionError::Unsupported(BlackBoxFunc::FixedBaseScalarMul))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ impl GeneratedAcir {
}
}
BlackBoxFunc::FixedBaseScalarMul => BlackBoxFuncCall::FixedBaseScalarMul {
input: inputs[0][0],
low: inputs[0][0],
high: inputs[1][0],
outputs: (outputs[0], outputs[1]),
},
BlackBoxFunc::Keccak256 => {
Expand Down Expand Up @@ -901,8 +902,8 @@ fn black_box_func_expected_input_size(name: BlackBoxFunc) -> Option<usize> {
| BlackBoxFunc::EcdsaSecp256k1
| BlackBoxFunc::EcdsaSecp256r1 => None,
// Inputs for fixed based scalar multiplication
// is just a scalar
BlackBoxFunc::FixedBaseScalarMul => Some(1),
// is the low and high limbs of the scalar
BlackBoxFunc::FixedBaseScalarMul => Some(2),
// Recursive aggregation has a variable number of inputs
BlackBoxFunc::RecursiveAggregation => None,
}
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/grumpkin_scalar_mul.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ 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)
fixed_base_embedded_curve(scalar.low, scalar.high)
}
2 changes: 1 addition & 1 deletion noir_stdlib/src/scalar_mul.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
// The embedded curve being used is decided by the
// underlying proof system.
#[foreign(fixed_base_scalar_mul)]
fn fixed_base_embedded_curve(_input : Field) -> [Field; 2] {}
fn fixed_base_embedded_curve(_low : Field, _high : Field) -> [Field; 2] {}